javascript - How can I put multiple canvas elements in to one canvas element? -
i have multiple canvas elements created javascript. question is: "how can put multiple canvas elements 1 canvas element?".
here go;
- it grab each canvas , make them png's
- access buffer canvas , write png's on top of each other inside it
- write buffer canvas png , send img-tag
just give #buffer
canvas element display: none;
, happen invisible (check here; http://jsfiddle.net/allendar/uqxcy/2/).
html
<body onload="generatepng()"> <canvas id="c1"></canvas> <canvas id="c2"></canvas> <canvas id="c3"></canvas> <canvas id="buffer"></canvas> <div id="output">empty</div> </body>
css
canvas[id^=c], div[id=output] { border: 1px solid red; } canvas[id=buffer] { border: 1px dotted green; } #output { padding: 15px; } #output img { border: 1px dotted blue; }
javascript
function generatepng() { var b_canvas1 = document.getelementbyid("c1"); var b_context1 = b_canvas1.getcontext("2d"); b_context1.fillrect(10, 50, 50, 50); var b_canvas2 = document.getelementbyid("c2"); var b_context2 = b_canvas2.getcontext("2d"); b_context2.fillrect(80, 50, 50, 50); var b_canvas3 = document.getelementbyid("c3"); var b_context3 = b_canvas3.getcontext("2d"); b_context3.fillrect(150, 50, 50, 50); var img1 = new image(); img1.src = b_canvas1.todataurl("image/png"); var img2 = new image(); img2.src = b_canvas2.todataurl("image/png"); var img3 = new image(); img3.src = b_canvas3.todataurl("image/png"); var buffer = document.getelementbyid("buffer"); var buffer_context = buffer.getcontext("2d"); buffer_context.drawimage(img1, 0, 0); buffer_context.drawimage(img2, 0, 0); buffer_context.drawimage(img3, 0, 0); var buffer_img = new image(); buffer_img.src = buffer.todataurl("image/png"); var output = document.getelementbyid('output'); output.innerhtml = '<img src="' + buffer_img.src + '" alt="canvas image" />'; }
Comments
Post a Comment