- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
FabricJS β Applying scale multiplier to a Polygon converted to a HTMLCanvasElement
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 HTMLCanvasElement we use the toCanvasElement method. It returns the DOM element of type HTMLCanvasElement, an interface which inherits its properties and methods from the HTMLElement interface. We use the multiplier property to set a multiplier to the Polygon converted to HTMLCanvasElement.
Syntax
toCanvasElement({ multiplier: Number }: Object): HTMLCanvasElement
Parameters
options (optional) β This parameter accepts an Object which provides additional customizations to our HTMLCanvasElement. Using this parameter height, left crop offset and a lot of other properties can be changed related to the HTMLCanvasElement of which multiplier is a property.
Options Keys
multiplier β This property accepts a Number value which allows us to set the multiplier to scale by. Its default value is 1. This parameter is optional.
Example 1: Using the toCanvasElement method
Letβs see a code example to see the logged output when the toCanvasElement method is used. On using the toCanvasElement method, the DOM element of type HTMLCanvasElement is returned. In this case, the default value of multiplier property is used, which is 1.
<!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 toCanvasElement method</h2> <p>You can open console from dev tools to 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); // Initiate a polygon object var polygon = new fabric.Polygon( [ { x: 600, y: 310 }, { x: 650, y: 450 }, { x: 600, y: 480 }, { x: 550, y: 480 }, { x: 450, y: 460 }, { x: 300, y: 210 }, ], { fill: "#778899", stroke: "blue", strokeWidth: 5, top: 50, left: 100, scaleX: 0.5, scaleY: 0.5, } ); // Adding it to the canvas canvas.add(polygon); // Using toCanvasElement method console.log( "The output on using toCanvasElement method is:", polygon.toCanvasElement() ); </script> </body> </html>
Example 2: Using the Multiplier Property
Letβs see a code example to see the logged output when the multiplier property is used. Here, we have console logged the value of the Polygon converted to HTMLCanvasElement with multiplier as 1(default value) and 2. We can see that when multiplier is set to 2, the height(138) and width(178) values increase such that they become 276 and 356 respectively. This means that the scale multiplier has been applied.
<!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 multiplier property</h2> <p> You can open console from dev tools to see the difference when multiplier property is passed its default value and when it is passed a value of 2 </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 polygon object var polygon = new fabric.Polygon( [ { x: 600, y: 310 }, { x: 650, y: 450 }, { x: 600, y: 480 }, { x: 550, y: 480 }, { x: 450, y: 460 }, { x: 300, y: 210 }, ], { fill: "#778899", stroke: "blue", strokeWidth: 5, top: 50, left: 100, scaleX: 0.5, scaleY: 0.5, } ); // Adding it to the canvas canvas.add(polygon); // Using toCanvasElement method and passing the default value of multiplier console.log( "Using the default value of multiplier:", polygon.toCanvasElement({ multiplier: 1, }) ); // Using toCanvasElement method and passing a custom value to multiplier property console.log( "Using multiplier and passing the value as 2:", polygon.toCanvasElement({ multiplier: 2, }) ); </script> </body> </html>
Conclusion
In this tutorial, we used two simple examples to demonstrate how you can apply scale multiplier to a Polygon converted to HTMLCanvasElement using FabricJS.