FabricJS – 以程式設計方式實現僅對選中物件進行刪除操作
我們可以透過建立fabric.Polygon的例項來建立一個多邊形物件。多邊形物件可以由任何由一組連線的直線段組成的封閉形狀來表徵。由於它是 FabricJS 的基本元素之一,我們也可以透過應用角度、不透明度等屬性輕鬆自定義它。
為了實現僅對選中物件進行刪除操作,我們需要使用remove方法。
語法
remove( ...Object): Self
引數
物件 − 此屬性接受一個fabric.Object值,即我們要刪除的物件。
示例 1:以程式設計方式在多邊形上實現僅對選中物件進行刪除操作
讓我們來看一個程式碼示例,瞭解如何使用 FabricJS 對選定的多邊形物件執行刪除操作。remove方法允許我們從畫布中刪除物件。在本例中,我們建立了一個deleteHandler,它將在每次發生mouse:down事件時被呼叫。因此,單擊物件時,它將從畫布中刪除。
<!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 operation only for selected object programmatically on Polygon
</h2>
<p>
You can see that on clicking on an object, it instantly gets removed from the canvas
</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiating a points array
var points = [
{ x: 30, y: 50 },
{ x: 70, y: 50 },
{ x: 0, y: 0 },
{ x: 70, y: 0 },
];
// Initiating a polygon object
var polygon1 = new fabric.Polygon(points, {
left: 100,
top: 40,
fill: "#1e90ff",
strokeWidth: 4,
stroke: "black",
scaleX: 1.2,
scaleY: 1.2,
});
// Initiating another polygon object
var polygon2 = new fabric.Polygon(
[
{ x: 60, y: 90 },
{ x: 45, y: 30 },
{ x: 20, y: 48 },
],
{
left: 30,
top: 30,
fill: "#080808",
strokeWidth: 4,
stroke: "red",
scaleX: 2,
scaleY: 2,
}
);
// Adding them to the canvas
canvas.add(polygon1, polygon2);
// Initiating a function called deleteHandler
var deleteHandler = function removeFunction(obj) {
var selectedObject = obj.target;
canvas.remove(selectedObject);
canvas.renderAll();
};
// Using the mouse:down event
canvas.on("mouse:down", deleteHandler);
</script>
</body>
</html>
示例 2:以程式設計方式在圓形上實現僅對選中物件進行刪除操作
讓我們來看一個程式碼示例,瞭解如何使用 FabricJS 對選定的圓形物件執行刪除操作。我們初始化了兩個圓形物件,名為circle1和circle2,並將它們新增到畫布中。此外,我們也使用了類似的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 operation only for selected object programmatically on Circle
</h2>
<p>
You can see that on clicking on an object, it instantly gets removed from the canvas
</p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// Initiating a circle object
var circle1 = new fabric.Circle({
left: 100,
top: 80,
radius: 30,
fill: "#1e90ff",
strokeWidth: 4,
stroke: "black",
scaleX: 1.2,
scaleY: 1.2,
});
// Initiating another circle object
var circle2 = new fabric.Circle({
radius: 15,
left: 30,
top: 30,
fill: "#080808",
strokeWidth: 4,
stroke: "red",
scaleX: 2,
scaleY: 2,
});
// Adding them to the canvas
canvas.add(circle1, circle2);
// Initiating a function called deleteHandler
var deleteHandler = function removeFunction(obj) {
var selectedObject = obj.target;
canvas.remove(selectedObject);
canvas.renderAll();
};
// Using the mouse:down event
canvas.on("mouse:down", deleteHandler);
</script>
</body>
</html>
結論
在本教程中,我們使用兩個簡單的示例演示瞭如何使用 FabricJS 以程式設計方式實現僅對選中物件進行刪除操作。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP