Home  >  Article  >  Web Front-end  >  How to non-uniformly resize an object by corners using FabricJS?

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

WBOY
WBOYforward
2023-08-25 10:13:071050browse

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

Parameters

  • Element - This parameter is em> 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>

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>

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete