<html>
<head>
<title>生成海报图Demotitle>
head>
<body>
<div id="poster" style="width: 400px; height: 300px;overflow: hidden;">
<h1>海报图标题h1>
<img src="https://www.w3school.com.cn/i/html_editor_notepad.gif" style="width:100%;"/>
div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js">script>
<script src="https://cdn.bootcdn.net/ajax/libs/html2canvas/1.4.1/html2canvas.js">script>
<script>
$(function(){
html2canvas(document.getElementById('poster')).then(function(canvas) {
console.log(canvas.toDataURL());
document.body.appendChild(canvas);
});
});
script>
body>
html>
执行效果:
html2canvas 依赖 HTMLCanvasElement,HTMLCanvasElement 存在跨域问题。当 HTMLCanvasElement 跨域时,图片将显示异常。
html在线编辑器:https://www.w3school.com.cn/tiy/t.asp?f=eg_html_basic
html2canvas官网:https://github.com/niklasvh/html2canvas
html2canvas镜像:https://gitee.com/mirrors/html2canvas
Vue使用html2canvas将页面转化为图片
https://html2canvas.hertzen.com/