JavaScript Spring Oscillator Super Compact version fully conforms to the energy conservation, Hooke's theorem _javascript skills

Source: Internet
Author: User
Tags compact
The last edition uses the physics thought, this time uses the mathematics thought, if you have downloaded the first edition the code can obviously feel the mathematics powerful!!!!!
Here is the spring of the JS code:
Copy Code code as follows:

<script type= "Text/javascript" >
This spring begin/////////////////////
var x=0.1//x axis Increment
var t=0;//x axis initial position
var m=200; Original amplitude multiple
The attenuation number of amplitude multiples of Var a=0.7;//
var d= ' R '//direction of motion
var o=0;//element
var l=0;//location
var ti=30;
var S=false;
var Hr=false;

function R () {
Hr=true;
if (S)
{
Return
}
o.style.left= l+ (Math.sin (T)) *m+ "px";//Get Spring motion speed
t=t+x;//x axis Growth
m-=a;//Unit Time Decay
if (m<=0)//amplitude is zero
{
S=true;
return;//exit
}
SetTimeout ("R ()", TI);/callback
}
This spring over///////////////////////////
function I ()
{
if (HR) {
Return
}
S=false;
O=document.getelementbyid ("div");
X=parsefloat (document.getElementById ("X"). Value);
M=parsefloat (document.getElementById ("M"). Value);
A=parsefloat (document.getElementById ("A"). Value);
Ti=parsefloat (document.getElementById ("TI"). Value);
L=document.getelementbyid ("div"). offsetleft;
R ();
}
function TS ()
{
S=true;
}
function B ()
{
if (S)
{
T=0
Hr=false;
S=false;
X=parsefloat (document.getElementById ("X"). Value);
M=parsefloat (document.getElementById ("M"). Value);
A=parsefloat (document.getElementById ("A"). Value);
document.getElementById ("div"). style.left=l+ "px";
}
}

</script>

Here is the HTML in the page (no use, for demo)
Copy Code code as follows:

<div id= "P" style= "width:500px;height:400px;" >
<input style= "width:50px" type= "text" id= "X" value= "0.1"/>x axis increment <br/><input style= "width:50px" type= " Text "id=" M "value="/> Original amplitude multiples <br/>
<input style= "width:50px" type= "text" id= "A" value= "0.7"/> Amplitude multiplier attenuation <br/>
<input style= "width:50px" type= "text" id= "TI" value= "/>" Run time interval (ms) <br/>
<input style= "width:80px" type= "button" onclick= "TS ()" value= "Stop"/> Please activate <br/><input style= "width : 80px "type=" button "onclick=" B () "value=" New activation/>
</div><div style= "Position:absolute;" ><div id= "div" onclick= "I ()" Value= settings after setting point me! "style=" Position:absolute; top:-264px; left:211px; width:50px; height:50px; Background-color: #FF0; " ></div></div>

This is much better than the last code! 100% Original!
Reprint http://www.cnblogs.com/NONE/
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.