如何使用 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 使多邊形物件對拖放事件做出反應。
廣告