How to make controlling corners of Text transparent using FabricJS?


In this tutorial, we are going to learn how to make the controlling corners of Text transparent 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. The transparentCorners property allows us to make the controlling corners of Text object transparent.

Syntax

new fabric.Text(text: String ,{ transparentCorners: Boolean }: 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 transparentCorners is a property.

Options Keys

  • transparentCorners − This property accepts a Boolean value which allows us to render the controlling corners of an object as transparent. Its default value is true.

Example 1

Passing transparentCorners property as key with a ‘false’ value

Let’s see a code example to create a text object with controlling corners that are not transparent. To achieve this, we need to pass the transparentCorners property a false value.

<!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 transparentCorners property as key with a ‘false’ value</h2> <p>You can select the text object to see that the controlling corners are opaque</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.", { left: 60, top: 70, fill: "violet", stroke: "blue", textAlign: "center", transparentCorners: false, }); // Add it to the canvas canvas.add(text); </script> </body> </html>

Example 2

Passing transparentCorners as key with a ‘true’ value

In this example, we are passing the transparentCorners property a true value. This will make sure that the controlling corners are rendered as transparent. Note, that this is also the default behaviour

<!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 transparentCorners as key with a ‘true’ value</h2> <p>You can select the text to see that the controlling corners are transparent</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.", { left: 60, top: 70, fill: "violet", stroke: "blue", transparentCorners: true, }); // Add it to the canvas canvas.add(text); </script> </body> </html>

Updated on: 14-Sep-2022

58 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements