在本教程中,我們將學習如何使用從頂部設置圖像的位置
FabricJS。我們可以通過創建fabric.Image的實例來創建一個Image對象。自從
它是FabricJS的基本元素之一,我們也可以通過應用輕松定制它
角度、不透明度等屬性。為了設置圖像從頂部的位置,我們使用
top 屬性。
語法
new fabric.Image( element: HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | String, { top: Number }: Object, callback: function)
登錄后復制
參數
element – 此參數接受表示圖像元素的 HTMLImageElement、HTMLCanvasElement、HTMLVideoElement 或 String。該字符串應該是一個 URL,并將作為圖像加載。
選項(可選) – 此參數是一個對象,它提供了額外的自定義我們的對象。使用此參數,可以更改與 top 為屬性的圖像對象相關的原點、描邊寬度和許多其他屬性。
回調(可選) – 此參數是要調用的函數應用最終過濾器后。
選項鍵
top – 該屬性接受一個Number,它允許我們設置距離
圖像畫布頂部。
Image 對象的默認外觀
示例
讓我們看一個代碼示例來了解當 top 時圖像對象的外觀
未使用屬性。
<!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 the default appearance of Image object</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, {
left: 50,
});
// Add it to the canvas
canvas.add(image);
</script>
</body>
</html>
登錄后復制
將 top 屬性作為帶有自定義值的鍵傳遞
示例
在此示例中,我們將值 70 分配給 top 屬性。這將確保
我們的圖像對象放置在距離頂部 70 像素的位置。
<!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 top property as key with a custom value</h2>
<p>
You can see that the Image object is placed at a distance of 70px from the top
</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: 70,
left: 50,
});
// Add it to the canvas
canvas.add(image);
</script>
</body>
</html>
登錄后復制
以上就是如何使用FabricJS設置圖像從頂部的位置?的詳細內容,更多請關注www.92cms.cn其它相關文章!






