在本教程中,我们将学习如何使用 FabricJS 禁用 Rectangle 的居中旋转。矩形是 FabricJS 提供的各种形状之一。为了创建一个矩形,我们必须创建一个 Fabric.Rect 类的实例并将其添加到画布中。默认情况下,FabricJS 中的所有对象都使用其中心作为旋转点。但是,我们可以使用 centeredRotation 属性来更改此行为。
语法
new fabric.Rect({ centeredRotation: Boolean }: Object)
参数
选项(可选) - 此参数是一个提供额外自定义的对象到我们的矩形。使用此参数,可以更改与 centeredRotation 属性相关的对象的颜色、光标、描边宽度等属性。
centeredRotation - 该属性接受一个布尔值,允许我们可以通过控件来控制对象旋转时是否使用中心点作为其变换原点。它的默认值为True。
选项键
示例1
FabricJS中矩形旋转的默认行为
让我们看一个描述矩形对象默认行为的代码示例。由于 centeredRotation 属性默认设置为 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>Default behaviour of rotation of Rectangle in FabricJS</h2> <p>Click on the rectangle and rotate it. You will notice that the object rotates around its center, which is 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 rectangle object var rect = new fabric.Rect({ left: 125, top: 90, width: 170, height: 70, fill: "#cf1020", borderColor: "black", borderScaleFactor: 3, }); // Add it to the canvas canvas.add(rect); </script> </body> </html>
示例 2
传递值为“false”的 centeredRotation 键
现在我们已经看到了默认行为,让我们看一下代码示例,以了解为 centeredRotation 属性分配 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 centeredRotation key with the value as “false”</h2> <p>Click on the rectangle and rotate it to see the changed center of rotation</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 rectangle object var rect = new fabric.Rect({ left: 125, top: 90, width: 170, height: 70, fill: "#cf1020", borderColor: "black", borderScaleFactor: 3, centeredRotation: false, }); // Add it to the canvas canvas.add(rect); </script> </body> </html>
以上就是如何使用FabricJS禁用矩形的居中旋转?的详细内容,更多请关注编程之家其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。