JavaScript模拟重力状态下抛物运动的方法
本文实例讲述了JavaScript模拟重力状态下抛物运动的方法。分享给大家供大家参考。具体分析如下:
这段JavaScript代码模拟重力状态下的抛物运动,可设置以下参数:横向初速度、纵向初速度、重力加速度(如果这个加速度是一个随时间变化的值,就能达到其他非匀加速运动的效果了)、动画间隔时间等,相对专业
<!doctypehtml> <html> <head> <title>js抛物运动</title> <metacharset="utf-8"/> <styletype="text/css"> *{padding:0;margin:0;} body{font-size:13px;padding:10px;} p{margin:2px;} .wrap{position:relative;width:1000px;height:550px;margin:0auto;border:1pxsolid#ccc;margin-top:50px;} #fall{width:20px;font-size:1px;height:20px;background:#000;position:absolute;top:0;left:0;} </style> </head> <body> <h3>模拟重力状态下的抛物运动(假使1px==1mm)</h3> <p>横向初速度:<inputid="Vx"type="text"value="2"/>px/ms</p> <p>纵向初速度:<inputid="Vy"type="text"value="-2"/>px/ms</p> <p>重力加速度:<inputid="a"type="text"value="0.0098"/>px/平方ms</p> <p>(如果这个加速度是一个随时间变化的值,就能达到其他非匀加速运动的效果了。)</p> <p>单位时间:<inputid="t"type="text"value="10"/>(记录运动的时间间隔) <p><inputtype="button"value="演示"onclick="demo(document.getElementById('Vx').value,document.getElementById('Vy').value,document.getElementById('a').value,document.getElementById('t').value)"/></p> <divclass="wrap"> <divid="fall">o</div> </div> </body> <scripttype="text/javascript"> functiondemo(x,y,a,t){ varf=document.getElementById('fall'); varVx=parseInt(x), Vy=parseInt(y), g=a, t=parseInt(t), h=0,l=0,Sx=0,Sy=0; vari=setInterval(function(){ if(f){ Sx+=Vx*t; l=Sx; Vy+=g*t; h+=Vy*t; f.style.left=l+'px'; f.style.top=h+'px'; if(h>500||l>900)clearInterval(i); } },t); } </script> </html>
希望本文所述对大家的javascript程序设计有所帮助。