代码改变世界

速度向量之角速度

2011-12-12 13:09  LoujaDy  阅读(1497)  评论(0编辑  收藏  举报

角度速度是指物体设着某一个角动进行运动

 

源码

//获取到舞台
            var canvas=document.getElementById("stage");
            //舞台2d绘图接口
            var context=canvas.getContext("2d");
            //获取中心点置
            var centerX=canvas.width/2;
            var centerY=canvas.height/2;
            
            //当前度
            var angle=45;
            //角速度
            var angleSpeed=1;
            
            //将中心点作为0,0点坐标
            context.translate(centerX, centerY);
            
           /**
            * 球对象
            
*/
           var Ball=function(x,y,radius,color)
           {
                //球的中心点位置
                this.x=x;
                this.y=y;
                //球的半径
                this.radius=radius;
                //球的颜色
                this.color=color;
                //球的边框颜色
                context.strokeStyle = color;
                //球体颜色
                context.fillStyle=color; 
                /**
                    * 绘制球
                    
*/ 
                this.draw=function()
                {                
                   context.beginPath(); 
                   context.arc(this.x, this.y, this.radius, 0, 2 * Math.PI, false); 
                   context.fill(); 
                   context.stroke();
                   context.closePath();
                }
           } 
 
            
            //在中心点绘制一个球
            var ball=new Ball(0,0,20,"#ff0000");
            ball.draw();
            
            //动画处理
            var drawAsync = eval(Jscex.compile("async", function () {
                        while(true)
                        {                        
                            //清除上一次绘制的球            
                            context.clearRect(ball.x-ball.radius-2,ball.y-ball.radius-2,ball.radius*2+8,ball.radius*2+8);
                            //计算球在当前角度上的位置
                            ball.x+=Math.cos(angle*Math.PI/180)*angleSpeed;
                            ball.y+=Math.sin(angle*Math.PI/180)*angleSpeed;    

                            //计算边界,如果出界了换个角度                    
                            if(ball.x>=centerX || ball.x<=-centerX || ball.y>=centerY|| ball.y<=-centerY)
                            {
                                //出界了
                                ball.x=0;
                                ball.y=0;
                                angle+=45;
                                if(angle>360)
                                {
                                    angle=45;
                                }                                
                
                            }
                            document.getElementById("pInfo").innerText="当前角度:"+angle;
                            ball.draw();
                            $await(Jscex.Async.sleep(frames));                
                            
                        }    
                        
                                                                    
            }));
           drawAsync().start();
            

 作者:Louja

出处:http://loujady.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此声明,且在文章页面给出原文连接,否则保留追究法律责任的权利。