html2canvas将HTML页面转为图片并保存

因为将图片设置为背景,需要往框里填入动态二维码,但因手机像素大小不同导致兼容性很差,所以就使用图片加文字加二维码的形式直接生成图片。

使用插件:html2canvas.js

1.首先写好界面布局
css:

 * {
            margin: 0px;
            padding: 0px;
        }

        .container {
            display: flex;
            flex-direction: column;
        }
        .header{
            width:100%;
        }

        .img {
            width: 100%;
        }

        .body {
            display: flex;
            flex-direction: row;

            justify-content: space-around;

        }

        .item1 {
            flex-grow: 2;
            word-break: break-all;
            margin-top: 5px;
        }

        .item2 {
            flex-grow: 1;
            text-align: center;
            margin-top: 10px;
            margin-right: 10px;
        }

        .title {
            margin-bottom: 10px;
        }

        .titleHeader {
            background: rgb(243, 140, 5);
            color: white;
            padding: 3px;
            border-radius: 5px;
            font-weight: bold;
            font-size: 15px;
        }

        .titleBody {
            font-weight: bold;
            font-size: 15px;
            line-height: 22px;
        }

        .tips {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            margin-bottom: 5px;
        }

        .tipsStyle {
            background: rgb(231, 129, 129);
            padding: 3px;
            border-radius: 5px;
            color: white;
            font-size: 10px;
            margin-left:10px;
        }

html:

 <div class="container">
        <div class="header">
            <img class="img" src="" alt="图片加载中,请稍后">
        </div>
        <div class="body">
            <div class="item1" style="padding: 10px;">
                <div class="title">
                    <span class="titleHeader">职位名称</span> <span class="titleBody"></span>
                </div>
                <div class="title">
                    <span class="titleHeader">综合工资</span> <span class="titleBody"></span>
                </div>
                <div class="title">
                    <span class="titleHeader">职位亮点</span> <span class="titleBody"></span>
                </div>
                <div class="title">
                    <span class="titleHeader">工作地址</span> <span class="titleBody"></span>
                </div>
            </div>
            <div class="item2">
                <div id="container">
                    <canvas width="100" height="100"></canvas>
                </div>
                <p style="font-size:14px;color:rgb(110, 110, 110);">长按识别保存</p>
            </div>
        </div>
        <div class="tips">
            <div class="tipsStyle">实名认证/3免服务/10万授信</div>
        </div>
    </div>
    <img class="imgDownload" src="" style="width:100%;">

布局好了,大概样式是这样的:

可以看见,目前还是Dom元素。
在JS中,我们先将Dom元素转为canvas,然后使用toDataURL()方法转换成base64码图片。

//第一步,我们先生成二维码
$("#container").erweima({
    quiet: 1,
    mode: 0,
    mSize: 20,
    background: "#fff",
    fill: "black",
    radius: 0,
    size: 105,
    text: "XXXXX"
});
//获取需要转化成canvas的dom对象
var saveDom = $(".container")[0];
使用html2canvas转化成canvas
html2canvas(saveDom).then(function (canvas) {
    //将canvas转化成base64图片
    var tempSrc = canvas.toDataURL("image/png");
    //将base64传给img标签
    $(".imgDownload").attr("src", tempSrc);
    //删除Dom节点
    $(".container").remove();
});

因为是在微信端使用,所以长按保存即可打开微信保存图片按钮。
在手机浏览器可以长按保存,也可以写a标签点击下载(微信端限制不可用)。

你可以使用wxml2canvas库将微信小程序页面换为图片。首先,在小程序的代码中引入wxml2canvas库,并在页面的js文件中进行配置和调用。以下是一个示例: 1. 安装wxml2canvas库: ```shell npm install wxml2canvas ``` 2. 在页面的js文件中引入wxml2canvas库: ```javascript import wxml2canvas from 'wxml2canvas'; ``` 3. 在页面的js文件中配置和调用wxml2canvas: ```javascript Page({ // 其他页面代码... // 将页面换为图片 convertToImage() { const query = wx.createSelectorQuery(); query.select('#canvasId').node().exec((res) => { const canvas = res[0].node; const ctx = canvas.getContext('2d'); wxml2canvas.canvasToTempFilePath({ canvas, success: (res) => { const tempFilePath = res.tempFilePath; console.log('功', tempFilePath); // 在此处可以对图片进行保存或分享等操作 }, fail: (err) => { console.error('换失败', err); }, }, this); }); }, // 其他页面代码... }) ``` 4. 在wxml文件中添加一个canvas元素,并设置id为"canvasId": ```html <canvas id="canvasId"></canvas> ``` 在上述代码中,`convertToImage`函数会通过`wx.createSelectorQuery`选择器获取到canvas元素,并调用`wxml2canvas.canvasToTempFilePath`方法将该canvas换为临时文件路径。你可以在`success`回调函数中处理换后的图片,例如进行保存或分享。 请注意,wxml2canvas库的具体用法可能会根据你的项目结构和需求有所不同,上述代码仅作为一个简单示例。你可以根据自己的实际情况进行调整和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值