I have two canvases which are on top of each other using z-index.
But I am getting a weird problem with them: when I draw to the canvas that is above
the lower canvas, what ever was drawn on the lower canvas gets erased.
This is how i have done it:
CSS:
.c {
position: absolute;
left: 0;
top: 0;
width: 100 % ;
height: 500px;
margin: 0 auto;
border: 1px solid red;
}
JavaScript:
window.canvas = new Object();
for (var i = 1; i < 3; i++) {
document.getElementById("G").innerHTML += '<canvas id="layer' + i + '" class="c" style="z-index:' + i + ';" oncontextmenu="return false;"></canvas>';
temp = document.getElementById('layer' + i);
objname = 'canvas' + i;
canvas[objname] = temp;
canvas[objname].ctx = temp.getContext("2d");
}
function draw() {
canvas.canvas1.ctx.clearRect(0, 0, settings.width, settings.height);
canvas.canvas2.ctx.clearRect(0, 0, settings.width, settings.height);
abposx = 50;
abposy = 50;
//doesn't draw
canvas.canvas1.ctx.drawImage(gfx['ground'][0], abposx, abposy);
//draws
canvas.canvas2.ctx.drawImage(gfx['building'][0], abposx + 120, abposy + 120);
}
In the above example, I only see canvas2
drawn, if I remove that canvas all together I then see canvas1
drawn. Both images also show if I draw both images to the same
canvas.
BUT if I draw one to canvas1
and the other to canvas2
I ONLY see what is drawn on canvas2
, and canvas1
appears to be erased.
Why might this be ? I don't know if this is replicated for any one else trying this but its getting annoying! I cannot solve the issue! Ideas/suggestions/fixes are much appreciated.
JSfiddle with the issue: http://jsfiddle.net/xJZrQ/18/