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 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.

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 Canvas

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 Using the set Method

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>

Key Properties for Line Customization

FabricJS Line objects support various styling properties that can be set during initialization or later using the set method:

  • stroke - Sets the line color
  • strokeWidth - Controls line thickness
  • strokeDashArray - Creates dashed lines
  • left/top - Position coordinates
  • angle - Rotation angle in degrees
  • opacity - Transparency level (0-1)

Conclusion

Creating lines in FabricJS is straightforward using the fabric.Line constructor with a points array. You can customize line appearance through the options parameter or the set method for dynamic styling.

Updated on: 2026-03-15T23:19:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements