Home >Web Front-end >JS Tutorial >How to add a dashed stroke to a rectangle using FabricJS?
In this tutorial, we will learn how to add a dashed stroke to a rectangle using FabricJS. Rectangle is one of the various shapes provided by FabricJS. In order to create a rectangle, we must create an instance of the Fabric.Rect class and add it to the canvas. The StrokeDashArray property allows us to specify a dash pattern for the object's strokes.
new fabric.Rect( { strokeDashArray: Array }: Object)
Options (optional) - This parameter is a ## that provides additional customization #Object to our rectangle. Using this parameter, you can change the color, cursor, stroke width, and many other properties associated with the object for which StrokeDashArray is a property.
StrokeDashArray - This property allows us to specify a dash pattern for the object's strokes. We can do this by passing it an array. For example, if we pass an array with values [2,3], it represents a dash pattern of 2px dashes and 3px gaps, and this pattern repeats infinitely.
Default appearance of object strokes
Let's look at an example describing the default appearance of a rectangular object stroke Code examples. Since we are not using theStrokeDashArray property, the dash pattern is not displayed.
<!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 an object’s stroke</h2> <p>You can see that there is no dashed stroke on the rectangle</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiate a rectangle object var rect = new fabric.Rect({ left: 55, top: 70, width: 170, height: 70, fill: "#f4f0ec", stroke: "#2a52be", strokeWidth: 7, }); // Add it to the canvas canvas.add(rect); </script> </body> </html>Example 2
Passing the StrokeDashArray property as a key
In this example, we pass a The value is [9,2]. This means that a dashed pattern will be created with a 9 pixel long line followed by a 2 pixel long gap, then again a 9 pixel long line will be drawn, and so on.<!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>Passing strokeDashArray property as key</h2> <p>You can see now a dashed stroke has been added around the rectangle</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiate a rectangle object var rect = new fabric.Rect({ left: 55, top: 70, width: 170, height: 70, fill: "#f4f0ec", stroke: "#2a52be", strokeWidth: 7, strokeDashArray: [9, 2], }); // Add it to the canvas canvas.add(rect); </script> </body> </html>
The above is the detailed content of How to add a dashed stroke to a rectangle using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!