HTML5练习(2)陪你去看流星雨

HTML5练习(2)陪你去看流星雨_第1张图片

在上一篇的基础上
1、新增流星雨对象

 
//
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  =  1 ; //
         this . getRandomColor ( ) ;
         //
         var  x  =  Math . random ( )  *  80  +  150 ;
         this . length  =  Math . ceil ( x ) ;
//                  x = Math.random()*10+30;
         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  ( ) {
         var  a  =  255  *  Math . random ( ) ; 
         a  =  Math . ceil ( a ) ;
         var  a1  =  255  *  Math . random ( ) ; 
         a1  =  Math . ceil ( a1 ) ;
         var  a2  =  255  *  Math . random ( ) ; 
         a2  =  Math . ceil ( a2 ) ;
         //
         this . color1  =  "rgba("  +  a . toString ( )  +  ","  +  a1 . toString ( )  +  ","  +  a2 . toString ( )  +  ",1)" ;
         //
         this . color2  =  "black" ;
     }
      /*********************************/
     this . countPos  =  function  ( ) //
     {
         //,xy
         this . x  =  this . x  -  this . offset_x ;
         this . y  =  this . y  +  this . offset_y ;
     }
     /**********************************/
     this . getPos  =  function  ( )  //
     {
         //200--1200
         var  x  =  Math . random ( )  *  1000  +  400 ;
         this . x  =  Math . ceil ( x ) ;
         x  =  Math . random ( )  *  600 ;
         //600
         this . y  =  Math . ceil ( x ) ;
     }
      /*******************************/
     this . draw  =  function  ( )  //
     {
         context . save ( ) ;
         context . beginPath ( ) ;
         context . lineWidth  =  3 ;
         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.1 ,  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 ) ; 
//                  context.strokeStyle="red";
//                  context.strokeRect(x,y-1,this.offset_x+1,this.offset_y+1);
         //
         this . countPos ( ) ;
         //
         this . alpha  -=  0.002 ;
         //
         this . draw ( ) ; 
     }
    
}
 
该对象已自带绘制自己的方法,主要是设置渐变色,设置线宽,然后画一条线
还有移动的方法,就是做偏移,将偏移后的尾巴清除,再重绘

2、初始化若干个流星
在onload监听中加入:
 
//
var  rains  =  new  Array ( ) ;
var  rainCount  =  20 ;
//onload
//
for  ( var  i = 0 ; i < rainCount ; i ++ )  {
     var  rain  =  new  MeteorRain ( ) ;
     rain . init ( ) ;
     rain . draw ( ) ;
     rains . push ( rain ) ;
}
 
3、流星出现后,要让其移动
 
function  playRains ( ) {
     for  ( var  n  =  0 ;  n  <  rainCount ;  n ++ ) {  
         var  rain  =  rains [ n ] ;
         rain . move ( ) ; //
         if ( rain . y > 600 ) { //
              context . clearRect ( rain . x , rain . y - rain . height , rain . width , rain . height ) ;
              rains [ n ]  =  new  MeteorRain ( ) ;
              rains [ n ] . init ( ) ;
         }
     }  
     setTimeout ( "playRains()" , 50 ) ;
}
 
然后在onload中加入代码:
 
playRains ( ) ;
 
4、添加背景音乐

 
< audio  src = "audio/F4 - .mp3"  autoplay = "true"  loop = "true" > </ audio >
 


收工!!

你可能感兴趣的:(canvas,html5)