Home >Web Front-end >JS Tutorial >How to non-uniformly resize an object by corners using FabricJS?
In this article, we will learn how to non-uniformly resize objects by corners using FabricJS. In FabricJS, when an object is dragged from a corner, the object transforms proportionally. However, we can control this behavior by pressing uniScaleKey.
new fabric.Canvas(element: HTMLElement|String, { uniScaleKey: String }: Object)
Element - This parameter is
Options (optional) - This parameter is an object that provides additional customization of our canvas. Using this parameter, you can change many attributes related to the canvas, such as color, cursor, and border width, among which uniScaleKey is one attribute. It accepts a string value indicating which key toggles unified scaling. Its default value is shiftKey. Possible key values are: altKey, shiftKey and ctrlKey.
Press the shift key to disable uniform scaling
When the object maintains the aspect ratio When you transform by dragging an edge, we say that the object is scaled uniformly. uniScaleKey allows us to control this behavior on the spot. By default, objects in FabricJS scale proportionally. Let's look at a code example to see how an object scales non-uniformly when the shift key is pressed.
<!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>Resizing an object non-uniformly via corner points</h2> <p>Hold the <b>shift</b> key and drag the object from its corners. The object will resize non-uniformly. </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); // Creating an instance of the fabric.Rect class var circle = new fabric.Circle({ left: 70, top: 90, radius: 40, fill: "#006400", }); // Adding it to the canvas canvas.add(circle); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
Change the value of uniScaleKey to ctrlKey
Although its default value is shiftKey, we can Values can also be used: 'ctrlKey' and 'altKey'. If NULL or any other key is specified, this feature will be disabled.
<!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>Resizing an object non-uniformly via corner points</h2> <p>Hold the <b>ctrl</b> key and drag the object from its corners. The object will resize non-uniformly. </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas", { uniScaleKey: "ctrlKey", }); // Creating an instance of the fabric.Rect class var circle = new fabric.Circle({ left: 70, top: 90, radius: 40, fill: "#006400", }); // 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 non-uniformly resize an object by corners using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!