设为首页收藏本站
12
返回列表 发新帖
鲜花(3) 鸡蛋(0)
 楼主| 发表于 2015-4-8 19:09 | 显示全部楼层
鲜花(3) 鸡蛋(0)
 楼主| 发表于 2015-4-8 21:19 | 显示全部楼层
[javascript] view plaincopyprint?
var Car=function()//构建车的对象  
{     this.color="red";    
      this.price=100000;    
      this.length=2.5;    
      this.speed=80;   //车的一些属性    
      this.x=0;    
      this.updatePos=function()  //更新车位置的方法    
      {    this.x+=this.speed;    
      }  
}  


  
构建好对象之后就可以构建对象了,var mycar=new Car();  

setInterval():这是javascript中的定时器函数,有两个参数,前面是待执行的代码,后面一个是间隔时间。但是有几点需要注意的问题。

1:假设有一个函数GetPos()需要用定时器执行,可以setInterval(GetPos,1000)或者setInterval("GetPos()",1000) 如果写成setInterval(GetPos,1000)一般只会执行一次这个函数,我曾经犯过这个错误。

2:在setInterval()中尽量使用全局变量,因为它重复调用,局部变量容易出错。

好了,罗嗦了一会儿,我想大家应该都知道这些的。

这个gif图片中包括了星星对象和文字(后面讲述)对象。对于绘制星星,应该充分利用五角星的对称性。在HTML5 canvas对象中,大家最好要灵活使用坐标变换,我把主要的坐标变换函数说一下:

save() //保存当前画布状态  
restore()回复画布状态  
translate(x,y) //将画布中心坐标平移至x,y  
rotate(angle) //将画布旋转angle角度  
transform 和setTransform是对画布矩形进行变换的,比较难用
现在构建星星对象,星星在天空中需要哪些属性呢? 一般有坐标、亮度、大小、倾斜角度等,我定义的如下

   this.x = -1;  
    this.y = -1;  //表示横纵坐标  
    this.style = "";  
    this.r = -1;  
    this.scale = 1; //表示缩放倍数  
    this.angle = 0; //旋转的角度  
    this.angle1 = 0; //辅助参数

其次需要绘制星星的方法,前面说过需要利用星星的对称性,因此实际上只需要绘制五分之一,再利用旋转就可以绘制全部的图形了。星星需要填充颜色,HTML5中可以对路径进行填充,因此可以使用路径beginPath()和closePath(),但是最后记住使用fill()函数填充的。

例如我绘制五分之一图形函数:

 this.drawPartStar = function () //部分  
    {  
        cxt.save();  
        cxt.beginPath();  
        cxt.lineCap = "round"; cxt.lineWidth = 5;  
        cxt.fillStyle = this.style;  
        cxt.translate(this.x, this.y); //位移  
        cxt.rotate(this.angle1);  
        //cxt.globalAlpha = this.alpha; //设置透明度  
        cxt.moveTo(0, 0);  
        var xx = 0 - this.r * Math.sin(36 / 180 * 3.14);  
        var yy = 0 - this.r * Math.cos(36 / 180 * 3.14);  
        cxt.lineTo(xx, yy);  
        xx = 0;  
        yy = 0 - this.r * Math.cos(36 / 180 * 3.14) - this.r * Math.sin(36 / 180 * 3.14) * Math.tan(72 / 180 * 3.14);  
        cxt.lineTo(xx, yy);  
        xx = this.r * Math.sin(36 / 180 * 3.14);  
        yy = 0 - this.r * Math.cos(36 / 180 * 3.14);  
        cxt.lineTo(xx, yy);  
        cxt.lineTo(0, 0);  
        cxt.closePath();  
        cxt.fill();  
        cxt.restore();  
    }

然后利用画布旋转就可以得到整个的图形了

this.drawStar = function () //绘制完整的花  
    {  
        for (var i = 0; i <= 4; i++) {  
            this.angle1 = i * 72 / 180 * 3.14 + this.angle;  
            this.drawPartStar();  
        }  
    }

关于星星的坐标,半径大小,倾斜角度都可以使用随机函数实现,利用实现m到n之间的随机数:

var x=(n-m)*Math.random()+m;

通过合理控制m,n就可以得到需要的值了。


好了,到这里星星对象创建过程结束,其次需要构建这些星星。 由于星星数目多,可以使用数组对象,每一个数组对象都是一个星星对象:

 var stars = new Array();   //创建星星对象  
    var starCount = 40; //星星的数目,默认是40   
  
for (var m = 0; m < starCount; m++) //  
    {  
        var s = new star(cxt);  
        s.init();stars[m] = s;  
    }

