- 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 IText using FabricJS?
In this tutorial, we are going to learn how to add stroke to IText using FabricJS. The IText class was introduced in FabricJS version 1.4, extends fabric.Text and is used to create IText instances. An IText instance gives us the freedom to select, cut, paste or add new text without additional configurations. There are also various supported key combinations and mouse/touch combinations which make text interactive which are not provided in Text.
Textbox, however, which is based on IText allows us to resize the text rectangle and wraps lines automatically. This is not true for IText as height is not adjusted based on the wrapping of lines. We can manipulate our IText object by using various properties. Similarly, we can add stroke by using the stroke property.
Syntax
new fabric.IText(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 IText object. 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 IText 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 “#097969” which is the colour green.
<!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 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 an itext object var itext = new fabric.IText( "Add sample text here.
Lorem ipsum dolor sit amet
consectetur adipiscing.",{ width: 300, left: 50, top: 70, fill: "white", stroke: "#097969", } ); // Add it to the canvas canvas.add(itext); </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 rgba(255,11,15,1) which is the colour red 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 red 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 an itext object var itext = new fabric.IText( "Add sample text here.
Lorem ipsum dolor sit amet
consectetur adipiscing.",{ width: 300, left: 50, top: 70, fill: "white", stroke: "rgba(255,11,15,1)", } ); // Add it to the canvas canvas.add(itext); </script> </body> </html>
- Related Articles
- How to add stroke to Text using FabricJS?
- How to check if the IText object has stroke using FabricJS?
- How to add linethrough 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 underline in IText 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 space between characters in IText using FabricJS?
