如何使用 FabricJS 設定文字框控制角的大小?


在本教程中,我們將學習如何使用 FabricJS 設定文字框控制角的大小。物件的控制角允許我們縮放、拉伸或更改其位置。我們可以透過多種方式自定義控制角,例如為其新增特定顏色、更改其大小等。我們可以使用cornerSize屬性更改大小。

語法

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

引數

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

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

選項鍵

  • cornerSize − 此屬性接受一個數字,它允許我們操作所選物件控制角的大小。其預設值為 13。

示例 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 size of the controlling corners</h2> <p>You can select the textbox to see the default size of the 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("The best way to teach your kids about taxes is by eating 30 percent of their ice cream.", { backgroundColor: "rgba(204,255,0,0.2)", width: 400, top: 20, left: 110, cornerColor: "#87a96b", }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>

示例 2

cornerSize作為鍵傳遞,並使用自定義值

在此示例中,我們將cornerSize屬性作為鍵傳遞,其值為 17。我們可以看到當文字框物件被選中時,它如何改變控制角的大小。

<!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 cornerSize as key with a custom value</h2> <p>You can select the textbox to see the size of the 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("The best way to teach your kids about taxes is by eating 30 percent of their ice cream.", { backgroundColor: "rgba(204,255,0,0.2)", width: 400, top: 70, left: 110, cornerColor: "#87a96b", cornerSize: 17, }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>

更新於: 2022年8月2日

139 次檢視

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.