- 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 add stroke to Text using FabricJS?
In this tutorial, we are going to learn how to add stroke to 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. We can add stroke by using the stroke property.
Syntax
new fabric.Text(text: String, { stroke: 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, stroke width and a lot of other properties can be changed related to the object of which stroke is a property.
Options Keys
stroke − This property accepts a String which determines the colour of that object's border.
Example 1
Passing stroke property as key with a hexadecimal value
Let’s see a code example to understand how our text object appears when the stroke property is used. A hexadecimal colour code starts with a # followed by a six-digit number representing a colour. In this case we have used “#ffc0cb” which is the colour pink.
<!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 stroke property as key with a hexadecimal value</h2> <p>You can see that the stroke around the text is of pink colour</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", { top: 70, left: 50, fontStyle: "bold", fill: "black", stroke: "#ffc0cb", }); // Add it to the canvas canvas.add(text); </script> </body> </html>
Example 2
Passing an rgba value to the stroke property
In this example, we will see how to assign an rgba value to the stroke property. 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 case, we have passed the rgba value as (0,128,0,1) which is the colour green with opacity 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>Passing an rgba value to the stroke property</h2> <p>You can see that the stroke around the text is of green colour</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", { top: 70, left: 50, fontStyle: "bold", fill: "black", stroke: "rgba(0,128,0,1)", }); // Add it to the canvas canvas.add(text); </script> </body> </html>
- Related Articles
- How to add stroke to IText using FabricJS?
- How to add stroke to an Ellipse using FabricJS?
- How to add stroke to a Circle using FabricJS?
- How to add stroke to a Triangle using FabricJS?
- How to add stroke to a Rectangle using FabricJS?
- How to add stroke to a Textbox using FabricJS?
- How to add dashed stroke to an Ellipse using FabricJS?
- How to add dashed stroke to a Triangle using FabricJS?
- How to add dashed stroke to a Rectangle using FabricJS?
- How to add dashed stroke to a Textbox using FabricJS?
- How to add dashed stroke to a Circle using FabricJS?
- How to add linethrough to Text using FabricJS?
- How to add underline to Text using FabricJS?
- How to add animation in Text using FabricJS?
- How to add subscript with Text using FabricJS?
