How to add a line through to a canvas-type text using Fabric.js?


To add a line through to a canvas-type text using Fabric.js, you can use the "set('textDecoration', 'line-through')" method on the text object. This method allows you to set the textDecoration property of the text object to "line-through", which will add a line through the text. Finally, you need to call the "renderAll()" method to update the canvas with the new text decoration.

Fabric.js is a JavaScript library for working with object-oriented canvas graphics. It provides an easy-to-use API for creating and manipulating canvas elements, such as shapes, text, and images.

With Fabric.js, you can create complex canvas graphics and animations using JavaScript, without the need for low-level canvas API calls. It also provides a set of built-in objects, such as rectangles, circles, and text, that can be easily manipulated using the library's API.

Additionally, fabric.js provides many useful features such as events handling, undo/redo, object serialization, and more. It also has a wide variety of built-in filters and image-manipulation functions.

Fabric.js is widely used in web development, particularly in creating interactive graphics and charts, and creating user interface elements such as image editors and custom form inputs.

Approach

You can add a line-through to a canvas text using Fabric.js by setting the "linethrough" property of the text object to true.

Here's an example −

var canvas = new fabric.Canvas('canvas');
var text = new fabric.Text('Hello, World!', {
   linethrough: true
});
canvas.add(text);

This will create a canvas with a text "Hello, World!" with a line through the text. You can adjust the position of the text and font size as needed.

Example

Here's a complete working example that demonstrates how to add a line through to a canvas text using Fabric.js −

<!DOCTYPE html>
<html>
<head>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.3/fabric.min.js"></script>
</head>
<body>
   <canvas id="canvas" width="300" height="200"></canvas>
   <script>
      var canvas = new fabric.Canvas('canvas');
      var text = new fabric.Text('Hello, World!', {
         linethrough: true
      });
      canvas.add(text);
   </script>
</body>
</html>

Updated on: 06-Feb-2023

306 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements