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 add the coordinates in a Polygon using FabricJS?
We can create a Polygon object by creating an instance of fabric.Polygon. A polygon object can be characterized by any closed shape consisting of 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 can add the coordinates in a polygon by using points property.
Syntax
new fabric.Polygon(points: Array, options: Object)
Parameters
points ? This parameter accepts an Array which denotes the array of points that make up the 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 Polygon object of which points is a property.
Options Keys
points ? This property accepts an Array which allows us to set the points array.
Example 1: Default Appearance of Polygon Object
Let's see a code example of how we can add a polygon object to our canvas. In this case we have not used the points property. We have specified the points array as the first argument itself which denotes the coordinate values to be used.
<!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>Default appearance of Polygon object</h2>
<p>You can see the Polygon object has been added to the canvas</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 polygon object
var polygon = new fabric.Polygon(
[
{ x: -20, y: -35 },
{ x: 20, y: -35 },
{ x: 40, y: 0 },
{ x: 20, y: 35 },
{ x: -20, y: 35 },
{ x: -40, y: 0 },
],
{
top: 50,
left: 50,
}
);
// Adding it to the canvas
canvas.add(polygon);
</script>
</body>
</html>
Example 2: Using the Points Property
In this example, we have used the points property and assigned it an Array that consists of the coordinate values of the polygon where each point is an object with "x" and "y" values. As it can be seen, although we have already specified the points of the Polygon object, as soon as we use the points property, it overrides those values and the new coordinates are used.
<!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>Using the points property</h2>
<p>You can see the Polygon object's coordinates have changed</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: 0, y: 30 },
{ x: 30, y: 30 },
{ x: 30, y: 0 },
{ x: 0, y: 0 },
];
// Initiating a polygon object
var polygon = new fabric.Polygon(points, {
top: 50,
left: 50,
points: [
{ x: 0, y: 70 },
{ x: 70, y: 70 },
{ x: 70, y: 0 },
{ x: 0, y: 0 },
],
});
// Adding it to the canvas
canvas.add(polygon);
</script>
</body>
</html>
How Points Property Overrides
The key difference between the two examples is that when you specify the points property in the options object, it overrides the initial points array passed as the first argument. This allows you to dynamically change the polygon's shape after creation.
Coordinate System
Each point in the points array is an object with x and y properties representing the coordinate values. The coordinates are relative to the polygon's center, and the final position on canvas is determined by the top and left properties.
Conclusion
The points property in FabricJS polygons allows flexible coordinate management. You can define coordinates either as the first parameter or override them using the points property in the options object.
