How to set the border opacity of a Triangle while moving using FabricJS?


<p>In this tutorial, we are going to set the border opacity of a Triangle while moving using FabricJS. Triangle is one of the various shapes provided by <em>FabricJS</em>. In order to create a triangle, we will have to create an instance of fabric.Triangle class and add it to the canvas. We can change the opacity of a triangle while moving it around in the canvas by using the <em>borderOpacityWhenMoving</em> property.</p><h2>Syntax</h2><pre class="just-code notranslate language-javascript" data-lang="javascript">new fabric.Triangle({ borderOpacityWhenMoving: 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>borderOpacityWhenMoving</em> is a property.</p></li></ul><h2>Options Keys</h2><ul class="list"><li><p><strong>borderOpacityWhenMoving</strong> − This property accepts a <strong>Number</strong> that specifies how opaque we want the borders to be while moving the triangle around. It allows us to control the opacity of the borders while moving a triangle object. The default value is <em>0.4.</em></p></li></ul><h2>Example 1</h2><p><strong>Displaying the default behaviour of <em>borderOpacityWhenMoving</em> property</strong></p><p>Let's see a code example that shows the default behaviour of <em>boderOpacityWhenMoving</em> property. When we select our triangle object and move it around the canvas, the selection border changes its opacity from 1 (fully opaque) to 0.4 which makes it look a bit translucent.</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>Displaying the default behaviour of borderOpacityWhenMoving property</h2>    <p>Move the triangle to see the default behaviour</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",       });       // Add it to the canvas       canvas.add(triangle);    </script> </body> </html></pre><h2>Example 2</h2><p><strong>Passing <em>borderOpacityWhenMoving</em> as key</strong></p><p>Let's see a code example to assign a value to the <em>borderOpacityWhenMoving</em> property. In this case we have assigned the value as 0. This tells us that when we move our triangle around, the border opacity will change to 0 and will not be visible.</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 borderOpacityWhenMoving as key</h2>    <p>Move the triangle to see the change in value of borderOpacityWhenMoving</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",          borderOpacityWhenMoving: 0,       });       // Add it to the canvas       canvas.add(triangle);    </script> </body> </html></pre>

Updated on: 27-Jun-2022

77 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements