FabricJS – How to change the format of the URL string of a Line object?



In this tutorial, we are going to learn about how to change the format of the URL string of Line object using FabricJS. A Line element is one of the basic elements provided in FabricJS. It is used for creating straight lines. Because line elements are geometrically one-dimensional and do not contain an interior, they are never filled. We can create a line object by creating an instance of fabric.Line, specifying the x and y coordinates of the line and adding it to the canvas. In order to change the format of the URL string of Line object we use the format property.

Syntax

toDataURL({ format: String }: Object): String

Parameters

  • options (optional) − This parameter is an Object which provides additional customizations to the URL representation of the Line object. Using this parameter height, quality, multiplier and a lot of other properties can be changed of which format is a property.

Options Keys

  • format − This property accepts a String value which allows us to define the format of the output image. Accepted values are “jpeg” or “png”. The default value is “png”.

Default value without using the format property

Example

Let’s see a code example to see the logged output when using the toDataURL method without using the format property. As soon as we open the console from the dev tools, we can see the URL representation of the Line object. We can copy that URL and paste it into the address bar of a new tab to see the final output. Since we have not specified the format of our image, it will be according to the set default which is “png”.

<!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 value without using the format property</h2> <p> You can open console from dev tools and see that the URL representation of the Line object has a "png" format by default </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 Line object var line = new fabric.Line([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, angle: 70, }); // Add it to the canvas canvas.add(line); // Using the toDataURL method console.log(line.toDataURL()); </script> </body> </html>

Using the toDataURL method along with format property

Example

Let’s see a code example to see how the Line object looks like when the toDataURL method is used along with the format property. In this case we will be able to specify the format of our final image. Since the value assigned here is “jpeg”, the final image will be in “jpeg” format.

<!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>Using the toDataURL method along with format property</h2> <p> You can open console from dev tools and see that the URL representation of the Line object has a "jpeg" format now </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 Line object var line = new fabric.Line([200, 100, 100, 40], { stroke: "blue", strokeWidth: 20, angle: 70, }); // Add it to the canvas canvas.add(line); // Using the toDataURL method console.log(line.toDataURL({format: "jpeg"})); </script> </body> </html>

Advertisements