How to draw a dotted line with Polyline using FabricJS?


We can create a Polyline object by creating an instance of fabric.Polyline. A polyline object can be characterised by a set of connected straight-line segments. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity, etc.

We can use the strokeDashArray property to create a dotted line with Polyline.

Syntax

new fabric.Polyline(points: Array, { strokeDashArray: Array }: Object)

Parameters

  • points − This parameter accepts an Array which denotes the array of points that make up the polyline object.

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

Options Keys

  • strokeDashArray − This property accepts an Array which specifies the dotted pattern by stating intervals via an array. For example, if we pass an array with values [2,3], it means a dash pattern of 2px dash and 3px gap and repeating this pattern infinitely.

Example 1: Creating an Instance of fabric.Polyline() and Adding it to our Canvas

Let’s see a code example of how we can add a polyline object to our canvas first. The only required parameter is the points Array whereas the second argument is the optional options 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.Polyline() and adding it to our canvas </h2>
   <p>You can see that the polyline object has been added</p>
   <canvas id="canvas"></canvas>
   <script>
      
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiating a points array
      var points = [
         { x: 30, y: 500 },
         { x: 0, y: 0 },
         { x: 600, y: 0 },
      ];
      
      // Initiating a polyline object
      var polyline = new fabric.Polyline(points, {
         left: 100,
         top: 40,
         fill: "white",
         strokeWidth: 4,
         stroke: "pink",
      });
      
      // Adding it to the canvas
      canvas.add(polyline);
   </script>
</body>
</html> 

Example 2: Using the strokeDashArray to Add the dotted Pattern

As we can see in this example, we will use the strokeDashArray property to add a dotted line to our Polyline.

<!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>Using the strokeDashArray to add the dotted pattern</h2>
   <p>You can see that the polyline object has been added with dotted line</p>
   <canvas id="canvas"></canvas>
   <script>
      
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiating a points array
      var points = [
         { x: 30, y: 500 },
         { x: 0, y: 0 },
         { x: 600, y: 0 },
      ];
      
      // Initiating a polyline object with strokeDashArray
      var polyline = new fabric.Polyline(points, {
         left: 100,
         top: 40,
         fill: "white",
         strokeWidth: 4,
         stroke: "pink",
         strokeDashArray: [7, 10],
      });
      
      // Adding it to the canvas
      canvas.add(polyline);
   </script>
</body>
</html> 

Updated on: 16-Feb-2023

452 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements