FabricJS – 將多邊形轉換為HTMLCanvasElement後移除物件的變換?
我們可以透過建立一個fabric.Polygon例項來建立一個Polygon物件。多邊形物件可以由任何由一組連線的直線段組成的封閉形狀來表示。由於它是FabricJS的基本元素之一,我們也可以透過應用角度、不透明度等屬性輕鬆自定義它。
為了將多邊形物件轉換為HTMLCanvasElement,我們使用toCanvasElement方法。它返回HTMLCanvasElement型別的DOM元素,這是一個從HTMLElement介面繼承其屬性和方法的介面。我們使用withoutTransform屬性來移除轉換為HTMLCanvasElement的多邊形的物件變換。
語法
toCanvasElement({ withoutTransform: Boolean }: Object):
HTMLCanvasElement
引數
options (可選) − 此引數接受一個物件,該物件為我們的HTMLCanvasElement提供額外的自定義。使用此引數,可以更改與HTMLCanvasElement相關的許多屬性,例如高度、左側裁剪偏移量,其中withoutTransform是一個屬性。
選項鍵
withoutTransform − 此屬性接受一個布林值,用於確定是否移除當前物件的變換。此屬性是可選的。
示例1:使用withoutTransform屬性並傳遞“false”值
讓我們來看一個程式碼示例,看看使用withoutTransform屬性以及toCanvasElement方法時的日誌輸出。傳遞“false”值時,withoutTransform屬性會保留其物件變換。因此,在這種情況下,scaleX、scaleY和flipX物件變換將存在於其輸出影像中。我們已將toDataURL方法與toCanvasElement方法同步使用,以顯示輸出影像的實際外觀。
<!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 withoutTransform property and passing it a ‘false’ value</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's image contains object transformation
</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,
flipX: true,
}
);
// Adding it to the canvas
canvas.add(polygon);
// Using toCanvasElement method
var withoutTransform = polygon.toCanvasElement({
withoutTransform: false,
});
// Using toDataURL method
console.log(withoutTransform.toDataURL());
</script>
</body>
</html>
示例2:使用withoutTransform屬性並傳遞“true”值
讓我們來看一個程式碼示例,看看使用withoutTransform屬性以及toCanvasElement方法時的日誌輸出。傳遞“true”值時,withoutTransform屬性將不會保留其物件變換。因此,在這種情況下,scaleX、scaleY和flipX物件變換將從其輸出影像中移除。
<!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 withoutTransform property and passing it a ‘true’ value</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's image does not contain object transformation
</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,
flipX: true,
}
);
// Adding it to the canvas
canvas.add(polygon);
// Using toCanvasElement method
var withoutTransform = polygon.toCanvasElement({
withoutTransform: true,
});
// Using toDataURL method
console.log(withoutTransform.toDataURL());
</script>
</body>
</html>
結論
在本教程中,我們使用兩個簡單的示例演示瞭如何使用FabricJS移除轉換為HTMLCanvasElement的多邊形的物件變換。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP