您好,欢迎访问一九零五行业门户网

如何使用 FabricJS 禁用 Textbox 的居中缩放?

在本教程中,我们将学习如何使用 fabricjs 禁用 textbox 的居中缩放。我们可以自定义、拉伸或移动文本框中写入的文本。为了创建文本框,我们必须创建 fabric.textbox 类的实例并将其添加到画布中。通过控件进行缩放时,为 centeredscaling 属性分配一个真值,使用中心作为对象的变换原点。
语法new fabric.textbox(text: string, { centeredscaling: boolean }: object)
参数text − 此参数接受一个string,它是我们要使用的文本字符串。想要在我们的文本框中显示。
选项(可选) - 此参数是一个对象,它提供了额外的自定义我们的文本框。使用此参数,可以更改与 centeredscaling 属性相关的对象的颜色、光标、描边宽度等属性。
options keyscenteredscaling - 此属性接受布尔值并允许我们控制对象是否应该是否使用其中心作为变换原点。
示例 1传递 centeredscaling 作为键,并且为其分配“true”值
让我们看一个代码示例,了解启用 centeredscaling 属性时文本框对象的行为。当我们放大对象时,变换的原点是文本框的中心。
<!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 centeredscaling as key and assigning a "true" value to it</h2> <p>try scaling the textbox to see that centered scaling has been enabled</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("success is the child of audacity.", { backgroundcolor: "#ffe5b4", width: 400, top: 70, left: 110, centeredscaling: true, }); // add it to the canvas canvas.add(textbox); </script></body></html>
示例 2禁用 centeredscaling 属性
我们可以通过为其指定“false”值来禁用 centeredscaling 属性。这将不再使用文本框的中心作为变换的中心。这是一个代码示例来演示 -
<!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>disabling centeredscaling property</h2> <p>try scaling the textbox to see that centered scaling has been disabled</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("success is the child of audacity.", { backgroundcolor: "#ffe5b4", width: 400, top: 70, left: 110, centeredscaling: false, }); // add it to the canvas canvas.add(textbox); </script></body></html>
以上就是如何使用 fabricjs 禁用 textbox 的居中缩放?的详细内容。
其它类似信息

推荐信息