How to set the border colour of a Line in FabricJS?


In this tutorial, we are going to learn about how to set the border colour of Line in FabricJS. A Line element is one of the basic elements provided in FabricJS. It is used for creating straight lines. Because line elements are geometrically one-dimensional and do not contain an interior, they are never filled.

We can create a line object by creating an instance of fabric.Line, specifying the x and y coordinates of the line and adding it to the canvas. The property borderColor allows us to manipulate the colour of the border when our object is active.

Syntax

new fabric.Line( points: Array, { borderColor: String }: Object)

Parameters

  • points − This parameter accepts an Array of points which determines the (x1, y1) and (x2, y2) values, those being the x- and y-axis coordinates of the start and end of the line respectively.

  • options (optional) − This parameter is an Object which provides additional customizations to our object. Using this parameter origin, stroke width and a lot of other properties can be changed related to the object of which borderColor is a property.

Options Keys

  • borderColor − This property accepts a String which specifies the colour of the border when our line object is selected. Its default value is rgb(178,204,255).

Passing borderColor key with a String value

Example

Let’s see a code example of how we can assign a value to the borderColor property. We have assigned the value “red” to the borderColor key which helps to create the red border on selection of our line object.

<!DOCTYPE html> <html> <head> <!-- Adding the Fabric JS Library--> <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script> </head> <body> <h2>Passing borderColor key with a String value</h2> <p> You can select the line object to see that the border colour is red. </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiate a Line object var line = new fabric.Line([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, borderColor: "red" }); // Add it to the canvas canvas.add(line); </script> </body> </html>

Passing a rgba value to the borderColor key

Example

Instead of passing simple colour names as a String, we can also use RGBA values, whose components specify the amount of Red, Green, Blue and Alpha, where alpha denotes the opacity. In this example we have used rgba(164,0, 0,1) which is the rgb value for the colour dark red.

<!DOCTYPE html> <html> <head> <!-- Adding the Fabric JS Library--> <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/510/fabric.min.js"></script> </head> <body> <h2>Passing a rgba value to the borderColor key</h2> <p> You can select the line object to see the border colour added using rgba value </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiate a Line object var line = new fabric.Line([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, borderColor: "rgba(164,0,0,1)" }); // Add it to the canvas canvas.add(line); </script> </body> </html>

Updated on: 25-Oct-2022

129 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements