How to set the border scale factor of Triangle using FabricJS?


<p>In this tutorial, we are going to set the border scale factor of Triangle using FabricJS. Triangle is one of the various shapes provided by FabricJS. In order to create a triangle, we will have to create an instance of <em>fabric.Triangle</em> class and add it to the canvas.</p><p>We can use the <em>borderScaleFactor</em> property which specifies the scale factor of the object's controlling borders.</p><h2>Syntax</h2><pre class="just-code notranslate language-javascript" data-lang="javascript">new fabric.Triangle({ borderScaleFactor: Number }: Object)</pre><h2>Parameters</h2><ul class="list"><li><p><strong>Options</strong><strong> (optional) </strong>− This parameter is an <em>Object</em> 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>borderScaleFactor</em> is a property.</p></li></ul><h2>Options Keys</h2><ul class="list"><li><p><strong>borderScaleFactor</strong> − This property accepts a <em>Number</em> which specifies the border thickness. The default value is 1.</p></li></ul><h2>Example 1</h2><p><strong>Default behaviour of <em>borderScaleFactor</em> property</strong></p><p>Let's see a code example that depicts the default behaviour of the <em>borderScaleFactor</em> property. Although we have specified it in this example, by default the value that <em>borderScaleFactor</em> uses even when unspecified is 1.</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 behaviour of borderScaleFactor property</h2>    <p>Select the triangle to see the default value of borderScaleFactor</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: 105,          top: 60,          width: 100,          height: 70,          fill: "#deb887",          borderColor: "red",          borderScaleFactor: 1,       });       // Add it to the canvas       canvas.add(triangle);    </script> </body> </html></pre><h2>Example 2</h2><p><strong>Passing borderScaleFactor as key</strong></p><p>Let's see a code example to increase the border thickness of the triangle object when it is actively selected. In this example, we have assigned the <em>borderScaleFactor</em> a value of 5 which specifies the thickness of our border.</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 borderScaleFactor as key</h2>    <p>Select the triangle to see the changed value of borderScaleFactor</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: 105,          top: 60,          width: 100,          height: 70,          fill: "#deb887",          borderColor: "red",          borderScaleFactor: 5,       });       // Add it to the canvas       canvas.add(triangle);    </script> </body> </html></pre>

Updated on: 27-Jun-2022

84 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements