javascript代码实例教程-Javascript摸拟自由落体与上抛运动 说明!

发布时间:2019-01-23 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-Javascript摸拟自由落体与上抛运动 说明!脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。 JavaScript 代码

 

复制代码

//****************************************

        //名称:Javascript摸拟自由落体与上抛运动!

        //作者:Gloot

        //邮箱:glootz@gmail.COM 

        //  QQ:345268267 

        //网站:https://www.cnblogs.com/edITor/ 

        //操作:在页面不同地方点几下

        //***************************************

        VAR $ = function(el) { return document.getElementById(el); };

        function LuoRun()

        {

            this.h = 0;

            this.s = 0;

            this.g = 9.8;

            this.isup = false;

            this.rh = 0;

            this.t = 0;

            this.timer = null;

            this.mt = 0;

            this.top = 0;

            this.left = 0;    

            this.id = null;    

        }

        

        LuoRun.PRototyPE.Po = function(obj) {

            this.left += 0.3;

            obj.style.left = (this.left)+'px';

            

            if (!this.isup) {

                if (this.t == 0)

                {

                    this.top = obj.offsetTop;

                    this.h = 570 - 22 - this.top;

                    this.mt = Math.sqrt(2*this.h/(this.g*100));

                    //alert(mt+' '+isup+' '+t)

                }

                

                this.t+=0.01;

                

                if (this.t >= this.mt)

                {

                    this.t = this.mt;

                    this.rh = (1/2)*this.g*this.t*this.t*100;

                    this.s = this.g*this.t*100;

                    obj.style.top = (this.rh+this.top)+'px';

                    //t = 0;

                    this.s = this.s-50>0 ? this.s-50 : 0;

                    this.isup = true;

                    this.t = 0;

                }

                else

                {

                    this.rh = (1/2)*this.g*this.t*this.t*100;

                    this.s = this.g*this.t*100;

                    

                    obj.style.top = (this.rh+this.top)+'px';

                }

            } else { //up

                //return;

                

                if (this.s == 0) {

                    clearInterval(this.timer);

                    this.id.parentNode.removeChild(this.id);

                    return;

                }

                

                if (this.t == 0) {

                    this.h = this.s*this.s/(2*this.g*100);

                    this.mt = this.s/(this.g*100);

                    this.top = obj.offsetTop;

                    //alert(mt+' '+isup+' '+t)

                }

                

                this.t+=0.01;

                if (this.t>=this.mt) {

                    this.t = this.mt;

                    

                    this.rh = this.s*this.t - (1/2)*this.g*this.t*this.t*100;

                    obj.style.top = (this.top - this.rh)+'px';

                    this.s = 0;

                    this.isup = false;

                    this.t = 0;

                }else {

                    this.rh = this.s*this.t - (1/2)*this.g*this.t*this.t*100;

                    

                    obj.style.top = (this.top - this.rh)+'px';

                }

            }

        }

        

        LuoRun.prototype.Go = function(obj) {

            var self = this;

            if (obj == null)

                obj = $('box');

            self.timer = setInterval(function() {

                

                self.Po(obj);

                

                if (self.h<=0) {

                    clearInterval(self.timer);

                    self.id.parentNode.removeChild(self.id);

                }

            },10);

        }

        

        document.onmousedown = function(e) {

            e = e?e:window.event;

            

            var crtDiv = document.createElement('p');

            crtDiv.style.position = 'absolute';

            crtDiv.style.left = e.clientX + 'px';

            crtDiv.style.top = e.clientY + 'px';

            crtDiv.style.background = '#333';

            crtDiv.style.width = '22px';

            crtDiv.style.height = '22px';

            

            document.body.appendChild(crtDiv);

            

            crtDiv.innerHTML = '&amp;nbsp;';

            var C = new LuoRun();

            C.left = e.clientX;

            C.id = crtDiv;

            document.onmouseup = function() {

                document.onmouSEMove = null;

                window.setTimeout(function() { C.Go(crtDiv); },1000);

            }

        }

复制代码

Css 样式

 

