- Trending Categories
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
How to add dashed stroke to a Circle using FabricJS?
In this tutorial, we are going to learn how to add a dashed stroke to a Circle using FabricJS. Circle is one of the various shapes provided by FabricJS. In order to create a circle, we will have to create an instance of fabric.Circle class and add it to the canvas. The strokeDashArray property allows us to specify a dash pattern for the object's stroke.
Syntax
new fabric.Circle( { strokeDashArray: Array }: Object)
Parameters
options (optional) − This parameter is an Object which provides additional customizations to our circle. Using this parameter, a lot of properties such as colour, cursor, and stroke width can be changed related to the object of which strokeDashArray is a property.
Options Keys
strokeDashArray − This option is an Array which is used to define the pattern of the dash. For example, if we pass an array with values [2,3], it means a dash pattern of 2px dash and 3px gap and repeating this pattern infinitely.
Example 1
Default appearance of an object's stroke
Let's see an example that depicts the default appearance of the stroke of a circle object. Since we have not used the strokeDashArray property, there is no dash pattern being 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>Adding dashed stroke to a circle using FabricJS</h2> <p>Here you cannot see the dashed stokes as we have not used the <b>strokeDashArray</b> property. This is the default appearance.</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); var cir = new fabric.Circle({ left: 215, top: 100, fill: "#adff2f", radius: 50, stroke: "#228b22", strokeWidth: 15 }); // Adding it to the canvas canvas.add(cir); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
Example 2
Passing strokeDashArray property as key
In this example, we are passing the strokeDashArray property a value of [9,2]. This means that a dash pattern will be created such that there is a 9px long line followed by a 2px gap, then again a 9px 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>Adding dashed stroke to a circle using FabricJS</h2> <p>Notice that here we have used the <b>strokeDashArray</b> property. </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); var cir = new fabric.Circle({ left: 215, top: 100, fill: "#adff2f", radius: 50, stroke: "#228b22", strokeWidth: 15, strokeDashArray: [9, 2] }); // Adding it to the canvas canvas.add(cir); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
- Related Articles
- How to add dashed stroke to a Triangle using FabricJS?
- How to add dashed stroke to a Rectangle using FabricJS?
- How to add dashed stroke to a Textbox using FabricJS?
- How to add dashed stroke to an Ellipse using FabricJS?
- How to add stroke to a Circle using FabricJS?
- How to add stroke to a Triangle using FabricJS?
- How to add stroke to a Rectangle using FabricJS?
- How to add stroke to a Textbox using FabricJS?
- How to add stroke to IText using FabricJS?
- How to add stroke to Text using FabricJS?
- How to add stroke to an Ellipse using FabricJS?
- How to set the width of stroke of Circle using FabricJS?
- How to add shadow to a Circle using FabricJS?
- How to set stroke width of a canvas circle using Fabric.js ?
- How to maintain stroke width of a Rectangle while scaling using FabricJS?
