How to randomly generate Polyline objects with a button click in FabricJS?


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 will create a program where pressing a button will randomly generate a Polyline object and add it to our canvas for us.

Syntax

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

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. 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: 50 },
         { x: 0, y: 0 },
         { x: 60, y: 0 },
      ];
      
      // Initiating a polyline object
      var polyline = new fabric.Polyline(points, {
         left: 100,
         top: 40,
         fill: "white",
         strokeWidth: 4,
         stroke: "cyan",
      });
      
      // Adding it to the canvas
      canvas.add(polyline);
   </script>
</body>
</html>

Example 2: Adding a button to randomly generate the Polyline Objects

Let’s see a code example to see how we can randomly generate the Polyline objects. We will add a button on pressing which randomly generated Polyline will be added to the canvas. We will be using a function to randomly generate a Polyline where we will use the Math.random() method to generate random points.

<!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>Adding a button to randomly generate the Polyline objects</h2>
   <p> Click on the `Add Polyline!` Button to add a randomly generated polyline to the canvas </p>
   <canvas id="canvas"></canvas>
   <button type="button" onclick="addPolyline()">Add Polyline!</button>
   <script>
      
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiate a Polyline object
      var polyLine = new fabric.Polyline([
         { x: 500, y: 200 },
         { x: 550, y: 60 },
         { x: 550, y: 200 },
         { x: 350, y: 100 },
         { x: 350, y: 600 },
      ], {
         stroke: "cyan",
         fill: "white",
         strokeWidth: 5,
      });
      
      // Add it to the canvas instance
      canvas.add(polyLine);

      // Function to generate random Polyline and adding it to canvas
      function addPolyline() {
         var randomPolyLine = new fabric.Polyline([
         {x: Math.random() * 500, y: Math.random() * 200},
         {x: Math.random() * 500, y: Math.random() * 600},
         {x: Math.random() * 300, y: Math.random() * 100}],
         {
            stroke: "cyan",
            fill: "rgb(256,256,256,0)",
            strokeWidth: 5,
         });
         canvas.add(randomPolyLine)
      }
   </script>
</body>
</html>

Updated on: 16-Feb-2023

135 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements