在本教程中,我們將使用 FabricJS 設置橢圓的旋轉角度。橢圓形是 FabricJS 提供的各種形狀之一。為了創建一個橢圓,我們必須創建一個 Fabric.Ellipse 類的實例并將其添加到畫布中。 FabricJS 中的角度屬性定義了對象的 2D 旋轉角度。我們還有 centeredRotation 屬性,它允許我們使用橢圓的中心點作為變換的原點。
語法
new fabric.Ellipse({ angle: Number, centeredRotation: Boolean }: Object)
登錄后復制
參數
選項(可選)- 此參數是一個對象 為我們的橢圓提供額外的定制。使用此參數可以更改與畫布相關的顏色、光標、描邊寬度和許多其他屬性,其中角度和centeredRotation是屬性。
選項鍵
角度 – 此屬性接受數字 指定橢圓的旋轉角度(以度為單位)。
centeredRotation – 這屬性接受一個布爾值,該值確定橢圓的中心是否是變換的原點。
示例 1
傳遞 angle 作為具有自定義值的鍵,并禁用橢圓的居中旋轉
讓我們舉個例子,在 FabricJS 中設置橢圓的旋轉角度。負角度指定逆時針方向,而正角度指定順時針方向。由于我們為centeredRotation分配了一個“false”值,因此橢圓將在使用其角點作為旋轉中心的同時進行旋轉。
<!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>How to set the angle of rotation of an Ellipse using FabricJS?</h2>
<p>Select the object and rotate it. Here we have set the angle of rotation at <b>-40</b></p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate an Ellipse instance
var ellipse = new fabric.Ellipse({
left: 180,
top: 180,
rx: 90,
ry: 50,
fill: "green",
stroke: "blue",
strokeWidth: 2,
angle: -40,
centeredRotation: false
})
// Adding it to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
登錄后復制
示例 2
啟用橢圓居中旋轉
從這個示例中我們可以看出,通過設置 centeredRotation 屬性為“true”,我們的橢圓現在使用其中心作為旋轉中心。在版本 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>How to set the angle of rotation of an Ellipse using FabricJS?</h2>
<p>Select the object and rotate it. You will notice that the object rotates around its center as we have set the <b>centeredRotation</b> property to True. </p>
<canvas id="canvas"></canvas>
<script>
// Initiate a canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate an Ellipse instance
var ellipse = new fabric.Ellipse({
left: 180,
top: 180,
rx: 90,
ry: 50,
fill: "green",
stroke: "blue",
strokeWidth: 2,
angle: -40,
centeredRotation: true
});
// Adding it to the canvas
canvas.add(ellipse);
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
</script>
</body>
</html>
登錄后復制
以上就是如何使用 FabricJS 設置橢圓的旋轉角度?的詳細內容,更多請關注www.92cms.cn其它相關文章!






