I'd like to copy ALL contents of one canvas and transfer them to another all on the client-side. I would think that I would use the canvas.toDataURL()
and context.drawImage()
method to implement this but I am running into a few issues.
My solution would be to get Canvas.toDataURL()
and store this in an Image object in Javascript, and then use the context.drawImage()
method to place it back.
However, I believe the toDataURL
method returns a 64 bit encoded tag with "data:image/png;base64,"
prepended to it. This does not seem to be a valid tag, (I could always use some RegEx to remove this), but is that 64 bit encoded string AFTER the "data:image/png;base64,"
substring a valid image? Can I say image.src=iVBORw...ASASDAS
, and draw this back on the canvas?
I've looked at some related issues: Display canvas image from one canvas to another canvas using base64
But the solutions don't appear to be correct.
I managed to get it working, what I did appears correct:
(Above URL is an example)
This appears to work when I run it on the console in Chrome and FireBug
Actually you don't have to create an image at all.
drawImage()
will accept aCanvas
as well as anImage
object.Way faster than using an
ImageData
object orImage
element.Note that
sourceCanvas
can be a HTMLImageElement, HTMLVideoElement, or a HTMLCanvasElement. As mentioned by Dave in a comment below this answer, you cannot use a canvas drawing context as your source. If you have a canvas drawing context instead of the canvas element it was created from, there is a reference to the original canvas element on the context undercontext.canvas
.Here is a jsPerf to demonstrate why this is the only right way to clone a canvas: http://jsperf.com/copying-a-canvas-element
@robert-hurst has a cleaner approach, but this solution may be used in places when you actually want to have a copy of Data Url after copying. Examples: when you are building a website that uses lots of image/canvas operations.