Why does canvas.toDataURL() throw a security exception?

后端 未结 10 1500
天涯浪人
天涯浪人 2020-11-22 12:56

Did I not get enough sleep or what? This following code

var frame=document.getElementById(\"viewer\");
frame.width=100;
frame.height=100;

var ctx=frame.getC         


        
10条回答
  •  北海茫月
    2020-11-22 13:43

    Setting cross origin attribute on the image objects worked for me (i was using fabricjs)

        var c = document.createElement("img");
        c.onload=function(){
            // add the image to canvas or whatnot
            c=c.onload=null
        };
        c.setAttribute('crossOrigin','anonymous');
        c.src='http://google.com/cat.png';
    

    For those using fabricjs, here's how to patch Image.fromUrl

    // patch fabric for cross domain image jazz
    fabric.Image.fromURL=function(d,f,e){
        var c=fabric.document.createElement("img");
        c.onload=function(){
            if(f){f(new fabric.Image(c,e))}
            c=c.onload=null
        };
        c.setAttribute('crossOrigin','anonymous');
        c.src=d;
    };
    

提交回复
热议问题