HTML5 Canvas Screenshot: Step-by-Step Guide
In this article
This guide shows you how to capture a screenshot of a webpage or a specific element using HTML5 Canvas and JavaScript. You'll learn to draw the page (or a part of it) onto a canvas, then export it as an image. This works entirely in the browser—no server needed. You'll need a basic understanding of HTML, CSS, and JavaScript, and a modern browser (Chrome, Firefox, Edge, or Safari).
What You'll Need
- A text editor (VS Code, Sublime, etc.)
- A modern web browser with developer tools
- Basic knowledge of HTML, CSS, and JavaScript
- For capturing full-page screenshots, you may need to handle scrolling and device pixel ratio—covered below.
Step-by-Step
- Set up your HTML structure. Create an HTML file with a container element you want to capture (e.g., a
<div id="capture">). Include a button to trigger the screenshot and a canvas element (hidden or visible) to hold the result. - Reference the elements in JavaScript. Use
document.getElementById()to get the target element, the button, and the canvas. - Determine the dimensions. Get the element's bounding rectangle using
getBoundingClientRect(). Note that this returns viewport-relative coordinates, which you'll need to adjust for scrolling. - Adjust for scroll position. Add
window.scrollXandwindow.scrollYto the left and top values to get document-relative coordinates. - Set canvas size. Set the canvas width and height to the element's width and height. For crisp screenshots on high-DPI displays, multiply by
window.devicePixelRatioand scale the canvas context accordingly. - Draw the page onto the canvas. Use the
html2canvaslibrary (a popular JavaScript library) or the nativeCanvasRenderingContext2D.drawWindow()(Firefox only, requires privileged permissions). The most practical approach is to usehtml2canvas. Include it via CDN or npm. - If using html2canvas: Call
html2canvas(element).then(canvas => { ... }). This returns a canvas element with the captured content. You can then append it to the DOM or convert it to an image. - Export the canvas as an image. Use
canvas.toDataURL('image/png')to get a base64-encoded PNG. You can then create a download link or display the image. - Trigger download. Create an
<a>element with thedownloadattribute, set itshrefto the data URL, and click it programmatically.
Common Mistakes
- Ignoring scroll offsets: If you use
getBoundingClientRect()without adding scroll offsets, the captured area will be wrong when the page is scrolled. - Not handling device pixel ratio: Screenshots may appear blurry on high-DPI screens if you don't scale the canvas.
- Using
drawWindow()in non-Firefox browsers: This method is not supported in Chrome, Edge, or Safari. - Forgetting to wait for images/fonts: If the page has images or custom fonts, they may not be loaded when the screenshot is taken. Ensure all resources are loaded before capturing.
- Cross-origin images causing tainted canvas: If the page contains images from a different origin without CORS headers, the canvas becomes 'tainted' and
toDataURL()will throw an error.
Tips
- Use html2canvas for most cases: It handles many edge cases (like scroll, DPI, and cross-origin images) automatically.
- Set a timeout before capturing: If you're capturing after a dynamic change, use
setTimeout()to ensure the DOM is updated. - Capture only the visible viewport for speed: For large pages, capturing the full page can be slow. Consider capturing just the visible area if that's all you need.
- Test on different browsers: html2canvas works on all modern browsers, but rendering may vary slightly.
- Use
toBlob()for large images: For very large screenshots,toBlob()is more memory-efficient thantoDataURL().
Troubleshooting
- Canvas is blank or black: Check that the element is visible and has dimensions. Ensure you're not capturing before the element is rendered.
- SecurityError when exporting: This is due to tainted canvas. Remove cross-origin images or set
crossOrigin="anonymous"on them if the server supports CORS. - Screenshot is blurry: Multiply canvas dimensions by
window.devicePixelRatioand scale the context withctx.scale(dpr, dpr). - Captured area is offset: Recalculate the coordinates using
getBoundingClientRect()pluswindow.scrollX/window.scrollY. - html2canvas doesn't capture background images: Some CSS properties (like
background-imagewith gradients) may not render. Try using inline styles or solid colors.
Frequently Asked Questions
Can I capture the entire page, not just a section?
Yes, you can pass document.body or document.documentElement to html2canvas. However, be aware that very long pages may produce large images and take time.
Is this possible without any external library?
In Firefox, you can use drawWindow() but it requires special permissions. In other browsers, there is no native way to capture arbitrary DOM content; html2canvas is the standard solution.
Will the screenshot include CSS animations?
No, the screenshot captures the current state. Animations will appear as they are at the moment of capture.
Can I capture a screenshot of a specific element that is off-screen?
Yes, html2canvas can capture off-screen elements, but you may need to temporarily scroll to it or adjust the canvas offset.
How do I save the screenshot as a file?
Use canvas.toBlob() to get a Blob, then create an object URL and trigger a download via an <a> element.
Does this work on mobile browsers?
Yes, html2canvas works on mobile browsers, but you may need to handle touch events and viewport differences.