
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 Fill Colour of a Triangle Using Fabric.js
<p>In this tutorial, we are going to learn how we can change the appearance of a Triangle object by changing its fill colour 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 the fill colour by using the <em>fill</em> property which allows us to specify the colour of the object's fill.</p><h2>Syntax</h2><pre class="just-code notranslate language-javascript" data-lang="javascript">new fabric.Triangle({ fill: 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>fill</em> is a property.</p></li></ul><h2>Options Keys</h2><ul class="list"><li><p><strong>fill</strong> This property accepts a <strong>String</strong> which allows us to change an object's fill colour. Its default value is <em>rgb(0,0,0)</em> which is the colour black.</p></li></ul><h2>Example 1</h2><p><strong>Default fill colour of a triangle object</strong></p><p>Let's see a code example that shows us the default fill colour of a triangle object in FabricJS. If we completely skip the fill property while creating a triangle object, it will be rendered with the fill colour as black.</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 fill colour of a triangle object</h2> <p>You can see that the default fill colour of triangle is black</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, stroke: "#507d2a", strokeWidth: 5, }); // Add it to the canvas canvas.add(triangle); </script> </body> </html></pre><h2>Example 2</h2><p><strong>Passing the <em>fill</em> property as key</strong></p><p>We can also assign the <em>fill</em> property any colour name or RGBA value. In this example, we have assigned it a value of "blue" which thus changes the fill colour accordingly.</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 the fill property as key</h2> <p>You can see that the fill colour of triangle is blue</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, stroke: "#507d2a", strokeWidth: 5, fill: "blue", }); // Add it to the canvas canvas.add(triangle); </script> </body> </html></pre>