
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
Add Shadow to a Triangle Using Fabric.js
In this tutorial, we are going to learn how to add a shadow to 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 fabric.Triangle class and add it to the canvas.
Our triangle object can be customized in various ways like changing its dimensions, adding a background color or even adding a shadow to it. We can add a shadow to the triangle by using the shadow property.
Syntax
new fabric.Triangle({ shadow : fabric.Shadow }: Object)
Parameters
Options (optional) This parameter is an Object 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 shadow is a property.
Options Keys
Shadow This property accepts a fabric.Shadow object which allows us to add a shadow to our triangle object.
Example 1
Passing the shadow object to the shadow property
Let's see a code example to understand how we can assign the shadow property a value such that a shadow is added to our triangle object. Firstly, we need to make a new instance of fabric.Shadow and then assign that instance to our shadow property.
<!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 shadow object to the shadow property</h2> <p>You can see that an orange shadow has been added to the triangle</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 shadow instance var shadow = new fabric.Shadow({ color: "orange", blur: 20, }); // Initiate a triangle object var triangle = new fabric.Triangle({ left: 120, top: 70, width: 90, height: 80, fill: "#228b22", stroke: "#d8e4bc", strokeWidth: 7, shadow: shadow, }); // Add it to the canvas canvas.add(triangle); </script> </body> </html>
Example 2
Passing an RGBA value to the shadow object
We can also adjust the shadow and give it a blurred out appearance by assigning it an RGBA value which stands for red, green, blue and alpha. Alpha determines the opacity of the colour.
<!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 an rgba value to the shadow object</h2> <p>You can see the shadow created using RGBA colour 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 shadow instance var shadow = new fabric.Shadow({ color: "rgba(139,0,139,0.8)", blur: 20, }); // Initiate a triangle object var triangle = new fabric.Triangle({ left: 120, top: 70, width: 90, height: 80, fill: "#228b22", stroke: "#d8e4bc", strokeWidth: 7, shadow: shadow, }); // Add it to the canvas canvas.add(triangle); </script> </body> </html>