如何使用 FabricJS 停用橢圓的中心旋轉?
在本教程中,我們將學習如何使用 FabricJS 停用橢圓的中心旋轉。橢圓是 FabricJS 提供的各種形狀之一。為了建立一個橢圓,我們必須建立一個fabric.Ellipse類的例項並將其新增到畫布中。預設情況下,FabricJS 中的所有物件都使用它們的中心作為旋轉點。但是,我們可以使用centeredRotation屬性來更改此行為。
語法
new fabric.Ellipse({ centeredRotation: Boolean }: Object)
引數
options (可選) − 此引數是一個物件,它為我們的橢圓提供額外的自定義。使用此引數可以更改與物件的許多屬性相關的顏色、游標、筆劃寬度等,其中centeredRotation是一個屬性。
選項鍵
centeredRotation − 此屬性接受一個布林值,允許我們控制物件在透過控制元件旋轉時是否使用其中心點作為變換的原點。其預設值為True。
示例 1
FabricJS 中橢圓旋轉的預設行為
讓我們來看一個示例,該示例描述了橢圓物件的預設行為。由於centeredRotation屬性預設設定為“true”,因此橢圓物件使用其中心作為旋轉點。
<!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 disable the centered rotation of Ellipse using FabricJS?</h2> <p>Select the object and rotate it. The ellipse will by default rotate around its center. This is the default behavior.</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: 215, top: 100, fill: "white", rx: 90, ry: 50, stroke: "#c154c1", strokeWidth: 5, borderColor: "#daa520", }); // Adding it to the canvas canvas.add(ellipse); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
示例 2
將 centeredRotation 鍵的值設定為“false”
現在我們已經看到了預設行為,讓我們來看一段程式碼來了解當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 disable the centered rotation of Ellipse using FabricJS?</h2> <p>Select the object and try to rotate it. The ellipse will not rotate around its center because we have set <b>centeredRotation</b> to False. </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: 215, top: 100, fill: "white", rx: 90, ry: 50, stroke: "#c154c1", strokeWidth: 5, borderColor: "#daa520", centeredRotation: false, }); // Adding it to the canvas canvas.add(ellipse); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
廣告