Home > Article > Web Front-end > How to enable centered scaling on canvas using FabricJS?
In this article, we will learn how to enable centered scaling on canvas using FabricJS. In FabricJS, when an object is dragged from a corner, the object transforms proportionally. We can use the centeredScaling property to use the center as the origin of the transformation.
new fabric.Canvas(element: HTMLElement|String, { centeredScaling: Boolean }: Object)
Element - This parameter is
Options (optional) - This parameter is an object that provides additional customization of our canvas. You can use this parameter to change many attributes related to the canvas, such as color, cursor, and border width, among which centeredScaling is one attribute. It accepts a Boolean value that determines whether the object should use the center point as the origin of the transformation. The default value is False.
Passing the centeredScaling key with a value of false
Let’s look at a code example to understand when centeredScaling is set How the object scales when False.
<!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>Enabling centered scaling in canvas using FabricJS</h2> <p>Select the object and try to resize it by its corners. The object will scale non-uniformly from its center.</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas", { centeredScaling: false }); // Creating an instance of the fabric.Rect class var circle = new fabric.Circle({ left: 200, top: 100, radius: 40, fill: "blue", }); // Adding it to the canvas canvas.add(circle); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
Passing the centeredScaling key to a class with a value of True
By default, the centeredScaling key is Set to false. Therefore, we need to pass the key to the class and give it a real value so that the objects can use their center as the origin of the transformation.
<!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>Enabling centered scaling in canvas using FabricJS</h2> <p>Here we have set <b>centeredScaling</b> to True. Select the object and try to resize it by its corners. The object will scale uniformly from its center. </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas", { centeredScaling: true }); // Creating an instance of the fabric.Rect class var circle = new fabric.Circle({ left: 200, top: 100, radius: 40, fill: "blue", }); // Adding it to the canvas canvas.add(circle); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
The above is the detailed content of How to enable centered scaling on canvas using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!