Download Canvas Image Png Chome/Safari

Posted by user2639176 on Stack Overflow See other posts from Stack Overflow or by user2639176
Published on 2013-10-29T03:50:59Z Indexed on 2013/10/29 3:53 UTC
Read the original article Hit count: 103

Filed under:
|

Works in Firefox, and won't work in Safari, or Chrome.

 function loadimage()
    {
    var canvas = document.getElementById("canvas");

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
       xmlhttp=new XMLHttpRequest();
       xmlhttp2=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
       xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
    }
       xmlhttp.onreadystatechange=function()
    {

    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    rasterizeHTML.drawHTML(xmlhttp.responseText, canvas);
    var t=setTimeout(function(){copy()},3000)
    }
  }
  xmlhttp.open("GET","/sm/<?=$sm[0];?>",true);
  xmlhttp.send();
}

function copy()
{
var canvas = document.getElementById("canvas");
var img    = canvas.toDataURL("image/png");

document.getElementById('dl').href = img;
document.getElementById('dl').innerHTML = "Download";
}

Now I didn't write this, so I don't know too much javascript. But the script works in Firefox. In Chrome, getting: Uncaught Security Error: An attempt was made to break through the security policy of the user-agent. For toDataURL("image/png")

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about canvas