usingMaths.com
Demonstrating and showing pupils and students one application of Mathematics.







<< PreviousNext >>

Code to Animate an Image Body through a Sine or Cosine Curve in JavaScript



How to Simulate Sinusoidal Curves in JavaScript

Periodic functions produce an infinite order of sinusoidal curves.
The sine function has the general form y = asinθ;
and the cosine function has the general form y = acosθ + c;
where θ is angle in radians and a is an arbitrary constant that heightens the curve.



Code to Animate a Graphic Object through a Periodic Curve in JavaScript

To make a graphic (dot) travel by the equation of a sine / cosine function, continuously increment θ by some angle interval (in radians), and use the equation to get the corresponding y value; using any c that gives a satisfactory height.

Create a new file; On Notepad++: File, New.
Call it periodic_path.html.
Type out the adjoining JavaScript code for animating an image body through the path of a sine / cosine curve.



Note: To get a cosine curve animation, just replace sin with cos.









<< PreviousNext >>