Document address:https://html2canvas.hertzen.com/
安装:
npm install --save html2canvas
或者
yarn add html2canvas
使用:
html2canvas(document.getElementById('screenshot'), {
// 配置参数 文档:[https://html2canvas.hertzen.com/configuration](https://html2canvas.hertzen.com/configuration)
}).then((canvas) => {
// 创建图片容器
var imgElement = new Image();
imgElement.src = canvas.toDataURL();
console.log(canvas.toDataURL());
});
Problem summary
-
When converting a page containing an input box to html2canvas, the text in the input box is not vertically centered:
Change the version to 1.0.0! This happens when using version 1.4.1; -
If the intercepted node is a long image, the entire page will be captured when the page slides down:
(1) The scroll bar is on top. Before starting the screenshot, the scroll bar is on top, and then the screenshot is completed. Restore scroll bars. Applicable to version 1.0.0// 置顶滚动条 let oldScrollTop = window.scrollY || document.body.scrollTop; document.documentElement.scrollTop = document.body.scrollTop = 0; // 设置滚动条原来位置 document.documentElement.scrollTop = document.body.scrollTop = oldScrollTop;
(2) Add configuration parameters. Version 1.0.0 is invalid
html2canvas(document.getElementById('screenshot'), { height: document.getElementById('screenshot').scrollHeight, windowHeight: document.getElementById('screenshot').scrollHeight }).then(canvas => { //在此处拿到tempImg.src就是生成的base64图片,再进行后续业务逻辑处理 })
-
Only capture the current window page:
在长节点外层包裹一层盒子设置盒子属性:`
width: 100%;
height: 100%;
overflow: auto;
将盒子设为截取DOM对象