在本教程中,我們將學習如何在 FabricJS 中識別 Image 實例的類型。我們可以通過創建fabric.Image的實例來創建一個Image對象。由于它是 FabricJS 的基本元素之一,我們還可以通過應用角度、不透明度等屬性輕松自定義它。為了識別 Image 實例的類型,我們使用 isType 方法。
語法
isType(type: String): Boolean
登錄后復制
參數
type – 此參數接受一個String,它指定我們要檢查的類型。
使用isType方法
示例
讓我們看一個代碼示例,看看使用 isType 方法時記錄的輸出。 isType 方法返回 true 或 false 值,具體取決于實例的類型是否與我們要檢查的類型匹配。在這種情況下,由于類型匹配,因此返回 true 值。
<!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 isType method</h2> <p> You can open console from dev tools and see that the logged output contains a true value </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, }); // Add it to the canvas canvas.add(image); // Using isType method console.log( "Is the specified type identical to an image instance? : ", image.isType("image") ); </script> </body> </html>
登錄后復制
使用具有不同值的 isType 方法
示例
在此示例中,我們使用 isType 來檢查指定的圓圈類型是否與圖像實例相同。此處,由于它們不相同,因此返回錯誤值。
<!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 isType method with a different value</h2> <p> You can open console from dev tools and see that the logged output contains a false value </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, }); // Add it to the canvas canvas.add(image); // Using isType method console.log( "Is the specified type identical to an image instance? : ", image.isType("circle") ); </script> </body> </html>
登錄后復制
以上就是如何使用FabricJS識別Image實例的類型?的詳細內容,更多請關注www.92cms.cn其它相關文章!