前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >搞来两端模拟自由落体与抛物运动的JS玩

搞来两端模拟自由落体与抛物运动的JS玩

作者头像
练小习
发布2017-12-29 11:49:09
8950
发布2017-12-29 11:49:09
举报
文章被收录于专栏:练小习的专栏练小习的专栏

这里有更详细的讲解呵呵

http://www.cnblogs.com/cloudgamer/archive/2009/01/06/Tween.html

自由落体

<p>横向系数:<input id="hp" type="text" value="0.1" /> (0-1)</p>

<p>纵向系数:<input id="vp" type="text" value="0.6" /> (0-1)</p>

<p>速度:<input id="sp" type="text" value="30" />(20-1000)

<input type="button" value="demo" onclick="demo(document.getElementById('hp').value, document.getElementById('vp').value, document.getElementById('sp').value)" /></p>

<div style="margin-left:600px;">

<div id="fall" style="width:30px; height:30px; background:#f90; position:relative; right:0px; bottom:40px"></div>

</div>

<script type="text/javascript">

var i = null;

function demo(hp,vp,sp) {

var f=document.getElementById('fall');

f.style.right='0px';

f.style.bottom='40px';

i&&clearInterval(i);

var h=1,v=1,hp=(hp>0&&hp<1)?hp:0.2,vp=(vp>0&&vp<1)?vp:0.5,sp=(sp>20 || sp<1000)?sp:30;

i=setInterval(function(){

if(f){

var r=parseInt(f.style.right)+h,b=parseInt(f.style.bottom)-v;

f.style.right=r+'px';

f.style.bottom=b+'px';

if(r>1000)clearInterval(i);

if(b>-210){

v+=2

} else {

h=(v>0)?v*hp:0;

v*=(v>0)?-1*vp:0

}

}

},sp);

}

</script>

提示:你可以先修改部分代码再运行。

抛物运动

<!doctype html>

<html>

<head>

<meta charset="utf-8" />

<style type="text/css">

*{padding:0;margin:0;}

body{font-size:13px;padding:10px;}

p{margin:2px;}

.wrap{position:relative;width:1000px;height:550px;margin:0 auto;border:1px solid #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>横向初速度:<input id="Vx" type="text" value="2" />px/ms</p>

<p>纵向初速度:<input id="Vy" type="text" value="-2" />px/ms</p>

<p>重力加速度:<input id="a" type="text" value="0.0098" />px/平方ms</p>

<p>(如果这个加速度是一个随时间变化的值,就能达到其他非匀加速运动的效果了。)</p>

<p>单位时间:<input id="t" type="text" value="10" />(记录运动的时间间隔)

<p><input type="button" value="演示" onclick="demo(document.getElementById('Vx').value, document.getElementById('Vy').value, document.getElementById('a').value, document.getElementById('t').value)"/></p>

<body>

<div class="wrap">

<div id="fall">o</div>

</div>

</body>

<script type="text/javascript">

function demo(x,y,a,t) {

var f=document.getElementById('fall');

var Vx=parseInt(x),

Vy=parseInt(y),

g=a,

t=parseInt(t),

h=0,l=0,Sx=0,Sy=0;

var i=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>

提示:你可以先修改部分代码再运行。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档