Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
Creating an instance of fabric.Polyline() and adding it to our canvas
You can see that the polyline object has been added
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.
Adding a button to randomly generate the Polyline objects
Click on the `Add Polyline!` Button to add a randomly generated polyline to the canvas
