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 Textbox using FabricJS?
In this tutorial, we are going to learn how to set the opacity of Textbox using FabricJS. Textbox is one of the various shapes provided by FabricJS. In order to create a textbox, we will have to create an instance of fabric.Textbox class and add it to the canvas. We can customize a textbox 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.Textbox(text: String, { opacity: Number }: Object)
Parameters
text ? This parameter accepts a String which is the text string that we want to display inside our textbox.
options (optional) ? This parameter is an Object which provides additional customizations to our textbox. Using this parameter, properties such as 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 value ranges from 0 (completely transparent) to 1 (completely opaque). Default value of opacity property is 1.
Example 1: Default Appearance
Default appearance of a textbox object
Let's see a code example to see how our textbox object looks like with the default value of opacity property. In this example, we will not pass any opacity key to the class:
<!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 a textbox object</h2>
<p>You can see our textbox is fully opaque</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 textbox object
var textbox = new fabric.Textbox("Positive thoughts lead to positive results.", {
left: 110,
top: 45,
fill: "orange",
stroke: "green",
width: 400,
backgroundColor: "#f3e5ab",
});
// Add it to the canvas
canvas.add(textbox);
</script>
</body>
</html>
Example 2: Using Opacity Property
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 textbox object in our canvas. Here we have used 0.3 as opacity which makes our textbox 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 textbox is not fully opaque</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 textbox object
var textbox = new fabric.Textbox("Positive thoughts lead to positive results.", {
left: 110,
top: 45,
fill: "orange",
stroke: "green",
width: 400,
backgroundColor: "#f3e5ab",
opacity: 0.3,
});
// Add it to the canvas
canvas.add(textbox);
</script>
</body>
</html>
Key Points
The
opacityproperty accepts values between 0 and 1Value 0 makes the textbox completely transparent
Value 1 makes the textbox completely opaque (default)
Values between 0 and 1 create varying levels of transparency
Conclusion
The opacity property in FabricJS provides an easy way to control the transparency of textbox objects. By adjusting the opacity value between 0 and 1, you can create various visual effects and layering in your canvas applications.
