FabricJS – 将多边形转换为HTMLCanvasElement后移除对象的变换?
我们可以通过创建一个fabric.Polygon实例来创建一个Polygon对象。多边形对象可以由任何由一组连接的直线段组成的封闭形状来表示。由于它是FabricJS的基本元素之一,我们也可以通过应用角度、不透明度等属性轻松自定义它。
为了将多边形对象转换为HTMLCanvasElement,我们使用toCanvasElement方法。它返回HTMLCanvasElement类型的DOM元素,这是一个从HTMLElement接口继承其属性和方法的接口。我们使用withoutTransform属性来移除转换为HTMLCanvasElement的多边形的对象变换。
语法
toCanvasElement({ withoutTransform: Boolean }: Object):
HTMLCanvasElement
参数
options (可选) − 此参数接受一个对象,该对象为我们的HTMLCanvasElement提供额外的自定义。使用此参数,可以更改与HTMLCanvasElement相关的许多属性,例如高度、左侧裁剪偏移量,其中withoutTransform是一个属性。
选项键
withoutTransform − 此属性接受一个布尔值,用于确定是否移除当前对象的变换。此属性是可选的。
示例1:使用withoutTransform属性并传递“false”值
让我们来看一个代码示例,看看使用withoutTransform属性以及toCanvasElement方法时的日志输出。传递“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>
// 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,
flipX: true,
}
);
// Adding it to the canvas
canvas.add(polygon);
// Using toCanvasElement method
var withoutTransform = polygon.toCanvasElement({
withoutTransform: false,
});
// Using toDataURL method
console.log(withoutTransform.toDataURL());
</script>
</body>
</html>
示例2:使用withoutTransform属性并传递“true”值
让我们来看一个代码示例,看看使用withoutTransform属性以及toCanvasElement方法时的日志输出。传递“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>
// 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,
flipX: true,
}
);
// Adding it to the canvas
canvas.add(polygon);
// Using toCanvasElement method
var withoutTransform = polygon.toCanvasElement({
withoutTransform: true,
});
// Using toDataURL method
console.log(withoutTransform.toDataURL());
</script>
</body>
</html>
结论
在本教程中,我们使用两个简单的示例演示了如何使用FabricJS移除转换为HTMLCanvasElement的多边形的对象变换。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP