Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to set the opacity of Image using FabricJS?
In this tutorial, we are going to learn how to set the opacity of Image using FabricJS. We can create an Image object by creating an instance of fabric.Image. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc. In order to set the opacity of Image, we use the opacity property.
Syntax
new fabric.Image( element: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | String, { opacity: Number }: Object, callback: function)
Parameters
element ? This parameter accepts HTMLImageElement, HTMLCanvasElement, HTMLVideoElement or String which denotes the image element. The String should be a URL and would be loaded as an image.
options (optional) ? This parameter is an Object which provides additional customizations to our object. Using this parameter origin, stroke width and a lot of other properties can be changed related to the image object of which opacity is a property.
callback (optional) ? This parameter is a function which is to be called after eventual filters are applied.
Options Keys
opacity ? This property accepts a Number that allows us to control the opacity of an object. Default value of opacity property is 1 (fully opaque). Values range from 0 (transparent) to 1 (fully opaque).
Default Appearance of Image Object
Let's see a code example to see how our image object looks like with the default value of opacity property. We will not pass any opacity key in this example:
<!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 appearance of Image object</h2>
<p>You can see that the image object is fully opaque</p>
<canvas id="canvas"></canvas>
<img src="https://www.tutorialspoint.com/images/logo.png" id="img1" style="display: none" />
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiating the image element
var imageElement = document.getElementById("img1");
// Initiate an Image object
var image = new fabric.Image(imageElement, {
top: 50,
left: 50,
});
// Add it to the canvas
canvas.add(image);
</script>
</body>
</html>
Using the opacity Property
In this example, we will see how assigning a value to the opacity property changes the opacity of the image object in our canvas. Here we have used 0.3 as opacity which thus makes our image 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>Passing the opacity property as key</h2>
<p>You can see our image object is not fully opaque</p>
<canvas id="canvas"></canvas>
<img src="https://www.tutorialspoint.com/images/logo.png" id="img1" style="display: none" />
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiating the image element
var imageElement = document.getElementById("img1");
// Initiate an Image object
var image = new fabric.Image(imageElement, {
top: 50,
left: 50,
opacity: 0.3,
});
// Add it to the canvas
canvas.add(image);
</script>
</body>
</html>
Key Points
The
opacityproperty accepts values between 0 and 1Default opacity value is 1 (fully opaque)
Value 0 makes the image completely transparent
Values between 0 and 1 create semi-transparent effects
Conclusion
The opacity property in FabricJS allows you to control the transparency of image objects. By adjusting values from 0 to 1, you can create various visual effects from fully transparent to fully opaque images.
