
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create Textbox with Background Colour Using Fabric.js
In this tutorial, we are going to create a Textbox with background colour using FabricJs. We can customize, stretch or move around the text written in a textbox. We can also customize the text itself by using properties like fontSize, fontFamily, fontStyle, fontWeight etc. In order to create a textbox, we will have to create an instance of fabric.Textbox class and add it to the canvas. The backgroundColor property allows us to assign a colour to our object’s background and it is rectangular in shape for the textbox.
Syntax
new fabric.Textbox(text: String, { backgroundColor: 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, properties such as colour, cursor, stroke width, and a lot of other properties can be changed related to the Textbox of which backgroundColor is a property.
Options Keys
backgroundColor This property accepts a String value which determines the background colour.
Example 1
Passing backgroundColor property as key with a hexadecimal value
Let’s see a code example to assign a background colour to our Textbox object using hexadecimal value of colour. In this example we have used the hex colour code "#ffe4e1" which is a very light shade of red.
<!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 backgroundColor property as key with a hexadecimal value</h2> <p>You can see that the background colour is a very light shade of 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("Details matter, it's worth waiting to get it right.", { backgroundColor: "#ffe4e1", width: 400, top: 70, left: 110, }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>
Example 2
Passing backgroundColor property as key with a rgba value
We can use an RGBA value, instead of a hexadecimal colour code, which stands for red, green, blue and alpha. The alpha parameter specifies the opacity of a colour. In this example, we have used the rgba value (0,206,209,0.4) which is the colour dark turquoise with 0.4 opacity.
<!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 backgroundColor property as key with an RGBA value</h2> <p>You can see that the background colour is a dark turquoise colour with 0.4 opacity.</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("Details matter, it's worth waiting to get it right.", { backgroundColor: "rgba(0,206,209, 0.4)", width: 400, top: 70, left: 110, }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>