-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpendulum-simple.js
62 lines (57 loc) · 1.97 KB
/
pendulum-simple.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function simple_pendulum() {
var canvas = document.getElementById("simple-pendulum");
var context = canvas.getContext("2d");
function rk4(dt, theta, dottheta) {
var
a = [dottheta , -Math.sin(theta )].map(x => x * dt),
b = [dottheta + a[1]/2, -Math.sin(theta + a[0]/2)].map(x => x * dt),
c = [dottheta + b[1]/2, -Math.sin(theta + b[0]/2)].map(x => x * dt),
d = [dottheta + c[1] , -Math.sin(theta + b[0] )].map(x => x * dt);
return [
(a[0] + 2*b[0] + 2*c[0] + d[0])/6,
(a[1] + 2*b[1] + 2*c[1] + d[1])/6
];
}
var fps = 60, average_fps = fps;
var fps_sum = 60, count = 1;
function draw(theta) {
context.save();
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = "grey";
context.fillText(
fps.toPrecision(3) +
" FPS (average " +
(
fps > 1000 ?
average_fps :
(average_fps = (fps_sum += Math.min(fps, 120))/++count)
).toPrecision(3) + ")",
0, 20
);
context.translate(canvas.width/2, 10);
context.rotate(theta);
context.scale(20, 20);
context.beginPath();
context.strokeStyle = "green";
context.moveTo(0, 0);
context.lineTo(0, 10);
context.stroke();
context.beginPath();
context.strokeStyle = "red";
context.arc(0, 10, 2, 0, Math.PI*2);
context.stroke();
context.restore();
}
var theta = -Math.PI/3, dottheta = 0, then = Date.now()/1000;
(function animate() {
var now = Date.now()/1000;
var dt = Math.min(0.1, now - then);
fps = 1/dt;
var diff = rk4(dt, theta, dottheta);
then = now;
draw(theta);
theta += diff[0];
dottheta += diff[1];
window.requestAnimationFrame(animate);
})();
}