- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 behaviour of a Textbox object in the canvas
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
Passing editable as key with "false" value
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>
- Related Articles
- How to set the opacity of Textbox using FabricJS?
- How to set the padding of Textbox using FabricJS?
- How to set the width of Textbox using FabricJS?
- How to set the border scale factor of Textbox using FabricJS?
- How to set the horizontal scale factor of Textbox using FabricJS?
- How to set the position of Textbox from left using FabricJS?
- How to set the position of Textbox from top using FabricJS?
- How to set the vertical scale factor of Textbox using FabricJS?
- How to set the angle of rotation of a Textbox using FabricJS?
- How to set the background colour of selection of Textbox using FabricJS?
- How to set the colour of controlling corners of Textbox using FabricJS?
- How to set the style of controlling corners of Textbox using FabricJS?
- How to set the size of the controlling corners of Textbox using FabricJS?
- How to set the alignment of text in a Textbox using FabricJS?
- How to set the border opacity of Textbox while moving using FabricJS?
