- 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 minimum allowed scale value of Textbox using FabricJS?
In this tutorial, we are going to learn how to set the minimum allowed scale 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. Similarly, we can also set its minimum allowed scale by using the minScaleLimit property.
Syntax
new fabric.Textbox(text: String, { minScaleLimit : 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, properties such as colour, cursor, border width and a lot of other properties can be changed related to the object of which minScaleLimit is a property.
Options Keys
minScaleLimit − This property accepts a Number as a value which allows us to control the minimum allowed scale value of a textbox.
Example 1
Default appearance of the Textbox object
Let’s see a code example to see how our textbox object looks like when the minScaleLimit property is not used. In this case, we will be able to freely scale our object since there is no minimum limit set.
<!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 the Textbox object</h2> <p>You can scale the textbox object to see that there is no minimum limit set</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("That which does not kill us makes us stronger.", { backgroundColor: "#e5e4e2", width: 400, left: 50, top: 70, fill: "#e1a95f", }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>
Example 2
Passing the minScaleLimit property as key with a custom value
In this example, we will see how assigning a value to the minScaleLimit property changes the minimum allowed scale value of the textbox object in our canvas. Here we have used 0.8 as value which means we will not be able to scale down our object lesser than a width of 240px which is calculated by width * limit (0.8 *300 = 240px).
<!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 minScaleLimit property as key with a custom value</h2> <p> You can scale the textbox object to see that there is a minimum limit set</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("That which does not kill us makes us stronger.", { backgroundColor: "#e5e4e2", width: 400, left: 50, top: 70, fill: "#e1a95f", minScaleLimit: 0.8, }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>