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


<p>In this tutorial, we are going to set the colour of the controlling corners of Triangle using FabricJS. The <em>cornerColor</em> property allows us to manipulate the colour of the controlling corners when the object is active.</p><h2>Syntax</h2><pre class="just-code notranslate language-javascript" data-lang="javascript">new fabric.Triangle({ cornerColor: 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 the <em>cornerColor</em> is a property.</p></li></ul><h2>Options Keys</h2><ul class="list"><li><p><strong>cornerColor</strong> − This property accepts a <em><strong>String</strong></em> which allows us to assign a color to the controlling corners when the object is actively selected. The default value is rgb(<em>178,204,255</em>).</p></li></ul><h2>Example 1</h2><p><strong>Passing <em>cornerColor</em> as key with a color name as value</strong></p><p>Let's see a code example to change the colour by using the <em>cornerColor</em> property. In this case, we have assigned the value "orange" to the key which thus makes the controlling corners appear orange.</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 cornerColor as key with a color name as value</h2>    <p>Select the triangle to see the corner colour is orange</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: "orange",       });       // Add it to the canvas       canvas.add(triangle);    </script> </body> </html></pre><h2>Example 2</h2><p><strong>Assigning an RGBA value to the <em>cornerColor</em> property</strong></p><p>Instead of passing a simple colour name as the <strong>String</strong> value to the key, we can also assign an RGBA value. RGBA stands for red, green, blue and alpha where alpha is the opacity. Let us see a code example of how we can do that:</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>Assigning an RGBA value to the cornerColor property</h2>    <p>Select the triangle to see the corner colour applied using RGBA value</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>

Updated on: 27-Jun-2022

83 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements