- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
FabricJS – Determining Whether fill or Stroke Should be Drawn First for a Polygon Object?
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. In order to determine whether fill or stroke should be drawn first, we use the paintFirst property.
Syntax
new fabric.Polygon( points: Array, { paintFirst: String }: Object )
Parameters
points − This parameter accepts an Array which denotes the array of points that make up the polygon object where each point is an object with x and y.
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 paintFirst is a property.
Options Keys
paintFirst − This property accepts a String value which defines if the fill or stroke is drawn first. The default value is ‘fill’.
Example 1: Default Appearance of a Polygon Object
Let’s see a code example of how the polygon object appears when the paintFirst property is not applied. In this case, the default value is used which is "fill". This implies that while painting the object, fill colour will be painted first and will be followed by stroke colour.
<!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 default appearance of polygon object</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: 200, y: 10 }, { x: 250, y: 50 }, { x: 250, y: 180 }, { x: 150, y: 180 }, { x: 150, y: 50 }, { x: 200, y: 10 }, ], { fill: "green", stroke: "blue", strokeWidth: 20, } ); // Adding it to the canvas canvas.add(polygon); </script> </body> </html>
Example 2: Using the paintFirst Property
Let’s see a code example to see how we can change the default behaviour of polygon object by using the paintFirst property. Here, we have passed the paintFirst property a value of “stroke”. This ensures that the stroke is drawn first instead of filled.
<!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 paintFirst property</h2> <p>You can see that the stroke is painted first now</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: 200, y: 10 }, { x: 250, y: 50 }, { x: 250, y: 180 }, { x: 150, y: 180 }, { x: 150, y: 50 }, { x: 200, y: 10 }, ], { fill: "green", stroke: "blue", strokeWidth: 20, paintFirst: "stroke", } ); // Adding it to the canvas canvas.add(polygon); </script> </body> </html>
Conclusion
In this tutorial, we used two simple examples to demonstrate how you can determine whether fill or stroke should be drawn first for a Polygon using FabricJS.