js实现的牛顿摆效果
牛顿摆是一个1960年代发明的桌面演示装置,五个质量相同的球体由吊绳固定,彼此紧密排列。又叫:牛顿摆球、动量守恒摆球、永动球、物理撞球、碰碰球等。
(function(window,undefined){ window.None||(window.None={}); //重力加速度 varG=9.8; varPI=Math.PI; //帧频 varFPS=48; /* //IE角度转换 functionrorateIt(node,deg){ //取得末变形前矩形的中点 varrect=node.getBoundingClientRect(), cx1=(rect.right-rect.left)/2,//centerx cy1=(rect.bottom-rect.top)/2,//centery deg2rad=Math.PI/180,//角度转弧度 rad=deg*deg2rad, cos=Math.cos(rad), sin=Math.sin(rad); varident="DXImageTransform.Microsoft.Matrix"; node.style.filter="progid:"+ident+"(M11='1.0',sizingmethod='autoexpand')"; //http://www.satzansatz.de/cssd/onhavinglayout.html if(!node.currentStyle.hasLayout){//在IE7中,如果没有获得hasLayout,滤镜会失效 node.style.writingMode="tb-rl"; } varfilter=node.filters.item(ident); //+-------+-------+ //|M11|M12| //+-------+-------+ //|M21|M22| //+-------+-------+ filter.M11=cos; filter.M12=-sin; filter.M21=sin; filter.M22=cos; //取得当前中心 rect=node.getBoundingClientRect(); varcx=(rect.right-rect.left)/2; varcy=(rect.bottom-rect.top)/2; //调整此元素的坐标系,实现CSS3transform-origin的功能 node.style.marginLeft=cx1-cx+"px"; node.style.marginTop=cy1-cy+"px"; } */ //外部函数引用 //是否IE functionisIE(){ returnnavigator.userAgent.indexOf("MSIE")>-1; } //获取当前样式 functionreturnStyle(obj,styleName){ varmyObj=typeofobj=="string"?document.getElementById(obj):obj; if(document.all){ returneval("myObj.currentStyle."+styleName); }else{ returneval("document.defaultView.getComputedStyle(myObj,null)."+styleName); } } //外部函数引用 //图片方法 varimg=function(src){ varimg=newImage(); img.src=src; returnimg; } //方向类,以垂直向下为0度逆时针为正 varface=function(deg,rad){ //0-360 this.unit='deg'; if(rad)deg=180/PI*deg; this.deg=deg; this.rad=PI/180*this.deg; } //矢量类 varvector=function(size,fx){ varcstrct=this.constructor; this.size=size; this.face=fx||newface(0); fx=this.face; this.toHv=function(){ varh=newcstrct(Math.sin(fx.rad)*size,90); varv=newcstrct(Math.cos(fx.rad)*size,0); return[h,v]; } } //继承,obj:需要从矢量继承的对象,arg:arguments vector.extend=function(obj,arg){ vector.apply(obj,arg); } //矢量合并方法 vector.merger=function(arrvector){ if(arguments.length>1)arrvector=arguments; varcstrct=arrvector[0].constructor; vari=0,ilav=arrvector.length; varsum=[0,0]; for(;i<ilav;i++){ varhv=arrvector[i].toHv(); sum[0]+=hv[0].size; sum[1]+=hv[1].size; } varsize=Math.sqrt(sum[0]*sum[0]+sum[1]*sum[1]); varfa=newface(Math.atan(sum[0]/sum[1]),'rad'); returnnewcstrct(size,fa); } //力类,参数为大小和方向 varforce=function(size,face){ this.unit='N'; //继承自矢量 vector.apply(this,arguments); } //加速度类 vara=function(size,face){ this.unit='m/s^2'; vector.extend(this,arguments); } //速度类 varspeed=function(size,face){ this.unit='m/s'; vector.extend(this,arguments); } //刚体类,参数(body:IMG对象,m为质量) varrigid=function(body,m){ //一般情况下body为一个img对象,所以暂且只有正方形或长方形两种形式 this.body=body; this.m=m;//质量 this.focus=(this.bodyinstanceofImage)?point(this.body.width/2,this.body.height/2):point(this.body.style.width/2,this.body.style.height/2); this.axis=point(0,0);//轴心位置(point对象) this.force=newforce(0); this.a=newa(this.force.size/this.m,this.force.face); this.speed=newspeed(0); //设置,可重新设置上面所有参数 this.set=function(prop,value){ this[prop]=value; } this.addForce=function(f){ returnthis.force=vector.merger(f,this.force); } //旋转 this.rotate=function(face,axis){ axis=axis||this.axis||this.focus; //cx1=returnStyle(this.body,"left"); //cy1=returnStyle(this.body,"left"); varrect=this.body.getBoundingClientRect(), //cx1=rect.left+axis.x, //cy1=rect.top+axis.y, cx1=(rect.right-rect.left)/2, cy1=(rect.bottom-rect.top)/2, rad=face.rad, cos=Math.cos(rad), sin=Math.sin(rad); if(isIE()){ varident="DXImageTransform.Microsoft.Matrix"; this.body.style.filter="progid:"+ident+"(M11='1.0',sizingmethod='autoexpand')"; if(!returnStyle(this.body,"hasLayout")){//在IE7中,如果没有获得hasLayout,滤镜会失效 this.body.style.writingMode="tb-rl"; } varfilter=this.body.filters.item(ident); filter.M11=cos; filter.M12=-sin; filter.M21=sin; filter.M22=cos; } rect=this.body.getBoundingClientRect(); //varcx=rect.left+axis.x; //varcy=rect.top+axis.y; varcx=(rect.right-rect.left)/2; varcy=(rect.bottom-rect.top)/2; //调整此元素的坐标系,实现CSS3transform-origin的功能 this.body.style.left=parseInt(this.body.style.left,10)+cx1-cx+"px"; this.body.style.top=parseInt(this.body.style.top,10)+cy1-cy+"px"; //CSS3 varsdeg="rotate("+face.deg+"deg)"; varpaxis=axis.x+"px"+axis.y+"px"; this.body.style.transformOrigin=paxis; this.body.style.MozTransformOrigin=paxis; this.body.style.WebkitTransformOrigin=paxis; this.body.style.OTransformOrigin=paxis; this.body.style.WebkitTransform=sdeg; this.body.style.MozTransform=sdeg; this.body.style.OTransform=sdeg; this.body.style.transform=sdeg; } } //刚体组合 rigid.merger=function(){ } //无弹性绳子类 varrope=function(body,length,maxForce){ this.body=body; this.length=length; this.maxForce=maxForce||Infinity||Number.MAX_VALUE; } //组合体类 varcomb=function(arrObject){ } //单摆类 varpendulum=function(ripe,rigid){ } //类单摆 varlikePend=function(rigid,fa,time){//刚体,初始角度,摆动频率 varself=this; this.rigid=rigid; this.body=this.rigid.body; this.axis=this.rigid.axis; this.dom=this.rigid.dom; this.m=this.rigid.m; this.rad=fa?fa.rad:fa=newface(PI/6,'rad'); //摆动 //角度30 varfx=fa.rad; //角度平方根 varm=Math.sqrt(fx); //摆动时间1秒 varatime=time||0.5; varfnum=FPS*atime; //单位增量 varfm=m/fnum; //i:角度平方根到0 vari=-m,tid; varg=newa(G); vart=1000/FPS; //o:{s:1,f:30,t:0,fun:callback}:s:(-1,1)速度增加还是减小,f:从多少度,t:到多少度,fun:摆完后运行的函数 this.swing=function(o,fun){ /*varasps=g.size*Math.cos(PI/2-fx)/(24*24*self.m); varfss=newforce(asps,newface(fx-PI/2)); this.rigid.force=fss; vara=this.rigid.a; //单位时间内速度的增量 vara1=Math.acos((asps+this.rigid.speed.size)/2) this.rigid.speed=newspeed(a1,newface(fx-PI/2)); a2=a1*2;*/ //{s:-1,inc:1} o=o||{s:1}; vary=-o.s*i*i+fx*o.s; if(i>=m){ self.rigid.rotate(newface(0),self.axis); clearTimeout(tid); i=-m; if(fun)fun(); return; } varf=newface(y,'rad'); self.rigid.rotate(f,axis=self.axis); i+=fm; tid=setTimeout(function(){self.swing.call(self,o,fun)},t); } this.moveTo=function(p){ self.body.style.left=p.x+"px"; self.body.style.top=p.y+"px"; } } //世界 None.world=function(param){ //param:{force:[多个force对象]} this.param=param||{}; this.config={ //全局外力 g:newa(G), className:'', width:'100%', height:'100%', left:0, top:-200, arrNav:['about','myWork','site','other','myTools'], imgW:60, imgN:5, sDeg:5, hitSound:'sounds/hit.wav', vol:0.1 } this.init(); }; None.world.prototype={ //初始化 dom:{}, init:function(){ varc=this.config; varp=this.param; //dom vardom=document.createElement("div"); dom.className=c.className; dom.style.position="absolute"; dom.style.width=p.width||c.width; dom.style.height=p.height||c.height; dom.style.left=(p.left||c.left)+"px"; dom.style.top=(p.top||c.top)+"px"; this.dom=dom; document.body.appendChild(this.dom); }, //添加一个刚体 addRigid:function(rigid,p){ if(!(rigidinstanceofArray)){ rigid=[rigid]; } if(!p)p=[point(0,0)]; if(!(pinstanceofArray))p=[p]; for(vari=0,rl=rigid.length;i<rl;i++){ p[i]=p[i]||p[0]; rigid[i].body.style.position="absolute"; rigid[i].body.style.left=p[i].x+"px"; rigid[i].body.style.top=p[i].y+"px"; this.dom.appendChild(rigid[i].body); } }, //添加一个外作用力 addForce:function(){ }, start:function(){ varp=this.param, c=this.config; varshit=p.hitSound||c.hitSound; varsndHit=newNone.sounds(shit); sndHit.volume=p.vol||c.vol; varw=document.documentElement.offsetWidth; varimgWidth=p.imgW||c.imgW;//图片宽度 varimgNum=p.imgN||c.imgN;//图片数量 varjd=p.sDeg||c.sDeg;//最大角度 vares=p.arrNav||c.arrNav; vartime=p.time||c.time; varbasex=w/2-imgWidth*imgNum/2; varjiaodu=newface(jd); varrig=[],ball=[],pend=[],axis=point(imgWidth/2,0); for(vari=0,el=es.length;i<el;i++){ rig[i]=document.getElementById(es[i]); ball[i]=newrigid(rig[i],1); ball[i].set("axis",axis); pend[i]=newlikePend(ball[i],jiaodu,time); } this.addRigid(pend); resize(); if(!isIE())swing(); window.onresize=resize; functionhit(v){ sndHit.play(); } functionswing(){ varrunfun=arguments.callee; varlast=pend.length-1; if(p.hitSound)hit(0); pend[0].swing({s:1},function(){ if(p.hitSound)hit(1); pend[last].swing({s:-1},runfun); }); } functionresize(){ varw=document.documentElement.offsetWidth; varbasex=w/2-imgWidth*imgNum/2; varn=pend.length; for(vari=0;i<n;i++){ pend[i].moveTo(point(basex+imgWidth*i,0)); } } } }; })(window);
以上所述就是本文的全部内容了,希望大家能够喜欢。能够对大家学习javascript有所帮助。