如何使用 FabricJS 查詢影像的物件縮放因子?
在本教程中,我們將學習如何使用 FabricJS 查詢影像的物件縮放因子。我們可以透過建立 fabric.Image 的例項來建立影像物件。由於它是 FabricJS 的基本元素之一,因此我們也可以透過應用角度、不透明度等屬性輕鬆地自定義它。為了查詢影像的物件縮放因子,我們使用 getTotalObjectScaling 方法。
語法
getTotalObjectScaling(): Object
使用 getTotalObjectScaling 方法
示例
在本示例中,我們使用了 getTotalObjectScaling 方法來獲取影像的物件縮放因子。在這種情況下,記錄的輸出分別約為 scaleX 和 scaleY 的 1.5。
<!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 getTotalObjectScaling method</h2> <p> You can open the console from dev tools to see that the logged output contains the object scale factor </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, skewX: 15, }); // Add it to the canvas canvas.add(image); // Using the getTotalObjectScaling method console.log( "The scale factor of the Image object is: ", image.getTotalObjectScaling() ); </script> </body> </html>
使用 getTotalObjectScaling 方法以及 scaleX 屬性
示例
讓我們看看使用 getTotalObjectScaling 方法以及 scaleX 屬性時記錄輸出的程式碼示例。在這裡,我們將 scaleX 屬性的值傳遞為 2。因此,記錄的輸出顯示 scaleX 的值為大約 3,而 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 getTotalObjectScaling method along with scaleX property</h2> <p> You can open the console from dev tools to see that the logged output contains the object scale factor </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, skewX: 15, scaleX: 2, }); // Add it to the canvas canvas.add(image); // Using the getTotalObjectScaling method console.log( "The scale factor of the Image object is: ", image.getTotalObjectScaling() ); </script> </body> </html>
廣告