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 edit mode of Textbox using FabricJS?
In this tutorial, we are going to learn how to enable the edit mode of a Textbox using FabricJS. Just as we can specify the position, colour, opacity and dimension of a textbox object in the canvas, we can also edit the text in our Textbox. This can be enabled or disabled by using the editable property.
Syntax
new fabric.Textbox(text: String, { editable : 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 editable is a property.
Options Keys
editable: This property accepts a Boolean value. If we assign it a "true" value, then we are allowed to edit the text inside a Textbox. Its default value is true.
Example 1: Default Editable Behavior
Let's see a code example to understand the default behaviour of a Textbox object when editable property is not used. We are allowed to edit the text inside a Textbox object by default.
<!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 behaviour of a Textbox object in the canvas</h2>
<p>You can double click on the Textbox and edit the text</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("It doesn't matter how slow you go, as long as you don't stop.", {
width: 400,
left: 110,
top: 70,
fill: "orange",
stroke: "green",
textAlign: "center",
});
// Add it to the canvas
canvas.add(textbox);
</script>
</body>
</html>
Example 2: Disabling Edit Mode
In this example, we will see how we can make the text inside the textbox object uneditable by using the editable property. As we can see, although we can select the textbox object, we can no longer edit the text inside it.
<!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 editable as key with "false" value</h2>
<p>You can double-click on the textbox to see that the text is not editable</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("It doesn't matter how slow you go, as long as you don't stop.", {
width: 400,
left: 110,
top: 70,
fill: "orange",
stroke: "green",
textAlign: "center",
editable: false,
});
// Add it to the canvas
canvas.add(textbox);
</script>
</body>
</html>
Key Points
- The
editableproperty controls whether users can edit text in a Textbox - Default value is
true, allowing text editing by double-clicking - Setting
editable: falsemakes the text read-only while preserving selection - This property is useful for creating labels or fixed text elements in interactive canvases
Conclusion
The editable property in FabricJS Textbox provides precise control over text editing functionality. Use editable: false to create read-only text elements while maintaining visual interactivity.
