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
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 coordinates.
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'.
paintFirst Property Values
| Value | Description | Effect |
|---|---|---|
"fill" |
Default behavior | Fill color is painted first, then stroke |
"stroke" |
Alternative behavior | Stroke is painted first, then fill color |
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>
Why paintFirst Matters
The paintFirst property becomes particularly important when you have thick strokes and semi-transparent fills. When stroke is painted first, the fill color may partially overlap the stroke, creating different visual effects. This is especially noticeable with transparency or when using blend modes.
Conclusion
The paintFirst property in FabricJS allows you to control the rendering order of fill and stroke for Polygon objects. Use "fill" for default behavior or "stroke" to paint the stroke layer first, which can create different visual effects especially with thick strokes and transparency.
