HTML 中的 toDataURL 抛出未捕获的安全异常
为了解决未捕获的安全异常,您需要添加 crossorigin 属性
<img id = "myid" crossorigin = "anonymous" src="https://tutorialspoint.com/images/seaborn-4.jpg?v=2"> function getBase64() { var myImg = document.getElementById("myid"); var c = document.createElement("canvas"); c.width = myImg.width; c.height = myImg.width; var context = c.getContext("2d"); context.drawImage(img, 0, 0); var dataURL = c.toDataURL("image/png"); alert(dataURL.replace(/^data:image\/(png|jpg);base64,/, "")); } getBase64();
广告