Home  >  Article  >  Web Front-end  >  How to enable centered scaling on canvas using FabricJS?

How to enable centered scaling on canvas using FabricJS?

王林
王林forward
2023-08-29 13:29:02925browse

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

Syntax

new fabric.Canvas(element: HTMLElement|String, { centeredScaling: Boolean }: 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. 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.

Example 1

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>

Example 2

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!

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