如何使用 FabricJS 關閉多邊形物件的快取?
我們可以透過建立 fabric.Polygon 的例項來建立一個多邊形物件。多邊形物件可以由任何由一組連線的直線段組成的閉合形狀來表示。由於它是 FabricJS 的基本元素之一,因此我們也可以透過應用角度、不透明度等屬性輕鬆地對其進行自定義。
FabricJS 物件快取在一個額外的畫布上,以便在重用物件時節省時間。為了關閉多邊形物件的快取,我們使用 objectCaching 屬性。
語法
new fabric.Polygon( points: Array, { objectCaching: Boolean }: Object )
引數
points − 此引數接受一個 Array,表示構成多邊形物件的點的陣列,其中每個點都是一個包含 x 和 y 的物件。
options(可選) − 此引數是一個 Object,它為我們的物件提供了額外的自定義選項。使用此引數,可以更改與多邊形物件相關的原點、筆觸寬度和許多其他屬性,其中 objectCaching 是一個屬性。
選項鍵
objectCaching − 此屬性接受一個 Boolean 值,表示物件是否快取在附加畫布上。預設值為“true”。
示例 1:多邊形物件的預設外觀
讓我們看一個程式碼示例,瞭解當 objectCaching 設定為“true”(預設值)時,多邊形物件如何對事件做出反應。FabricJS 快取物件以最佳化並允許物件快速繪製在畫布上。
在這裡,我們選擇了選擇和取消選擇事件,以在使用者選擇或取消選擇多邊形物件時顯示填充顏色的變化。由於 objectCaching 已開啟,因此在選擇或取消選擇物件時不會有任何變化。
<!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 select and deselect the object to see that the fill colour does not change
</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: 0, y: 0 },
{ x: 0, y: 80 },
{ x: 80, y: 80 },
{ x: 80, y: 0 },
],
{
left: 100,
fill: "black",
stroke: "blue",
strokeWidth: 2,
scaleX: 2,
scaleY: 2,
}
);
// Add it to the canvas
canvas.add(polygon);
// Using the selected event
polygon.on("selected", () => {
polygon.fill = "blue";
canvas.renderAll();
});
// Using the deselected event
polygon.on("deselected", () => {
polygon.fill = "black";
canvas.renderAll();
});
</script>
</body>
</html>
示例 2:將 objectCaching 屬性傳遞“false”值
讓我們看一個程式碼示例,瞭解如何透過將 objectCaching 屬性傳遞“false”值來阻止畫布快取多邊形物件。在這裡我們可以看到,由於 objectCaching 已關閉,因此選擇和取消選擇物件會更改其填充顏色。
<!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>Passing objectCaching property a “false” value</h2>
<p>
You can select and deselect the object to see that the fill colour changes
</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: 0, y: 0 },
{ x: 0, y: 80 },
{ x: 80, y: 80 },
{ x: 80, y: 0 },
],
{
left: 100,
fill: "black",
stroke: "blue",
strokeWidth: 2,
scaleX: 2,
scaleY: 2,
objectCaching: false,
}
);
// Add it to the canvas
canvas.add(polygon);
// Using the selected event
polygon.on("selected", () => {
polygon.fill = "blue";
canvas.renderAll();
});
// Using the deselected event
polygon.on("deselected", () => {
polygon.fill = "black";
canvas.renderAll();
});
</script>
</body>
</html>
結論
在本教程中,我們使用兩個簡單的示例演示瞭如何使用 FabricJS 關閉多邊形物件的快取。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP