Get security error when saving canvas object into an image

后端 未结 2 1392
眼角桃花
眼角桃花 2020-12-19 22:57

Well not exactly. If I just draw (ex lines,rect...) and try to export the canvas as an image. It works fine. If I however use the canvas.drawImage(...) function

相关标签:
2条回答
  • 2020-12-19 23:30

    Are you waiting for the image to fully load before calling canvas.drawImage?

    var img = new Image();
    img.onload = function(){
        canvas.drawImage(img,0,0);
        //do whatever else here
    };
    img.src = 'foo.jpg';
    
    0 讨论(0)
  • 2020-12-19 23:56

    The behavior you describe is per the specification. Excerpted:

    All canvas elements must start with their origin-clean set to true. The flag must be set to false if any of the following actions occur:

    • The element's 2D context's drawImage() method is called with an HTMLImageElement or an HTMLVideoElement whose origin is not the same as that of the Document object that owns the canvas element.

    [...]

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

    The only way to circumvent this is to use a server-side technology to fetch the remote image for you and re-serve it from your same domain.

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