canvas绘制流星雨

偶然看到用canvas绘制流星雨,好好看呀!

遇上一个人,只需擦肩而过的缘分;喜欢一个人,只需一见钟情的瞬间;爱上一个人只需流星划过的刹那;

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>流星雨</title>
    <style>
        body {
            background-color: black;
        }

        body, html {
            width: 100%;
            height: 100%;
            overflow: hidden;
        }
    </style>
</head>
<body>
    <canvas id="starrySky"></canvas>
</body>
<script>
    var context;
    var rainCount = 20;
    var starCount = 800;
    var arr = new Array();   
    var meteorShower = new Array();

    function init() {
        var starrySky = document.getElementById("starrySky");
        windowWidth = window.innerWidth; //当前的窗口的高度
        starrySky.width = windowWidth;
        starrySky.height = window.innerHeight;
        context = starrySky.getContext("2d");
    }

    var Star = function () {
        this.x = windowWidth * Math.random();//横坐标
        this.y = 5000 * Math.random();//纵坐标
        this.text = ".";//文本
        this.color = "white";//颜色
        this.getColor = function () {
            var ran = Math.random();
            if (ran < 0.5) {
                this.color = "#333";
            } else {
                this.color = "white";
            }
        }
    //初始化
        this.init = function () {
            this.getColor();
        }
    //绘制
        this.draw = function () {
            context.fillStyle = this.color;
            context.fillText(this.text, this.x, this.y);
        }
    }

    function drawMoon() {
        var moon = new Image();
        context.drawImage(moon, -5, -10);
    }
    window.onload = function () {
        init();

        for (var i = 0; i < starCount; i++) {
            var star = new Star();
            star.init();
            star.draw();
            arr.push(star);
        }
    //流星
        for (var i = 0; i < rainCount; i++) {
            var rain = new MeteorRain();
            rain.init();
            rain.draw();
            meteorShower.push(rain);
        }
        drawMoon();//月亮
        playstarrySky();//星星
        playmeteorShower();//流星

    }
    function playstarrySky() {
        for (var n = 0; n < starCount; n++) {
            arr[n].getColor();
            arr[n].draw();
        }

        setTimeout("playstarrySky()", 100);
    }
    var MeteorRain = function () {
        this.x = -1;
        this.y = -1;
        this.length = -1;
        this.angle = 30;
        this.width = -1;
        this.height = -1;
        this.speed = 1;
        this.offset_x = -1;
        this.offset_y = -1;
        this.alpha = 1;
        this.color1 = "";
        this.color2 = ""; 
        this.init = function ()
        {
            this.getPos();
            this.alpha = 0.8//透明度
            this.getRandomColor();
            //最小长度,最大长度
            var x = Math.random() * 80 + 150;
            this.length = Math.ceil(x);
            this.angle = 30; //流星倾斜角
            x = Math.random() + 0.5;
            this.speed = Math.ceil(x); //流星的速度
            var cos = Math.cos(this.angle * 3.14 / 180);
            var sin = Math.sin(this.angle * 3.14 / 180);
            this.width = this.length * cos; //流星所占宽度
            this.height = this.length * sin;//流星所占高度
            this.offset_x = this.speed * cos;
            this.offset_y = this.speed * sin;
        }
        this.getRandomColor = function () {
            this.color1 = "rgba(" + 51 + "," + 102 + "," + 255 + ",1)";
        //结束颜色
            this.color2 = "#0c0c0c";
        }
        this.countPos = function ()//
        {

            this.x = this.x - this.offset_x;
            this.y = this.y + this.offset_y;
        }
        this.getPos = function () //
        {
            this.x = Math.random() * window.innerWidth; //窗口高度
            this.y = Math.random() * window.innerHeight; //窗口宽度
        }
        this.draw = function () //绘制一个流星的函数
        {
            context.save();
            context.beginPath();
            context.lineWidth = 1; //宽度
            context.globalAlpha = this.alpha; //设置透明度
            //创建横向渐变颜色,起点坐标至终点坐标
            var line = context.createLinearGradient(this.x, this.y,
                this.x + this.width,
                this.y - this.height);
            line.addColorStop(0, "white");
            line.addColorStop(0.3, this.color1);
            line.addColorStop(0.6, this.color2);
            context.strokeStyle = line;
            context.moveTo(this.x, this.y);
            context.lineTo(this.x + this.width, this.y - this.height);
            context.closePath();
            context.stroke();
            context.restore();
        }
        this.move = function () {
        //清空流星像素
            var x = this.x + this.width - this.offset_x;
            var y = this.y - this.height;
            context.clearRect(x - 3, y - 3, this.offset_x + 5, this.offset_y + 5);

            this.countPos();
            this.alpha -= 0.002;
            this.draw();
        }
    }

    //流星
    function playmeteorShower() {

        for (var n = 0; n < rainCount; n++) {
            var rain = meteorShower[n];
            rain.move();//移动
            if (rain.y > window.innerHeight) {
                context.clearRect(rain.x, rain.y - rain.height, rain.width, rain.height);
                meteorShower[n] = new MeteorRain();
                meteorShower[n].init();
            }
        }
        setTimeout("playmeteorShower()", 2);
    }
</script>

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值