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

后端 未结 10 1502
天涯浪人
天涯浪人 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:53

    In the specs it says:

    Whenever the toDataURL() method of a canvas element whose origin-clean flag is set to false is called, the method must raise a SECURITY_ERR exception.

    If the image is coming from another server I don't think you can use toDataURL()

    0 讨论(0)
  • 2020-11-22 13:58

    If you are simply drawing some images on a canvas, make sure you are loading the images from the same domain.

    www.example.com is different to example.com

    So make sure your images and the url you have in your address bar are the same, www or not.

    0 讨论(0)
  • 2020-11-22 14:00

    Finally i found the solution. Just need add the crossOrigin as third param in fromURL func

    fabric.Image.fromURL(imageUrl, function (image) {
                //your logic
        }, { crossOrigin: "Anonymous" });
    
    0 讨论(0)
  • 2020-11-22 14:00

    I'm using fabric.js and could resolve this by using toDatalessJSON instead of toDataURL:

    canvas.toDatalessJSON({ format: 'jpeg' }).objects[0].src
    

    Edit: Nevermind. This results in just the background image being exported to JPG, without the drawing on top so it was not entirely useful after all.

    0 讨论(0)
提交回复
热议问题