当前位置: 移动技术网 > IT编程>网页制作>Html5 > 移动端Html5页面生成图片解决方案

移动端Html5页面生成图片解决方案

2019年07月25日  | 移动技术网IT编程  | 我要评论

现在有很多微信公众号运营活动,都有生成图片的需求,生成图片后可以发送给好友和发到朋友圈扩散,利于产品的宣传!

1.生成图片可以用canvas,但是由于已经有了html2canvas这个开源库,所以为了节省时间就没有自己写了

github地址:

少啰嗦,先看东西!!!

/**
     * 根据window.devicepixelratio获取像素比
     */
    function dpr() {
        if (window.devicepixelratio && window.devicepixelratio > 1) {
            return window.devicepixelratio;
        }
        return 1;
    }
    /**
     *  将传入值转为整数
     */
    function parsevalue(value) {
        return parseint(value, 10);
    };
    /**
     * 绘制canvas
     */
    async function drawcanvas (selector) {
        // 获取想要转换的 dom 节点
        const dom = document.queryselector(selector);
        const box = window.getcomputedstyle(dom);
        // dom 节点计算后宽高
        const width = parsevalue(box.width);
        const height = parsevalue(box.height);
        // 获取像素比
        const scaleby = dpr();
        // 创建自定义 canvas 元素
        var canvas = document.createelement('canvas');
        // 设定 canvas 元素属性宽高为 dom 节点宽高 * 像素比
        canvas.width = width * scaleby;
        canvas.height = height * scaleby;
        // 设定 canvas css宽高为 dom 节点宽高
        canvas.style.width = `${width}px`;
        canvas.style.height = `${height}px`;

        // 获取画笔
        const context = canvas.getcontext('2d');

        // 将所有绘制内容放大像素比倍
        context.scale(scaleby, scaleby);

        let x = width;
        let y = height;
        return await html2canvas(dom, {canvas}).then(function () {
            convertcanvastoimage(canvas, x ,y)
        })
    }

    /**
     * 图片转base64格式
     */
    function convertcanvastoimage(canvas, x, y) {
        let image = new image();
        let _container = document.getelementsbyclassname('container')[0];
        let _body = document.getelementsbytagname('body')[0];
        image.width = x;
        image.height = y;
        image.src = canvas.todataurl("image/png");
        _body.removechild(_container);
        document.body.appendchild(image);
        return image;
    }
    drawcanvas('.container')

2.由于现在的手机都是高清屏,所以如果你不做处理就会出现模糊的情况,为什么会出现模糊的情况?这个就涉及到设备像素比 devicepixelratio js 提供了 window.devicepixelratio 可以获取设备像素比

function dpr() {
        if (window.devicepixelratio && window.devicepixelratio > 1) {
            return window.devicepixelratio;
        }
        return 1;
    }

这个dpr函数就是获取设备的像素比, 那获取像素比之后要做什么呢?

var canvas = document.createelement('canvas');
        // 设定 canvas 元素属性宽高为 dom 节点宽高 * 像素比
        canvas.width = width * scaleby;
        canvas.height = height * scaleby;
        // 设定 canvas css宽高为 dom 节点宽高
        canvas.style.width = `${width}px`;
        canvas.style.height = `${height}px`;

        // 获取画笔
        const context = canvas.getcontext('2d');

        // 将所有绘制内容放大像素比倍
        context.scale(scaleby, scaleby);

3.获取设备像素比之后将canavs.width 和 canvas.height 去乘以设备像素比 也就是 scaleby; 这个时候在去设置canvas.style.width 和 canvas.style.height 为dom的宽和高。想想为什么要这么写?最后在绘制的饿时候将所绘制的内容放大像素比倍

举个例子iphone6s是设备宽高是375 x 667 ,6s的 window.devicepixelratio = 物理像素 / dips(2=750/375)所以设计师一般给你的设计稿是不是都是750*1334的?所以如果按照一比一去绘制在高清屏下就会模糊,看图说话6s dpr=2

6plus dpr=3

4.最后调用canvas.todataurl("image/png");赋值给image.src,由于微信里面无法保存图片,所以只能生成图片文件,调用微信自带的长按保存到图片到相册功能,如图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网