Tainted canvases may not be exported

后端 未结 10 1352
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 05:21

I want to save my canvas to a img. I have this function:

function save() {
    document.getElementById(\"canvasimg\").style.border = \"2px solid\";
    var d         


        
相关标签:
10条回答
  • 2020-11-22 05:52

    I also solved this error by adding useCORS : true, in my code like -

    html2canvas($("#chart-section")[0], {
            useCORS : true,
            allowTaint : true,
            scale : 0.98,
            dpi : 500,
            width: 1400, height: 900
        }).then();
    
    0 讨论(0)
  • 2020-11-22 05:55

    Seems like you are using an image from a URL that has not set correct Access-Control-Allow-Origin header and hence the issue.. You can fetch that image from your server and get it from your server to avoid CORS issues..

    0 讨论(0)
  • 2020-11-22 05:59

    For security reasons, your local drive is declared to be "other-domain" and will taint the canvas.

    (That's because your most sensitive info is likely on your local drive!).

    While testing try these workarounds:

    • Put all page related files (.html, .jpg, .js, .css, etc) on your desktop (not in sub-folders).

    • Post your images to a site that supports cross-domain sharing (like dropbox.com). Be sure you put your images in dropbox's public folder and also set the cross origin flag when downloading the image (var img=new Image(); img.crossOrigin="anonymous" ...)

    • Install a webserver on your development computer (IIS and PHP web servers both have free editions that work nicely on a local computer).

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

    I resolved the problem using useCORS: true option

     html2canvas(document.getElementsByClassName("droppable-area")[0], { useCORS:true}).then(function (canvas){
            var imgBase64 = canvas.toDataURL();
            // console.log("imgBase64:", imgBase64);
            var imgURL = "data:image/" + imgBase64;
            var triggerDownload = $("<a>").attr("href", imgURL).attr("download", "layout_"+new Date().getTime()+".jpeg").appendTo("body");
            triggerDownload[0].click();
            triggerDownload.remove();
        });
    
    0 讨论(0)
  • 2020-11-22 06:00

    Check out [CORS enabled image][1] from MDN. Basically you must have a server hosting images with the appropriate Access-Control-Allow-Origin header.

    <IfModule mod_setenvif.c>
        <IfModule mod_headers.c>
            <FilesMatch "\.(cur|gif|ico|jpe?g|png|svgz?|webp)$">
                SetEnvIf Origin ":" IS_CORS
                Header set Access-Control-Allow-Origin "*" env=IS_CORS
            </FilesMatch>
        </IfModule>
    </IfModule>

    You will be able to save those images to DOM Storage as if they were served from your domain otherwise you will run into security issue.

    var img = new Image,
        canvas = document.createElement("canvas"),
        ctx = canvas.getContext("2d"),
        src = "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"; // insert image url here
    
    img.crossOrigin = "Anonymous";
    
    img.onload = function() {
        canvas.width = img.width;
        canvas.height = img.height;
        ctx.drawImage( img, 0, 0 );
        localStorage.setItem( "savedImageData", canvas.toDataURL("image/png") );
    }
    img.src = src;
    // make sure the load event fires for cached images too
    if ( img.complete || img.complete === undefined ) {
        img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
        img.src = src;
    }

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

    If you're using ctx.drawImage() function, you can do the following:

    var img = loadImage('../yourimage.png', callback);
    
    function loadImage(src, callback) {
        var img = new Image();
    
        img.onload = callback;
        img.setAttribute('crossorigin', 'anonymous'); // works for me
    
        img.src = src;
    
        return img;
    }
    

    And in your callback you can now use ctx.drawImage and export it using toDataURL

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