HTML5练习(1)制作满天星

本文介绍如何使用HTML5的canvas元素创建满天星的动态效果。通过onload事件触发,详细代码展示了如何利用canvas绘制星星并实现动画播放。
摘要由CSDN通过智能技术生成
 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" ;
        
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值