用H5的canvas做出弹幕效果


当前第2页 返回上一页

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

65

66

//html<div style="position:relative;width:500px;height:400px;text-align:center;">

    <video controls="controls" autoplay="autoplay" style="width:100%;height:100%;">

        <source src="http://www.w3school.com.cn/i/movie.ogg" type="video/ogg" />

        <source src="http://www.w3school.com.cn/i/movie.mp4" type="video/mp4" />

        Your browser does not support the video tag.

    </video>

    <canvas id="canvas" width="500" height="400" style="position:absolute;top:0;left:0;">

        您的浏览器不支持canvas标签。

    </canvas>

</div>//js(function () {    class Barrage {

        constructor(canvas) {

            this.canvas = document.getElementById(canvas);

            let rect = this.canvas.getBoundingClientRect();

            this.w = rect.right - rect.left;

            this.h = rect.bottom - rect.top;

            this.ctx = this.canvas.getContext('2d');

            this.ctx.font = '20px Microsoft YaHei';

            this.barrageList = [];

        }        //添加弹幕列表

        shoot(value) {

            let top = this.getTop();

            let color = this.getColor();

            let offset = this.getOffset();

            let width = Math.ceil(this.ctx.measureText(value).width);

            let barrage = {

                value: value,

                top: top,

                left: this.w,

                color: color,

                offset: offset,

                width: width

            }

            this.barrageList.push(barrage);

        }        //开始绘制

        draw() {            if (this.barrageList.length) {

                this.ctx.clearRect(0, 0, this.w, this.h);                for (let i = 0; i < this.barrageList.length; i++) {

                    let b = this.barrageList[i];                    if (b.left + b.width <= 0) {

                        this.barrageList.splice(i, 1);

                        i--;                        continue;

                    }

                    b.left -= b.offset;

                    this.drawText(b);

                }

            }

            requestAnimationFrame(this.draw.bind(this));

        }        //绘制文字

        drawText(barrage) {

            this.ctx.fillStyle = barrage.color;

            this.ctx.fillText(barrage.value, barrage.left, barrage.top);

        }        //获取随机颜色

        getColor() {            return '#' + Math.floor(Math.random() * 0xffffff).toString(16);

        }        //获取随机top

        getTop() {            //canvas绘制文字x,y坐标是按文字左下角计算,预留30px

            return Math.floor(Math.random() * (this.h - 30)) + 30;

        }        //获取偏移量

        getOffset() {            return +(Math.random() * 4).toFixed(1) + 1;

        }

    }

    let barrage = new Barrage('canvas');

    barrage.draw();    const textList = ['弹幕', '666', '233333333',

        'javascript', 'html', 'css', '前端框架', 'Vue', 'React',        'Angular','测试弹幕效果'

    ];

    textList.forEach((t) => {

        barrage.shoot(t);

    })

})();

1.gif

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

前端开发中的SVG动画

canvas怎样做出黑色背景带特效碎屑烟花

以上就是用H5的canvas做出弹幕效果的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

详细介绍html5实现3d迷宫的代码案例

h5中文件上传的详细介绍

详细分析html5 formdata对象的使用

zip压缩和解压技术在html5中的应用的代码案例(图)

javascript canvas方法有哪些

如何用canvas绘制矩形?canvas画矩形的两种方法介绍

html5标准学习-编码详解

html5的头部head的详解

html5比flash好在哪

关于html5中自定义属性的介绍

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




打赏

取消

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

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

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

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

评论

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