How to set the style of controlling corners of a Triangle using FabricJS?


<p>In this tutorial, we are going to learn how to set the style of controlling corners 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>The controlling corners of an object allow us to scale, stretch or change its position. We can customize our controlling corners in many ways such as adding a specific colour to it, changing its size, etc. We can change the style by using the <em>cornerStyle</em> property.</p><h2>Syntax</h2><pre class="just-code notranslate language-javascript" data-lang="javascript">new fabric.Triangle({ cornerStyle: String }: 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>cornerStyle</em> is a property.</p></li></ul><h2>Options Keys</h2><ul class="list"><li><p><strong>cornerStyle</strong> − This property accepts a <strong>String</strong> which allows us to specify the style of controlling corners that we want.</p></li></ul><h2>Example 1</h2><p><strong>Default style of controlling corners</strong></p><p>Let's see a code example that shows the default style of controlling corners.</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 style of controlling corners</h2>    <p>Select the triangle to see the default style of 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: 105,          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>cornerStyle</em> as key with the value "circle"</strong></p><p>We can specify the style or appearance of the controlling corners of an actively selected object by passing the values as "circle" or "rect". Passing the value as "circle" will make the controlling corners appear circular, as we have done in the example below −</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 cornerStyle as key with the value "circle"</h2>    <p>Select the triangle to see that the style of controlling corners is circle now</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: "#5f9ea0",          cornerColor: "rgba(255,103,0,0.9)",          cornerStyle: "circle",       });       // Add it to the canvas       canvas.add(triangle);    </script> </body> </html></pre>

Updated on: 27-Jun-2022

113 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements