如何使用 FabricJS 禁用三角形的中心旋转?
在本教程中,我们将学习如何使用 FabricJS 禁用三角形的中心旋转。三角形是 FabricJS 提供的各种形状之一。为了创建三角形,我们必须创建一个 fabric.Triangle 类的实例并将其添加到画布中。
默认情况下,FabricJS 中的所有对象都使用其中心作为旋转点。但是,我们可以使用 centeredRotation 属性更改此行为。
语法
new fabric.Triangle({ 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 Triangle in FabricJS</h2>
<p>Rotate the triangle to see the default behaviour of centeredRotation</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 triangle object
var triangle = new fabric.Triangle({
left: 105,
top: 60,
width: 100,
height: 70,
fill: "#deb887",
});
// Add it to the canvas
canvas.add(triangle);
</script>
</body>
</html>示例 2
将 centeredRotation 键的值传递为“false”
现在我们已经看到了默认行为,让我们看一个代码示例来了解当 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 as key with the value "false"</h2>
<p>Rotate the triangle and notice that now its center of rotation has changed. The triangle rotates around one of its corners instead of its center.</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 triangle object
var triangle = new fabric.Triangle({
left: 105,
top: 60,
width: 100,
height: 70,
fill: "#deb887",
centeredRotation: false,
});
// Add it to the canvas
canvas.add(triangle);
</script>
</body>
</html>
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP