官方文档
https://html2canvas.hertzen.com/
案例代码
自己创建一个html然后试一下
<!DOCTYPE html>
<html>
<head>
<title>HTML to Image Example</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.3.2/html2canvas.min.js"></script>
</head>
<body>
<div id="content">
<h1>Hello World</h1>
<p>This is a sample content to convert to image.</p>
</div>
<button onclick="convertToImage()">Convert to Image</button>
<script>
function convertToImage() {
html2canvas(document.querySelector("#content")).then(canvas => {
// 创建一个图片元素
var img = new Image();
// 将canvas转换为DataURL格式的图片(png)
img.src = canvas.toDataURL("image/png");
// 将图片添加到body中
document.body.appendChild(img);
});
}
</script>
</body>
</html>