在本教程中,我們將學習如何使用 FabricJS 將 Image 物件縮放到給定高度。我們可以透過建立fabric.Image的實例來建立一個Image物件。由於它是 FabricJS 的基本元素之一,我們也可以透過應用角度、不透明度等屬性輕鬆自訂它。為了將 Image 物件縮放到給定高度,我們使用 scaleToHeight 方法.
scaleToHeight(value: Number, absolute: Boolean): fabric.Object
value - 此參數接受一個Number,它決定 Image 物件的新高度值。
< /里>absolute - 此參數接受一個布林值,該值決定是否忽略視口。
讓我們看一個程式碼範例,看看不使用 scaleToHeight 方法時圖像物件的外觀。在這種情況下,我們的影像物件將不會在水平或垂直方向上縮放。
<!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>Default appearance of the Image object</h2> <p> You can see that the object has not been scaled in horizontal or vertical direction </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); </script> </body> </html>
在此範例中,我們將了解如何為 scaleToHeight 方法指派值將影像物件縮放到給定高度。由於我們已將值傳遞為 100,因此這將是圖像物件的新高度。
<!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>Passing the scaleToHeight method with a custom value</h2> <p>You can see that the new height of our image object is 100</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, }); // Using scaleToHeight method image.scaleToHeight(100, false); // Add it to the canvas canvas.add(image); </script> </body> </html>
以上是FabricJS – 如何將影像物件縮放到給定高度?的詳細內容。更多資訊請關注PHP中文網其他相關文章!