gpt4 book ai didi

javascript - 叠加的 HTML Canvas 元素

转载 作者:行者123 更新时间:2023-12-03 04:34:29 28 4
gpt4 key购买 nike

我有一个 WWWeb 页面,其中堆叠了 <canvas>元素。目标是在下部 Canvas 中绘制持久的内容,并在上部 Canvas 中重复绘制、清除和重绘。根据 w3.org 和 this StackOverflow article ( How do I make a transparent canvas in html5? ), Canvas 元素默认是透明的。我看到的是,它们不仅不透明,而且通过 z-index 覆盖。不管用。这是页面代码的“普通”版本(完整并用于生成图像):

    <html>
<head>
<title>Canvas Stack</title>
<script>
function doStuff() {
drawStuff(document.getElementById("lowerCanvas"), 0, 1);
drawStuff(document.getElementById("upperCanvas"), 1, 0);
}
function drawStuff(cvs, p0X, p1X) {
var ctx = cvs.getContext("2d");
ctx.clearRect(0, 0, cvs.width, cvs.height);
ctx.strokeStyle = cvs.style.color;
ctx.beginPath();
ctx.moveTo(p0X * cvs.width, 0);
ctx.lineTo(p1X * cvs.width, cvs.height);
ctx.stroke();
}
</script>
</head>
<body onload="doStuff();" style="border:solid;">
<canvas id="lowerCanvas" style="color:red;height:200px;left:0;position:inherit;top:0;width:300px;z-index:0;" />
<canvas id="upperCanvas" style="color:blue;height:200px;left:0;position:inherit;top:0;width:300px;z-index:1;" />
</body>
</html>

结果如下:

enter image description here

如果我交换 Canvas 元素的顺序

    <canvas id="upperCanvas" style="color:blue;height:200px;left:0;position:inherit;top:0;width:300px;z-index:1;" />
<canvas id="lowerCanvas" style="color:red;height:200px;left:0;position:inherit;top:0;width:300px;z-index:0;" />

结果是

enter image description here

看起来好像 z-index已被忽略,并且声明的顺序具有首要性。

我想要的是

enter image description here

(合成图像)

Windows 10 下的 Chrome、Edge、Firefox 和 Internet Explorer 中的行为是相同的。这让我确信我只是错过了一些细节,而不是遇到了异常。然而,我的额头因撞墙而变得扁平,非常感谢这里的一些指导。

谢谢大家!

一些注释(也许不相关;也许不相关):

  • style="position:absolute; ...(没有 lefttop 条目)似乎与 style="left:0;position:absolute;top:0; 做同样的事情...
  • 以类似的方式,style="position:inherit; ...如果 Canvas 要驻留在表的 <td> 中,则可能是必要的那<td>不是 <tr> 中的第一个.

最佳答案

    <html>
<head>
<title>Canvas Stack</title>
<script>
function doStuff() {
drawStuff(document.getElementById("lowerCanvas"), 0, 1);
drawStuff(document.getElementById("upperCanvas"), 1, 0);
}
function drawStuff(cvs, p0X, p1X) {
var ctx = cvs.getContext("2d");
ctx.clearRect(0, 0, cvs.width, cvs.height);
ctx.strokeStyle = cvs.style.color;
ctx.beginPath();
ctx.moveTo(p0X * cvs.width, 0);
ctx.lineTo(p1X * cvs.width, cvs.height);
ctx.stroke();
}
</script>
</head>
<body onload="doStuff();" style="border:solid;">
<div style="position:relative;min-height:200px">
<canvas id="lowerCanvas" style="color:red;height:200px;left:0;position:absolute;top:0;width:300px;z-index:0;"></canvas>
<canvas id="upperCanvas" style="color:blue;height:200px;left:0;position:absolute;top:0;width:300px;z-index:1;"></canvas>
</body>
</html>

您需要明确关闭 Canvas 元素。只需使用以下命令关闭它:

<canvas id="lowerCanvas" style="color:red;height:200px;left:0;position:inherit;top:0;width:300px;z-index:0;"></canvas>

而不是嵌入的关闭“/>”

我还将它们包含在 div 中并使用了position:absolute,以便它们实际上重叠...

关于javascript - 叠加的 HTML Canvas 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43355160/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com