FabricJS – How to set the colour of the controlling corners of a Line?


In this tutorial, we are going to learn about how to set the colour of the controlling corners of Line using 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 cornerColor property allows us to manipulate the colour of the controlling corners when the object is active.

Syntax

new fabric.Line( points: Array, { cornerColor: 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 colour, cursor, stroke width and a lot of other properties can be changed related to the object of which the cornerColor is a property.

Options Keys

  • cornerColor βˆ’ This property accepts a String which allows us to assign a color to the controlling corners when the object is actively selected. The default value is rgb(178,204,255).

Passing cornerColor as key with a color name as value

Example

Let’s see a code example to change the colour by using the cornerColor property. In this case we have assigned the value β€œorange” to the key which thus makes the controlling corners appear orange.

<!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 cornerColor as key with a color name as value</h2> <p> You can select the line object to see that the corner colour is orange </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, cornerColor: "orange" }); // Add it to the canvas canvas.add(line); </script> </body> </html>

Assigning an RGBA value to the cornerColor property

Example

Instead of passing a simple colour name as the String value to the key, we can also assign an RGBA value. RGBA stands for red, green, blue and alpha where alpha is the opacity. Let us see a code example of how we can do that.

<!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>Assigning an RGBA value to the cornerColor property</h2> <p> You can select the line object to see the corner colour </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, cornerColor: "rgba(255,69,0, 0.8)", }); // Add it to the canvas canvas.add(line); </script> </body> </html>

Updated on: 25-Oct-2022

304 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements