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

How to enable centered scaling on canvas using FabricJS?

王林
Release: 2023-08-29 13:29:02
forward
911 people have browsed it

如何使用 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)
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. 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>
Copy after login

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

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!

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!