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 make controlling corners of Text transparent using FabricJS?
In this tutorial, we are going to learn how to make the controlling corners of Text transparent using FabricJS. We can display text on canvas by adding an instance of fabric.Text. Not only does it allow us to move, scale and change the dimensions of the text but it also provides additional functionality like text alignment, text decoration, line height which can be obtained by the properties textAlign, underline and lineHeight respectively. The transparentCorners property allows us to make the controlling corners of Text object transparent.
Syntax
new fabric.Text(text: String, { transparentCorners: Boolean }: Object)
Parameters
text ? This parameter accepts a String which is the text string that we want to display.
options (optional) ? This parameter is an Object which provides additional customizations to our text. Using this parameter colour, cursor, stroke width and a lot of other properties can be changed related to the object of which transparentCorners is a property.
Options Keys
transparentCorners ? This property accepts a Boolean value which allows us to render the controlling corners of an object as transparent. Its default value is true.
Example 1: Non-Transparent Corners
Passing transparentCorners property as key with a 'false' value
Let's see a code example to create a text object with controlling corners that are not transparent. To achieve this, we need to pass the transparentCorners property a false value.
<!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 transparentCorners property as key with a 'false' value</h2>
<p>You can select the text object to see that the controlling corners are 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 text object
var text = new fabric.Text("Add sample text here.", {
left: 60,
top: 70,
fill: "violet",
stroke: "blue",
textAlign: "center",
transparentCorners: false,
});
// Add it to the canvas
canvas.add(text);
</script>
</body>
</html>
Example 2: Transparent Corners
Passing transparentCorners as key with a 'true' value
In this example, we are passing the transparentCorners property a true value. This will make sure that the controlling corners are rendered as transparent. Note that this is also the default behaviour.
<!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 transparentCorners as key with a 'true' value</h2>
<p>You can select the text to see that the controlling corners are transparent</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 text object
var text = new fabric.Text("Add sample text here.", {
left: 60,
top: 70,
fill: "violet",
stroke: "blue",
transparentCorners: true,
});
// Add it to the canvas
canvas.add(text);
</script>
</body>
</html>
Key Points
The transparentCorners property controls the visual appearance of the selection corners around Fabric.js objects
When set to false, corners appear as solid opaque squares
When set to true (default), corners appear transparent with only their borders visible
This property affects the user experience when selecting and manipulating text objects on the canvas
Conclusion
The transparentCorners property in FabricJS provides control over the visual appearance of text object selection corners. Use false for opaque corners or true (default) for transparent corners based on your design requirements.
