當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Fabric.js Polygon absolutePositioned屬性用法及代碼示例


僅當將該對象用作clipPath時,它才有意義。畫布多邊形表示該多邊形是可移動的,並且可以根據需要進行拉伸。此外,當涉及初始筆觸顏色,形狀,填充顏色或筆劃寬度時,可以自定義多邊形。

為了設置畫布多邊形的absolutePositioned,使用了名為FabricJS的JavaScript庫。導入庫後,我們將在body標簽中創建一個包含多邊形的canvas塊。此後,我們將初始化FabricJS提供的Canvas和多邊形的實例,並使用absolutePositioned屬性設置畫布多邊形的絕對位置。

用法:

fabric.Polygon([
   { x:pixel, y:pixel },
   { x:pixel, y:pixel },
   { x:pixel, y:pixel},
   { x:pixel, y:pixel},
   { x:pixel, y:pixel }],
   { absolutePositioned:boolean }
);

參數:該函數接受如上所述和以下描述的單個參數:

  • absolutePositioned:它擁有布爾值。

以下示例說明了JavaScript中的Fabric.JS Polygon absolutePositioned屬性:

範例1:在這裏,absolutePosition屬性設置為false。

<!DOCTYPE html>  
<html>  
  
<head>  
    <!-- Loading the FabricJS library -->
    <script src=  
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js">  
    </script>  
</head>  
  
<body>  
    <div style="text-align:center;width:600px;">  
        <h1 style="color:green;">  
            GeeksforGeeks  
        </h1>  
        <b>  
            Fabric.js | Polygon absolutePositioned Property  
        </b>  
    </div>  
  
    <canvas id="canvas"
            width="600"
            height="200"
            style="border:1px solid #000000;">  
    </canvas>  
  
    <script>  
  
        // Initiate a Canvas instance  
        var canvas = new fabric.Canvas("canvas");  
  
        // Initiate a polygon instance  
        var polygon = new fabric.Polygon([  
        { x:295, y:10 },  
        { x:235, y:198 },  
        { x:385, y:78},  
        { x:205, y:78},  
        { x:355, y:198 }], {  
            absolutePositioned:false 
        });  
  
        // Render the polygon in canvas  
        canvas.add(polygon);  
    </script>  
</body>  
  
</html>

輸出:

範例2:在這裏,absolutePosition屬性設置為true。

<!DOCTYPE html>  
<html>  
  
<head>  
    <!-- Loading the FabricJS library -->
    <script src=  
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js">  
    </script>  
</head>  
  
<body>  
    <div style="text-align:center;width:600px;">  
        <h1 style="color:green;">  
            GeeksforGeeks  
        </h1>  
        <b>  
            Fabric.js | Polygon absolutePositioned Property  
        </b>  
    </div>  
  
    <canvas id="canvas"
            width="600"
            height="200"
            style="border:1px solid #000000;">  
    </canvas>  
  
    <script>  
  
        // Initiate a Canvas instance  
        var canvas = new fabric.Canvas("canvas");  
  
        // Initiate a polygon instance  
        var polygon = new fabric.Polygon([  
        { x:295, y:10 },  
        { x:235, y:198 },  
        { x:385, y:78},  
        { x:205, y:78},  
        { x:355, y:198 }], {  
            absolutePositioned:true 
        });  
  
        // Render the polygon in canvas  
        canvas.add(polygon);  
    </script>  
</body>  
  
</html>

輸出:

參考:http://fabricjs.com/docs/fabric.Polygon.html#absolutePositioned




相關用法


注:本文由純淨天空篩選整理自skyridetim大神的英文原創作品 Fabric.js Polygon absolutePositioned Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。