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 the controlling corners of a Textbox transparent using FabricJS?
In this tutorial, we are going to learn how to make the controlling corners of a Textbox transparent using FabricJS. We can customize, stretch or move around the text written in a textbox. In order to create a textbox, we will have to create an instance of fabric.Textbox class and add it to the canvas. The transparentCorners property allows us to make the controlling corners of Textbox transparent.
Syntax
new fabric.Textbox(text: String, { transparentCorners: Boolean }: 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, stroke width, and a lot of other properties can be changed related to the object of which transparentCorners is a property.
transparentCorners Property
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: Opaque Corners (transparentCorners: false)
Let's see a code example to create a textbox 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 textbox 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 textbox object
var textbox = new fabric.Textbox("Failure is success if we learn from it.", {
backgroundColor: "#fffff0",
width: 400,
left: 110,
top: 70,
fill: "violet",
strokeWidth: 2,
stroke: "blue",
textAlign: "center",
transparentCorners: false,
});
// Add it to the canvas
canvas.add(textbox);
</script>
</body>
</html>
Example 2: Transparent Corners (transparentCorners: true)
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 textbox 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 textbox object
var textbox = new fabric.Textbox("Failure is success if we learn from it.", {
backgroundColor: "#fffff0",
width: 400,
left: 110,
top: 70,
fill: "violet",
strokeWidth: 2,
stroke: "blue",
textAlign: "center",
transparentCorners: true,
});
// Add it to the canvas
canvas.add(textbox);
</script>
</body>
</html>
Key Points
The
transparentCornersproperty controls the visual appearance of selection corners when a textbox is selected.Setting it to
falsemakes corners solid and visible, whiletruemakes them transparent.The default value is
true, so corners are transparent by default.This property is useful for customizing the user interface and selection feedback in canvas applications.
Conclusion
The transparentCorners property in FabricJS allows you to control the visual appearance of textbox selection corners. Use false for opaque corners or true for transparent corners based on your design requirements.
