FabricJS – 以编程方式实现只针对选定对象的删除操作
我们可以通过创建一个fabric.Polygon的实例来创建一个Polygon对象。一个多边形对象可以被描述为由一组连接的直线段组成的任何封闭形状。由于它是FabricJS的基本元素之一,我们也可以通过应用角度、不透明度等属性轻松地定制它。
为了只对一个选定的对象实现删除操作,我们需要使用remove方法。
语法
remove( ...Object): Self
参数
**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以编程方式实现只针对选定对象的删除操作。