码迷,mamicode.com
首页 > Web开发 > 详细

HTML5制作满天星

时间:2014-11-23 20:18:54      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:html5   canvas   

 1、了解canvas
 
<canvas id="stars" height="600"></canvas>
 
这是画布
2、设置body背景色
 
<style type="text/css">
    body{
        background-colorblack;
    }
</style>
 
3、初始化画布及context
 
 context作为全局变量
var context;
 
function init(){
    //canvas
    var stars = document.getElementById("stars");
    windowWidth = window.innerWidth;
    stars.width=windowWidth;
    //context
    context = stars.getContext("2d");
}
 
4、定义星星对象
 
//
var Star = function (){
    this.x = -1;//
    this.y = -1;//
    this.text="*";//
    this.font="italic bold 24px serif";
    this.color = "white";//
    //
    this.getPos=function(){
        var xx = windowWidth * Math.random(); //不要超出边界
        var yy = 600 * Math.random(); //不要超出边界
        this.x = Math.floor(xx);
        this.y = Math.floor(yy);
    }
    //色,四种不同亮度的星星
    this.getColor=function(){
        var _r = Math.random();
        if(_r<0.2){
            this.color = "#0000FF";
        }else if(_r<0.5){
            this.color = "white";
        }else if(_r<0.8){
            this.color = "#989898";
        }else{
            this.color = "#B8B8B8";
        }
    }
    //fontSize,最大是15个像素,最小3个像素
    this.getFont = function(){
        var _r = Math.random()*12+3;
        this.font = "italic bold "+Math.ceil(_r)+"px serif"; 
    }
    //
    this.init=function(){
        this.getPos();
        this.getColor();
        this.getFont();
    }
    //
    this.draw=function(){
        context.fillStyle=this.color;
        context.font=this.font;
        context.fillText(this.text,this.x,this.y);
    }
    
}
 
5、画月亮
//
function drawMoon(){
    var moon = new Image();
  moon.src = "../img/moon.png"
  context.drawImage(moon,10,10);
}
 
6、在onload事件中绘制星星及月亮
 
var arr = new Array();
var starCount = 1000;
window.onload = function() {
    init();
    //
    for (var i=0;i<starCount;i++) {
        var star = new Star();
        star.init();
        star.draw();
        arr.push(star);
    }
    drawMoon();
}
 
之所以要用数组,是因为等下我们要让星星闪起来

7、星星闪起来
 
//
function playStars(){
    for (var n = 0; n < starCount; n++){  
        arr[n].getColor();  //
        arr[n].draw();      //
    }  
    setTimeout("playStars()",500);//
}
 

在onload事件的回调函数最后一行加入调playStars的代码

效果:

bubuko.com,布布扣


全部代码:

 
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <script>
            var arr = new Array();
            var starCount = 1000;
            var context;
            //context
            function init(){
                //canvas
                var stars = document.getElementById("stars");
                windowWidth = window.innerWidth;
                stars.width=windowWidth;
                //context
                context = stars.getContext("2d");
            }
            //
            var Star = function (){
                this.x = -1;//
                this.y = -1;//
                this.text="*";//
                this.font="italic bold 24px serif";
                this.color = "white";//
                //
                this.getPos=function(){
                    var xx = windowWidth * Math.random(); 
                    var yy = 600 * Math.random(); 
                    this.x = Math.floor(xx);
                    this.y = Math.floor(yy);
                }
                //
                this.getColor=function(){
                    var _r = Math.random();
                    if(_r<0.2){
                        this.color = "#0000FF";
                    }else if(_r<0.5){
                        this.color = "white";
                    }else if(_r<0.8){
                        this.color = "#989898";
                    }else{
                        this.color = "#B8B8B8";
                    }
                }
                //fontSize
                this.getFont = function(){
                    var _r = Math.random()*12+3;
                    this.font = "italic bold "+Math.ceil(_r)+"px serif"; 
                }
                //
                this.init=function(){
                    this.getPos();
                    this.getColor();
                    this.getFont();
                }
                //
                this.draw=function(){
                    context.fillStyle=this.color;
                    context.font=this.font;
                    context.fillText(this.text,this.x,this.y);
                }
                
            }
            window.onload = function() {
                init();
                //
                for (var i=0;i<starCount;i++) {
                    var star = new Star();
                    star.init();
                    star.draw();
                    arr.push(star);
                }
                drawMoon();
                playStars();
            }
            //
            function drawMoon(){
                 var moon = new Image();
  moon.src = "../img/moon.png"
  context.drawImage(moon,10,10);
            }
            //
            function playStars(){
                for (var n = 0; n < starCount; n++){  
                    arr[n].getColor();  
                    arr[n].draw();  
                }  
                setTimeout("playStars()",500);
            }
        </script>
        <style type="text/css">
            body{
                background-colorblack;
            }
        </style>
    </head>
    <body>
        <canvas id="stars" height="600"></canvas>
    </body>
</html>
 
原创,转载请注明出处 

HTML5制作满天星

标签:html5   canvas   

原文地址:http://blog.csdn.net/zhengwei223/article/details/41415085

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!