-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrav.html
More file actions
229 lines (184 loc) · 4.99 KB
/
grav.html
File metadata and controls
229 lines (184 loc) · 4.99 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<html>
<head>
<style type="text/css">
body {
background-color: black;
margin:0;
width:100%;
height:100%;
}
#main {
width:100%;
height:100%;
}
</style>
</head>
<body>
<canvas id="main" width="1500" height="1500"></canvas>
<script type="text/javascript">
var canvas = document.getElementById("main");
var ctx = canvas.getContext("2d");
var orbs = [];
function omd(e) {
canvasBCR = canvas.getBoundingClientRect();
var cx,cy;
if(e.type == 'mousedown') {
cx = e.clientX;
cy = e.clientY;
}
else {
cx = e.touches[0].clientX;
cy = e.touches[0].clientY;
}
var x = 1500.0*(cx-canvasBCR.left)/(canvasBCR.right-canvasBCR.left);
var y = 1500.0*(cy-canvasBCR.top)/(canvasBCR.bottom-canvasBCR.top);
orbs.push(new Orb(x,y, 0, 30, 100));
}
canvas.addEventListener("mousedown",omd);
//canvas.addEventListener("touchstart",omd);
function Orb(x, y, dx, dy, mass) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
this.mass = mass;
this.path = [];
this.radius = Math.sqrt(this.mass);
}
var orbFrameCount = 0;
var fpsUpdateRate = 500; // ms
var lastFpsUpdate = 0;
var physicsUpdateRate = 1000/120.0;
var lastPhysicsUpdate = 0;
var physicsFrameCount = 0;
var time = new Date().getTime();
function ran(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var G = 0.5;
var trailcount = 200;
function updateOrbs(dt) {
var dtsec = dt/1000.0;
for(var i in orbs) {
var orb1 = orbs[i];
for(var j in orbs) {
if(i === j) continue;
var orb2 = orbs[j];
var dvec = { // distance vector between orb1 and orb2
x: orb2.x-orb1.x,
y: orb2.y-orb1.y
};
// distance between orb1 and orb2 squared
var r = Math.sqrt(Math.pow(dvec.x, 2) + Math.pow(dvec.y, 2));
// acceleration on orb1
var a = (G * orb2.mass)/(r);
var ndvec = { // dvec normalised
x: dvec.x/r,
y: dvec.y/r
};
orb1.dx += ndvec.x * a;
orb1.dy += ndvec.y * a;
}
orb1.path.push({x:orb1.x, y:orb1.y});
if(orb1.path.length > trailcount) orb1.path.shift();
}
for(var k in orbs) {
var orb = orbs[k];
orb.x += orb.dx * dtsec;
orb.y += orb.dy * dtsec;
if(orb.x > 1500) orb.x = 0;
if(orb.x < 0) orb.x = 1500;
if(orb.y > 1500) orb.y = 0;
if(orb.y < 0) orb.y = 1500;
}
physicsFrameCount++;
}
function renderOrbs() {
clearOrbs();
for(var i in orbs) {
var orb = orbs[i];
drawOrb(orb);
}
orbFrameCount++;
function drawOrb(orb) {
orb.lastX = orb.x;
orb.lastY = orb.y;
for(var i = 0; i < orb.path.length; i+=2) {
var ghost = orb.path[i];
var size = tailfn(orb, i);
if(size > 0.4)
drawCircle(ghost.x, ghost.y, size, "rgba(102, 153, 204, 1)");
}
drawCircle(orb.x, orb.y, orb.radius, "#FFFFFF");
}
}
function tailfn(orb, i) {
return (i/trailcount)*orb.radius;
}
function clearOrbs() {
for(var i in orbs) {
var orb = orbs[i];
clearOrb(orb);
}
function clearOrb(orb) {
var clearPadding = 2;
drawCircle(orb.lastX, orb.lastY, orb.radius + 2, "#000000");
var minx = 10000,
maxx = 0,
miny = 10000,
maxy = 0;
for(var i in orb.path) {
var ghost = orb.path[i];
var r = orb.radius;
if(ghost.x-r < minx) minx = ghost.x-r;
if(ghost.x+r > maxx) maxx = ghost.x+r;
if(ghost.y-r < miny) miny = ghost.y-r;
if(ghost.y+r > maxy) maxy = ghost.y+r;
}
ctx.fillStyle = "#000000";
var buffer = 2;
ctx.fillRect(minx - buffer, miny - buffer, maxx-minx + 2*buffer, maxy-miny + 2*buffer);
}
}
function renderFps() {
clearFps();
ctx.font="20px Georgia";
ctx.fillStyle = "#FFFFFF";
ctx.fillText((orbFrameCount/(fpsUpdateRate/1000.0)) + " drawfps",1250,25);
ctx.fillText((physicsFrameCount/(fpsUpdateRate/1000.0)) + " updatefps",1250,50);
orbFrameCount = 0;
physicsFrameCount = 0;
function clearFps() {
ctx.fillStyle = "#000000";
ctx.fillRect(1250,7,140,50);
}
}
function drawCircle(x, y, r, c) {
ctx.fillStyle = c;
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI*2, true);
ctx.closePath();
ctx.fill();
}
(function loop() {
window.webkitRequestAnimationFrame(loop);
var now = new Date().getTime();
var dt = now - time;
if(dt > 20) dt = 20; // update slow if running slow
// updating
//if((now-lastPhysicsUpdate) > physicsUpdateRate) {
updateOrbs(dt);
//lastPhysicsUpdate = now;
//}
// rendering
renderOrbs();
//if((now - lastFpsUpdate) > fpsUpdateRate) {
//renderFps();
//lastFpsUpdate = now;
//}
time = now;
//setTimeout(loop, 0);
})();
</script>
</body>
</html>