如何使用 FabricJS 设置文本的上划线?


在本教程中,我们将学习如何使用 FabricJS 设置 Text 的文本上划线。我们可以通过添加 fabric.Text 的实例在画布上显示文本。它不仅允许我们移动、缩放和更改文本的尺寸,还提供了其他功能,例如文本对齐、文本装饰、行高,这些功能可以通过 textAlign、underline 和 lineHeight 属性分别获得。类似地,我们还可以使用 overline 属性设置文本上划线。

语法

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

参数

  • text − 此参数接受一个字符串,即我们要显示的文本字符串。

  • options (可选) − 此参数是一个对象,它为我们的文本提供了额外的自定义选项。使用此参数可以更改与对象的许多其他属性相关的颜色、光标、边框宽度等,其中 overline 是一个属性。

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

选项键

  • overline − 此属性接受一个布尔值,它允许我们控制文本上划线。

示例 1

Text 对象的默认外观

让我们来看一个代码示例,看看当不使用 overline 属性时我们的文本对象是什么样子。在这种情况下,我们的文本对象将不包含文本装饰上划线。

Open Compiler
<!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,因此文本现在将带有上划线。

Open Compiler
<!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 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告