如何使用FabricJS禁用矩形的居中旋转?
在本教程中,我们将学习如何使用 FabricJS 禁用 Rectangle 的居中旋转。矩形是 FabricJS 提供的各种形状之一。为了创建一个矩形,我们必须创建一个 Fabric.Rect 类的实例并将其添加到画布中。默认情况下,FabricJS 中的所有对象都使用其中心作为旋转点。但是,我们可以使用 centeredRotation 属性来更改此行为。
语法
new fabric.Rect({ centeredRotation: Boolean }: Object)
参数
-
选项(可选) – 此参数是一个提供额外自定义的对象到我们的矩形。使用此参数,可以更改与 centeredRotation 属性相关的对象的颜色、光标、描边宽度等属性。
-
centeredRotation – 该属性接受一个布尔值,允许我们可以通过控件来控制对象旋转时是否使用中心点作为其变换原点。它的默认值为True。
< /ul>
选项键
示例1
FabricJS中矩形旋转的默认行为
< p>让我们看一个描述矩形对象默认行为的代码示例。由于 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禁用矩形的居中旋转?的详细内容,更多请关注双恒网络其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。



