FabricJS – 設定多邊形物件的HTMLCanvasElement的裁剪偏移量
我們可以透過建立fabric.Polygon的例項來建立一個多邊形物件。多邊形物件可以由任何由一組連線的直線段組成的封閉形狀來表徵。由於它是FabricJS的基本元素之一,我們也可以透過應用角度、不透明度等屬性來輕鬆自定義它。
為了將多邊形物件轉換為HTMLCanvasElement,我們使用toCanvasElement方法。它返回型別為HTMLCanvasElement的DOM元素,這是一個繼承其屬性和方法自HTMLElement介面的介面。我們使用left和top屬性來設定多邊形物件的HTMLCanvasElement的裁剪偏移量。
語法
toCanvasElement({ left: Number | top: Number }: Object):
HTMLCanvasElement
引數
options (可選) − 此引數接受一個物件,該物件為我們的HTMLCanvasElement提供額外的自定義。使用此引數,可以更改與HTMLCanvasElement相關的許多屬性,其中left和top是屬性,例如高度、左側裁剪偏移量等等。
選項鍵
left − 此屬性接受一個數字值,表示裁剪左側偏移量。此屬性是可選的。
top − 此屬性接受一個數字值,表示裁剪頂部偏移量。此屬性是可選的。
示例1:使用left屬性
讓我們看一個程式碼示例,看看在使用left屬性以及toCanvasElement方法時記錄的輸出。由於我們傳遞給它一個值為100的值,它將用作左側裁剪偏移量。我們還使用了toDataURL方法來顯示left屬性實際上如何影響多邊形物件。我們可以從開發者工具中開啟控制檯,並在新標籤頁中開啟url字串以檢視輸出影像。
<!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 left property</h2>
<p>
You can open console from dev tools, copy the url string and paste it in a new tab to see that the polygon object has been cropped by 100px offset from the left
</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 with left key
var leftCropOffset = polygon.toCanvasElement({
left: 100,
});
// Using toDataURL method
console.log(leftCropOffset.toDataURL());
</script>
</body>
</html>
示例2:使用top屬性
讓我們看一個程式碼示例,看看在使用top屬性以及toCanvasElement方法時記錄的輸出。由於我們傳遞了一個值為100的值,它將用作多邊形的頂部裁剪偏移量。
<!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 top property</h2>
<p>
You can open console from dev tools, copy the url string and paste it in a new tab to see that the polygon object has been cropped by 100px offset from the top
</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 with top key
var topCropOffset = polygon.toCanvasElement({
top: 100,
});
// Using toDataURL method
console.log(topCropOffset.toDataURL());
</script>
</body>
</html>
結論
在本教程中,我們使用兩個簡單的示例演示瞭如何使用FabricJS設定多邊形物件的HTMLCanvasElement的裁剪偏移量。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP