搜索

首页  >  问答  >  正文

javascript - canvas使用toDataUrl()出错,img设置img.crossOrigin = "*"也没用。

页面要做一个截图的功能,本地生成了一张图片,然后转成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)
            }
        }
过去多啦不再A梦过去多啦不再A梦2702 天前832

全部回复(1)我来回复

  • 世界只因有你

    世界只因有你2017-07-05 10:59:31

    这有一片文章,里面部分代码或许有帮助http://fex.baidu.com/blog/201...

    回复
    0
  • 取消回复