Home > Web Front-end > JS Tutorial > body text

How to non-uniformly resize an object by corners using FabricJS?

WBOY
Release: 2023-08-25 10:13:07
forward
1018 people have browsed it

如何使用 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.

Syntax

new fabric.Canvas(element: HTMLElement|String, { uniScaleKey: String }: Object)
Copy after login

Parameters

  • Element - This parameter is The element itself can be derived using Document.getElementById() or the id of the element itself. The FabricJS canvas will be initialized on this element.

  • 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.

Example 1

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>
Copy after login

Example 2

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>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!