如何使用 FabricJS 锁定文本框的水平缩放?
在本教程中,我们将学习如何使用 FabricJS 锁定文本框的水平缩放。就像我们可以在画布中指定文本框对象的位
语法
new fabric.Textbox(text: String, { lockScalingX : Boolean }: Object)
参数
text − 此参数接受一个 字符串,表示我们希望在文本框内显示的文本字符串。
options (可选) − 此参数是一个 对象,它为我们的文本框提供了额外的自定义选项。使用此参数,可以更改与对象相关的许多属性,例如颜色、光标、笔触宽度等,其中 lockScalingX 就是一个属性。
选项键
lockScalingX − 此属性接受一个 布尔值。如果我们为其分配一个“true”值,则对象的水平缩放将被锁定。
示例 1
画布中文本框对象的默认行为
让我们看一个代码示例来了解当不使用 lockScalingX 属性时文本框对象的默认行为。可以在水平和垂直方向上缩放对象。
<!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 behaviour of a Textbox object in the canvas</h2> <p>You can scale the textbox in horizontal and vertical directions to verifythat scaling is possible in both directions.</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("Stars can’t shine without darkness.", { width: 400, left: 110, top: 70, fill: "violet", strokeWidth: 2, stroke: "pink", textAlign: "center", }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>
示例 2
将 lockScalingX 作为键传递,值为“true”
在本示例中,我们将看到如何使用 lockScalingX 属性来阻止文本框对象水平缩放。如我们所见,虽然可以垂直缩放文本框对象,但我们不允许在水平方向上执行相同的操作。
<!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 lockScalingX as key with "true" value</h2> <p>You can see a "not-allowed" cursor if you try to scale the textbox in the X-direction </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("Stars can't shine without darkness.", { width: 400, left: 110, top: 70, fill: "violet", strokeWidth: 2, stroke: "pink", textAlign: "center", lockScalingX: true, }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP