@cantoo/html2canvas

中文文档
1.5.9发布于 2024/03/13MITTypeScriptESM + CJS近 30 天下载 3214

Screenshots with JavaScript

原始使用文档
文档目录6 节
  1. How does it work?
  2. Browser compatibility
  3. Usage
  4. Building
  5. Examples
  6. Contributing

html2canvas

Homepage | Downloads | Questions

Gitter CI NPM Downloads NPM Version

JavaScript HTML renderer

The script allows you to take "screenshots" of webpages or parts of it, directly on the users browser. The screenshot is based on the DOM and as such may not be 100% accurate to the real representation as it does not make an actual screenshot, but builds the screenshot based on the information available on the page.

How does it work?

The script renders the current page as a canvas image, by reading the DOM and the different styles applied to the elements.

It does not require any rendering from the server, as the whole image is created on the client's browser. However, as it is heavily dependent on the browser, this library is not suitable to be used in nodejs. It doesn't magically circumvent any browser content policy restrictions either, so rendering cross-origin content will require a proxy to get the content to the same origin.

The script is still in a very experimental state, so I don't recommend using it in a production environment nor start building applications with it yet, as there will be still major changes made.

Browser compatibility

The library should work fine on the following browsers (with Promise polyfill):

  • Firefox 3.5+
  • Google Chrome
  • Opera 12+
  • IE9+
  • Safari 6+

As each CSS property needs to be manually built to be supported, there are a number of properties that are not yet supported.

Usage

The html2canvas library utilizes Promises and expects them to be available in the global context. If you wish to support older browsers that do not natively support Promises, please include a polyfill such as es6-promise before including html2canvas.

To render an element with html2canvas, simply call: html2canvas(element[, options]);

The function returns a Promise containing the <canvas> element. Simply add a promise fulfillment handler to the promise using then:

html2canvas(document.body).then(function(canvas) {
    document.body.appendChild(canvas);
});

Building

You can download ready builds here.

Clone git repository:

$ git clone git://github.com/niklasvh/html2canvas.git

Install dependencies:

$ npm install

Build browser bundle

$ npm run build

Examples

For more information and examples, please visit the homepage or try the test console.

Contributing

If you wish to contribute to the project, please send the pull requests to the develop branch. Before submitting any changes, try and test that the changes work with all the support browsers. If some CSS property isn't supported or is incomplete, please create appropriate tests for it as well before submitting any code changes.

版本与发布记录

@cantoo/html2canvas 共发布 11 个版本,首次发布于 2023/08/31;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.5.9latest2024/03/132 年前
1.5.82024/03/072 年前
1.5.72023/09/013 年前
1.5.62023/08/313 年前
1.5.52023/08/313 年前
1.5.42023/08/313 年前

关于 @cantoo/html2canvas 的常见问题

@cantoo/html2canvas 是什么?

Screenshots with JavaScript

如何安装 @cantoo/html2canvas?

在项目目录运行 npm install @cantoo/html2canvas,或使用 pnpm add / yarn add / bun add。

@cantoo/html2canvas 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cantoo/html2canvas 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@cantoo/html2canvas 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=8.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cantoo/html2canvas 可以商用吗?

@cantoo/html2canvas 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cantoo/html2canvas 的下载量有多少?

根据 npm 官方下载统计,@cantoo/html2canvas 在 2026/10/09 – 2026/11/07 这 30 天内被下载 3,214 次,日均约 107 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cantoo/html2canvas 还在维护吗?

最新版本 1.5.9 发布于 2024/03/13(2 年前);至今共发布 11 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/08/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。