在本教程中,我們將學習如何使用 FabricJS 獲取圖像的對象比例因子。我們可以通過創建fabric.Image的實例來創建一個Image對象。由于它是 FabricJS 的基本元素之一,我們還可以通過應用角度、不透明度等屬性輕松自定義它。為了獲取 Image 的對象比例因子,我們使用 getObjectScaling 方法。
語法
getObjectScaling(): Object
登錄后復制
使用getObjectscaling方法
示例
讓我們看一個代碼示例,以查看使用 getObjectScaling 方法時記錄的輸出。在這種情況下,控制臺中將顯示默認的scaleX和scaleY值為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 the getObjectscaling method</h2> <p> You can open console from dev tools and see that the logged value is being displayed in the console </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 getObjectScaling method console.log("The scale factor is", image.getObjectScaling()); </script> </body> </html>
登錄后復制
使用getObjectScaling方法并傳遞scaleX屬性
示例
讓我們看一個代碼示例,以查看當 getObjectScaling 方法與 scaleX 屬性結合使用時記錄的輸出。在這種情況下,圖像對象的scaleX值將顯示為2,而scaleY值將保持不變。
<!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 getObjectScaling method and passing the scaleX property</h2> <p> You can open console from dev tools and see that the logged value is being displayed in the console </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, scaleX: 2, }); // Add it to the canvas canvas.add(image); // Using getObjectScaling method console.log("The scale factor is", image.getObjectScaling()); </script> </body> </html>
登錄后復制
以上就是如何使用FabricJS獲取Image的對象比例因子?的詳細內容,更多請關注www.92cms.cn其它相關文章!