Home > Web Front-end > JS Tutorial > FabricJS - How to center a Line object vertically on the current viewport of the canvas?

FabricJS - How to center a Line object vertically on the current viewport of the canvas?

WBOY
Release: 2023-09-14 12:41:09
forward
1947 people have browsed it

FabricJS – 如何使 Line 对象在画布的当前视口上垂直居中?

In this tutorial, we will learn how to vertically center a Line object on the current viewport of the canvas 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 a fabric.Line instance, specifying the x and y coordinates of the line and adding it to the canvas. To center the Line object vertically on the current viewport of the canvas, we use the viewportCenterV method.

Syntax

 viewportCenterV(): fabric.Object 
Copy after login

Default appearance of Line objects

Example

Let’s look at a code example to see how it works without using viewportCenterV method is the appearance of our Line object. In this case, the line object will not be vertically centered on the canvas.

<!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 appearance of the Line object</h2>
   <p>
      You can see that the line object is not centered vertically with respect to the current viewport of canvas
   </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: 6,
      });
      
      // Add it to the canvas
      canvas.add(line);
   </script>
</body>
</html>
Copy after login

Use viewportCenterVMethod

Example

Let’s look at a code example to see when Use the viewportCenterV method. In this case our line object will be vertically centered Relative to the current viewport of the canvas.

<!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 viewportCenterV method</h2>
   <p>
      You can see that the line object is centered vertically with respect to the current viewport of canvas
   </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: 6,
      });
      
      // Add it to the canvas
      canvas.add(line);
      
      // Using the viewportCenterV method
      line.viewportCenterV();
   </script>
</body>
</html>
Copy after login

The above is the detailed content of FabricJS - How to center a Line object vertically on the current viewport of the canvas?. 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