如何在 FabricJS 中透過點選按鈕隨機生成 Polyline 物件?


Polyline 物件可以用一組連線的直線段來表示。由於它是 FabricJS 的基本元素之一,我們也可以透過應用角度、不透明度等屬性輕鬆地自定義它。

我們將建立一個程式,按下按鈕將隨機生成一個 Polyline 物件並將其新增到我們的畫布中。

語法

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

引數

  • points − 此引數接受一個陣列,表示構成 polyline 物件的點陣列。

  • options (可選) − 此引數是一個物件,它為我們的物件提供額外的自定義選項。使用此引數可以更改與 Polyline 物件相關的原點、筆劃寬度和許多其他屬性。

示例 1:建立 fabric.Polyline() 的例項並將其新增到我們的畫布

讓我們來看一個程式碼示例,說明如何將 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:新增一個按鈕以隨機生成 Polyline 物件

讓我們來看一個程式碼示例,說明如何隨機生成 Polyline 物件。我們將新增一個按鈕,按下該按鈕將在畫布上新增隨機生成的 Polyline。我們將使用一個函式來隨機生成 Polyline,在該函式中我們將使用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>
   <p> Click on the `Add Polyline!` Button to add a randomly generated polyline to the canvas </p>
   <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>

更新於: 2023年2月16日

196 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.