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

How to find the complexity of a Line instance using FabricJS?

WBOY
Release: 2023-08-31 17:45:07
forward
522 people have browsed it

如何使用 FabricJS 查找 Line 实例的复杂度?

In this tutorial, we will learn how to find the complexity of a line 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 get the complexity of a Line object, we use the complexity method. If the current object inherits directly from the base class rather than a subclass, this method will return 1.

Syntax

 complexity(): Number 
Copy after login

Using complexity methods

Example

Let’s look at a code example to see the complexity of getting a Line instance using complexity methods The recorded output. Complexity is 1 unless subcategorizing.

<!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 complexity method</h2>
   <p>You can open console from dev tools and see the logged output</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([70, 100, 150, 200], {
         stroke: "blue",
      });
      
      // Add it to the canvas
      canvas.add(line);
      
      // Using the complexity method
      console.log("The complexity of Line instance is: ", line.complexity());
   </script>
</body>
</html>
Copy after login

Using complexity methods to compare different objects

Example

In this example, we use the complexity method to compare the complexity of a line instance and a polygon instance. You can open the console from the development tools to see the difference in complexity.

<!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 complexity method to compare different objects</h2>
   <p>You can open console from dev tools and see that the complexities are different
   </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([70, 100, 150, 200], {
         stroke: "blue",
      });
      
      // Initiate a Polygon object
      var polygon = new fabric.Polyline(
         [
            { x: 50, y: 30 },
            { x: 105, y: 10 },
            { x: 160, y: 30 },
            { x: 100, y: 150 },
         ],
            {
               fill: "red",
               left: 300,
               top: 70,
            }
      );
      
      // Add both to the canvas
      canvas.add(line);
      canvas.add(polygon);
      
      // Using the complexity method
      console.log("The complexity of Line instance is: ", line.complexity());
      console.log(
         "The complexity of Polygon instance is: ",
         polygon.complexity()
      );
   </script>
</body>
</html>
Copy after login

The above is the detailed content of How to find the complexity of a Line instance 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!