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.

Uncaught (in promise) DOMException: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The image argument is a canvas element with a width or height of 0.

See original GitHub issue
Uncaught (in promise) DOMException: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The image argument is a canvas element with a width or height of 0.

Specifications:

  • html2canvas version tested with: 1.0.0-rc.2
  • Browser & version: chromium latest release
  • Operating system: fedora 29

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:22 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
kluescommented, Jun 2, 2020

I got the similar error InvalidStateError: CanvasRenderingContext2D.createPattern: Passed-in canvas has width 0 which only occurs in Firefox. After some investigation I found out it occurs on svg-images set as background-image.

See this jsfiddle for demo.

Including the lines

ignoreElements: (node) => {
    return node.style['background-image'].indexOf('image/svg') !== -1;
}

is a quickfix.

2reactions
shaji-Devcommented, Sep 20, 2019

@rdpatel3996 Yes, I just needed to add the ignoreElements configuration and all is well, appearently the new version of Google Maps use iframe elements inside which makes it impossible to capture.

const mapWrapper = document.getElementById('mapWrapper');
      html2canvas(mapWrapper, {
        useCORS: true,
        allowTaint: false,
        logging: isDevMode(),
        ignoreElements: (node) => {
          return node.nodeName === 'IFRAME';
        }
      }).then(canvas => {
        canvas.toBlob(blob => {
          resolve(blob);
        });
      }).catch(reject);

The key is to add this config:

ignoreElements: (node) => {
          return node.nodeName === 'IFRAME';
        }
Read more comments on GitHub >

github_iconTop Results From Across the Web

The image argument is a canvas element with a width or ...
"Failed to execute 'createPattern' on 'CanvasRenderingContext2D': The image argument is a canvas element with a width or height of 0.".
Read more >
CanvasRenderingContext2D.drawImage() - Web APIs | MDN
The width to draw the image in the destination canvas. This allows scaling of the drawn image. If not specified, the image is...
Read more >
Blazor signature Uncaught DOMException: Failed to execute ...
Uncaught DOMException : Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The image argument is a canvas element with a width or height of 0....
Read more >
Failed to execute 'drawImage' on 'CanvasRenderingContext2D'
The error message is saying that cowpies is not an image element. You can only draw image elements onto the canvas. When you...
Read more >
Failed to execute 'drawImage' on 'CanvasRenderingContext2D
toJSON(), the console send me this error:Failed to execute 'drawImage' on ... drawImage( image, 0, 0, image.width, image.height );; }; canvas = _canvas; ......
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