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

Passing the editingBorderColor property as key

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>

Updated on: 02-Aug-2022

333 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements