在本教程中,我們將學習如何檢查 Image 物件是否與 使用 FabricJS 的另一個物件。我們可以透過建立實例來建立一個Image對象 fabric.Image。由於它是FabricJS的基本元素之一,我們也可以輕鬆地 透過應用角度、不透明度等屬性來自訂它。為了檢查影像是否 物件與另一個物件相交,我們使用 intersectsWithObject 方法。
intersectsWithObject(other: Object, absolute: Boolean, calculate:Boolean ): Boolean
other - 此參數接受一個物件,它指定我們要測試的物件。
absolute(可選) - 此參數接受一個 String 值,該值指定是否使用不帶 viewportTransform 的座標。此參數是可選的。
計算(可選) - 此參數接受一個布林值,該值指定是否使用目前位置的座標。此參數是可選的。
讓我們來看一個程式碼範例,看看 intersectsWithObject 時記錄的輸出 使用方法。 intersectsWithObject 方法在檢查是否回傳 true 或 false 圖像物件與另一個物件相交。在這裡,我們初始化了兩個矩形 物件即矩形1和矩形2。由於我們的圖像物件與 矩形1,傳回真值。
<!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>Using intersectsWithObject method</h2> <p>You can open console from dev tools and see the logged output</p> <canvas id="canvas"></canvas> <img src="https://www.tutorialspoint.com/images/logo.png" id="img1" style="display: none" /> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiating the image element var imageElement = document.getElementById("img1"); // Initiate an Image object var image = new fabric.Image(imageElement, { top: 50, left: 110, }); // Initiate a rectangle object var rectangleRed = new fabric.Rect({ width: 60, height: 20, top: 40, left: 80, fill: "red", strokeWidth: 6, }); // Initiate another rectangle object var rectangleBlue = new fabric.Rect({ width: 20, height: 40, top: 70, left: 520, fill: "blue", }); // Add them to the canvas canvas.add(image); canvas.add(rectangleRed); canvas.add(rectangleBlue); // Using intersectsWithObject method console.log( "Does the image object intersect with rectangleRed?: ", image.intersectsWithObject(rectangleRed) ); console.log( "Does the image object intersect with rectangleBlue?: ", image.intersectsWithObject(rectangleBlue) ); </script> </body> </html>
在此範例中,我們使用了 intersectsWithObject 方法以及不同的 物件來證明此方法可以用於任何物件。
<!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>Using intersectsWithObject method with different objects</h2> <p>You can open console from dev tools and see the logged output</p> <canvas id="canvas"></canvas> <img src="https://www.tutorialspoint.com/images/logo.png" id="img1" style="display: none" /> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiating the image element var imageElement = document.getElementById("img1"); // Initiate an Image object var image = new fabric.Image(imageElement, { top: 50, left: 110, }); // Initiate a triangle object var triangle = new fabric.Triangle({ width: 90, height: 70, top: 40, left: 80, fill: "red", strokeWidth: 6, }); // Initiate a circle object var circle = new fabric.Circle({ radius: 40, top: 70, left: 520, fill: "blue", }); // Add them to the canvas canvas.add(image); canvas.add(triangle); canvas.add(circle); // Using intersectsWithObject method console.log( "Does the image object intersect with triangle?: ", image.intersectsWithObject(triangle) ); console.log( "Does the image object intersect with circle?: ", image.intersectsWithObject(circle) ); </script> </body> </html>
在本教學中,我們使用兩個範例來示範如何檢查 Image 物件是否 使用 FabricJS 與另一個物件相交。
以上是如何使用 FabricJS 檢查一個 Image 物件是否與另一個物件相交?的詳細內容。更多資訊請關注PHP中文網其他相關文章!