Fabric.js 多边形hasRotatingPoint属性
画布多边形意味着多边形是可移动的,并且可以根据需要拉伸。此外,可以自定义多边形的初始描边颜色,形状,填充颜色或描边宽度。
为了设置画布多边形的旋转点,使用了名为FabricJS的JavaScript库。在导入库之后,我们将在body标签中创建一个包含多边形的画布块。然后,我们将初始化由FabricJS提供的Canvas和多边形的实例,并使用hasRotatingPoint属性设置画布多边形的旋转点,并根据以下示例在Canvas上渲染多边形。
语法:
fabric.Polygon([
{ x: pixel, y: pixel },
{ x: pixel, y: pixel },
{ x: pixel, y: pixel},
{ x: pixel, y: pixel},
{ x: pixel, y: pixel }],
{ hasRotatingPoint: boolean }
);
参数: 此函数接受一个参数,如上所述,并在下面进行描述:
- hasRotatingPoint: 它指定是否显示旋转点。
注意: 必须使用尺寸像素创建多边形。
以下示例演示了JavaScript中的Fabric.JS多边形 hasRotatingPoint 属性:
示例1: 在此示例中,将hasRotatingPoint设置为false,因此无法旋转多边形的画布。
<!DOCTYPE html>
<html>
<head>
<!-- Loading the FabricJS library -->
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js">
</script>
</head>
<body>
<div style="text-align: center;width: 600px;">
<h1 style="color: green;">
GeeksforGeeks
</h1>
<b>
Fabric.js | Polygon hasRotatingPoint Property
</b>
</div>
<canvas id="canvas"
width="600"
height="200"
style="border:1px solid #000000;">
</canvas>
<script>
// Initiate a Canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate a polygon instance
var polygon = new fabric.Polygon([
{ x: 295, y: 10 },
{ x: 235, y: 198 },
{ x: 385, y: 78},
{ x: 205, y: 78},
{ x: 355, y: 198 }], {
hasRotatingPoint: false
});
// Render the polygon in canvas
canvas.add(polygon);
</script>
</body>
</html>
输出:

示例2: 在这个示例中,将 hasRotatingPoint 设置为true,以便可以旋转多边形的画布。
<!DOCTYPE html>
<html>
<head>
<!-- Loading the FabricJS library -->
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.6.2/fabric.min.js">
</script>
</head>
<body>
<div style="text-align: center;width: 600px;">
<h1 style="color: green;">
GeeksforGeeks
</h1>
<b>
Fabric.js | Polygon hasRotatingPoint Property
</b>
</div>
<canvas id="canvas"
width="600"
height="200"
style="border:1px solid #000000;">
</canvas>
<script>
// Initiate a Canvas instance
var canvas = new fabric.Canvas("canvas");
// Initiate a polygon instance
var polygon = new fabric.Polygon([
{ x: 295, y: 10 },
{ x: 235, y: 198 },
{ x: 385, y: 78},
{ x: 205, y: 78},
{ x: 355, y: 198 }], {
hasRotatingPoint: true
});
// Render the polygon in canvas
canvas.add(polygon);
</script>
</body>
</html>
输出:

参考: http://fabricjs.com/docs/fabric.Polygon.html#hasRotatingPoint
极客教程