移动端H5页面实现生成图片的代码


本文摘自PHP中文网,作者不言,侵删。

本篇文章给大家带来的内容是关于移动端H5页面实现生成图片的代码,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

/**

 * 根据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 可以获取设备像素比

1

2

3

4

5

6

function DPR() {

        if (window.devicePixelRatio && window.devicePixelRatio > 1) {

            return window.devicePixelRatio;

        }

        return 1;

    }

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

1

2

3

4

5

6

7

8

9

10

11

12

13

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的宽和高。想想为什么要这么写?最后在绘制的饿时候将所绘制的内容放大像素比倍

阅读剩余部分

相关阅读 >>

text/javascript有什么用

HTML5移动应用现状谈发展趋势的详细介绍

聊一聊javascript 中的 url 对象

HTML5 input新增type属性color颜色拾取器的实例代码

HTML5中设置或返回音视频是否在加载后即开始播放的属性autoplay

h5的之sse服务器发送事件eventsource详解

分享几款炫酷的HTML5实现的图片特效

javascript怎么把字符转数组

HTML5本地存储storage实例详解

javascript怎么改变css

更多相关阅读请进入《HTML5》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...