如何使用 FabricJS 鎖定文字框的水平傾斜?


在本教程中,我們將學習如何使用 FabricJS 鎖定文字框的水平傾斜。就像我們可以在畫布上指定文字框物件的的位置、顏色、不透明度和尺寸一樣,我們還可以指定是否要停止水平傾斜物件。這可以透過使用 lockSkewingX 屬性來實現。

語法

new fabric.Textbox(text: String, { lockSkewingX : Boolean }: Object)

引數

  • text − 此引數接受一個字串,即我們希望在文字框內顯示的文字字串。

  • options (可選) − 此引數是一個物件,它為我們的文字框提供額外的自定義選項。使用此引數,可以更改與物件的許多屬性相關的屬性,例如顏色、游標、描邊寬度等,其中 lockSkewingX 就是一個屬性。

選項鍵

  • lockSkewingX此屬性接受一個布林值。如果我們為其分配“true”值,則物件的水平傾斜將被鎖定。

示例 1

畫布中文字框物件的預設行為

讓我們看一個程式碼示例,以瞭解當不使用 lockSkewingX 屬性時文字框物件的預設行為。透過按Shift 鍵,然後沿水平或垂直方向拖動,可以實現物件在水平和垂直方向上的傾斜。

<!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 press the shift-key and drag the edge along the X or Y-axis to see that skewing 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("The eyes are useless when the mind is blind.", { width: 400, left: 110, top: 70, fill: "orange", strokeWidth: 2, stroke: "green", textAlign: "center", }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>

示例 2

lockSkewingX 作為鍵傳遞,並使用“true”值

在此示例中,我們將看到如何使用 lockSkewingX 屬性停止文字框物件水平傾斜的能力。正如我們所看到的,儘管我們可以垂直傾斜文字框物件,但我們不允許水平執行相同的操作。

<!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 lockSkewingX as key withtrue’ value</h2> <p>You can try and see that skewing along the x-axis is no longer feasible</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("The eyes are useless when the mind is blind.", { width: 300, left: 110, top: 70, fill: "orange", strokeWidth: 2, stroke: "green", textAlign: "center", lockSkewingX: true, }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>

更新於: 2022年8月2日

158 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.