javascript - canvas uses toDataUrl() error, img setting img.crossOrigin = "*" is useless.
过去多啦不再A梦
过去多啦不再A梦 2017-07-05 10:57:31
0
1
813

The page has a screenshot function, and a picture is generated locally, then converted to canvas, and then converted from canvas to png, but canvas.toDataUrl() keeps reporting an error:

Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.

code show as below:

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梦

reply all(1)
世界只因有你

Here is an article, some of the codes in it may be helpful http://fex.baidu.com/blog/201...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!