5

I want to join multiple canvases to make a single image. So is there any method to covert more than one canvases to toDataURL to make a single image?

3
  • 1
    You can copy all canvases' drawings into a larger temporary canvas and then call toDataURL() of the later. Commented Jun 7, 2012 at 9:36
  • thnx @Ammar can you provide me the reference plz Commented Jun 7, 2012 at 9:51
  • 1
    @Muhammad Usmar: Answer provided by bennedich below is the same approach I was talking about. Use drawImage() function of canvas. For any further difficulties let us know. Commented Jun 7, 2012 at 10:00

3 Answers 3

5

create a function that takes multiple arguments (canvas elements), puts them on one blank canvas, then returns the dataurl for the newly made canvas.

var getImadeData = function () { var i = arguments.length, tempCanvas = document.createElement("canvas"), ctx = tempCanvas.getContext("2d"); while (i--) { ctx.drawImage(arguments[i], 0, 0); }; return tempCanvas.toDataURL(); }; 

Now just feed the function multiple canvas elements you want to put into one dataurl like so.

var newData = getImageData(canvas1, canvas2, canvas3); 

In this example, the last canvas is placed on the blank canvas first so make sure to order your canvas elements appropriately.

Sign up to request clarification or add additional context in comments.

2 Comments

please provide some explanations along with the code. That way future visitors will know what they are looking and you might receive more upvotes for your contributions
I added some explanation, but it honestly feels a bit superfluous. Was there something in particular that you felt needed more detail?
4

Yes, the drawImage method on the canvas 2d rendering context accepts canvas elements as image elements. So all you have to do is:

  • Create a new canvas
  • Get its context
  • Draw all other canvases to it with drawImage
  • Extract the final image from this new canvas

Comments

3

try this example hope it will help see here

 //html block <canvas id="myCanvas" width="200" height="100" style="border:1px solid #c3c3c3;"> </canvas> <canvas id="myCanvas1" width="200" height="100" style="border:1px solid #c3c3c3;"> Your browser does not support the canvas element. </canvas> <canvas id="Canvasimage" width="500" height="100" style="border:1px solid #c3c3c3;"> Your browser does not support the canvas element. </canvas> <img id="finalimage" width="500" height="100" style="border:1px solid #c3c3c3;"/> //script block function loadImages(sources, callback) { var images = {}; var loadedImages = 0; var numImages = 0; // get num of sources for (var src in sources) { numImages++; } for (var src in sources) { images[src] = new Image(); images[src].onload = function () { if (++loadedImages >= numImages) { callback(images); } }; images[src].src = sources[src]; } } window.onload = function (images) { //Canvas first here var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); ctx.fillStyle = "#FF0000"; ctx.fillRect(0, 0, 200, 100); //Canvas second here var c1 = document.getElementById("myCanvas1"); var ctx1 = c1.getContext("2d"); ctx1.fillStyle = "#00FF00"; ctx1.fillRect(0, 0, 200, 100); //Canvas final here. var canvas = document.getElementById("Canvasimage"); var context = canvas.getContext("2d"); var sources = { darthVader: c.toDataURL("image/png"), yoda: c1.toDataURL("image/png") }; loadImages(sources, function (images) { context.drawImage(images.darthVader, 100, 30, 200, 137); context.drawImage(images.yoda, 350, 55, 93, 104); //finalimage here which has two canvas data var finalimage = document.getElementById("finalimage"); finalimage.src = canvas.toDataURL("image/png"); }); }; 

1 Comment

no. there is one canvas and multiple images. I need multiple canvases

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.