FabricJS – 删除转换为 HTMLCanvasElement 的多边形的对象变换?
我们可以通过创建 fabric.Polygon 的实例来创建多边形对象。多边形对象的特征可以是任何由一组连接的直线段组成的封闭形状。由于它是 FabricJS 的基本元素之一,我们还可以通过应用角度、不透明度等属性轻松自定义它。
为了将多边形对象转换为 HTMLCanvasElement,我们使用 toCanvasElement 方法。它返回 HTMLCanvasElement 类型的 DOM 元素,该接口从 HTMLElement 接口继承其属性和方法。我们使用 withoutTransform 属性来删除转换为 HTMLCanvasElement 的多边形的对象变换。
语法
toCanvasElement({ withoutTransform: Boolean }: Object):
HTMLCanvasElement
参数
options(可选) − 此参数接受 Object,它为我们的 HTMLCanvasElement 提供额外的自定义。使用此参数高度、左裁剪偏移和许多其他属性可以更改与 HTMLCanvasElement 相关的属性,其中 withoutTransform 是一个属性。
选项键
withoutTransform − 此属性接受 Boolean 值,该值确定是否要删除当前对象变换。此属性是可选的。
示例 1:使用 withoutTransform 属性并向其传递"false"值
让我们看一个代码示例,以查看当 toCanvasElement 方法与 withoutTransform 属性一起使用时记录的输出。在向其传递"false"值时,withoutTransform 属性将保留其对象变换。因此,在这种情况下,scaleX、scaleY 和 flipX 对象变换将出现在其输出图像中。我们已将 toDataURL 方法与 toCanvasElement 方法同步使用,以显示输出图像的实际外观。
<!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 withoutTransform property and passing it a ‘false’ value</h2>
<p>
You can open console from dev tools, copy the url string and paste it in a new tab to see that the polygon object's image contains object transformation
</p>
<canvas id="canvas"></canvas>
<script>
// 启动一个canvas实例
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// 初始化多边形对象
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,
flipX: true,
}
);
// 将其添加到画布
canvas.add(polygon);
// 使用 toCanvasElement 方法
var withoutTransform = polygon.toCanvasElement({
withoutTransform: false,
});
// 使用 toDataURL 方法
console.log(withoutTransform.toDataURL());
</script>
</body>
</html>
示例 2:使用 withoutTransform 属性并向其传递"true"值
让我们看一个代码示例,以查看当 toCanvasElement 方法与 withoutTransform 属性一起使用时记录的输出。在向其传递"true"值时,withoutTransform 属性将不会保留其对象变换。因此,在这种情况下,scaleX、scaleY 和 flipX 对象变换将在其输出图像中被删除。
<!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 withoutTransform property and passing it a ‘true’ value</h2>
<p>
You can open console from dev tools, copy the url string and paste it in a new tab to see that the polygon object's image does not contain object transformation
</p>
<canvas id="canvas"></canvas>
<script>
// 启动一个canvas实例
var canvas = new fabric.Canvas("canvas");
canvas.setWidth(document.body.scrollWidth);
canvas.setHeight(250);
// 初始化多边形对象
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,
flipX: true,
}
);
// 将其添加到画布
canvas.add(polygon);
// 使用 toCanvasElement 方法
var withoutTransform = polygon.toCanvasElement({
withoutTransform: true,
});
// 使用 toDataURL 方法
console.log(withoutTransform.toDataURL());
</script>
</body>
</html>
结论
在本教程中,我们使用了两个简单示例来演示如何使用 FabricJS 移除转换为 HTMLCanvasElement 的多边形的对象变换。
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

