如何使用 FabricJS 鎖定橢圓的旋轉?
在本教程中,我們將學習如何使用 FabricJS 鎖定橢圓的旋轉。就像我們可以在畫布上指定橢圓物件的位 置、顏色、不透明度和尺寸一樣,我們也可以指定是否要旋轉它。這可以透過使用`lockRotation` 屬性來實現。
語法
new fabric.Ellipse({ lockRotation : Boolean }: Object)
引數
options (可選) − 此引數是一個物件,它為我們的橢圓提供額外的自定義。使用此引數可以更改與物件相關的顏色、游標、筆劃寬度以及許多其他屬性,其中`lockRotation` 是一個屬性。
選項鍵
lockRotation − 此屬性接受一個布林值。如果我們將其賦值為“true”,則物件的旋轉將被鎖定。
示例 1
畫布中橢圓物件的預設行為
讓我們來看一個例子,瞭解當不使用`lockRotation` 屬性時橢圓物件的預設行為。預設情況下,我們可以逆時針或順時針旋轉橢圓物件。
<!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 lock the rotation of Ellipse using FabricJS</h2> <p>You can select the object and rotate it freely, as we have not used the <b>lockRotation</b> property. 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: 115, top: 50, fill: "white", rx: 80, ry: 50, stroke: "black", strokeWidth: 5, }); // Adding it to the canvas canvas.add(ellipse); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
示例 2
將 lockRotation 作為鍵並賦值為 'true'
在這個例子中,我們將看到如何使用`lockRotation` 屬性來阻止橢圓物件旋轉的能力。我們可以看到,一旦我們嘗試旋轉橢圓物件,就會顯示一個禁止的游標。這意味著旋轉操作不再允許。
<!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 lock the rotation of Ellipse using FabricJS</h2> <p>Here you can select the object but cannot rotate it freely, as we have set <b>lockRotation</b> 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: 115, top: 50, fill: "white", rx: 80, ry: 50, stroke: "black", strokeWidth: 5, lockRotation: true, }); // Adding it to the canvas canvas.add(ellipse); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
廣告