How to exclude Text object from being saved while using JSON.stringify() in FabricJS?


In this tutorial, we are going to learn how to exclude Text object from being saved while using JSON.stringify() in 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 we can also add additional functionalities. Serialization is used in order to export canvas contents. In order to achieve this we use toObject() and toJSON() methods. The excludeFromExport property allows us to include or omit the object when being exported.

Syntax

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

Options Keys

  • excludeFromExport − This property accepts a Boolean value. On passing a true value, the object is not exported as object or JSON.

Example 1

Logged output when excludeFromExport property is not used

Let’s see a code example to see the logged output when the excludeFromExport property is not used. In this case, we can see in our browser console (available in dev tools) the object has not been excluded from export and begins with “type”:“text” hence indicating about the text object.

<!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>Logged output when excludeFromExport property is not used</h2> <p> You can open dev tools and see in console that the serialized text object is being logged </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: 50, width: 300, fill: "green", textAlign: "center", }); // Add it to the canvas canvas.add(text); // Using JSON.stringify method to serialize the canvas console.log(JSON.stringify(canvas)); </script> </body> </html>

Example 2

Logged output when excludeFromExport property is used

In this example, we will see how by using the excludeFromExport property and passing it a true value, we can omit the inclusion of the serialized text object.

<!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>Logged output when excludeFromExport property is used</h2> <p> You can inspect and see that the serialized text object is not being console logged </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: 50, width: 300, fill: "green", textAlign: "center", excludeFromExport: true, }); // Add it to the canvas canvas.add(text); // Using JSON.stringify method to serialize the canvas console.log(JSON.stringify(canvas)); </script> </body> </html>

Updated on: 14-Sep-2022

237 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements