頁面要做一個截圖的功能,本地產生了一張圖片,然後轉成canvas,再從canvas變成png,但是canvas.toDataUrl()一直報錯:
Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.
程式碼如下:
var url = DOMURL.createObjectURL(svg);
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight*1.25;
cacheImg(url,function(){
ctx.drawImage(this , 0 , 0);
var image = canvas.toDataURL("image/png").replace("image/png", 'image/octet-stream');
DOMURL.revokeObjectURL(url);
});
function cacheImg(url , callback){
var img = new Image()
img.setAttribute('crossOrigin', 'Anonymous');
img.src = url;
img.onload = function() {
callback.call(img)
}
}
這裡有一片文章,裡面部分程式碼或許有幫助http://fex.baidu.com/blog/201...