首頁  >  文章  >  web前端  >  FabricJS – 確定對於多邊形物件應該先繪製填滿還是描邊?

FabricJS – 確定對於多邊形物件應該先繪製填滿還是描邊?

王林
王林轉載
2023-08-24 17:01:101359瀏覽

FabricJS – 确定对于多边形对象应该首先绘制填充还是描边?

我們可以透過建立fabric.Polygon的實例來建立一個Polygon物件。多邊形物件的特徵可以是由一組連接的直線段組成的任何閉合形狀。由於它是 FabricJS 的基本元素之一,因此我們也可以透過應用角度、不透明度等屬性輕鬆自訂它。為了確定應該先繪製填充還是描邊,我們使用 paintFirst財產。

文法

new fabric.Polygon( points: Array, { paintFirst: String }: Object )

參數

  • points - 此參數接受一個Array,它表示組成多邊形物件的點數組,其中每個點都在具有x 和y 的對象。

  • 選項(可選) - 此參數是一個物件,它為我們的物件提供額外的自訂。使用此參數,可以變更與 Polygon 物件相關的原點、描邊寬度和許多其他屬性,其中 paintFirst 是該物件的屬性。

選項鍵

paintFirst - 此屬性接受一個 String 值,定義是否先繪製填滿或描邊。預設值為“填充”。

範例 1:多邊形物件的預設外觀

讓我們來看一個程式碼範例,了解未套用 paintFirst 屬性時多邊形物件的顯示方式。在本例中,使用預設值「fill」。這意味著在繪製物件時,將首先繪製填滿顏色,然後繪製描邊顏色。

<!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>Default appearance of polygon object</h2>
   <p>You can see the default appearance of polygon object</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 polygon object
      var polygon = new fabric.Polygon(
         [
            { x: 200, y: 10 },
            { x: 250, y: 50 },
            { x: 250, y: 180 },
            { x: 150, y: 180 },
            { x: 150, y: 50 },
            { x: 200, y: 10 },
         ],
         {
            fill: "green",
            stroke: "blue",
            strokeWidth: 20,
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
   </script>
</body> 
</html> 

範例2:使用paintFirst屬性

讓我們來看一個程式碼範例,了解如何使用 paintFirst 屬性來變更多邊形物件的預設行為。在這裡,我們向 paintFirst 屬性傳遞了值「lines」。這確保首先繪製筆劃而不是填充。

<!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 paintFirst property</h2>
   <p>You can see that the stroke is painted first now</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 polygon object
      var polygon = new fabric.Polygon(
         [
            { x: 200, y: 10 },
            { x: 250, y: 50 },
            { x: 250, y: 180 },
            { x: 150, y: 180 },
            { x: 150, y: 50 },
            { x: 200, y: 10 },
         ],
         {
            fill: "green",
            stroke: "blue",
            strokeWidth: 20,
            paintFirst: "stroke",
         }
      );
      
      // Adding it to the canvas
      canvas.add(polygon);
   </script>
</body> 
</html>

結論

在本教學中,我們使用兩個簡單的範例來示範如何使用 FabricJS 來決定應先為多邊形繪製填滿還是描邊。

以上是FabricJS – 確定對於多邊形物件應該先繪製填滿還是描邊?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除