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>

Updated on: 12-Sep-2022

164 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements