在本教程中,我們將學(xué)習(xí)如何使用 FabricJS拉直圖像對(duì)象。
我們可以通過創(chuàng)建fabric.Image的實(shí)例來創(chuàng)建一個(gè)Image對(duì)象。既然是一個(gè)
FabricJS 的基本元素,我們還可以通過應(yīng)用輕松自定義它
諸如角度、不透明度等屬性。為了拉直圖像對(duì)象,我們使用
拉直方法。
語法
straighten(): fabric.Object
登錄后復(fù)制
在不使用straighten的情況下向角度屬性傳遞值
方法
示例
讓我們看一個(gè)代碼示例,看看當(dāng) straighten 時(shí)我們的 Image 對(duì)象看起來如何
方法沒有被使用。 straighten 方法通過將對(duì)象從其所在位置旋轉(zhuǎn)來straighten
當(dāng)前角度為 0、90、180 或 270 等,具體取決于更接近的角度。角度
屬性設(shè)置對(duì)象的旋轉(zhuǎn)角度(以度為單位)。在這里,我們指定了
角度為 45。但是由于我們沒有應(yīng)用 straighten 屬性,所以旋轉(zhuǎn)角度
將保持 45 度。
<!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 angle property a value without using the straighten method
</h2>
<p>You can see that the Image object has an angle of 45 degrees</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,
angle: 45,
});
// Add it to the canvas
canvas.add(image);
</script>
</body>
</html>
登錄后復(fù)制
Using the straighten 方法
示例
讓我們看一個(gè)代碼示例,看看當(dāng) straighten 時(shí) Image 對(duì)象是什么樣子的
方法與角度屬性結(jié)合使用。雖然我們已經(jīng)設(shè)定了角度
旋轉(zhuǎn)為 45 度,我們的圖像對(duì)象將通過將其旋轉(zhuǎn)回 0 來拉直
degree as we have used the 拉直方法。
<!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 straighten method</h2>
<p>
You can see that the angle of rotation is 0 degree for the 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, {
top: 50,
left: 110,
angle: 45,
});
// Add it to the canvas
canvas.add(image);
// Using the straighten method
image.straighten();
</script>
</body>
</html>
登錄后復(fù)制
以上就是如何使用 FabricJS 拉直 Image 對(duì)象?的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注www.92cms.cn其它相關(guān)文章!






