How to set the dash pattern of controlling corners of Triangle using FabricJS?


<p>In this tutorial, we are going to learn how we can implement the dash pattern of controlling corners of Triangle using FabricJS. The controlling corners of an object allow us to scale, stretch or change its position.</p><p>We can customize our controlling corners in many ways such as adding a specific colour to it, changing its size, etc. We can also specify a dash pattern for the controlling corners by using the <em>cornerDashArray</em> property.</p><h2>Syntax</h2><pre class="just-code notranslate language-javascript" data-lang="javascript">new fabric.Triangle({ cornerDashArray: Array }: Object)</pre><h2>Parameters</h2><ul class="list"><li><p><strong>Options</strong><strong> (optional)</strong> − This parameter is an <strong>Object</strong> which provides additional customizations to our triangle. Using this parameter, properties such as colour, cursor, stroke width, and a lot of other properties can be changed related to the object of which <em>cornerDashArray</em> is a property.</p></li></ul><h2>Options Keys</h2><ul class="list"><li><p><strong>cornerDashArray</strong> − This property accepts an <strong>Array</strong> which allows us to specify a dash pattern for the controlling corners. 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.</p></li></ul><h2>Example 1</h2><p><strong>Default appearance of controlling corners</strong></p><p>Let's see a code example that depicts the default appearance of the controlling corners of a triangle object. Since we have not used the <em>cornerDashArray</em> property, there is no dash pattern being displayed.</p><pre class="demo-code notranslate language-javascript" data-lang="javascript"><!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 controlling corners</h2>    <p>Select the triangle to see the default appearance of the controlling corners.</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 triangle object       var triangle = new fabric.Triangle({          left: 155,          top: 60,          width: 100,          height: 70,          fill: "#5f9ea0",          cornerColor: "rgba(255,103,0,0.9)",       });       // Add it to the canvas       canvas.add(triangle);    </script> </body> </html></pre><h2>Example 2</h2><p><strong>Passing <em>cornerDashArray</em> property as key</strong></p><p>In this example, we are passing the <em>cornerDashArray</em> property a value of [1,2,1]. This means that a dash pattern will be created such that there is a 1px long line, followed by a 2px gap, then again a 1px long line will be drawn and after which a 1px gap will be made and so on.</p><pre class="demo-code notranslate language-javascript" data-lang="javascript"><!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 cornerDashArray property as key</h2>    <p>Select the triangle to see the appearance of the controlling corners with dash pattern.</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 triangle object       var triangle = new fabric.Triangle({          left: 155,          top: 60,          width: 100,          height: 70,          fill: "#5f9ea0",          cornerColor: "rgba(255,103,0,0.9)",          cornerDashArray: [1, 2, 1],       });       // Add it to the canvas       canvas.add(triangle);    </script> </body> </html></pre>

Updated on: 27-Jun-2022

72 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements