FabricJS - 如何獲取轉換為 HTMLCanvasElement 的 Polygon 物件的尺寸?
我們可以透過建立 fabric.Polygon 的例項來建立一個 Polygon 物件。多邊形物件可以由任何由一組連線的直線段組成的閉合形狀來表示。由於它是 FabricJS 的基本元素之一,因此我們還可以透過應用角度、不透明度等屬性輕鬆自定義它。
為了將多邊形物件轉換為 HTMLCanvasElement,我們使用 toCanvasElement 方法。它返回型別為 HTMLCanvasElement 的 DOM 元素,該介面繼承了 HTMLElement 介面的屬性和方法。我們使用 HTMLCanvasElement 從其父級 HTMLElement 繼承的 width 和 height 屬性來查詢轉換為 HTMLCanvasElement 的 Polygon 物件的尺寸。
語法
HTMLCanvasElement.height HTMLCanvasElement.width
示例 1:使用 toCanvasElement 方法和 Width 屬性
讓我們看一個程式碼示例,以瞭解使用 toCanvasElement 方法以及 width 屬性時 Polygon 物件的外觀。width 是一個正整數,表示畫布一行上的畫素數。我們可以從開發者工具中開啟控制檯,檢視 width 值為 200。
<!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 and using the width property</h2>
<p>
You can open console from dev tools to see that the width value is being displayed as 200
</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
var polygonCanvas = polygon.toCanvasElement({
width: 200,
});
// Using the width property
console.log("The width is as follows:", polygonCanvas.width);
</script>
</body>
</html>
示例 2:使用 toCanvasElement 方法和 Height 屬性
讓我們看一個程式碼示例,以檢視使用 toCanvasElement 方法以及 height 屬性時記錄的輸出。height 是一個正整數,表示畫布一列上的畫素數。在這種情況下,我們可以從開發者工具中開啟控制檯,檢視 height 值為 200。
<!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 and using the height property</h2>
<p>
You can open console from dev tools to see that the height value is being displayed as 200
</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
var polygonCanvas = polygon.toCanvasElement({
height: 200,
});
// Using the height property
console.log("The height is as follows:", polygonCanvas.height);
</script>
</body>
</html>
結論
在本教程中,我們使用兩個簡單的示例演示瞭如何使用 FabricJS 查詢轉換為 HTMLCanvasElement 的 Polygon 物件的尺寸。
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP