Javascript如何實(shí)現(xiàn)摸擬自由落體與上拋運(yùn)動(dòng)的原理與方法

本篇內(nèi)容主要講解“Javascript如何實(shí)現(xiàn)摸擬自由落體與上拋運(yùn)動(dòng)的原理與方法”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Javascript如何實(shí)現(xiàn)摸擬自由落體與上拋運(yùn)動(dòng)的原理與方法”吧!

創(chuàng)新互聯(lián)"三網(wǎng)合一"的企業(yè)建站思路。企業(yè)可建設(shè)擁有電腦版、微信版、手機(jī)版的企業(yè)網(wǎng)站。實(shí)現(xiàn)跨屏營(yíng)銷,產(chǎn)品發(fā)布一步更新,電腦網(wǎng)絡(luò)+移動(dòng)網(wǎng)絡(luò)一網(wǎng)打盡,滿足企業(yè)的營(yíng)銷需求!創(chuàng)新互聯(lián)具備承接各種類型的網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)項(xiàng)目的能力。經(jīng)過(guò)10余年的努力的開拓,為不同行業(yè)的企事業(yè)單位提供了優(yōu)質(zhì)的服務(wù),并獲得了客戶的一致好評(píng)。

Java的特點(diǎn)有哪些

Java的特點(diǎn)有哪些 1.Java語(yǔ)言作為靜態(tài)面向?qū)ο缶幊陶Z(yǔ)言的代表,實(shí)現(xiàn)了面向?qū)ο罄碚摚试S程序員以優(yōu)雅的思維方式進(jìn)行復(fù)雜的編程。 2.Java具有簡(jiǎn)單性、面向?qū)ο?、分布式、安全性、平臺(tái)獨(dú)立與可移植性、動(dòng)態(tài)性等特點(diǎn)。 3.使用Java可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序等。

JavaScript 代碼

//****************************************
    //名稱:Javascript摸擬自由落體與上拋運(yùn)動(dòng)!
    //作者:Gloot
    //郵箱:glootz@gmail.com
    // QQ:345268267
    //網(wǎng)站:http://www.cnblogs.com/editor/
    //操作:在頁(yè)面不同地方點(diǎn)幾下
    //***************************************
    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('div');
      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);
      }
    }

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">

  <div class="css1">
    名稱:Javascript摸擬自由落體與上拋運(yùn)動(dòng)!<br />
    作者:Gloot<br />
    郵箱:glootz@gmail.com <br />
     QQ:345268267 <br />
    網(wǎng)站:http://www.cnblogs.com/editor/ <br />
    操作:在頁(yè)面不同地方點(diǎn)幾下
  </div>

  <div id="line" class="css2">&nbsp;</div>
  </form>
</body>

代碼說(shuō)明

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 類當(dāng)于一個(gè)(C#中的)類;

var func = new JsFunc();

相當(dāng)于初始化了一個(gè)類,創(chuàng)建了一個(gè)對(duì)象;

this.a, this.b 相當(dāng)于 類中的成員;

JsFunc.prototype.method 相當(dāng)于創(chuàng)建這個(gè)類下的一個(gè)方法函數(shù);

如果這個(gè)JsFunc 多次 new 操作的話,其下 this成員,將各自的運(yùn)行操作,互不影響;

所以當(dāng) 對(duì) JsFunc new 后創(chuàng)建一個(gè)新對(duì)象時(shí),對(duì)這對(duì)象的成員或方法進(jìn)行 setTimeout, setInterval 操作話,就會(huì)產(chǎn)生類似于并行操作的效果;

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 表示設(shè)定的高度, 或物體上拋的最高高度;

this.isup 表示正處于上升還是下降狀態(tài);

this.rn 表示下降距當(dāng)前頂?shù)奈灰?,或上拋距離初始速度位置的位移;

this.t  下降或上拋的時(shí)間;

this.mt 表示從某一高度落體至某一低點(diǎn)所用的時(shí)間,或以某一初始速度上拋至零速度所用的時(shí)間;

this.timer 表示定時(shí)器

this.top, this.left 表示物體相對(duì)于容器頂部及左邊的當(dāng)前偏移;

this.id 表示當(dāng)前創(chuàng)建方塊的對(duì)象id值;

LuoRun.prototype.Po = function(obj) {

}

表示物休自由落體及上拋運(yùn)動(dòng)的方法;

this.left += 0.3; 表示物體每落體或上拋向左跳動(dòng)的偏移量(像素);

Po 方法是在定時(shí)器 setInterval 下拋行的一個(gè)動(dòng)作,每次執(zhí)行時(shí)都會(huì)根據(jù)配置偏移量以及自由落體及上拋相關(guān)公式計(jì)算當(dāng)前參數(shù)值變化,并設(shè)定當(dāng)前物體的位置;

obj.style.left = (this.left)+'px'; 初始化當(dāng)前步驟的 左偏移;

落體狀態(tài)

if (!this.isup) {...} 表示是否是落體狀態(tài);

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)
}

當(dāng)時(shí)間為 0 時(shí),表示當(dāng)前處于落體的最頂點(diǎn),記錄當(dāng)前距頂部的偏移值,設(shè)定落體的高度,以及計(jì)算此高度落體所用的時(shí)間;

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;
}

當(dāng)落體所用時(shí)間,大于 this.mt 的最大時(shí)間時(shí),將時(shí)間設(shè)置為 this.mt 的落體總時(shí)間;

this.rh 根據(jù)公式 1/2gt2 得出的位移值,會(huì)等于 this.h 的值,或接近于 this.h 的高度值;

this.s 根據(jù) 速度在加速度隨時(shí)間變化的公式計(jì)算出 當(dāng)前的速度,也即最大帶度,這也是初始的上拋速度;

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

這個(gè) 50 即為阻尼系數(shù),即每次上拋所受阻力所減的速度值;

this.isup 設(shè)置 true; 表示進(jìn)入上拋狀態(tài);

obj.style.top = (this.rh+this.top)+'px'; 設(shè)置物體本步驟落體的當(dāng)前位置;

上拋運(yùn)行

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)
}

當(dāng)時(shí)間為 0 時(shí),表示處于上拋開始點(diǎn),計(jì)算 按落體后的速度及公式: v2/(2g) 上升的最大高度 this.h; 最大上升時(shí)間 this.mt; 保存當(dāng)前距頂部的偏移 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;
}

當(dāng)時(shí)間 this.t 大于 this.mt 這個(gè)最大上拋時(shí)間時(shí),將時(shí)間設(shè)置為 this.mt;

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

重置 this.t及this.s 時(shí)間與速度,并將 this.isup 置為 false,開始落體動(dòng)作;

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 是個(gè)定時(shí)器,10 毫秒執(zhí)行一次物體偏移移動(dòng)操作;

當(dāng) this.h 小于等于 0 時(shí),清除物體,該對(duì)象方塊一個(gè)落體與上拋過(guò)程結(jié)束;

document.onmousedown = function(e) {
      e = e?e:window.event;

      var crtDiv = document.createElement('div');
      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);
      }
    }

當(dāng)鼠標(biāo)點(diǎn)擊頁(yè)面時(shí),就創(chuàng)建一個(gè)灰黑底,寬高 22 像素的方塊;

并初始化 (創(chuàng)建新對(duì)象) LuoRun 類;

當(dāng)鼠標(biāo)松開后,過(guò)一秒鐘執(zhí)行 LuoRun的 Go 定時(shí)器,開始表現(xiàn)物體的落體與上拋過(guò)程;

到此,相信大家對(duì)“Javascript如何實(shí)現(xiàn)摸擬自由落體與上拋運(yùn)動(dòng)的原理與方法”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

當(dāng)前題目:Javascript如何實(shí)現(xiàn)摸擬自由落體與上拋運(yùn)動(dòng)的原理與方法
標(biāo)題來(lái)源:http://muchs.cn/article6/pdphog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google網(wǎng)站改版、全網(wǎng)營(yíng)銷推廣外貿(mào)建站、品牌網(wǎng)站制作商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化