如何使用 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.