0

I have two buttons, one button creates a canvas with an image. The other buttons converts the canvas to png data (At least, thats what I want to achieve).

The problem is that JavaScript somehow doesn't convert the canvas to image data.

Check jsfiddle here: http://jsfiddle.net/julekker/tjYzw/1/

I've tried to use window.location = finalcanvas.toDataURL("image/png"); and

var img = finalcanvas.toDataURL("image/png");
document.write('<img src="'+img+'"/>');

But they both didn't work.

How can you convert an canvas image which makes use of the drawImage method to png data?

Julian
  • 4,396
  • 5
  • 39
  • 51
  • In the jsfiddle code, you seem to draw an external image to the canvas. That will prevent getting access to the pixel data and toDataURL if it is a cross-domain access for security reasons. – Stefan Haustein Apr 28 '13 at 12:56
  • So you suggest that it's impossible? – Julian Apr 28 '13 at 13:08
  • No, just a little tricky: http://stackoverflow.com/questions/7129178/browser-canvas-cors-support-for-cross-domain-loaded-image-manipulation – Stefan Haustein Apr 28 '13 at 13:36

1 Answers1

1

In the jsfiddle code, you seem to draw an external image to the canvas. That will prevent getting access to the pixel data and toDataURL if it is a cross-domain access for security reasons.

This issue has been covered here many times; see Browser Canvas CORS Support for Cross Domain Loaded Image Manipulation for a discussion about using CORS to address this problem and browser support. Another option may be loading the images via a local proxy.

Community
  • 1
  • 1
Stefan Haustein
  • 18,427
  • 3
  • 36
  • 51