
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Set Background Colour of Triangle Selection using Fabric.js
<p>In this tutorial, we are going to learn how to set the background colour of selection of a 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 change an object's dimensions, rotate it or manipulate it when it is actively selected. We can change the background colour of selection of triangle by using the <em>selectionBackgroundColor</em> property.</p><h2>Syntax</h2><pre class="just-code notranslate language-javascript" data-lang="javascript">new fabric.Triangle({ selectionBackgroundColor : 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>selectionBackgroundColor</em> is a property.</p></li></ul><h2>Options Keys</h2><ul class="list"><li><p><strong>selectionBackgroundColor</strong> This property accepts a <strong>String</strong> value. The value that is assigned will determine the background colour of the selection.</p></li></ul><h2>Example 1</h2><p><strong>Default colour when <em>selectionBackgroundColor</em> property is not used</strong></p><p>Let's see a code example to understand how the selection appears when the <em>selectionBackgroundColor </em>property is not used. As we can see from this example, the selection area or the area behind the object has no colour.</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 colour when selectionBackgroundColor property is not used</h2> <p>You can select the triangle to see that the selection area has no colour.</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: 70, width: 90, height: 80, fill: "#228b22", stroke: "#d8e4bc", strokeWidth: 7, padding: 30, }); // Add it to the canvas canvas.add(triangle); </script> </body> </html></pre><h2>Example 2</h2><p><strong>Passing <em>selectionBackgroundColor</em> property as key</strong></p><p>In this example, we are assigning a value to the <em>selectionBackgroundColor</em> property. In this case, we have passed it the hexadecimal value "da70d6" which is the colour magenta and hence the selection area appears to be of that colour.</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 selectionBackgroundColor property as key</h2> <p>You can select the triangle to see that the selection area now has a magenta colour</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: 70, width: 90, height: 80, fill: "#228b22", stroke: "#d8e4bc", strokeWidth: 7, padding: 30, selectionBackgroundColor: "#da70d6", }); // Add it to the canvas canvas.add(triangle); </script> </body> </html></pre>