Home > Web Front-end > JS Tutorial > FabricJS - How to set the position of a Line object relative to the origin?

FabricJS - How to set the position of a Line object relative to the origin?

王林
Release: 2023-08-25 12:13:08
forward
1442 people have browsed it

FabricJS – 如何设置 Line 对象相对于原点的位置?

In this tutorial, we will learn how to set the position of a Line object relative to its origin using FabricJS. Line element is one of the basic elements provided in FabricJS. It is used to create straight lines. Since line elements are geometrically one-dimensional and contain no interiors, they are never filled. We can create a line object by creating an instance of fabric.Line, specifying the x and y coordinates of the line and adding it to the canvas. To set the position of a Line object relative to the origin, we use the setPositionByOrigin method.

grammar

setPositionByOrigin(pos: fabric.Point, originX: String, originY: String): void
Copy after login

parameter

  • pos - This property accepts a fabric.Point value that allows us to set the new position of the object. < /p>

  • originX - This property accepts a String value which allows us to set the horizontal origin. Possible values ​​are "left", "center", or "right".

  • originY - This property accepts a String value which allows us to set the vertical origin. Possible values ​​are "Top", "Center", or "Bottom".

Do not use setPositionByOriginDefault position of the method

Example

Let's look at a code example to see the default position of a Line object without using the setPositionByOrigin method.

<!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>Default position without using the setPositionByOrigin method </h2>
   <p>You can see the default position of the line object</p>
   <canvas id="canvas"></canvas>
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);

      // Initiate a Line object
      var line = new fabric.Line([200, 100, 100, 40], {
         stroke: "blue",
         strokeWidth: 20,
      });

      // Add it to the canvas
      canvas.add(line);
   </script>
</body>
</html>
Copy after login

Use setPositionByOriginMethod

Example

Let's look at a code example to see what a Line object looks like when using the setPositionByOrigin method. In this example, we specify the object's new position by initializing a Point object with x and y coordinates of 500 and 100 respectively. We'll set the line object's new position, thinking of its horizontal origin as the "center" and its vertical origin as the "top."

<!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>Using the setPositionByOrigin method</h2>
   <p>You can see the new position of the line object</p>
   <canvas id="canvas"></canvas>
   <script>
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);

      // Initiate a Line object
      var line = new fabric.Line([200, 100, 100, 40], {
         stroke: "blue",
         strokeWidth: 20,
      });

      // Initiate a Point object
      var pos = new fabric.Point(500, 100);

      // Using setPositionByOrigin method
      line.setPositionByOrigin(pos, "center", "top");

      // Add it to the canvas
      canvas.add(line);
   </script>
</body>
</html>
Copy after login

The above is the detailed content of FabricJS - How to set the position of a Line object relative to the origin?. 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