How to set the border scale factor of Textbox using FabricJS?

FabricJSHTML5 CanvasJavascript

In this article, we are going to set the border scale factor of Textbox 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. We can use the borderScaleFactor property which specifies the scale factor of the object's controlling borders.

Syntax

new fabric.Textbox(text: String, { borderOpacityWhenMoving: Number }: 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, stroke width and a lot of other properties can be changed related to the object of which borderScaleFactor is a property.

Options Keys

  • borderScaleFactor − This property accepts a Number which specifies the border thickness. The default value is 1.

Example 1

Default behaviour of borderScaleFactor property

Let’s see a code example that depicts the default behaviour of the borderScaleFactor property. Although we have specified it in this example, by default the value that borderScaleFactor uses even when unspecified is 1.

<!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 borderScaleFactor property</h2> <p>You can select the textbox to see the border thickness</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("Problems are not stop signs, they are guidelines.", { backgroundColor: "#ffe5b4", width: 400, top: 70, left: 110, borderColor: "red", borderScaleFactor: 1, }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>

Example 2

Passing borderScaleFactor as key

Let’s see a code example to increase the border thickness of the textbox object when it is actively selected. In this example we have assigned the borderScaleFactor a value of 5 which specifies the thickness of our border.

<!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 borderScaleFactor as key</h2> <p>You can select the textbox to see that the border thickness has increased</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("Problems are not stop signs, they are guidelines.", { backgroundColor: "#ffe5b4", width: 400, top: 70, left: 110, borderColor: "red", borderScaleFactor: 5, }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>
raja
Updated on 03-Aug-2022 07:33:25

Advertisements