如何使用FabricJS鎖定橢圓的水平縮放?
在本教程中,我們將學習如何使用FabricJS鎖定橢圓的水平縮放。正如我們可以在畫布中指定橢圓物件的位 置、顏色、不透明度和尺寸一樣,我們還可以指定是否要阻止物件的水平縮放。這可以透過使用`lockScalingX`屬性來完成。
語法
new fabric.Ellipse({ lockScalingX : Boolean }: Object)
引數
options (可選) − 此引數是一個物件,它為我們的橢圓提供了額外的自定義功能。使用此引數可以更改與物件的許多屬性相關的顏色、游標、筆劃寬度等,其中`lockScalingX`就是一個屬性。
選項鍵
lockScalingX − 此屬性接受一個布林值。如果我們將其賦值為“true”,則物件的水平縮放將被鎖定。
示例 1
畫布中橢圓物件的預設行為
讓我們來看一個例子,瞭解在不使用`lockScalingX`屬性時橢圓物件的預設行為。可以水平和垂直兩個方向縮放物件。
<!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 horizontal scaling of Ellipse using FabricJS?</h2> <p>Here you can select the object and scale it both horizontally and vertically because we have not used the <b>lockScalingX</b> property. </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
將`lockScalingX`作為鍵,值設定為'true'
在這個例子中,我們將看到如何使用`lockScalingX`屬性來阻止橢圓物件水平縮放。雖然我們可以垂直縮放橢圓物件,但是不允許水平縮放。
<!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 horizontal scaling of Ellipse using FabricJS?</h2> <p>Here you can select the object and scale it vertically, but you can't scale it horizontally because we have set <b>lockScalingX</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, lockScalingX: true, }); // Adding it to the canvas canvas.add(ellipse); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); </script> </body> </html>
廣告