如何在FabricJS中通过点击按钮随机生成折线对象

如何在FabricJS中通过点击按钮随机生成折线对象

折线对象可以由一组相连的直线段来定义。作为FabricJS的基本元素之一,我们也可以通过应用角度、透明度等属性来轻松地自定义它。

我们将创建一个程序,其中按下按钮将随机生成一个折线对象并将其添加到画布中。

语法

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

参数

  • points − 此参数接受一个 Array ,表示构成折线对象的点的数组。

  • options(可选) − 此参数是一个 Object ,提供了对对象的其他自定义设置。使用此参数可以更改与折线对象相关的原点、描边宽度和许多其他属性。

示例1:创建fabric.Polyline()的实例并将其添加到我们的画布中

让我们看一个代码示例,说明如何将折线对象添加到画布中。只需传入必需的参数 points 数组,而第二个参数是可选的 options 对象。

<!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 has been added</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: 30, y: 50 },
         { x: 0, y: 0 },
         { x: 60, y: 0 },
      ];

      // Initiating a polyline object
      var polyline = new fabric.Polyline(points, {
         left: 100,
         top: 40,
         fill: "white",
         strokeWidth: 4,
         stroke: "cyan",
      });

      // Adding it to the canvas
      canvas.add(polyline);
   </script>
</body>
</html>

示例2:添加一个按钮来随机生成多段线对象

让我们看一个代码示例,了解如何随机生成多段线对象。我们将添加一个按钮,按下该按钮时,将随机生成的多段线添加到画布上。我们将使用一个函数来随机生成多段线,其中我们将使用 Math.random() 方法来生成随机点。

<!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>Adding a button to randomly generate the Polyline objects</h2>
    Click on the `Add Polyline!` Button to add a randomly generated polyline to the canvas 
   <canvas id="canvas"></canvas>
   <button type="button" onclick="addPolyline()">Add Polyline!</button>
   <script>

      // Initiate a canvas instance
      var canvas = new fabric.Canvas("canvas");
      canvas.setWidth(document.body.scrollWidth);
      canvas.setHeight(250);

      // Initiate a Polyline object
      var polyLine = new fabric.Polyline([
         { x: 500, y: 200 },
         { x: 550, y: 60 },
         { x: 550, y: 200 },
         { x: 350, y: 100 },
         { x: 350, y: 600 },
      ], {
         stroke: "cyan",
         fill: "white",
         strokeWidth: 5,
      });

      // Add it to the canvas instance
      canvas.add(polyLine);

      // Function to generate random Polyline and adding it to canvas
      function addPolyline() {
         var randomPolyLine = new fabric.Polyline([
         {x: Math.random() * 500, y: Math.random() * 200},
         {x: Math.random() * 500, y: Math.random() * 600},
         {x: Math.random() * 300, y: Math.random() * 100}],
         {
            stroke: "cyan",
            fill: "rgb(256,256,256,0)",
            strokeWidth: 5,
         });
         canvas.add(randomPolyLine)
      }
   </script>
</body>
</html>

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程