html2canvas - Documentation

Introduction

What is html2canvas?

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.

Key Features and Capabilities

Use Cases and Examples

Browser Compatibility

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.

Installation and Setup

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');
  link.download = 'screenshot.png';
  link.href = imgData;
  link.click();
});

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.

Basic Usage

Capturing a Single Element

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.

Capturing the Entire Page

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);
});

Understanding the html2canvas() Function

The core of the library is the html2canvas() function. It takes at least one argument:

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.

Options and Parameters

The html2canvas() function accepts a second argument: an options object. This object allows you to fine-tune the rendering process. Some common options include:

Example with options:

html2canvas(element, {
  scale: 2,
  backgroundColor: '#fff',
  logging: true,
  useCORS: true
}).then(canvas => {
  // ...
}).catch(error => {
  // ...
});

Handling Promises and Asynchronous Operations

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.

Advanced Usage

Customizing Canvas Rendering

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
  ctx.fillStyle = 'red';
  ctx.fillRect(10, 10, 50, 50);

  // Example: Get image data and manipulate pixels
  const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
  // ... manipulate imageData.data ...
  ctx.putImageData(imageData, 0, 0);

  // ...further canvas manipulation...
  const imgData = canvas.toDataURL('image/png');
  // ...
}).catch(error => {
  // ...
});

Handling CSS and Styling

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.

Working with Images and Backgrounds

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.

Using Proxy and iframe

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.

Dealing with Complex Layouts

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.

Performance Optimization Techniques

Rendering large or complex pages can be slow. To optimize performance:

Troubleshooting and Debugging

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.

Options and Configuration

Detailed Explanation of Each Option

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:

Using Custom Options and Settings

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.

Option Combinations and Interactions

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.

Default Values and Behavior

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.

API Reference

html2canvas() Function Details

The core of the library is the html2canvas() function. Its signature is:

html2canvas(element, options)

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).

Event Handling and Callbacks

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 and Reporting

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.

Return Values and Data Structures

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.

Examples and Tutorials

Simple Screenshot Example

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.

Advanced Screenshot with Customizations

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');
  link.download = 'screenshot.png';
  link.href = imgData;
  link.click(); //Download the image

}).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).

Integrating with Other Libraries

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.

Real-world Application Examples

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.

Troubleshooting

Common Errors and Solutions

Here are some common errors encountered when using html2canvas and their potential solutions:

Debugging Techniques

Performance Issues and Optimization

Rendering large or complex pages can be time-consuming. Use these strategies to improve performance:

Cross-Browser Compatibility Issues

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.

Contributing

Development Setup

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.

  1. Clone the repository:
git clone https://github.com/niklasvh/html2canvas.git
cd html2canvas
  1. Install dependencies:
npm install

This will install all the necessary packages for building and testing the library.

  1. Build the library:
npm run build

This will create a minified version of the library in the dist directory.

Testing and Code Style

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

Submitting Pull Requests

  1. Fork the repository: Create a fork of the html2canvas repository on GitHub.

  2. Create a branch: Create a new branch for your changes. Use descriptive branch names (e.g., fix/bug-123 or feat/new-feature).

  3. Make your changes: Implement your changes, ensuring they pass all tests and adhere to the coding style.

  4. 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).

  5. Push your branch: Push your branch to your forked repository on GitHub.

  6. 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.

Community Guidelines

By following these guidelines, you can contribute to the improvement and maintenance of the html2canvas library. Your contributions are valuable to the community!