FabricJS – 將多邊形轉換為HTMLCanvasElement後應用縮放倍數


我們可以透過建立fabric.Polygon的例項來建立一個Polygon物件。多邊形物件可以由任何由一組連線的直線段組成的封閉形狀來表示。由於它是FabricJS的基本元素之一,我們也可以透過應用角度、不透明度等屬性輕鬆地對其進行自定義。

為了將多邊形物件轉換為HTMLCanvasElement,我們使用toCanvasElement方法。它返回型別為HTMLCanvasElement的DOM元素,這是一個介面,它繼承自HTMLElement介面的屬性和方法。我們使用multiplier屬性來設定轉換為HTMLCanvasElement的多邊形的倍數。

語法

toCanvasElement({ multiplier: Number }: Object): HTMLCanvasElement

引數

options (可選) − 此引數接受一個物件,該物件為我們的HTMLCanvasElement提供額外的自定義功能。使用此引數,可以更改HTMLCanvasElement的許多屬性,例如高度、左側裁剪偏移量等等,其中multiplier就是一個屬性。

選項鍵

multiplier − 此屬性接受一個數字值,允許我們設定縮放倍數。其預設值為1。此引數是可選的。

示例1:使用toCanvasElement方法

讓我們來看一個程式碼示例,看看使用toCanvasElement方法時的日誌輸出。使用toCanvasElement方法時,將返回型別為HTMLCanvasElement的DOM元素。在這種情況下,使用multiplier屬性的預設值1。

<!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 toCanvasElement method</h2>
   <p>You can open console from dev tools to 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 polygon object
      var polygon = new fabric.Polygon(
         [
            { x: 600, y: 310 },
            { x: 650, y: 450 },
            { x: 600, y: 480 },
            { x: 550, y: 480 },
            { x: 450, y: 460 },
            { x: 300, y: 210 },
         ],
         {
            fill: "#778899",
            stroke: "blue",
            strokeWidth: 5,
            top: 50,
            left: 100,
            scaleX: 0.5,
            scaleY: 0.5,
         } 
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using toCanvasElement method
      console.log(
         "The output on using toCanvasElement method is:",
         polygon.toCanvasElement()
      );
   </script>
</body>
</html>

示例2:使用Multiplier屬性

讓我們來看一個程式碼示例,看看使用multiplier屬性時的日誌輸出。在這裡,我們使用控制檯輸出了將多邊形轉換為HTMLCanvasElement的值,其中multiplier分別為1(預設值)和2。我們可以看到,當multiplier設定為2時,高度(138)和寬度(178)的值會增加,分別變為276和356。這意味著縮放倍數已應用。

<!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 multiplier property</h2>
   <p> You can open console from dev tools to see the difference when multiplier property is passed its default value and when it is passed a value of 2  </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 polygon object
      var polygon = new fabric.Polygon(
         [
            { x: 600, y: 310 },
            { x: 650, y: 450 },
            { x: 600, y: 480 },
            { x: 550, y: 480 },
            { x: 450, y: 460 },
            { x: 300, y: 210 },
         ],
         {
            fill: "#778899",
            stroke: "blue",
            strokeWidth: 5,
            top: 50,
            left: 100,
            scaleX: 0.5,
            scaleY: 0.5,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
      
      // Using toCanvasElement method and passing the default value of multiplier
      console.log(
         "Using the default value of multiplier:",
         polygon.toCanvasElement({
            multiplier: 1,
         })
      );
      
      // Using toCanvasElement method and passing a custom value to multiplier property
      console.log(
         "Using multiplier and passing the value as 2:",
         polygon.toCanvasElement({
            multiplier: 2,
         })
      );
   </script>
</body>
</html>

結論

在本教程中,我們使用兩個簡單的示例演示瞭如何使用FabricJS將縮放倍數應用於轉換為HTMLCanvasElement的多邊形。

更新於:2022年12月28日

瀏覽量:157

開啟你的職業生涯

完成課程獲得認證

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