html2canvas is a JavaScript library that allows you to take “screenshots” of web pages or specific elements on a web page and render them as images on the client-side. It achieves this by rendering the content directly within the browser, without needing server-side processing or external image generation services. This makes it a powerful tool for creating dynamic image exports, generating thumbnails, or capturing web page content for offline use. It works by emulating the browser’s rendering engine to create a pixel-perfect representation of the content.
html2canvas strives for broad browser compatibility but may have limitations or rendering differences across various browsers and versions. While it aims to support modern and older browsers, the most reliable and consistent results are typically achieved in up-to-date versions of Chrome, Firefox, Safari, and Edge. Refer to the project’s official documentation and issue tracker for the most up-to-date compatibility information. Some very old or uncommon browsers may not be fully supported.
html2canvas is typically installed via a package manager like npm or yarn. For use in simple projects without a build process, you can include the library directly via a <script>
tag.
Using npm or yarn:
npm install html2canvas
# or
yarn add html2canvas
Then, import it into your JavaScript code:
import html2canvas from 'html2canvas';
Using a <script>
tag (for simpler projects):
Download the library from the official repository and include it in your HTML file:
<script src="path/to/html2canvas.min.js"></script>
After including the library, you can use the html2canvas
function to capture elements:
html2canvas(document.getElementById('myElement')).then(canvas => {
// canvas is the generated image as a canvas element
document.body.appendChild(canvas); // append to the page
// or download the image:
const imgData = canvas.toDataURL('image/png');
const link = document.createElement('a');
.download = 'screenshot.png';
link.href = imgData;
link.click();
link; })
Remember to replace 'myElement'
with the ID of the element you want to capture. Consult the library’s documentation for advanced usage and configuration options.
The simplest way to use html2canvas is to capture a single HTML element. You’ll need to obtain a reference to the element using its ID or a suitable selector, then pass this reference to the html2canvas()
function. The function returns a Promise that resolves with a Canvas object representing the rendered image.
import html2canvas from 'html2canvas';
const element = document.getElementById('myElement'); // Get the element to capture
html2canvas(element).then(canvas => {
// 'canvas' is the generated canvas element.
document.body.appendChild(canvas); // Append the canvas to the body (optional)
// Convert the canvas to a data URL for further use (e.g., downloading):
const imgData = canvas.toDataURL('image/png');
console.log(imgData); // Output the data URL
.catch(error => {
})console.error('Error capturing element:', error);
; })
Replace "myElement"
with the actual ID of your element.
To capture the entire visible portion of the page, you can pass document.body
(or document.documentElement
for slightly different results) to the html2canvas()
function. Note that this will only capture what’s currently visible in the viewport; elements hidden due to scrolling will be excluded.
import html2canvas from 'html2canvas';
html2canvas(document.body).then(canvas => {
// Process the canvas as needed (e.g., append, download, etc.)
document.body.appendChild(canvas); // Append to body
.catch(error => {
})console.error('Error capturing page:', error);
; })
html2canvas()
FunctionThe core of the library is the html2canvas()
function. It takes at least one argument:
element
(required): A DOM element (or a selector string that can be used to select a single element) representing the content to render.The function returns a Promise
. The Promise resolves with a Canvas
object containing the rendered image. If an error occurs during the rendering process, the Promise rejects and throws an error. Error handling is crucial, as rendering complex pages or pages with problematic CSS can lead to failures.
The html2canvas()
function accepts a second argument: an options object. This object allows you to fine-tune the rendering process. Some common options include:
width
: Specify the width of the rendered canvas.height
: Specify the height of the rendered canvas.scale
: Scaling factor applied to the rendered content (default is 1). Increasing the scale improves the resolution but increases rendering time.backgroundColor
: Sets the background color of the generated image.logging
: Enables detailed logging for debugging purposes.useCORS
: Enables Cross-Origin Resource Sharing (CORS) for images from different domains (essential if you’re capturing images that aren’t on the same origin).allowTaint
: By default, html2canvas
will prevent rendering if it detects a potential taint (often due to cross-origin images). Setting allowTaint
to true
ignores this check but may result in issues if those images aren’t properly handled. Use with caution.Example with options:
html2canvas(element, {
scale: 2,
backgroundColor: '#fff',
logging: true,
useCORS: true
.then(canvas => {
})// ...
.catch(error => {
})// ...
; })
The html2canvas()
function is asynchronous. It returns a Promise, which means the rendering process happens in the background. You must use .then()
to handle the successful resolution of the Promise (accessing the generated canvas
object) and .catch()
to handle potential errors. Proper error handling is essential for a robust application, as rendering failures can occur due to various reasons (e.g., complex CSS, cross-origin issues, or network problems). Always wrap your call to html2canvas
within a .then
/.catch
block.
Beyond the basic options, you can further customize the rendering process using the Canvas API directly after obtaining the Canvas element from the html2canvas
Promise. This allows for post-processing effects, adding text overlays, or modifying the generated image before downloading or using it.
html2canvas(element).then(canvas => {
const ctx = canvas.getContext('2d');
// Example: Draw a red rectangle on the canvas
.fillStyle = 'red';
ctx.fillRect(10, 10, 50, 50);
ctx
// Example: Get image data and manipulate pixels
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
// ... manipulate imageData.data ...
.putImageData(imageData, 0, 0);
ctx
// ...further canvas manipulation...
const imgData = canvas.toDataURL('image/png');
// ...
.catch(error => {
})// ...
; })
html2canvas generally respects CSS styles applied to the elements. However, very complex or unusual CSS might not render perfectly. Ensure that your CSS is well-formed and avoids overly complex selectors or properties that might confuse the rendering engine. Consider simplifying stylesheets for better compatibility. Use the browser’s developer tools to inspect the rendered content and identify any discrepancies between the original and captured image. Sometimes, using a higher scale
value in the options can improve rendering accuracy for detailed or small elements. Be mindful that display: none
will completely hide an element, while visibility: hidden
might render it as a blank space.
Images are a common source of issues with html2canvas. Ensure that images are loaded before attempting to render the element containing them. For images from a different origin, you’ll almost certainly need to enable useCORS: true
in the options. Remember that images that aren’t properly accessible (due to CORS restrictions or 404 errors) might prevent rendering entirely or cause artifacts. Background images are also rendered, but ensuring they’re loaded is also crucial. Consider using a placeholder image while the background image is loading to prevent rendering inconsistencies.
For capturing pages or elements that are difficult to render due to complex CSS, JavaScript interactions, or cross-origin restrictions, you might consider using an iframe. Load the content you want to capture into an iframe, then use html2canvas
on the iframe’s content. This can isolate the content from potential conflicts with the main page. A proxy might be needed for certain scenarios involving complex dynamic content or severe cross-origin issues. However, using a proxy adds significant complexity and potential security concerns and should be a last resort.
Complex layouts, especially those heavily reliant on JavaScript frameworks (React, Angular, Vue, etc.), might require extra attention. Ensure that the JavaScript framework has fully rendered the content before calling html2canvas
. This often involves using timers, lifecycle hooks, or other mechanisms to ensure the DOM is stable. Consider using requestAnimationFrame
for best practices. For very complex layouts, breaking the capture into smaller sections and combining them later might be more efficient.
Rendering large or complex pages can be slow. To optimize performance:
scale
value: A higher scale reduces the need for upscaling later, but increases the rendering time proportionally. Experiment to find a balance.requestAnimationFrame
: To avoid blocking the main thread, use requestAnimationFrame
to schedule the html2canvas
call.Debugging issues with html2canvas often involves using your browser’s developer tools. Check the console for errors or warnings. The logging: true
option can provide useful debugging information. Inspect the captured canvas to visually compare it to the original element. If images aren’t rendering correctly, check CORS settings and image loading states. If the layout is incorrect, check CSS and ensure your JavaScript framework has completed rendering before calling html2canvas. If performance is a concern, profile your code to identify performance bottlenecks. Consider using a simpler example to isolate problems before attempting to render very complex pages. Refer to the official html2canvas documentation and issue tracker for help resolving specific problems.
The html2canvas
function accepts an optional second argument: an object containing configuration options. These options allow you to customize various aspects of the rendering process. Here’s a detailed explanation of some key options:
width
(Number): Specifies the width of the rendered canvas in pixels. If not provided, the width of the target element (or viewport for full-page captures) will be used.
height
(Number): Specifies the height of the rendered canvas in pixels. If not provided, the height of the target element (or viewport) will be used.
scale
(Number, default: 1): A scaling factor applied to the rendered content. A value of 2 will render at double the resolution. This improves the quality but increases rendering time significantly.
backgroundColor
(String, default: ‘rgba(0,0,0,0)’ - transparent): Sets the background color of the generated canvas. Use CSS color values (e.g., ‘#fff’, ‘rgb(255,255,255)’, ‘white’).
logging
(Boolean, default: false): Enables detailed logging to the console. Useful for debugging rendering issues.
useCORS
(Boolean, default: false): Crucial for handling images from different domains (Cross-Origin Resource Sharing). Setting this to true
allows html2canvas to attempt to render images from other origins. However, this still relies on the server allowing CORS access.
allowTaint
(Boolean, default: false): Controls the handling of “tainted” canvases. A canvas becomes tainted if it contains an image from a different origin without proper CORS headers. Setting this to true
will allow rendering even if a taint is detected, but may result in incomplete or incorrect rendering. Use with extreme caution. Generally, addressing the CORS issue is preferred over using allowTaint
.
x
(Number, default: 0): The x-coordinate offset for the rendered content within the canvas. Useful for cropping or positioning.
y
(Number, default: 0): The y-coordinate offset for the rendered content within the canvas. Useful for cropping or positioning.
imageTimeout
(Number, default: 10000): The maximum time (in milliseconds) to wait for images to load before rendering.
proxy
(String): A URL to a proxy server for handling cross-origin requests. This should generally be avoided unless absolutely necessary and used with caution due to security implications.
removeContainer
(Boolean, default: false): Removes the temporary container element used during the rendering process after capturing.
To use custom options, simply create an object containing the options you want to set and pass it as the second argument to html2canvas()
. Example:
const options = {
scale: 2,
backgroundColor: '#f0f0f0',
logging: true,
useCORS: true
;
}
html2canvas(document.getElementById('myElement'), options).then(canvas => {
// ...
; })
You can combine multiple options as needed.
Some options interact with each other. For instance, scale
affects the size of the output canvas, while width
and height
can override the dimensions derived from the scaling. useCORS
and allowTaint
handle cross-origin resources differently: useCORS
attempts to properly handle CORS, while allowTaint
bypasses CORS checks, often leading to incomplete or distorted results. Carefully consider the interactions when combining options. Testing is essential to ensure the desired outcome.
If options aren’t explicitly provided, html2canvas
uses default values. These defaults are generally designed for typical use cases. The default backgroundColor
is transparent (rgba(0,0,0,0)
), and the default scale
is 1. The default behavior is to render the entire visible area of the target element, respecting its CSS styles, within a canvas of the same size as the element. Understanding the default behavior is essential when modifying options to customize the rendering process. Be aware that the rendering engine’s interpretation of complex CSS might differ slightly from the browser’s rendering, leading to minor discrepancies in some cases.
html2canvas()
Function DetailsThe core of the library is the html2canvas()
function. Its signature is:
html2canvas(element, options)
element
(required): This parameter specifies the DOM element (or a CSS selector string that resolves to a single element) to be rendered. This can be any element on the page, including document.body
to render the entire page (visible portion). Invalid selectors or non-existent elements will lead to errors.
options
(optional): This parameter is an object containing configuration options (detailed in the “Options and Configuration” section). This allows customization of the rendering process (e.g., setting the scale, background color, handling CORS).
The function returns a Promise
. This Promise resolves with a Canvas
object if the rendering is successful. The Promise rejects with an error if the rendering fails for any reason (e.g., missing images, CORS issues, invalid CSS).
html2canvas
is primarily asynchronous and uses Promises for handling asynchronous operations. There are no direct event handling mechanisms or callbacks in the traditional sense. Instead, you use the .then()
method of the returned Promise to access the generated Canvas
element after the rendering completes successfully. The .catch()
method handles any errors that occur during the rendering process.
Example:
html2canvas(element).then(canvas => {
// Success: 'canvas' is the generated Canvas element.
// Process the canvas (e.g., download, append to page)
.catch(error => {
})// Error handling: 'error' contains information about the failure
console.error("html2canvas failed:", error);
; })
This approach is cleaner and more consistent with modern JavaScript asynchronous programming practices.
Error handling is crucial when using html2canvas
, as various factors can cause rendering failures (network issues, missing images, complex CSS, CORS problems, etc.). The Promise.catch()
method is the primary mechanism for handling errors. The error
object passed to the .catch()
callback typically contains information about the type of error and its source. Check the console for detailed error messages during development, as these often provide clues to solve the issue.
Best practice involves wrapping your html2canvas
call within a try...catch
block for more robust error management. Consider adding user-friendly error messages to improve the user experience in production environments.
The html2canvas()
function returns a Promise
. If successful, the Promise resolves to a single HTML5 Canvas
element. This Canvas
element contains the rendered image. You can access its properties (e.g., width
, height
) and methods (e.g., toDataURL()
, getContext()
) to manipulate or process the generated image further. You can then use the toDataURL()
method to get the image as a data URL (e.g., for downloading or embedding).
If the Promise rejects, the catch()
handler receives an Error
object. This object contains information about the error that occurred, often including a descriptive message to help identify the cause of the failure.
This example captures a single element with the ID “myElement” and displays the resulting image on the page:
<!DOCTYPE html>
<html>
<head>
<title>html2canvas Example</title>
<script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
const element = document.getElementById('myElement');
html2canvas(element).then(canvas => {
document.body.appendChild(canvas);
;
});
})</script>
</head>
<body>
<div id="myElement" style="width: 200px; height: 100px; background-color: lightblue; border: 1px solid black;">
This is my element!</div>
</body>
</html>
Remember to replace "https://html2canvas.hertzen.com/dist/html2canvas.min.js"
with the correct path if you’ve downloaded the library locally. This example showcases the most basic usage. It captures the element, and then appends the generated canvas to the body of the HTML document.
This example demonstrates more advanced usage, including scaling, background color, and error handling:
import html2canvas from 'html2canvas';
const element = document.getElementById('myElement');
const options = {
scale: 2, // Double the resolution
backgroundColor: '#f0f0f0', // Light gray background
logging: true //Enable logging for debugging
;
}
html2canvas(element, options).then(canvas => {
// Success: Canvas is ready
const imgData = canvas.toDataURL('image/png');
const link = document.createElement('a');
.download = 'screenshot.png';
link.href = imgData;
link.click(); //Download the image
link
.catch(error => {
})// Error handling
console.error('Screenshot failed:', error);
// Consider adding user-friendly error display here
; })
This example demonstrates more advanced features, including scaling, background color, and error handling. The resulting image is then downloaded automatically. Remember that you will need a suitable import mechanism for the html2canvas
library if you’re using a module bundler (like Webpack or Parcel).
html2canvas can be integrated with various JavaScript libraries. Here’s a conceptual example of integrating it with a library that handles image uploads:
import html2canvas from 'html2canvas';
import imageUpload from './imageUploadLibrary'; // Placeholder for your upload library
const element = document.getElementById('myElement');
html2canvas(element).then(canvas => {
const imgData = canvas.toDataURL('image/png');
imageUpload(imgData, 'screenshot.png'); //Use your upload library
.catch(error => {
})console.error('Error:', error);
; })
This is a general structure. The specifics will heavily depend on the functionalities and API of your chosen image upload library. Ensure you adapt this example to the library’s specific methods and requirements.
Web-based Screenshot Tools: Create a web application that allows users to capture screenshots of web pages or specific elements.
Social Media Sharing: Generate shareable images of product details or website content for social media platforms.
Dynamic Thumbnail Generation: Create dynamic thumbnails for articles or web pages, providing previews without server-side processing.
Client-Side Image Editing: Use the generated canvas as a base for client-side image editing functionalities.
Printable Web Pages: Offer printable versions of web pages by capturing the relevant content and generating a suitable image.
Report Generation: Dynamically generate reports by capturing sections of web pages as images and including them in the report document.
These examples provide a starting point. The possibilities are numerous, leveraging html2canvas’s capability for client-side image generation. Remember to always handle errors gracefully and optimize for performance, especially with complex or large pages.
Here are some common errors encountered when using html2canvas and their potential solutions:
“Uncaught (in promise) Error: Rendering failed”: This generic error often indicates a problem during the rendering process. Check the browser’s developer console for more specific error messages. Common causes include:
useCORS: true
option in html2canvas.html2canvas
.scale
value to improve rendering accuracy.imageTimeout
in the options.Blank or incomplete screenshots: This usually indicates a problem with image loading, CORS, or CSS. Check the browser’s developer console for errors. Examine the HTML structure to ensure all elements are visible and correctly rendered.
Incorrect layout or distorted elements: This might be due to complex CSS, especially issues with transform
, opacity
, or other CSS properties that might not be fully supported by html2canvas. Simplify your CSS if possible.
Performance Issues: Rendering large or complex pages can be slow. See the “Performance Issues and Optimization” section below for solutions.
Canvas is empty after rendering: Verify that you are correctly selecting the element to capture and that the Promise is resolving correctly. Double check your .then()
block is executing correctly.
Browser Developer Tools: Use your browser’s developer tools (usually accessed by pressing F12) to inspect the page, check for JavaScript errors in the console, and examine the network requests to ensure images are loading properly.
logging: true
Option: Set the logging
option to true
in the html2canvas configuration to enable detailed logging to the console. This provides insights into the rendering process and may reveal the source of errors.
Simplify the target element: If you are capturing a complex page, try capturing a smaller, simpler element first to isolate potential issues.
Inspect the Canvas: After the rendering is complete (in the .then()
block), use the browser’s developer tools to inspect the generated Canvas element directly. This allows you to visually verify the content and look for any rendering discrepancies.
Check CORS Headers: Use your browser’s developer tools’ network tab to examine the network requests made by html2canvas
, especially for images, to ensure the server is sending the correct CORS headers if necessary.
Rendering large or complex pages can be time-consuming. Use these strategies to improve performance:
Reduce the scale: Lowering the scale
value reduces rendering resolution, speeding up the process but decreasing image quality. Find a good balance.
Capture smaller sections: Instead of capturing the whole page, try capturing smaller sections separately and combining them afterward using the Canvas API.
Optimize images: Compress images to reduce their file size and improve loading times. Use appropriately sized images for your needs.
Simplify CSS: Complex CSS can slow down rendering. Try to simplify your CSS rules and avoid overly complex selectors or properties.
Use requestAnimationFrame: Consider using requestAnimationFrame
to schedule the html2canvas
call to avoid blocking the main thread.
Asynchronous Loading: Ensure all images and resources are fully loaded before calling html2canvas
. You might need to wait for events or use Promises to ensure everything is ready.
Reduce unnecessary content: Remove any unnecessary elements from the target area before capturing it.
While html2canvas strives for broad compatibility, minor rendering differences might exist across browsers. Always test your implementation thoroughly in the target browsers. Some less common or very old browsers might not be fully supported. Be aware that different browsers handle CSS differently; some subtle rendering discrepancies might be inevitable. Focus on ensuring your CSS works well across the most common browser platforms. If compatibility across a particularly wide range of browsers is critical, detailed testing across the targeted browser versions is crucial. Use a testing framework to ensure consistency across different browsers and browser versions.
To contribute to html2canvas, you’ll need to clone the repository and set up a development environment. These instructions assume a basic familiarity with Git and Node.js.
git clone https://github.com/niklasvh/html2canvas.git
cd html2canvas
npm install
This will install all the necessary packages for building and testing the library.
npm run build
This will create a minified version of the library in the dist
directory.
html2canvas uses a comprehensive test suite. Before submitting any pull requests, ensure your changes pass all existing tests and adhere to the project’s coding style.
npm test
Code Style: The project follows a consistent coding style. Use a code formatter (like Prettier) to ensure your code conforms to the style guide. You can typically run the formatter with a command like npm run format
(or a similar command specified in the project’s documentation).
Test Coverage: Strive to maintain high test coverage for any new features or bug fixes you introduce. Use the testing framework’s tools (likely Jest) to check coverage. This ensures that your changes don’t introduce regressions.
Fork the repository: Create a fork of the html2canvas repository on GitHub.
Create a branch: Create a new branch for your changes. Use descriptive branch names (e.g., fix/bug-123
or feat/new-feature
).
Make your changes: Implement your changes, ensuring they pass all tests and adhere to the coding style.
Commit your changes: Commit your changes with clear and concise commit messages. Follow a conventional commit message format (e.g., fix: Resolve issue #123
, feat: Add new feature
).
Push your branch: Push your branch to your forked repository on GitHub.
Create a pull request: Create a pull request from your branch to the main branch of the original html2canvas repository. Provide a clear description of your changes and address any comments or feedback from reviewers.
Be respectful: Treat all contributors and maintainers with respect. Constructive criticism is welcome, but personal attacks are not.
Follow the code of conduct: Adhere to the project’s code of conduct (if one exists).
Write clear and concise commit messages: Make it easy for others to understand your changes.
Provide clear and detailed descriptions for pull requests: Explain the purpose and scope of your changes.
Be patient: The review process can take time. Be patient and responsive to feedback.
Test your changes thoroughly: Ensure your changes work correctly and don’t introduce regressions.
By following these guidelines, you can contribute to the improvement and maintenance of the html2canvas library. Your contributions are valuable to the community!