FabricJS - 如何获取转换为 HTMLCanvasElement 的 Polygon 对象的尺寸?
我们可以通过创建 fabric.Polygon 的实例来创建一个 Polygon 对象。多边形对象可以由任何由一组连接的直线段组成的闭合形状来表示。由于它是 FabricJS 的基本元素之一,因此我们还可以通过应用角度、不透明度等属性轻松自定义它。
为了将多边形对象转换为 HTMLCanvasElement,我们使用 toCanvasElement 方法。它返回类型为 HTMLCanvasElement 的 DOM 元素,该接口继承了 HTMLElement 接口的属性和方法。我们使用 HTMLCanvasElement 从其父级 HTMLElement 继承的 width 和 height 属性来查找转换为 HTMLCanvasElement 的 Polygon 对象的尺寸。
语法
HTMLCanvasElement.height HTMLCanvasElement.width
示例 1:使用 toCanvasElement 方法和 Width 属性
让我们看一个代码示例,以了解使用 toCanvasElement 方法以及 width 属性时 Polygon 对象的外观。width 是一个正整数,表示画布一行上的像素数。我们可以从开发者工具中打开控制台,查看 width 值为 200。
<!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>Using the toCanvasElement method and using the width property</h2>
<p>
You can open console from dev tools to see that the width value is being displayed as 200
</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 polygon object
var polygon = new fabric.Polygon(
[
{ x: 600, y: 310 },
{ x: 650, y: 450 },
{ x: 600, y: 480 },
{ x: 550, y: 480 },
{ x: 450, y: 460 },
{ x: 300, y: 210 },
],
{
fill: "#778899",
stroke: "blue",
strokeWidth: 5,
top: 50,
left: 100,
scaleX: 0.5,
scaleY: 0.5,
}
);
// Adding it to the canvas
canvas.add(polygon);
// Using toCanvasElement method
var polygonCanvas = polygon.toCanvasElement({
width: 200,
});
// Using the width property
console.log("The width is as follows:", polygonCanvas.width);
</script>
</body>
</html>
示例 2:使用 toCanvasElement 方法和 Height 属性
让我们看一个代码示例,以查看使用 toCanvasElement 方法以及 height 属性时记录的输出。height 是一个正整数,表示画布一列上的像素数。在这种情况下,我们可以从开发者工具中打开控制台,查看 height 值为 200。
<!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>Using the toCanvasElement method and using the height property</h2>
<p>
You can open console from dev tools to see that the height value is being displayed as 200
</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 polygon object
var polygon = new fabric.Polygon(
[
{ x: 600, y: 310 },
{ x: 650, y: 450 },
{ x: 600, y: 480 },
{ x: 550, y: 480 },
{ x: 450, y: 460 },
{ x: 300, y: 210 },
],
{
fill: "#778899",
stroke: "blue",
strokeWidth: 5,
top: 50,
left: 100,
scaleX: 0.5,
scaleY: 0.5,
}
);
// Adding it to the canvas
canvas.add(polygon);
// Using toCanvasElement method
var polygonCanvas = polygon.toCanvasElement({
height: 200,
});
// Using the height property
console.log("The height is as follows:", polygonCanvas.height);
</script>
</body>
</html>
结论
在本教程中,我们使用两个简单的示例演示了如何使用 FabricJS 查找转换为 HTMLCanvasElement 的 Polygon 对象的尺寸。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP