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 scale factor of Textbox using FabricJS?
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, { borderScaleFactor: 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: Using Custom borderScaleFactor Value
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>
Key Points
The borderScaleFactor property only affects the border thickness when the textbox is selected
Higher values create thicker borders, making objects easier to manipulate
This property is particularly useful when working with small objects that need more prominent selection borders
Conclusion
The borderScaleFactor property in FabricJS provides control over the selection border thickness of textbox objects. Use higher values for better visibility when selecting and manipulating textbox elements.
