How to make the controlling corners of a Triangle transparent using FabricJS?


<p>In this tutorial, we are going to learn how to make the controlling corners of Triangle transparent 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. The <em>transparentCorners</em> property allows us to make the controlling corners of Triangle transparent.</p><h2>Syntax</h2><pre class="just-code notranslate language-javascript" data-lang="javascript">new fabric.Triangle( { transparentCorners: Boolean }: 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>transparentCorners </em>is a property.</p></li></ul><h2>Options Keys</h2><ul class="list"><li><p><strong>transparentCorners</strong> − This property accepts a <strong>Boolean</strong> value which allows us to render the controlling corners of an object as transparent or not transparent. Its default value is true.</p></li></ul><h2>Example 1</h2><p><strong>Passing <em>transparentCorners</em> property as key with a 'false' value</strong></p><p>Let's see a code example to create a triangle object with controlling corners that are not transparent. To achieve this, we need to pass the <em>transparentCorners</em> property a false value.</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 transparentCorners property as key with a 'false' value</h2>    <p>You can select the triangle to see that the controlling corners are opaque.</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: 180,          top: 80,          width: 90,          height: 80,          fill: "#b0e0e6",          stroke: "#5f9ea0",          strokeWidth: 7,          transparentCorners: false,       });       // Add it to the canvas       canvas.add(triangle);    </script> </body> </html></pre><h2>Example 2</h2><p><strong>Passing <em>transparentCorners</em> as key with a 'true' value</strong></p><p>In this example, we are passing the <em>transparentCorners</em> property a true value. This will make sure that the controlling corners are rendered as transparent. Note that this is also the default behaviour.</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 transparentCorners as key with a 'true' value</h2>    <p>You can select the triangle to see that the controlling corners are transparent.</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: 180,          top: 80,          width: 90,          height: 80,          fill: "#b0e0e6",          stroke: "#5f9ea0",          strokeWidth: 7,          transparentCorners: true,       });       // 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