如何在 HTML5 中创建透明画布?
htmlweb developmentfront end technology更新于 2025/8/18 7:40:17
globalAlpha 属性返回绘制的透明度值。该属性值为 1.0 表示不透明,值为 0.0 表示完全透明。因此,通过将 globalAlpha 属性设置为 0.0,我们可以获得透明画布。
示例
您可以尝试运行以下代码来创建透明画布。 −
<!DOCTYPE html>
<html>
<body>
<canvas id = "idCanvas" width = "400" height = "200" style = "border:2px solid #000000;">
Your browser does not support the HTML5 canvas tag.</canvas>
<script>
var c = document.getElementById("idCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = "green";
ctx.fillRect(20, 20, 75, 50);
ctx.globalAlpha = 0.0;
ctx.fillStyle = "yellow";
ctx.fillRect(50, 50, 75, 50);
ctx.fillStyle = "red";
ctx.fillRect(80, 80, 75, 50);
</script>
</body>
</html>
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

