如何使用 FabricJS 檢查影像是否應用了裁剪?
在本教程中,我們將展示如何使用 FabricJS 檢查影像是否應用了裁剪。我們可以透過建立fabric.Image的例項來建立影像物件。由於它是 FabricJS 的基本元素之一,因此我們也可以透過應用角度、不透明度等屬性輕鬆自定義它。為了查詢影像是否應用了裁剪,我們使用hasCrop方法。如果未應用裁剪,則此方法返回 false,如果應用了裁剪,則返回應用的裁剪值。
語法
hasCrop(): Boolean | Number
使用hasCrop方法
示例
在此示例中,我們使用了hasCrop方法來查詢影像物件是否應用了裁剪。在這種情況下,我們的影像物件沒有任何影像裁剪,因此記錄的輸出為 false。
<!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 the hasCrop method</h2> <p> You can open the console from dev tools to see that the logged output is false </p> <canvas id="canvas"></canvas> <img src="https://tutorialspoint.tw/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 the hasCrop method console.log("Is crop applied for the Image object?: ", image.hasCrop()); </script> </body> </html>
將hasCrop方法與cropY屬性一起使用
示例
讓我們看看在將hasCrop方法與cropY屬性一起使用時記錄輸出的程式碼示例。我們為cropY屬性傳遞了值 2,這將確保影像物件在 Y 方向上具有 2px 的影像裁剪。在這種情況下,裁剪值將在控制檯中返回。
<!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 the hasCrop method along with cropY property</h2> <p> You can open the console from dev tools to see that the logged output is 2 </p> <canvas id="canvas"></canvas> <img src="https://tutorialspoint.tw/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, cropY: 2, }); // Add it to the canvas canvas.add(image); // Using the hasCrop method console.log( "Crop value applied for the Image object is: ", image.hasCrop() ); </script> </body> </html>
結論
在本教程中,我們使用兩個示例演示瞭如何使用 FabricJS 檢查影像是否應用了裁剪。
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP