16

I have 3 layers of canvas - 1 is matrix, 2 & 3 is graphics, how to preserve them in one image?

<div style="position: relative;">
 <canvas id="matix" width="100" height="100" 
   style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas>
 <canvas id="layer1" width="100" height="100" 
   style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas>
 <canvas id="layer2" width="100" height="100" 
   style="position: absolute; left: 0; top: 0; z-index: 1;"></canvas>
</div>
ErgallM
  • 237
  • 3
  • 9
  • Do you want them combined into 1 image on layered on top of each other or side by side? – Castrohenge Sep 20 '10 at 13:05
  • 1
    combined into 1 image on layered on top of each other – ErgallM Sep 20 '10 at 14:04
  • How did you implemented layers? Are they separated canvas elements? First layer is matrix of what (do you mean pixel data?)? Is it something like this? http://stackoverflow.com/questions/3008635/html5-canvas-element-multiple-layers – pepkin88 Sep 20 '10 at 14:20

1 Answers1

23

You can draw one canvas into another with ctx.drawImage(other_canvas,0,0)

If you do that in the right order, you will have all the canvas contents correctly layered in one of them.

If you want to save the image, you can call canvas.toDataURL() to get the contents as a base64 encoded PNG file.

andrewmu
  • 14,276
  • 4
  • 39
  • 37