How to make a star with Polyline class 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. A star or pentagram consists of 10 isosceles triangles.

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

Before creating a start, 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: Creating a star with Polyline

In this example, we will create a star using the Polyline instance. We can select the coordinates in such a way that the shape forms a star as given below.



   
   


   

Creating a star with Polyline

You can see the star in the canvas now

Updated on: 2023-02-16T16:28:55+05:30

362 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements