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 border colour of text object while in editing mode in FabricJS?
In this tutorial, we are going to learn how to set the border color of text object while in editing mode using FabricJS. We can customize a textbox object by adding a fill colour to it, eliminate its borders or even make changes in its dimensions. Similarly, it can be indicated whether a text is editable or not. We can also change the border colour of text object in its editing mode by using the property called editingBorderColor.
Syntax
new fabric.Textbox(text: String, { editingBorderColor: String }: 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 colour, cursor, border width and a lot of other properties can be changed related to the object of which editingBorderColor is a property.
Options Keys
-
editingBorderColor: This property accepts a String that allows us to control the border color of text object while in editing mode. Default value of editingBorderColor property is rgba(102,153,255,0.25).
Example 1: 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 editingBorderColor property. We will not pass any editingBorderColor key to the class in this example as given below ?
<!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 double click on the textbox to enable editing mode</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("Being positive is a sign of intelligence.", {
left: 110,
top: 45,
fill: "black",
stroke: "green",
width: 400,
backgroundColor: "#ffffe7",
});
// Add it to the canvas
canvas.add(textbox);
</script>
</body>
</html>
Example 2: Using the editingBorderColor Property
In this example, we will see how assigning a value to the editingBorderColor property changes the color of the border of text object in editing mode. Here we have used the color "red" to demonstrate that.
<!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 editingBorderColor property as key</h2>
<p>You can double click on the text object to see that in editing mode the colour of the border is red</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("Being positive is a sign of intelligence.", {
left: 110,
top: 45,
fill: "black",
stroke: "green",
width: 400,
backgroundColor: "#ffffe7",
editingBorderColor: "red",
});
// Add it to the canvas
canvas.add(textbox);
</script>
</body>
</html>
Key Points
- The
editingBorderColorproperty only affects the border color when the textbox is in editing mode (double-clicked). - The default value is
rgba(102,153,255,0.25)which provides a light blue border. - You can use any valid CSS color value including hex codes, RGB values, or named colors.
- This property is separate from the regular
strokeproperty which affects the border in normal mode.
Conclusion
The editingBorderColor property in FabricJS allows you to customize the visual appearance of textbox borders during editing mode. This enhances user experience by providing clear visual feedback when text editing is active.
