如何使用 FabricJS 鎖定文字框的水平縮放?
在本教程中,我們將學習如何使用 FabricJS 鎖定文字框的水平縮放。就像我們可以在畫布中指定文字框物件的位
語法
new fabric.Textbox(text: String, { lockScalingX : Boolean }: Object)
引數
text − 此引數接受一個 字串,表示我們希望在文字框內顯示的文字字串。
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>Default behaviour of a Textbox object in the canvas</h2> <p>You can scale the textbox in horizontal and vertical directions to verifythat scaling is possible in both directions.</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiate a textbox object var textbox = new fabric.Textbox("Stars can’t shine without darkness.", { width: 400, left: 110, top: 70, fill: "violet", strokeWidth: 2, stroke: "pink", textAlign: "center", }); // Add it to the canvas canvas.add(textbox); </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>Passing lockScalingX as key with "true" value</h2> <p>You can see a "not-allowed" cursor if you try to scale the textbox in the X-direction </p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(250); // Initiate a textbox object var textbox = new fabric.Textbox("Stars can't shine without darkness.", { width: 400, left: 110, top: 70, fill: "violet", strokeWidth: 2, stroke: "pink", textAlign: "center", lockScalingX: true, }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>
廣告