在本教程中,我們將使用 FabricJS 設(shè)置圓的旋轉(zhuǎn)角度。圓形是 FabricJS 提供的各種形狀之一。為了創(chuàng)建一個圓圈,我們必須創(chuàng)建一個 Fabric.Circle 類的實例并將其添加到畫布中。 FabricJS 中的 angle 屬性定義了對象的 2D 旋轉(zhuǎn)角度。我們還有 centeredRotation 屬性,它允許我們使用圓的中心點作為變換的原點。
語法
new fabric.Circle({ angle: Number, centeredRotation: Boolean }: Object)
登錄后復(fù)制
參數(shù)
選項(可選) – 此參數(shù)是一個對象 為我們的對象提供額外的定制。使用此參數(shù),可以更改與圓相關(guān)的顏色、光標(biāo)、描邊寬度等屬性,其中 angle 和 centeredRotation 是屬性。 p>
選項鍵
角度 – 這個屬性接受一個數(shù)字,它指定圓的旋轉(zhuǎn)角度(以度為單位)。
centeredRotation – 此屬性接受一個布爾值值,該值確定圓心是否是變換的原點。
示例 1
將角度作為具有自定義值的鍵傳遞并禁用圓的居中旋轉(zhuǎn)
以下示例演示了如何在 FabricJS 中設(shè)置圓的旋轉(zhuǎn)角度。負(fù)角度指定逆時針方向,而正角度指定順時針方向。由于我們已為 centeredRotation 指定了 False 值,因此圓將在使用其角點作為旋轉(zhuǎn)中心的同時進(jìn)行旋轉(zhuǎn)。
<!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>Set the angle of rotation of a Circle using FabricJS</h2>
<p>Select the object and observe its controlling corners. You will notice that the angle of rotation is set at 60 degrees in the anti-clockwise direction, as we have set the <b>angle</b> at <b>-60</b>. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var cir = new fabric.Circle({
left: 215,
top: 100,
radius: 50,
fill: "green",
stroke: "blue",
strokeWidth: 2,
angle: -60,
centeredRotation: false,
});
// Adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
登錄后復(fù)制
示例2
啟用圓的居中旋轉(zhuǎn)
從這個示例中我們可以看到,通過設(shè)置centeredRotation 屬性為True,我們的圓現(xiàn)在使用其中心作為旋轉(zhuǎn)中心。在版本 1.3.4 之前,centeredScaling 和 centeredRotation 包含在一個名為 centerTransform 的屬性中。
<!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>Set the angle of rotation of a Circle using FabricJS</h2>
<p>Select the object and observe its controlling corners. Here we have set the angle of rotation at 60 degrees in the anti-clockwise direction, and when you rotate the circle, it will rotate around its center, as we have set <b>centeredRotation</b> to True. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
var cir = new fabric.Circle({
left: 215,
top: 100,
radius: 50,
fill: "green",
stroke: "blue",
strokeWidth: 2,
angle: -60,
centeredRotation: true,
});
// Adding it to the canvas
canvas.add(cir);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
登錄后復(fù)制
以上就是如何使用 FabricJS 設(shè)置圓的旋轉(zhuǎn)角度?的詳細(xì)內(nèi)容,更多請關(guān)注www.92cms.cn其它相關(guān)文章!






