如何使用 FabricJS 使多边形对象对拖放事件做出反应?
我们可以通过创建fabric.Polygon的实例来创建一个多边形对象。多边形对象可以由任何由一组连接的直线段组成的封闭形状来表示。因为它是在FabricJS中的基本元素之一,所以我们也可以通过应用角度、不透明度等属性轻松地对其进行自定义。我们使用event:dragover和event:drop事件来使多边形对象对拖放事件做出反应。
语法
event:dragover event:drop
示例 1:显示对象如何对drop事件做出反应
让我们来看一个代码示例,当我们将“h1”元素拖放到添加到画布的多边形对象上时,查找记录的输出。我们创建了一个“h1”元素并为其指定了id = "drop-me"。此外,我们使用getElementById()方法返回“h1”元素,并使用addEventListener()方法监听drop事件。因此,当我们抓住“h1”元素并将其拖放到多边形对象上时,与该事件相关的属性将显示出来。
<!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>Displaying how the object reacts to drop event</h2> <p> You can drop the box on the object and open the console from the dev tools to see that the event name, subTargets and target are being shown </p> <canvas id="canvas"></canvas> <h1 draggable="true" id="drop-me" style=" color: black; background-color: rgb(166, 103, 248); width: 130px; height: 70 px; padding: 10px;"> Drop me </h1> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiate a polygon instance var polygon = new fabric.Polygon( [ { x: 0, y: 0 }, { x: 0, y: 150 }, { x: 150, y: 150 }, { x: 150, y: 0 }, ], { left: 100, top: 60, fill: "red", stroke: "blue", strokeWidth: 2, objectCaching: false, } ); // Adding it to the canvas canvas.add(polygon); // Initiating the observe function function observe(eventName) { canvas.getObjects().forEach(function (object) { object.on(eventName, function evt(x) { console.log("Drop event has been fired", x); }); }); } // Using addEventListener and listening for drop event document .getElementById("drop-me") .addEventListener("change", observe("drop")); </script> </body> </html>
示例 2:显示对象如何对dragover事件做出反应
让我们来看一个代码示例,当我们将“h1”元素拖过添加到画布的多边形对象上时,查找记录的输出。当我们拖动对象并将其移动到不同的位置时,就会发生拖动事件。但是,当我们将一个对象拖动到另一个对象上时,就会发生dragover事件。在这里,当我们将“h1”元素拖过多边形对象时,事件名称、目标和子目标将记录在控制台中。
<!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>Displaying how the object reacts to dragover event</h2> <p> You can drag the box on the object and open the console from the dev tools to see that the event name, target and subTargets are being shown </p> <canvas id="canvas"></canvas> <h1 draggable="true" id="drag-me" style=" color: black; background-color: purple; width: 130px; height: 70 px; padding: 10px;" > Drag me </h1> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiate a polygon instance var polygon = new fabric.Polygon( [ { x: 0, y: 0 }, { x: 0, y: 150 }, { x: 150, y: 150 }, { x: 150, y: 0 }, ], { left: 100, top: 60, fill: "red", stroke: "blue", strokeWidth: 2, } ); // Adding it to the canvas canvas.add(polygon); // Initiating the observe function function observe(eventName) { canvas.getObjects().forEach(function (object) { object.on(eventName, function evt(x) { console.log("Dragover event has been fired", x); }); }); } // Using addEventListener and listening for dragover event document .getElementById("drag-me") .addEventListener("change", observe("dragover")); </script> </body> </html>
结论
在本教程中,我们使用两个简单的示例演示了如何使用 FabricJS 使多边形对象对拖放事件做出反应。
广告