如何使用 FabricJS 设置文本框的垂直缩放比例?


在本教程中,我们将学习如何使用 FabricJS 设置文本框的垂直缩放比例。我们可以自定义、拉伸或移动文本框中的文本。为了创建一个文本框,我们必须创建一个fabric.Textbox类的实例并将其添加到画布上。就像我们可以指定画布中文本框对象的 位置、颜色、不透明度和尺寸一样,我们也可以设置文本框对象的垂直缩放比例。这可以通过使用scaleY属性来实现。

语法

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

参数

  • text − 此参数接受一个字符串,即我们希望在文本框内显示的文本字符串。

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

选项键

  • scaleY  此属性接受一个数字值。分配的值决定了垂直对象缩放比例。其默认值为 1。

示例 1

不使用scaleY时的默认外观

让我们来看一个代码示例,该示例显示在不使用scaleY属性时文本框对象的外观。默认情况下,文本框对象的垂直缩放比例为 1。scaleY确定沿 Y 轴调整对象大小的变换。

<!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 when "scaleY" is not used</h2> <p>You can see that there is no resizing along the Y-axis</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("Politicians and diapers must be changed often, and for the same reason.", { backgroundColor: "#fffff0", width: 400, left: 50, top: 20, fill: "violet", strokeWidth: 2, stroke: "blue", textAlign: "center", }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>

示例 2

scaleY属性作为键传递

在这个例子中,我们将scaleY属性作为键传递,其值为 2。这意味着文本框对象在垂直方向上的缩放比例加倍。

<!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 scaleY property as key</h2> <p>You can see that the object's vertical width has doubled</p> <canvas id="canvas"></canvas> <script> // Initiate a canvas instance var canvas = new fabric.Canvas("canvas"); canvas.setWidth(document.body.scrollWidth); canvas.setHeight(450); // Initiate a textbox object var textbox = new fabric.Textbox("Politicians and diapers must be changed often, and for the same reason.", { backgroundColor: "#fffff0", width: 400, left: 50, top: 20, fill: "violet", strokeWidth: 2, stroke: "blue", textAlign: "center", scaleY: 2, }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>

更新于:2022年8月2日

浏览量:188

启动你的职业生涯

完成课程,获得认证

开始学习
广告
© . All rights reserved.