- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to set the opacity of a Circle using FabricJS?
In this tutorial, we are going to learn how to set the opacity of 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. We can customize a circle object by adding a fill colour to it, eliminate its borders or even make changes in its dimensions. Similarly, we can also change its opacity by using the opacity property.
Syntax
new fabric.Circle({ opacity: Number }: Object)
Parameters
options (optional) − This parameter is an Object which provides additional customizations to our circle. Using this parameter colour, cursor, border width and a lot of other properties can be changed related to the object of which opacity is a property
Options Keys
opacity − This property accepts a Number that allows us to control the opacity of an object. The default value of opacity property is 1.
Example 1
Default appearance of a circle object
Let's see a code to see how our circle object looks like with the default value of opacity 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>Setting the opacity of Circle using FabricJS</h2> <p>Here we haven't used the <b>opacity</b> property, but by default, it is set to 1. This is the default appearance. </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); var circle = new fabric.Circle({ left: 115, top: 50, radius: 50, fill: "#ff1493" }); // Adding it to the canvas canvas.add(circle); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
Example 2
Passing the opacity property as key
In this example, we will see how assigning a value to the opacity property changes the opacity of the circle object in our canvas. Here we have used 0.3 as opacity which makes our circle object look translucent instead of fully opaque.
<!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>Setting the opacity of Circle using FabricJS</h2> <p>Here we have set the <b>opacity</b> at 0.3, which is why the circle appears dull. </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); var circle = new fabric.Circle({ left: 115, top: 50, radius: 50, fill: "#ff1493", opacity: 0.3 }); // Adding it to the canvas canvas.add(circle); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
- Related Articles
- How to set the border opacity of a Circle while moving using FabricJS?
- How to set the opacity of a Rectangle using FabricJS?
- How to set the opacity of Triangle using FabricJS?
- How to set the opacity of Textbox using FabricJS?
- How to set the opacity of Image using FabricJS?
- How to set the opacity of an Ellipse using FabricJS?
- How to set the border opacity of a Triangle while moving using FabricJS?
- How to set the height of a Circle using FabricJS?
- How to set the padding of a Circle using FabricJS?
- How to set the radius of a Circle using FabricJS?
- How to set the border opacity of Rectangle while moving using FabricJS?
- How to set the border opacity of Textbox while moving using FabricJS?
- How to set the fill color of a Circle using FabricJS?
- How to set the angle of rotation of a Circle using FabricJS?
- How to set the horizontal scale factor of a Circle using FabricJS?
