如何使用 FabricJS 以程式設計方式實現全選刪除操作?
我們可以透過建立fabric.Polygon的例項來建立多邊形物件。多邊形物件可以由任何由一組連線的直線段組成的閉合形狀來表示。由於它是 FabricJS 的基本元素之一,因此我們還可以透過應用角度、不透明度等屬性輕鬆地對其進行自定義。
為了以程式設計方式實現全選刪除,我們需要使用clear方法。此方法清除例項的所有上下文。
語法
clear(): fabric.Canvas
示例 1:在多邊形上以程式設計方式實現全選刪除
讓我們來看一個程式碼示例,瞭解如何在多邊形上以程式設計方式實現全選刪除。我們建立了一個名為Remove()的函式,當我們點選Remove按鈕時將呼叫該函式。在這裡,我們使用了clear()方法從畫布中刪除所有多邊形例項。
<!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>Implementing delete-all programmatically on Polygon</h2>
<p>
You can click on the Remove button which removes all instances of Polygon from the canvas
</p>
<button id="btn" onclick="Remove()" style="padding: 3px">Remove</button>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiating Remove function
function Remove() {
canvas.clear().renderAll();
}
// Initiating a polygon object
var polygon1 = new fabric.Polygon(
[
{ x: 30, y: 50 },
{ x: 70, y: 50 },
{ x: 0, y: 0 },
{ x: 70, y: 0 },
],
{
left: 110,
top: 40,
fill: "#1e90ff",
strokeWidth: 4,
stroke: "black",
scaleX: 1.5,
scaleY: 1.5,
objectCaching: false,
}
);
// Initiating another polygon object
var polygon2 = new fabric.Polygon(
[
{ x: 60, y: 90 },
{ x: 100, y: 20 },
{ x: 55, y: 40 },
{ x: 75, y: 50 },
],
{
left: 200,
top: 60,
fill: "#080808",
strokeWidth: 4,
stroke: "red",
scaleX: 1.5,
scaleY: 1.5,
objectCaching: false,
}
);
// Adding them to the canvas
canvas.add(polygon1, polygon2);
</script>
</body>
</html>
示例 2:在矩形上以程式設計方式實現全選刪除
讓我們來看一個程式碼示例,瞭解如何在矩形上以程式設計方式實現全選刪除。我們初始化了兩個矩形物件並將它們新增到畫布中。一旦我們點選Remove按鈕,所有物件都將從畫布中刪除。
<!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>Implementing delete-all programmatically on Rectangle</h2>
<p>
You can click on the Remove button which removes all instances of rectangle from the canvas
</p>
<button id="btn" onclick="Remove()" style="padding: 3px">Remove</button>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiating Remove function
function Remove() {
canvas.clear().renderAll();
}
// Initiating a rectangle object
var rectangle1 = new fabric.Rect({
width: 50,
height: 30,
left: 110,
top: 40,
fill: "#1e90ff",
strokeWidth: 4,
stroke: "black",
objectCaching: false,
});
// Initiating another rectangle object
var rectangle2 = new fabric.Rect({
width: 50,
height: 30,
left: 200,
top: 60,
fill: "#080808",
strokeWidth: 4,
stroke: "red",
objectCaching: false,
});
// Adding them to the canvas
canvas.add(rectangle1, rectangle2);
</script>
</body>
</html>
結論
在本教程中,我們使用兩個簡單的示例演示瞭如何使用 FabricJS 以程式設計方式實現全選刪除操作。
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP