在本教程中,我們將學習如何使用FabricJS來尋找影像的物件比例因子。我們可以透過建立fabric.Image的實例來建立一個映像物件。由於它是FabricJS的基本元素之一,我們也可以透過應用諸如角度、不透明度等屬性來輕鬆自訂它。為了找到影像的物件比例因子,我們使用getTotalObjectScaling方法。
getTotalObjectScaling(): Object
在這個例子中,我們使用了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://www.tutorialspoint.com/images/logo.png" id="img1" style="max-width:90%" / alt="如何使用FabricJS找到影像的物件比例因子?" > <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屬性一起使用時,輸出的日誌如何。在這裡,我們將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://www.tutorialspoint.com/images/logo.png" id="img1" style="max-width:90%" / alt="如何使用FabricJS找到影像的物件比例因子?" > <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>
以上是如何使用FabricJS找到影像的物件比例因子?的詳細內容。更多資訊請關注PHP中文網其他相關文章!