- 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 background colour of text lines with Text using FabricJS?
In this tutorial, we are going to learn how to set the background colour of text lines with Text using FabricJS. We can display text on canvas by adding an instance of fabric.Text. Not only does it allow us to move, scale and change the dimensions of the text but it also provides additional functionality like text alignment, text decoration, line height which can be obtained by the properties textAlign, underline and lineHeight respectively. Similarly we can also set the background colour of text lines by using the textBackgroundColor property.
Syntax
new fabric.Text(text: String , { textBackgroundColor : String }: Object)
Parameters
text − This parameter accepts a String which is the text string that we want to display.
options (optional) − This parameter is an Object which provides additional customizations to our text. Using this parameter colour, cursor, border width and a lot of other properties can be changed related to the object of which textBackgroundColor is a property.
Options Keys
textBackgroundColor − This property accepts a String value which allows us to set the background colour of text lines.
Example 1
Passing the textBackgroundColor property as key with a hexadecimal value
Let’s see a code example to assign a background colour to our Triangle object using hexadecimal value of colour. In this example, we have used the hex colour code of #ebdef0 which is a lilac colour.
<!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 textBackgroundColor property as key with a hexadecimal value</h2> <p>You can see the background colour of the text lines</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 text object var text = new fabric.Text("Add sample
text here.", { width: 300, left: 60, top: 70, fill: "green", textBackgroundColor: "#ebdef0" }); // Add it to the canvas canvas.add(text); </script> </body> </html>
Example 2
Passing the textBackgroundColor 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 (255,20,147,0.8) which is the colour pink with 0.8 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 the textBackgroundColor property as key with a RGBA value</h2> <p>You can see the new background colour of the text lines</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 text object var text = new fabric.Text("Add sample
text here.", { width: 300, left: 60, top: 70, fill: "green", textBackgroundColor: "rgba(255,20,147,0.2)" }); // Add it to the canvas canvas.add(text); </script> </body> </html>
- Related Articles
- How to set the background colour of text lines with IText using FabricJS?
- How to set the fill colour of text in Textbox using FabricJS?
- How to set the text alignment of Text using FabricJS?
- How to set the text overline of Text using FabricJS?
- How to set the baseline of subscript with Text using FabricJS?
- How to set the baseline of superscript with Text using FabricJS?
- How to set the size of subscript with Text using FabricJS?
- How to set the size of superscript with Text using FabricJS?
- How to set the background colour of selection of Rectangle using FabricJS?
- How to set the background colour of selection of Textbox using FabricJS?
- How to set the background colour of selection of a Triangle using FabricJS?
- How to set the background colour of Line in FabricJS?
- How to set the text overline of IText using FabricJS?
- How to set the duration of animation in Text using FabricJS?
- How to create a Triangle with background colour using FabricJS?
