如何使用 FabricJS 設定文字的上劃線?


在本教程中,我們將學習如何使用 FabricJS 設定 Text 的文字上劃線。我們可以透過新增 fabric.Text 的例項在畫布上顯示文字。它不僅允許我們移動、縮放和更改文字的尺寸,還提供了其他功能,例如文字對齊、文字裝飾、行高,這些功能可以透過 textAlign、underline 和 lineHeight 屬性分別獲得。類似地,我們還可以使用 overline 屬性設定文字上劃線。

語法

new fabric.Text(text: String , { overline : Boolean }: Object)

引數

  • text − 此引數接受一個字串,即我們要顯示的文字字串。

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

選項鍵

  • overline − 此屬性接受一個布林值,它允許我們控制文字上劃線。

示例 1

Text 物件的預設外觀

讓我們來看一個程式碼示例,看看當不使用 overline 屬性時我們的文字物件是什麼樣子。在這種情況下,我們的文字物件將不包含文字裝飾上劃線。

<!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 appearance of the Text object</h2> <p>You can see that the text does not contain text decoration overline</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 text object var text = new fabric.Text("Add sample
text here."
, { width: 300, left: 60, top: 70, fill: "green", }); // Add it to the canvas canvas.add(text); </script> </body> </html>

示例 2

將 overline 屬性作為鍵傳遞,並帶有一個值

在此示例中,我們將看到如何為 overline 屬性賦值建立文字裝飾上劃線。由於我們已將值傳遞為 true,因此文字現在將帶有上劃線。

<!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 appearance of the Text object</h2> <p>You can see that the text contains text decoration overline</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 text object var text = new fabric.Text("Add sample
text here."
, { width: 300, left: 60, top: 70, fill: "green", overline: true, }); // Add it to the canvas canvas.add(text); </script> </body> </html>

更新於: 2022-09-14

157 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.