在本教學中,我們將學習如何使用 FabricJS 取得影像的物件比例因子。我們可以透過建立fabric.Image的實例來建立一個Image物件。由於它是 FabricJS 的基本元素之一,我們也可以透過應用角度、不透明度等屬性輕鬆自訂它。為了取得 Image 的物件比例因子,我們使用 getObjectScaling 方法。
getObjectScaling(): Object
讓我們看一個程式碼範例,以查看使用 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="max-width:90%" / alt="如何使用FabricJS取得Image的物件比例因子?" > <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 屬性結合使用時記錄的輸出。在這種情況下,影像物件的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="max-width:90%" / alt="如何使用FabricJS取得Image的物件比例因子?" > <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的物件比例因子?的詳細內容。更多資訊請關注PHP中文網其他相關文章!