如何使用 FabricJS 設定文字框控制角的樣式?


在本教程中,我們將學習如何使用 FabricJS 設定文字框控制角的樣式。我們可以自定義、拉伸或移動文字框中編寫的文字。為了建立文字框,我們必須建立一個 `fabric.Textbox` 類的例項並將其新增到畫布中。物件的控制角允許我們縮放、拉伸或更改其位置。我們可以透過多種方式自定義控制角,例如為其新增特定顏色、更改其大小等。我們可以使用 `cornerStyle` 屬性更改樣式。

語法

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

引數

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

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

選項鍵

  • cornerStyle − 此屬性接受一個字串,允許我們指定所需的控制角樣式。

示例 1

控制角的預設樣式

讓我們來看一個程式碼示例,該示例顯示了控制角的預設樣式。

<!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 style of controlling corners</h2> <p>You can select the textbox to see the default style of controlling corners</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("People are taking the comedians seriously and the politicians as a joke.", { backgroundColor: "rgba(204,255,0,0.2)", width: 400, top: 70, left: 110, cornerColor: "#87a96b", }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>

示例 2

cornerStyle作為鍵傳遞,值為“circle”

我們可以透過將值作為“circle”或“rect”傳遞來指定活動選中物件的控制角的樣式或外觀。“circle”值將使控制角呈圓形,如下例所示:

<!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 cornerStyle as key with the value "circle"</h2> <p>You can select the textbox to see that the corner style has changed to circle</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("People are taking the comedians seriously and the politicians as a joke.", { backgroundColor: "rgba(204,255,0,0.2)", width: 200, top: 70, left: 110, cornerColor: "#87a96b", cornerStyle: "circle", }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>

更新於:2022年8月2日

264 次瀏覽

啟動您的職業生涯

完成課程獲得認證

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