Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to add linethrough to Text using FabricJS?
In this tutorial, we are going to learn about how to add linethrough text decoration to a Text object 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, font family, line height which can be obtained by the properties textAlign, fontFamily and lineHeight respectively. We can add linethrough by using the linethrough property.
Syntax
new fabric.Text(text: String , { linethrough: 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, border width and a lot of other properties can be changed related to the object of which linethrough is a property.
Options Keys
linethrough ? This property accepts a Boolean value that allows us to specify whether we want text decoration linethrough or not
Example 1: Default appearance of text object
Let's see a code example to see how our text object looks like when linethrough property is not used.
<!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>Default appearance of text object</h2>
<p>You can see that there is no linethrough on text</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: 50,
top: 70,
});
// Add it to the canvas
canvas.add(text);
</script>
</body>
</html>
Example 2: Passing the linethrough property as key with the value as true
In this example, we are passing the linethrough property as key, with a value as true which will add linethrough to our text.
<!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 linethrough property as key with the value as true</h2>
<p>You can see that the linethrough has been added</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: 110,
top: 70,
linethrough: true,
});
// Add it to the canvas
canvas.add(text);
</script>
</body>
</html>
Key Points
The
linethroughproperty is a boolean value - set it totrueto enable strikethrough effectBy default, text objects in FabricJS do not have linethrough decoration
This property can be combined with other text styling properties like
fontSize,fontFamily, andfill
Conclusion
The linethrough property in FabricJS provides an easy way to add strikethrough decoration to text objects. Simply set linethrough: true in the options when creating a fabric.Text instance to apply this visual effect.
