
在本教程中,我们将学习如何使用 FabricJS 禁用 Rectangle 的居中旋转。矩形是 FabricJS 提供的各种形状之一。为了创建一个矩形,我们必须创建一个 Fabric.Rect 类的实例并将其添加到画布中。默认情况下,FabricJS 中的所有对象都使用其中心作为旋转点。但是,我们可以使用 centeredRotation 属性来更改此行为。
new fabric.Rect({ centeredRotation: Boolean }: Object)选项(可选) - 此参数是一个提供额外自定义的对象到我们的矩形。使用此参数,可以更改与 centeredRotation 属性相关的对象的颜色、光标、描边宽度等属性。
centeredRotation - 该属性接受一个布尔值,允许我们可以通过控件来控制对象旋转时是否使用中心点作为其变换原点。它的默认值为True。
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>传递值为“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禁用矩形的居中旋转?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号