Home > Web Front-end > JS Tutorial > How are polygons different from polylines in FabricJS?

How are polygons different from polylines in FabricJS?

王林
Release: 2023-08-27 08:21:17
forward
1185 people have browsed it

FabricJS 中的多边形与折线有何不同?

We can create Polyline objects by creating instances of fabric.Polyline, and fabric.Polygon can be used to create Polygon instances. A polyline object can be represented by a set of connected straight line segments. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties such as angle, opacity, etc.

Polygons always connect the first point to the last point to form a closed area, while polylines do not. This can be demonstrated by the example given below.

grammar

new fabric.Polyline(points: Array, options: Object)
Copy after login

parameter

  • points - This parameter accepts an Array, which represents the array of points that make up the polyline object.

  • Options (optional) - This parameter is an object that provides additional customization to our object. Use this parameter to change the origin, stroke width, and many other properties associated with the Polyline object.

Example 1

Create a Fabric.Polyline() instance and add it to the canvas

Let's look at a code example to see how to add a polyline object to the canvas. The only required parameter is the points array, while the second parameter is an optional options object. Additionally, we will use the same points array in the Polygon to demonstrate the difference.

<!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>Creating an instance of fabric.Polyline() and adding it to our canvas</h2>
   <p>You can see that the polyline object doesn’t connects start to end</p>
   <canvas id="canvas"></canvas>
   <script>
      
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiating a points array
      var points = [
         { x: 200, y: 150 },
         { x: 0, y: 100 },
         { x: 100, y: 0 },
         { x: 200, y: 100 },
      ];
      
      // Initiating a polyline object
      var polyline = new fabric.Polyline(points, {
         left: 100,
         top: 40,
         fill: "white",
         strokeWidth: 4,
         stroke: "green",
      });
      
      // Adding it to the canvas
      canvas.add(polyline);
   </script>
</body>
</html> 
Copy after login

Example 2

Create an instance of fabric.Polygon() and add it to our canvas

Let's look at a code example to see how to add a polygon object to the canvas. The only required parameter is the points array, while the second parameter is an optional options object, we will provide the same options object as in the polyline example.

<!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>Creating an instance of fabric.Polygon() and adding it to our canva</h2>
   <p>You can see that the polygon object connects start to end to make a closed area</p>
   <canvas id="canvas"></canvas>
   <script>
      
      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);
      
      // Initiating a points array
      var points = [
         { x: 200, y: 150 },
         { x: 0, y: 100 },
         { x: 100, y: 0 },
         { x: 200, y: 100 },
      ];
      
      // Initiating a polyline object
      var polygon = new fabric.Polygon(points, {
         left: 100,
         top: 40,
         fill: "white",
         strokeWidth: 4,
         stroke: "green",
      });
      
      // Adding it to the canvas
      canvas.add(polygon);
   </script>
</body>
</html> 
Copy after login

The above is the detailed content of How are polygons different from polylines in 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