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