We can create Polyline objects by creating instances of fabric.Polyline, and fabric.Polygon can be used to create Polygon instances. A polyline object can be represented 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 such as angle, opacity, etc.
Polygons always connect the first point to the last point to form a closed area, while polylines do not. This can be demonstrated by the example given below.
new fabric.Polyline(points: Array, options: Object)
points - This parameter accepts an Array, which represents the array of points that make up the polyline object.
Options (optional) - This parameter is an object that provides additional customization to our object. Use this parameter to change the origin, stroke width, and many other properties associated with the Polyline object.
Let's look at a code example to see how to add a polyline object to the canvas. The only required parameter is the points array, while the second parameter is an optional options object. Additionally, we will use the same points array in the Polygon to demonstrate the difference.
<!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 doesn’t connects start to end</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: 200, y: 150 }, { x: 0, y: 100 }, { x: 100, y: 0 }, { x: 200, y: 100 }, ]; // Initiating a polyline object var polyline = new fabric.Polyline(points, { left: 100, top: 40, fill: "white", strokeWidth: 4, stroke: "green", }); // Adding it to the canvas canvas.add(polyline); </script> </body> </html>
Let's look at a code example to see how to add a polygon object to the canvas. The only required parameter is the points array, while the second parameter is an optional options object, we will provide the same options object as in the polyline example.
<!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.Polygon() and adding it to our canva</h2> <p>You can see that the polygon object connects start to end to make a closed area</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: 200, y: 150 }, { x: 0, y: 100 }, { x: 100, y: 0 }, { x: 200, y: 100 }, ]; // Initiating a polyline object var polygon = new fabric.Polygon(points, { left: 100, top: 40, fill: "white", strokeWidth: 4, stroke: "green", }); // Adding it to the canvas canvas.add(polygon); </script> </body> </html>
The above is the detailed content of How are polygons different from polylines in FabricJS?. For more information, please follow other related articles on the PHP Chinese website!