如何使用 FabricJS 設定文字框 X 軸的傾斜角度?


在本教程中,我們將學習如何使用 FabricJS 設定文字框 X 軸的傾斜角度。我們可以自定義、拉伸或移動文字框中的文字。為了建立一個文字框,我們必須建立一個 fabric.Textbox 類的例項並將其新增到畫布中。我們的文字框物件可以透過多種方式進行自定義,例如更改其尺寸、新增背景顏色或更改 X 軸的傾斜角度。我們可以使用 skewX 屬性來實現這一點。

語法

new fabric.Textbox(text: String, { skewX : Number }: Object)

引數

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

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

選項鍵

  • skewX − 此屬性接受一個數字,用於確定物件 X 軸的傾斜角度。

示例 1

未應用 skewX 屬性時

讓我們看一個程式碼示例,瞭解當未應用 skewX 屬性時文字框物件的外觀。在這種情況下,文字框物件將沒有任何角度的傾斜。

<!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>When the skewX property is not applied</h2> <p>You can see there is no skew by any angle on the textbox by default</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("A smile is a curve that sets everything straight.", { backgroundColor: "#fffff0", width: 400, left: 110, top: 70, fill: "violet", strokeWidth: 2, stroke: "blue", textAlign: "center", }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>

示例 2

skewX 作為鍵併為其分配自定義值。

在這個示例中,我們將看到如何為 skewX 屬性分配數值。傳遞的值將決定沿 X 軸的傾斜。

<!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 skewX as key and assigning a custom value to it.</h2> <p>You can see the textbox has skewed on the X-axis at a 50-degree angle.</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("A smile is a curve that sets everything straight.", { backgroundColor: "#fffff0", width: 400, left: 110, top: 70, fill: "violet", strokeWidth: 2, stroke: "blue", textAlign: "center", skewX: 50, }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>

更新於: 2022年8月2日

瀏覽量 107

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.