How to create a canvas with Line using FabricJS?


In this tutorial, we are going to learn about how to create a canvas with a Line object 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 onedimensional 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.

Syntax

new fabric.Line( points: Array , options: 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 Line object. Using this parameter stroke, corner colour, origin and a lot of other properties can be changed related to the Line object.

Creating an instance of fabric.Line() and adding it to our canvas

Example

Let’s see a code example of how we can add a Line object to our canvas. The only required parameter is the points array whereas the second argument is the optional options object which allows us to assign different properties to the 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>Creating an instance of fabric.Line() and adding it to our canvas</h2> <p>You can see a Line object has been created</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([70, 100, 150, 200], { stroke: "red", }); // Add it to the canvas canvas.add(line); </script> </body> </html>

Manipulating the Line object by using the set method

Example

In this example we have assigned the properties to the Line object by using the set method which is a setter for values. Any property related to stroke, strokeWidth, angle, scaling, rotation, etc., can be mutated by using this method.

<!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>Manipulating the Line object by using the set method</h2> <p>You can see the Line object in canvas now with set values</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([70, 100, 150, 200]); // Set the properties line.set("stroke", "green"); line.set("strokeWidth", 10); // Add it to the canvas canvas.add(line); </script> </body> </html>

Updated on: 20-Oct-2022

790 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements