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 a Polygon is different from a Polyline in FabricJS?
We can create a Polyline object by creating an instance of fabric.Polyline while fabric.Polygon can be used to create a Polygon instance. 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 polygon always connects the first point to the last to make a closed area while a polyline doesn't. This can be proved by the examples given below.
Syntax
new fabric.Polyline(points: Array, options: Object) new fabric.Polygon(points: Array, options: Object)
Parameters
-
points ? This parameter accepts an Array which denotes the array of points that make up the polyline or polygon 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 object.
Key Differences
| Aspect | Polyline | Polygon |
|---|---|---|
| Shape | Open path | Closed shape |
| Connection | First and last points disconnected | First and last points connected automatically |
| Fill | No fill (only stroke visible) | Can have fill color |
| Use Case | Lines, paths, open shapes | Geometric shapes, closed areas |
Example 1: Creating a Polyline
Let's see a code example of how we can add a polyline object to our canvas. The polyline creates an open path without connecting the first and last 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>Creating an instance of fabric.Polyline() and adding it to our canvas</h2>
<p>You can see that the polyline object doesn't connect 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>
Example 2: Creating a Polygon
Let's see a code example of how we can add a polygon object to our canvas using the same points array. Notice how the polygon automatically closes the shape by connecting the last point to the first.
<!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 canvas</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 polygon 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>
Conclusion
The main difference between Polyline and Polygon in FabricJS is that polygons automatically close the shape by connecting the first and last points, while polylines remain open. Use polylines for paths and lines, and polygons for closed geometric shapes that can be filled.
