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 以编程方式实现仅对选中对象进行删除操作。
广告