In this tutorial, we will learn how to find the object scale factor of an image using FabricJS. We can create an image object by creating an instance of fabric.Image. Since it is one of the basic elements of FabricJS, we can also easily customize it by applying properties such as angle, opacity, etc. To find the object scaling factor of an image, we use the getTotalObjectScaling method.
getTotalObjectScaling(): Object
In this example, we use the getTotalObjectScaling method to get the object scaling factor of the image. In this case, the recorded output is approximately 1.5 for scaleX and scaleY respectively.
<!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="How to find the object scale factor of an image using 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>
Let's look at a code example of what the log output looks like when using the getTotalObjectScaling method with the scaleX property. Here, we set the value of the scaleX property to 2. So the log output shows that the value of scaleX is around 3, while the value of scaleY remains the same.
<!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="How to find the object scale factor of an image using 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>
The above is the detailed content of How to find the object scale factor of an image using FabricJS?. For more information, please follow other related articles on the PHP Chinese website!