最后使用定时器,使星星具有闪烁效果:

 /***************演示闪烁星星的函数**********************/  
    function playStars() //演示星星  
    {  
        for (var n = 0; n < starCount; n++)  
       {  
        stars[n].getColor();  
        stars[n].drawStar();  
        }  
    }
setInterval("playStars()",500);

这样,闪烁的星星就全部做完了。

以上就是我说的利用对象构建闪烁星星的过程。大家可以看到利用对象的简便和独立性,希望还不熟悉的朋友可以熟悉这种方法。


最后把star.js中关于星星对象的全部代码写在这里供大家参考

/******star.js******/  
/****以下是星星的对象**********************************/  
var star = function (cxt) //定义星的对象  
{  
    this.x = -1;  
    this.y = -1;  //表示横纵坐标  
    this.style = "";  
    this.r = -1;  
    this.scale = 1; //表示缩放倍数  
    this.angle = 0; //旋转的角度  
    this.angle1 = 0; //辅助参数  
    this.getPos = function () //获取随机坐标  
    {  
        var xx = 20 + 1200 * Math.random();  
        var yy = 20 + 250 * Math.random();  
        this.x = Math.ceil(xx); this.y = Math.ceil(yy); //获取了随机坐标  
    }  
    this.getAngle = function () //得到随机的角度  
    {  
        this.angle = Math.random() * Math.PI;  
    }  
    this.getR = function () //获取半径  
    {  
        var i = 1 + 4 * Math.random();  
        this.r = Math.ceil(i); //获取随机半径  
    }  
    this.getColor = function () //获取随机颜色  
    {  
        var n = Math.random();  
        if (n < 0.5)  
            this.style = "white";  
        else  
            this.style = "#BBAAB1"; //灰白色  
  
    }  
    this.drawPartStar = function () //部分  
    {  
        cxt.save();  
        cxt.beginPath();  
        cxt.lineCap = "round"; cxt.lineWidth = 5;  
        cxt.fillStyle = this.style;  
        cxt.translate(this.x, this.y); //位移  
        cxt.rotate(this.angle1);  
        //cxt.globalAlpha = this.alpha; //设置透明度  
        cxt.moveTo(0, 0);  
        var xx = 0 - this.r * Math.sin(36 / 180 * 3.14);  
        var yy = 0 - this.r * Math.cos(36 / 180 * 3.14);  
        cxt.lineTo(xx, yy);  
        xx = 0;  
        yy = 0 - this.r * Math.cos(36 / 180 * 3.14) - this.r * Math.sin(36 / 180 * 3.14) * Math.tan(72 / 180 * 3.14);  
        cxt.lineTo(xx, yy);  
        xx = this.r * Math.sin(36 / 180 * 3.14);  
        yy = 0 - this.r * Math.cos(36 / 180 * 3.14);  
        cxt.lineTo(xx, yy);  
        cxt.lineTo(0, 0);  
        cxt.closePath();  
        cxt.fill();  
        cxt.restore();  
    }  
    this.drawStar = function () //绘制完整的花  
    {  
        for (var i = 0; i <= 4; i++) {  
            this.angle1 = i * 72 / 180 * 3.14 + this.angle;  
            this.drawPartStar();  
        }  
    }  
    this.init = function ()  //初始化函数  
    {  
        this.getPos();  
        this.getR();  
        this.getColor();  
        this.getAngle();  
    }  
}  
/*****************以上是星星的对象**************************/

鲜花(3) 鸡蛋(0)
 楼主| 发表于 2015-4-8 21:25 | 显示全部楼层
本帖最后由 笑看红尘 于 2015-4-8 21:31 编辑

鲜花(3) 鸡蛋(0)
 楼主| 发表于 2015-4-8 21:27 | 显示全部楼层
本帖最后由 笑看红尘 于 2015-4-8 21:30 编辑

鲜花(3) 鸡蛋(0)
 楼主| 发表于 2015-4-8 21:28 | 显示全部楼层
本帖最后由 笑看红尘 于 2015-4-8 22:02 编辑

鲜花(3) 鸡蛋(0)
 楼主| 发表于 2015-4-8 21:29 | 显示全部楼层
本帖最后由 笑看红尘 于 2015-4-8 22:01 编辑

http://home.202030.com/qq1584801171
鲜花(3) 鸡蛋(0)
 楼主| 发表于 2015-4-8 21:37 | 显示全部楼层
本帖最后由 笑看红尘 于 2015-4-8 22:04 编辑

http://apps.202030.com/1/
鲜花(3) 鸡蛋(0)
 楼主| 发表于 2015-4-8 22:04 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则


GMT+8, 2024-4-30 08:00 , Processed in 0.045108 second(s), 16 queries , Gzip On, Redis On.


快速回复 返回顶部 返回列表