question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cross Domain images not rendering (No CORS or proxy)

See original GitHub issue

Hi,

Using the following code images are not rendering cross-domain.

html2canvas($(".canvas-surround"), {
        logging: true,
        width: 1920 * getScale(),
        height: 1080 * getScale(),
        useCORS: false,
        onrendered: function(canvas) {

            var bottomCtx = canvas.getContext("2d");
            var topCtx = canvas.getContext("2d");
            var bData = bottomCtx.getImageData(0, 0, canvas.width, canvas.height);
            var tData = topCtx.getImageData(0, 0, canvas.width, canvas.height);

            var merged = mergeData(bData, tData);

            bottomCtx.putImageData(merged, 0, 0);

            var newImg = canvas.toDataURL("image/png");
            console.log(newImg);
            var blob = b64toBlob(newImg, "image/png");
            console.log(blob);

            var blobUrl = URL.createObjectURL(blob);

            console.log(blobUrl);

            downloadURI(blobUrl);
        }
    });

Whilst trying to grab images from “http://www.thermmark.co.uk” from “http://www.playgroundmarkingsdirect.co.uk” the images would not render.

When I moved the code over to the “http://www.thermmark.co.uk” website the images rendered (I own both domains)

Any ideas?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:26 (3 by maintainers)

github_iconTop GitHub Comments

14reactions
gauravmishra24commented, Mar 17, 2017

Hi Rookev,

You can do so by passing one of the option in html2canvas function. And trust me this will surely work 😃

html2canvas($(‘#div_pdf’)[0], { useCORS: true, //By passing this option in function Cross origin images will be rendered properly in the downloaded version of the PDF onrendered: function (canvas) { canvasToImageSuccess(canvas); } }); For further info you can have a look at the documentation of the html2canvas library on below URL-: https://html2canvas.hertzen.com/documentation.html

10reactions
akaJescommented, May 21, 2015

Maybe You should use useCORS ptoperty as true, it intended for crossdomained web content

Read more comments on GitHub >

github_iconTop Results From Across the Web

Allowing cross-origin use of images and canvas - HTML
HTML provides a crossorigin attribute for images that, in combination with an appropriate CORS header, allows images defined by the <img> ...
Read more >
How do I handle CORS with html2Canvas and AWS S3 images?
See the edit, I did try setting the crossOrigin attribute with no luck, and I use useCORS set to true (forgot to mention...
Read more >
What Is a CORS Error and How to Fix It (3 Ways) - Bannerbear
A CORS error is common when making an HTTP request to another origin. You can get rid of it using one of the...
Read more >
3 Ways to Fix the CORS Error — and How the Access-Control ...
Fix one: install the Allow-Control-Allow-Origin plugin. The quickest fix you can make is to install the moesif CORS extension .
Read more >
How to win at CORS - JakeArchibald.com
By default, a cross-origin CORS request is made without credentials. So, no cookies, no client certs, no automatic Authorization header, and Set ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found