如何使用 FabricJS 锁定文本框的旋转?
在本教程中,我们将学习如何使用 FabricJS 锁定文本框的旋转。就像我们可以在画布中指定文本框对象的 位置、颜色、不透明度和尺寸一样,我们还可以指定是否允许旋转。这可以通过使用 lockRotation 属性来实现。
语法
new fabric.Textbox(text: String, { lockRotation : Boolean }: Object)
参数
text − 此参数接受一个字符串,它是我们想要在文本框内显示的文本字符串。
options(可选) − 此参数是一个对象,它为我们的文本框提供了额外的自定义功能。使用此参数,可以更改与对象的许多属性相关的属性,例如颜色、光标、笔划宽度等等,其中lockRotation 就是一个属性。
选项键
lockRotation − 此属性接受一个布尔值。如果我们将其赋值为“true”,则对象的旋转将被锁定。
示例 1
画布中文本框对象的默认行为
让我们来看一个代码示例,以了解当不使用lockRotation 属性时文本框对象的默认行为。默认情况下,我们可以逆时针或顺时针旋转文本框对象。
<!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 try rotating the Textbox using rotation handle to see the default behaviour</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("Don’t let yesterday take up too much of today.", { width: 400, left: 110, top: 70, fill: "orange", strokeWidth: 2, stroke: "green", textAlign: "center", }); // Add it to the canvas canvas.add(textbox); </script> </body> </html>
示例 2
将lockRotation 作为键传递,值为‘true’
在此示例中,我们将看到如何通过使用lockRotation 属性来停止文本框对象旋转的能力。正如我们所看到的,一旦我们尝试旋转文本框对象,就会显示一个不允许的光标。这意味着旋转操作不再允许。
<!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 lockRotation as key with ‘true’ value</h2> <p>Now we see a not-allowed cursor when we try to rotate the textbox</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("Don’t let yesterday take up too much of today.", { width: 400, left: 110, top: 70, fill: "orange", strokeWidth: 2, stroke: "green", textAlign: "center", lockRotation: 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