html2canvas README

The project's README from github.com/niklasvh/html2canvas. As one markdown file: README.md.

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

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.

html2canvas (niklasvh/html2canvas)

Searching Database…

这是什么?html2canvas 是一个 JavaScript 库,可以直接在用户的浏览器中为网页或其中的一部分生成“截图”,无需服务器支持。
它真的是截取了屏幕截图吗,还是别的什么?不是,它并不会真正截屏。它会读取 DOM 以及应用在元素上的 CSS 样式,再把页面重新绘制成一张画布图像,因此视觉效果可能无法做到 100% 准确。
在我的项目里该怎么使用?引入 html2canvas 后调用 html2canvas(element, options),它会返回一个 Promise,并解析为一个 canvas 画布,你可以把它添加到页面中,例如:html2canvas(document.body).then(canvas => document.body.appendChild(canvas))。
还需要别的东西吗,比如 Promise 的 polyfill?它依赖全局可用的 Promise。对于不支持原生 Promise 的老旧浏览器,需要在加载 html2canvas 之前先引入 es6-promise 等 polyfill 兼容库。
有什么需要注意的地方吗,比如跨域内容或 Node.js 支持?它高度依赖浏览器环境,因此无法在 Node.js 中运行;它也无法绕过浏览器的内容策略,所以要渲染跨域内容,就需要通过代理从同源提供这些内容。作者还称它仍处于实验阶段,目前不建议用于生产环境。
它支持哪些浏览器?支持 Firefox 3.5+、Chrome、Opera 12+、IE9+ 以及 Safari 6+(需配合 Promise polyfill 使用),因为每一项受支持的 CSS 属性都必须手动实现。
为什么它不能直接截取页面的真实像素?因为它完全在浏览器中运行,没有服务端渲染,所以必须通过读取 DOM 和 CSS 来重新构建页面的视觉效果,这也意味着并非所有 CSS 属性都已被支持。
CSS 支持是否完整?不完整。每一个 CSS 属性都需要手动实现才能获得支持,因此仍有不少 CSS 属性尚未被处理。
Creator
niklasvh
Size
114.6 MB
Stars
31.9K
Age
15y
Commits · 90 Days