jindutiao

<!DOCTYPE html>
<html>

    <head>

        <meta charset="UTF-8">

        <title>Canvas + requestAnimationFrame</title>


        <script src="/static/js/canvas.js"></script>

        <link rel="stylesheet" href="/static/css/canvas.css" type="text/css" />

    </head>

    <body>

        <audio src="/static/music/Momoko%20-%20禁绝边境线【日翻】(Cover%20ワルキューレ).mp3" autoplay="autoplay"></audio>

        <canvas></canvas>

    </body>

</html>
body{

  margin: 0px;

  padding: 0px;

  background: black;
}

canvas{

  position: absolute;

  top: 50px;

  left:200px;

}
onload = function() {

    var canvas = document.getElementsByTagName("canvas")[0];

    var ctx = canvas.getContext("2d");

    var particles = [];

    var w = 1000;

    var h = 400;

    canvas.width = w ;

    canvas.height = h;

    function progressbar() {

        this.widths = 0;

        this.hue = 0;

        this.draw = function() {

            ctx.fillStyle = 'hsla(' + this.hue + ', 100%, 50%, 1)';

//          ctx.fillStyle = 'white';

            ctx.fillRect(0, 80, this.widths, 25);

        }
    }

    function particle() {

        this.x =  bar.widths - 1;

        this.y = 82;

        this.g = 1 + Math.random() * 3;

        this.down = false;

        this.draw = function() {

            ctx.fillStyle = 'hsla(' + (bar.hue) + ', 100%, 40%, 1)';

            var size = Math.random() * 5;

            ctx.fillRect(this.x, this.y, size, size);

        }
    }


    function reset() {

        ctx.fillStyle = 'black';

//      ctx.fillRect(0, 0, w, h);

        ctx.clearRect(0, 0, w, h);

    }


    function draw() {

        reset();

        bar.hue += 1;

        bar.widths += 2;

        if(bar.widths > 1000) {

                reset();

                bar.hue = 0;

                bar.widths = 0;

                particles = [];


        } else {

            bar.draw();

                particles.push(new particle());

        }

        update();
    }

    function update() {

        for(var i = 0; i < particles.length; i++) {

            var p = particles[i];

            if(p.down == true) {

                p.g += 0.1;

                p.y += p.g;


            } else {

                if(p.g > 0) {

                    p.y -= p.g;

                    p.g -= 0.1;

                } else {

                    p.down = true;

                    p.g += 0.1;

                    p.y += p.g;

                }
            }

            p.draw();
        }
    }

    function animloop() {

        draw();

        requestAnimationFrame(animloop);
    }

//  http://web.jobbole.com/91017/

    bar = new progressbar();

    animloop();

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值