如何使用 FabricJS 使文本框的控制角透明?


在本教程中,我们将学习如何使用 FabricJS 使文本框的控制角透明。我们可以自定义、拉伸或移动文本框中的文字。为了创建一个文本框,我们必须创建一个fabric.Textbox类的实例并将其添加到画布中。transparentCorners属性允许我们将文本框的控制角设置为透明。

语法

new fabric.Textbox(text: String, { transparentCorners: Boolean }: Object)

参数

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

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

选项键

  • transparentCorners − 此属性接受一个布尔值,允许我们将对象的控制角渲染为透明。其默认值为 true。

示例 1

transparentCorners属性作为键,值为“false”

让我们来看一个代码示例,创建一个控制角不透明的文本框对象。为此,我们需要将transparentCorners属性设置为 False 值。

<!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 transparentCorners property as key with a ‘false’ value</h2> <p>You can select the textbox to see that the controlling corners are opaque </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("Failure is success if we learn from it.", { backgroundColor: "#fffff0", width: 400, left: 110, top: 70, fill: "violet", strokeWidth: 2, stroke: "blue", textAlign: "center", transparentCorners: false, }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>

示例 2

transparentCorners属性作为键,值为“true”

在这个例子中,我们将transparentCorners属性设置为 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>Passing transparentCorners as key with a "true" value</h2> <p>You can select the textbox to see that the controlling corners are transparent</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("Failure is success if we learn from it.", { backgroundColor: "#fffff0", width: 400, left: 110, top: 70, fill: "violet", strokeWidth: 2, stroke: "blue", textAlign: "center", transparentCorners: true, }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>

更新于: 2022年8月2日

浏览量 164

开启你的职业生涯

完成课程,获得认证

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