HTML Canvas image to Base64 problem

后端 未结 1 1109
野趣味
野趣味 2020-12-11 10:37

I\'m having a problem with retrieving the base64 version of an image displayed on a canvas. I\'ve looked at the other questions but none of the solutions including canvas2im

1条回答
  •  我在风中等你
    2020-12-11 10:49

    Your document.write() call occurs immediately upon page load, before your img is loaded and drawn and before your onclick handler can ever run. You need to wait to generate the data URI until after everything has finished happening to the canvas.

    Ignoring the onclick, here's something that writes out the data url after the image has loaded and been drawn to the canvas in a rotated fashion:

    
    
      Rotated Image Data URL
      
    
      
      
       
      
    
    

    You can see this demo in action here: http://phrogz.net/tmp/upside-down-image-url.html

    Note that the image you load must be on the same domain as your script, or else you will not be allowed to create the data URL.

    0 讨论(0)
提交回复
热议问题