展示效果:
按钮:

点击生成后:

html代码:(注意引用或下载需要的js)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>海报2实例</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
</head>
<body>
<style type="text/css">
.sheng{margin: auto;width: 100%;text-align: center;}
.sheng span{font-size: 14px;width: 80px;height: 30px;line-height: 30px;background: #ff6600;color: #ffffff;display: inline-block;margin-top: 100px;}
.img-responsive{ image-rendering: -moz-crisp-edges;
image-rendering: -o-crisp-edges;
image-rendering: -webkit-optimize-contrast;
image-rendering: crisp-edges;
-ms-interpolation-mode: nearest-neighbor; }
</style>
<div><span onclick="createPoster();">生成海报</span></div>
<!--排版默认隐藏-->
<div id="poster-container" style="display: none;">
<div style="background:#0b6cbc;padding-top: 20px;overflow: hidden;">
<div style="text-align: center;padding: 10px 0;">
<img id="images" src="https://img.dtmuban.com/202111/28/161459321.png.thumb.png" width="300" style="display: inline-block;" />
</div>
<h3 style="text-align: center;color: #fff;margin-bottom: 25px;">我真诚向您推荐</h3>
<div style="background: #fff;padding: 20px;border-top-left-radius: 5px;border-top-right-radius:5px;overflow: hidden;">
<div><button type="button" class="btn btn-danger btn-sm">分享</button><span style="color: #999;margin-left: 15px;">2022-04-19</span></div>
<h4 style="margin-top: 20px;text-align:left;">这里是标题信息标题信息标题信息标题信息标题信息</h4>
<div style="padding-top: 5px;">
<p>这里是介绍信息介绍信息介绍信息介绍信息介绍信息介绍信息</p>
</div>
<hr>
<div>
<div class="col-sm-4 col-xs-3" title="网站LOGO">
<img src="https://m.mbkfw.com/apple-touch-icon-precomposed.png" width="80" height="80">
</div>
<div class="col-sm-4 col-xs-5">
<p>长按识别二维码<br>获取更多内容</p>
</div>
<div class="col-sm-4 col-xs-3">
<div title="二维码区"></div>
</div>
</div>
</div>
</div>
</div>
<!--生成图片后显示-->
<div style="position:fixed;left:0px;top:0;background:#0b6cbc;width:100%; height:100%;display:none;z-Index:999;">
<div style="text-align:center;padding-top:0px;">
<div class="pull-right close-img" style="margin-top: 20px;color: #ffffff;font-size: 20px;"><i class="glyphicon glyphicon-remove"></i></div>
<img id="posterImg" style="width:100%;display:inline-block;" />
</div>
</div>
<script src="https://libs.baidu.com/jquery/1.7.0/jquery.min.js"></script>
<script src="http://www.jq22.com/js/jquery.qrcode.min.js"></script>
<script src="http://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
//生成二维码
$('.poster-qrcode').qrcode({
mode: 4,
mSize: 20,
//label: '文字Logo',//使用文字logo必须去掉mode,mSize参数
image: $(".poster-qrcode-logo")[0],
text: window.location.href,
size: 1080/3,
render: 'canvas',
width : 80,
height : 80
});
});
// 生成海报
function createPoster() {
$(".article").hide();
$(".toimg").show();
$("#poster-container").show();
$(".sheng").show();
var dom = $('.poster-container'); //内容区域
var width = $('.toimg').width(); //获取dom 宽度
var height = $('.poster-container-div').height(); //获取dom 高度
//var height2 = $('.text-responsive').height(); //获取文字区域 高度
var canvas = document.createElement("canvas"); //创建一个canvas节点
var scale = 2; //定义任意放大倍数 支持小数
canvas.width = width * scale; //定义canvas 宽度 * 缩放
canvas.height = height * scale; //定义canvas高度 *缩放
//当前滚动条的位置
var scrollY = $(document).scrollTop();
var scrollX = $(document).scrollLeft();
var el = document.querySelector(".poster-container");
console.log(typeof dom[0]);
html2canvas(dom[0], {
useCORS: true, //跨域
allowTaint: false,
canvas: canvas, //自定义 canvas
logging: false,
letterRendering: true,
taintTest: true, //在渲染前测试图片
dpi: window.devicePixelRatio, // window.devicePixelRatio是设备像素比
background: "#0b6cbc",
width: width,
height: height,
scale: scale, // 添加的scale 参数
scrollY: -scrollY,scrollX: -scrollX
}).then(function(canvas) {
//canvas.getContext("2d").scale(scale,scale); //获取context,设置scale
var imgUrl = canvas.toDataURL('image/jpeg');
document.getElementById('posterImg').setAttribute('src', imgUrl);
$("#poster-container").hide();
$(".sheng").hide();
$(".article").show();
})
}
$(".close-img").click(function(){
$(".toimg").hide();
$(".sheng").show();
})
</script>
</body>
</html>























18605917465
客服QQ