Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save many canvas element as image

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>
like image 371
ErgallM Avatar asked Sep 20 '10 10:09

ErgallM


People also ask

How do you save elements in canvas?

You can save a canvas to an image file by using the method canvas. toDataURL() , that returns the data URI for the canvas' image data.

Can you have multiple canvases HTML?

A webpage may contain multiple canvas elements. Each canvas may have an id using which you may target a specific canvas through JavaScript. Each canvas element has a 2D Context. This again has objects, properties, and methods.


1 Answers

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.

like image 186
andrewmu Avatar answered Oct 09 '22 18:10

andrewmu