
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 Circle Using Fabric.js
In this tutorial, we are going to learn how to add a shadow to a Circle using FabricJS. Circle is one of the various shapes provided by FabricJS. In order to create a circle, we will create an instance of fabric.Circle class and add it to the canvas. Our circle 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 a Circle by using the shadow property.
Syntax
new fabric.Circle({ shadow : fabric.Shadow }: Object)
Parameters
- options (optional) − This parameter is an Object which provides additional customizations to our circle. 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. For example, in order to add a shadow to our Circle object we need to create a new instance of fabric.Shadow and then assign that instance to shadow property.
Example 1
Passing the shadow object to the shadow property
Let's see an example to understand how we can assign the shadow property value, by which a shadow will be added to our Circle 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>Adding shadow to a circle using FabricJS</h2> <p>Notice the shadow around the circle object. Here we have applied the <b>shadow</b> property. </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); // Initiate a shadow instance var shadow = new fabric.Shadow({ color: "black", blur: 20 }); var circle = new fabric.Circle({ left: 115, top: 50, radius: 50, fill: "#85bb65", shadow: shadow }); canvas.add(circle); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </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, blue, green 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>Adding shadow to a circle using FabricJS</h2> <p>Notice the shadow around the circle object. Here we have passed an 'rgba' value to assign the color.</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); // Initiate a shadow instance var shadow = new fabric.Shadow({ color: "rgba(0,128,0,0.8)", blur: 20 }); var circle = new fabric.Circle({ left: 115, top: 50, radius: 50, fill: "#85bb65", shadow: shadow }); canvas.add(circle); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>