<style type="text/css">

        td,body {font-Size:12px;}

        .css1 {width:240px;display:table;position:absolute;left:20px;top:20px;border:1px solid green;background:#CAF4BD;line-height:18px;padding:3px;}

        .css2 {width:900px;height:22px;border-top:1px solid #333;position:absolute;top:570px;left:60px;}

</style>

Body HtML代码

 

复制代码

<body>

    <form id="form1">

    

    <p class="css1">

       名称:Javascript摸拟自由落体与上抛运动!<br />

       作者:Gloot<br />

       邮箱:glootz@gmail.com <br />

         QQ:345268267 <br />

       网站:https://www.cnbLOGs.com/editor/ <br />

       操作:在页面不同地方点几下

    </p>

    

    <p id="line" class="css2">&nbsp;</p>

    </form>

</body>

复制代码

代码说明

 

复制代码

function JsFunc() {

    this.a = "";

    this.b = "";

}    

 

JsFunc.prototype.method = function() {

    var me = this;

    me.a = ";method";

}

 

function init() {

    var func = new JsFunc();

    func.method();

}

复制代码

JsFunc 类当于一个(C#中的)类; 

 

var func = new JsFunc();

 

相当于初始化了一个类,创建了一个对象;

 

this.a, this.b 相当于 类中的成员;

 

JsFunc.prototype.method 相当于创建这个类下的一个方法函数;

 

如果这个JsFunc 多次 new 操作的话,其下 this成员,将各自的运行操作,互不影响

 

所以当 对 JsFunc new 后创建一个新对象时,对这对象的成员或方法进行 setTimeout, setInterval 操作话,就会产生类似于并行操作的效果;

 

复制代码

function LuoRun()

        {

            this.h = 0; 

            this.s = 0;

            this.g = 9.8;

            this.isup = false;

            this.rh = 0;

            this.t = 0;

            this.timer = null;

            this.mt = 0;

            this.top = 0;

            this.left = 0;    

            this.id = null;    

        }

复制代码

this.s 表示速度;

 

this.h 表示设定的高度, 或物体上抛的最高高度;

 

this.isup 表示正处于上升还是下降状态;

 

this.rn 表示下降距当前顶的位移,或上抛距离初始速度位置的位移;

 

this.t  下降或上抛的时间;

 

this.mt 表示从某一高度落体至某一低点所用的时间,或以某一初始速度上抛至零速度所用的时间;

 

this.timer 表示定时器

 

this.top, this.left 表示物体相对于容器顶部及左边的当前偏移;

 

this.id 表示当前创建方块的对象id值;

 

LuoRun.prototype.Po = function(obj) {

 

}

表示物休自由落体及上抛运动的方法; 

 

this.left += 0.3; 表示物体每落体或上抛向左跳动的偏移量(像素);

 

Po 方法是在定时器 setInterval 下抛行的一个动作,每次执行时都会根据配置偏移量以及自由落体及上抛相关公式计算当前参数值变化,并设定当前物体的位置;

 

obj.style.left = (this.left)+'px'; 初始化当前步骤的 左偏移;

 

落体状态 

 

if (!this.isup) {...} 表示是否是落体状态;

 

复制代码

if (this.t == 0)

{

         this.top = obj.offsetTop;

         this.h = 570 - 22 - this.top;

         this.mt = Math.sqrt(2*this.h/(this.g*100));

         //alert(mt+' '+isup+' '+t)

}

复制代码

当时间为 0 时,表示当前处于落体的最顶点,记录当前距顶部的偏移值,设定落体的高度,以及计算此高度落体所用的时间;

 

复制代码

 if (this.t >= this.mt)

{

        this.t = this.mt;

        this.rh = (1/2)*this.g*this.t*this.t*100;

        this.s = this.g*this.t*100;

        obj.style.top = (this.rh+this.top)+'px';

        //t = 0;

         this.s = this.s-50>0 ? this.s-50 : 0;

         this.isup = true;

         this.t = 0;

}

复制代码

当落体所用时间,大于 this.mt 的最大时间时,将时间设置为 this.mt 的落体总时间;

 

this.rh 根据公式 1/2gt2 得出的位移值,会等于 this.h 的值,或接近于 this.h 的高度值;

 

this.s 根据 速度在加速度随时间变化的公式计算出 当前的速度,也即最大带度,这也是初始的上抛速度;

 

this.s = this.s-50>0 ? this.s-50 : 0;

这个 50 即为阻尼系数,即每次上抛所受阻力所减的速度值;

 

this.isup 设置 true; 表示进入上抛状态;

 

obj.style.top = (this.rh+this.top)+'px'; 设置物体本步骤落体的当前位置;

 

上抛运行

 

if (this.t == 0) {

     this.h = this.s*this.s/(2*this.g*100);

     this.mt = this.s/(this.g*100);

     this.top = obj.offsetTop;

     //alert(mt+' '+isup+' '+t)

}

当时间为 0 时,表示处于上抛开始点,计算 按落体后的速度及公式: v2/(2g) 上升的最大高度 this.h; 最大上升时间 this.mt; 保存当前距顶部的偏移 this.top;

 

复制代码

 if (this.t>=this.mt) {

        this.t = this.mt;

                    

        this.rh = this.s*this.t - (1/2)*this.g*this.t*this.t*100;

        obj.style.top = (this.top - this.rh)+'px';

        this.s = 0;

        this.isup = false;

        this.t = 0;

}

复制代码

当时间 this.t 大于 this.mt 这个最大上抛时间时,将时间设置为 this.mt;

 

this.rh 表示上抛的高度; 公式: vt - (1/2)gt2 ;

 

重置 this.t及this.s 时间与速度,并将 this.isup 置为 false,开始落体动作;

 

复制代码

LuoRun.prototype.Go = function(obj) {

            var self = this;

            if (obj == null)

                obj = $('box');

            self.timer = setInterval(function() {

                

                self.Po(obj);

                

                if (self.h<=0) {

                    clearInterval(self.timer);

                    self.id.parentNode.removeChild(self.id);

                }

            },10);

}

复制代码

Go 是个定时器,10 毫秒执行一次物体偏移移动操作;

 

当 this.h 小于等于 0 时,清除物体,该对象方块一个落体与上抛过程结束;

 

复制代码

document.onmousedown = function(e) {

            e = e?e:window.event;

            

            var crtDiv = document.createElement('p');

            crtDiv.style.position = 'absolute';

            crtDiv.style.left = e.clientX + 'px';

            crtDiv.style.top = e.clientY + 'px';

            crtDiv.style.background = '#333';

            crtDiv.style.width = '22px';

            crtDiv.style.height = '22px';

            

            document.body.appendChild(crtDiv);

            

            crtDiv.innerHTML = '&nbsp;';

            var C = new LuoRun();

            C.left = e.clientX;

            C.id = crtDiv;

            document.onmouseup = function() {

                document.onmousemove = null;

                window.setTimeout(function() { C.Go(crtDiv);                          },1000);

            }

        }

复制代码

当鼠标点击页面时,就创建一个灰黑底,高 22 像素的方块;

 

并初始化 (创建新对象) LuoRun 类;

 

当鼠标松开后,过一秒钟执行 LuoRun的 Go 定时器,开始表现物体的落体与上抛过程;

觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的javascript代码实例教程-Javascript摸拟自由落体与上抛运动 说明!全部内容,希望文章能够帮你解决javascript代码实例教程-Javascript摸拟自由落体与上抛运动 说明!所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。