

GitHub - niklasvh/html2canvas: Screenshots with JavaScript
source link: https://github.com/niklasvh/html2canvas
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md
html2canvas
Homepage | Downloads | Questions | Donate
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 Promise
s and expects them to be available in the global context. If you wish to
support older browsers that do not natively support Promise
s, please include a polyfill such as
es6-promise before including html2canvas
.
Note! These instructions are for using the current dev version of 0.5, for the latest release version (0.4.1), checkout the old readme.
To render an element
with html2canvas, simply call:
html2canvas(element[, options]);
The function returns a Promise containing the <canvas>
element. Simply add a promise fullfillment 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
Running tests
The library has two sets of tests. The first set is a number of qunit tests that check that different values parsed by browsers are correctly converted in html2canvas. To run these tests with grunt you'll need phantomjs.
The other set of tests run Firefox, Chrome and Internet Explorer with webdriver. The selenium standalone server (runs on Java) is required for these tests and can be downloaded from here. They capture an actual screenshot from the test pages and compare the image to the screenshot created by html2canvas and calculate the percentage differences. These tests generally aren't expected to provide 100% matches, but while committing changes, these should generally not go decrease from the baseline values.
Start by downloading the dependencies:
$ npm install
Run tests:
$ npm test
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.
Recommend
-
74
使用html2canvas将网页保存为图片
-
54
我们可以直接在浏览器端使用html2canvas,对整个或局部页面进行‘截图’。但这并不是真的截图,而是通过遍历页面DOM结构,收集所有元素信息及相应样式,渲染出canvas image。 由于html2canvas只能将它能处理的生成canvas image,因...
-
63
Join GitHub today GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
-
18
nikersify/pico: Take browser screenshots in Javascript 📸 Join Gi...
-
11
结合 html2canvas 和 jsPDF 实现 html 页面转 pdf By :授客 QQ
-
16
html2canvas截图另存图片模糊问题解决办法发布于 10 月 17 日在使用html2canvas.js发现一个问题,当你截图区域有背景图片的情况下:html2canvas对如下写法
-
9
-
4
JELLY | html2canvas裁剪bug修复及渲染原理解析html2canvas裁剪bug修复及渲染原理解析上传日期:2022.03.24html2canvas是一个非常流行的前端开源工具,在GitHub上已有25k+的stars。在项目开发中,我发现了它在渲染某些DOM时...
-
12
MrLeiDeSen's Blog html2canvas 坑2022-05-07 21:38:30 · MrLeiDeSen
-
9
Generating PDF Signatures With Google Fonts And html2canvas In JavaScript By Ben Nadel on May 7, 2022 The oth...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK