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
Converting a Polygon object into a data-like URL string using FabricJS
We can create a Polygon object by creating an instance of fabric.Polygon. A polygon object can be characterized by any closed shape consisting of a set of connected straight line segments. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties like angle, opacity etc.
In order to convert a Polygon object into a data-like URL string we use the toDataURL method. This method converts an object into a data-like URL string that represents the visual appearance of the polygon as a base64-encoded image.
Syntax
toDataURL(options: Object): String
Parameters
options (optional) ? This parameter is an Object which provides additional customizations to the URL representation of the Polygon object. Using this parameter format, quality, multiplier and a lot of other properties can be changed.
Example 1: Default Value Without Using toDataURL Method
Let's see a code example to see how the Polygon object looks like when the toDataURL method is not used. In this example, we have created a Polygon object and assigned it various properties like stroke, fill etc. However, since we have not used the toDataURL method, we will see the polygon object's default representation logged to the console.
<!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 toDataURL method</h2>
<p>You can open console from dev tools and see the logged output</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiating a polygon object
var polygon = new fabric.Polygon(
[
{ x: -20, y: -35 },
{ x: 20, y: -35 },
{ x: 40, y: 0 },
{ x: 20, y: 35 },
{ x: -20, y: 35 },
{ x: -40, y: 0 },
],
{
stroke: "red",
left: 100,
top: 50,
fill: "black",
strokeWidth: 2,
strokeLineJoin: "bevel",
}
);
// Adding it to the canvas
canvas.add(polygon);
// Console logging the Polygon object
console.log("The Polygon object is as follows: ", polygon);
</script>
</body>
</html>
The Polygon object is as follows: fabric.Polygon {type: "polygon", version: "5.1.0", ...}
Example 2: Using the toDataURL Method
Let's see a code example to see the logged output when using the toDataURL method. As soon as we open the console from the dev tools, we can see the URL representation of the Polygon object. We can copy that URL and paste it into the address bar of a new tab to see the final output.
<!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</h2>
<p>You can open console from dev tools and see the output URL. You can copy that and paste it in the address bar of a new tab to see the final image.</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiating a polygon object
var polygon = new fabric.Polygon(
[
{ x: -20, y: -35 },
{ x: 20, y: -35 },
{ x: 40, y: 0 },
{ x: 20, y: 35 },
{ x: -20, y: 35 },
{ x: -40, y: 0 },
],
{
stroke: "red",
left: 100,
top: 50,
fill: "black",
strokeWidth: 2,
strokeLineJoin: "bevel",
}
);
// Adding it to the canvas
canvas.add(polygon);
// Using the toDataURL method
console.log("Data URL:", polygon.toDataURL());
</script>
</body>
</html>
Data URL: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChAGAHjuibwAAAABJRU5ErkJggg==
Key Points
- The
toDataURL()method converts the polygon into a base64-encoded data URL - The generated URL can be used as an image source or saved as a file
- Optional parameters allow customization of format, quality, and other output properties
- This method is useful for exporting FabricJS objects as images
Conclusion
The toDataURL() method in FabricJS provides an easy way to convert polygon objects into data URL strings. This is particularly useful for saving or exporting canvas content as images.
