How to use the Fabric.Polygon class in Canvas Fabric?

Jun 25, 2025

Leave a message

Helen Li
Helen Li
HR Manager at Shandong Shengrun Textile Co.,LTD. I focus on creating a positive work environment and developing our team of over 21,000 employees.interested in sharing insights about corporate culture and employee well-being.

Hey there! I'm a supplier of Canvas Fabric, and today I wanna chat about how to use the Fabric.Polygon class in Canvas Fabric. It's a pretty cool feature that can open up a whole new world of possibilities for your fabric - related projects.

What is the Fabric.Polygon class?

First off, let's understand what the Fabric.Polygon class is all about. In the world of Canvas Fabric, a polygon is a closed shape made up of straight - line segments. The Fabric.Polygon class allows you to create and manipulate these polygons on a canvas. You can use it to draw all sorts of shapes, from simple triangles to complex multi - sided figures.

Getting Started

To start using the Fabric.Polygon class, you'll need to have the Canvas Fabric library set up in your project. If you haven't already, you can easily include it in your HTML file like this:

<!DOCTYPE html>
<html>

<head>
    <title>Using Fabric.Polygon</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/4.6.0/fabric.min.js"></script>
</head>

<body>
    <canvas id="c" width="500" height="500"></canvas>
    <script>
        // Your code will go here
    </script>
</body>

</html>

Once you've got the library in place, you can start creating your canvas and adding polygons to it.

Creating a Simple Polygon

Let's create a basic triangle using the Fabric.Polygon class. Here's how you can do it:

// Get the canvas element
var canvas = new fabric.Canvas('c');

// Define the points for the triangle
var points = [
    { x: 200, y: 100 },
    { x: 300, y: 200 },
    { x: 100, y: 200 }
];

// Create the polygon
var triangle = new fabric.Polygon(points, {
    fill: 'blue',
    stroke: 'black',
    strokeWidth: 2
});

// Add the polygon to the canvas
canvas.add(triangle);

In this code, we first get the canvas element using fabric.Canvas(). Then we define an array of points that represent the vertices of our triangle. Each point is an object with an x and y coordinate. After that, we create a new fabric.Polygon object, passing in the points and some properties like the fill color, stroke color, and stroke width. Finally, we add the polygon to the canvas using the add() method.

Manipulating Polygons

One of the great things about the Fabric.Polygon class is that you can easily manipulate polygons after they're created. For example, you can change their position, size, and rotation.

Changing Position

To change the position of a polygon, you can use the left and top properties. Here's an example:

618

// Move the triangle to a new position
triangle.set({
    left: 300,
    top: 300
});

// Render the canvas to show the changes
canvas.renderAll();

In this code, we use the set() method to change the left and top properties of the triangle. Then we call canvas.renderAll() to update the canvas and show the new position of the triangle.

Changing Size

You can change the size of a polygon by scaling it. You can use the scaleX and scaleY properties. Here's how:

// Scale the triangle
triangle.set({
    scaleX: 1.5,
    scaleY: 1.5
});

// Render the canvas
canvas.renderAll();

This code scales the triangle by a factor of 1.5 in both the x and y directions.

Rotating a Polygon

To rotate a polygon, you can use the angle property. Here's an example:

// Rotate the triangle by 45 degrees
triangle.set({
    angle: 45
});

// Render the canvas
canvas.renderAll();

This code rotates the triangle 45 degrees clockwise.

Using Polygons in Real - World Projects

The Fabric.Polygon class can be used in all sorts of real - world projects. For example, if you're creating a design tool, you can use polygons to create custom shapes for your users. Or if you're working on a game, you can use polygons to represent characters or obstacles.

If you're into fabric - related projects, you can use polygons to create patterns on our Cotton Canvas Drop Cloth Cotton Fabric. You could design unique geometric patterns that could be printed or embroidered on the fabric.

More Advanced Usage

Creating Complex Polygons

You're not limited to simple triangles. You can create much more complex polygons by adding more points to the points array. For example, here's how you can create a pentagon:

var pentagonPoints = [
    { x: 250, y: 100 },
    { x: 350, y: 200 },
    { x: 300, y: 350 },
    { x: 200, y: 350 },
    { x: 150, y: 200 }
];

var pentagon = new fabric.Polygon(pentagonPoints, {
    fill: 'red',
    stroke: 'black',
    strokeWidth: 2
});

canvas.add(pentagon);

Grouping Polygons

You can also group multiple polygons together. This is useful when you want to treat multiple shapes as a single object. Here's an example:

// Create another triangle
var anotherTrianglePoints = [
    { x: 100, y: 100 },
    { x: 200, y: 200 },
    { x: 50, y: 200 }
];

var anotherTriangle = new fabric.Polygon(anotherTrianglePoints, {
    fill: 'green',
    stroke: 'black',
    strokeWidth: 2
});

// Group the two triangles
var group = new fabric.Group([triangle, anotherTriangle], {
    left: 200,
    top: 200
});

// Add the group to the canvas
canvas.add(group);

In this code, we create another triangle and then group it with the first triangle using fabric.Group(). We can then move and manipulate the group as a single unit.

Our Canvas Fabric Offerings

As a Canvas Fabric supplier, we have a wide range of high - quality fabrics that you can use in your projects. Whether you're looking for a durable Wholesale Tc 65/35 Hospital Dyed Pocketing Fabric Manufacturer or a stylish Wholesale Yarn Dyed Woven Cotton Polyester Twill Fabric, we've got you covered.

Conclusion

The Fabric.Polygon class in Canvas Fabric is a powerful tool that allows you to create and manipulate all sorts of polygons. With a little bit of code, you can create simple or complex shapes, change their properties, and use them in various projects.

If you're interested in our Canvas Fabric products or have any questions about using the Fabric.Polygon class in your projects, don't hesitate to reach out. We're here to help you with your fabric needs and make your projects a success. Let's start a conversation and see how we can work together!

References

  • Fabric.js Documentation
  • MDN Web Docs for HTML Canvas
Send Inquiry
you dream it, we design it
Shandong Shengrun Textile Co.,LTD.
contact us