我试着将两个画布放在彼此的顶部,同时让它们像这样居中,但我怀疑其中一个画布超出了框架。
CSS:
body { background-color: #000000; text-align: center; margin: 0px; padding: 0px; width:100%; height:100%; }
* { margin:0; padding:0; }
canvas { display:block; padding: 0; margin: auto; position: absolute; top: 0; bottom: 0; left: 0; right: 0; }
cnv1 { z-index: 2; }
cnv2 { z-index: 1; }HTML:
<canvas id="cnv1">U no do HTML5, fix.</canvas>
<canvas id="cnv2">U no do HTML5, fix.</canvas>Javascript:
var cnv = document.getElementById('cnv1')
var ctx = cnv.getContext('2d');
var cnv2 = document.getElementById('cnv2')
var ctx2 = cnv2.getContext('2d');如果我现在试着写像这样的东西
ctx.font = "30px Arial";
ctx.fillStyle = "red";
ctx.fillText('images loaded and ready to go', 180, 45);那就不会出现了。但是写信给ctx2就可以了。
发布于 2017-04-13 14:51:11
好吧,原来是我太累了。我只是忘了将#添加到css的cnv1和cnv2部分:
#cnv1 { z-index: 2; }
#cnv2 { z-index: 1; }它现在可以正常工作了。感谢大家抽出宝贵的时间。
发布于 2017-04-13 15:02:08
你想要这样的东西吗?后层和前层现在重叠。我要做的唯一一件事就是颠倒它们在html中出现的顺序。
我不知道为什么z-index不起作用/不被遵守。但颠倒它们的出现顺序效果很好。
var cnv = document.getElementById('cnv1')
var ctx = cnv.getContext('2d');
var cnv2 = document.getElementById('cnv2')
var ctx2 = cnv2.getContext('2d');
ctx.font = "30px Arial";
ctx.fillStyle = "red";
ctx.fillText('images loaded and ready to go', 25, 95);
ctx2.save();
//ctx2.globalAlpha = 0.5;
ctx2.clearRect(0,0,ctx2.width,ctx2.height);
ctx2.fillStyle="#eeeeee";
ctx2.fillRect(0,0,50,200);
ctx2.fillRect(50,50,50,150);
ctx2.fillRect(100,0,25,200);
ctx2.fillRect(150,50,50,150);
ctx2.fillStyle="#202020";
ctx2.fillText('Gotham City.', 20, 128);body {
background-color: #000000;
text-align: center;
margin: 0px;
padding: 0px;
width:100%;
height:100%; }
* {
margin:0;
padding:0;
}
canvas {
display:block;
padding: 0;
margin: auto;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color:transparent;
}
#cnv1 {
z-index: 2;
}
#cnv2 {
z-index: 1;
}<canvas id="cnv2">U no do HTML5, fix.</canvas>
<canvas id="cnv1">U no do HTML5, fix.</canvas>
https://stackoverflow.com/questions/43384755
复制相似问题