-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
369 lines (309 loc) · 12.6 KB
/
index.html
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<!DOCTYPE html>
<!--
Illustration of Astronomical Parallax inspired by the tutorial found at:
http://physics.weber.edu/schroeder/html5/
Latest code for this web page is available under the MIT licence at:
http://www.https://github.com/crpurcell/AstroParallax
The MIT License (MIT)
Copyright (c) 2017 Cormac R. Purcell
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
-->
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=610">
<title>Astronomical Parallax Simulation</title>
<style>
input[type="range"] {
width: 140px;
padding: 0px; /* remove extra space in IE */
-webkit-user-select: none;
-moz-user-select: -moz-none;
-ms-user-select: none;
user-select: none;
}
input[type="range"]::-ms-tooltip {
display: none; /* hide automatic readout in IE */
}
input[type="range"]::-ms-track {
color: transparent; /* hide tick marks in IE */
}
</style>
</head>
<body style="font-family:sans-serif;
font-size:15px;
width:610px;
margin-left:auto;
margin-right:auto;
background-color: #bababa;">
<h1>Astronomical Parallax</h1>
<p>This simulation allows you to explore the parallax behaviour of a
relatively nearby object seen against a far-field background.</p>
<div style="width:300px; float: left; ">
<img src="parallax_bg.png" width="300" height="400"
style="position:absolute;">
<canvas id="mainCanvas" width="300" height="400"
style="position:relative; z-index: 1;" >
Canvas not supported; please update your browser.
</canvas>
</div>
<div style="width:300px; float: right;">
<img src="star_field.png" width="300" height="300"
style="position:absolute;">
<canvas id="viewCanvas" width="300" height="300"
style="position:relative; z-index: 1;" >
</canvas>
Distance = <span id="distReadout" style="display:inline-block;
width:1.5em; text-align:right;">1.5</span> parsec
<input type="range" id="distSlider" min="0.5" max="2.0" step="0.1"
value="1.5" style="width: 280px;" oninput="showDist();"
onchange="showDist();"><br>
Inclination = <span id="incReadout" style="display:inline-block;
width:2.0em; text-align:right;">7</span> deg
<input type="range" id="incSlider" min="0.0" max="60.0" step="1"
value="7" style="width: 280px;" oninput="showInc();"
onchange="showInc();">
</div>
<!---------------------------------------------------------------------------->
<script>
// Canvas & widget elements
var mainCanvas = document.getElementById("mainCanvas");
var ctx1 = mainCanvas.getContext("2d");
var viewCanvas = document.getElementById("viewCanvas");
var ctx2 = viewCanvas.getContext("2d");
var distSlider = document.getElementById("distSlider");
var distReadout = document.getElementById("distReadout");
var incSlider = document.getElementById("incSlider");
var incReadout = document.getElementById("incReadout");
// Load the image of the Sun
var sunImg = new Image();
sunImg.src = 'sun.png';
// Positions on background image (in pixels unless otherwise stated)
var xE, yE // Earth position
var d_pc, d_pix // Object distance
var xO = 0, yO // Object position
var theta_deg = 0 // Orbit starting angle
var dTheta_deg = 1 // Orbit angle increment
var inc_deg = 7 // Orbit inclination angle
// Variables for 300px scaled image
var R = 60 // Earth orbit radius
var yOMin = 70, yOMax = 230 // Object position limits
var xSun = 150, ySun = 340 // Sun position
var annOff = 60 // Angle annotation offset
// Start the animation
animate();
function distToPix() {
// Convert the distance on the slider to pixel coordinates
d_pc = Number(distSlider.value);
var dMin_pc = Number(distSlider.min);
var dMax_pc = Number(distSlider.max);
tmp = (d_pc - dMin_pc) * (yOMax - yOMin) / (dMax_pc - dMin_pc);
yO = yOMax - tmp;
d_pix = ySun - yO
// Set the inclination angle
inc_deg = Number(incSlider.value);
}
function showDist() {
// Show the distance value in the HTML
distReadout.innerHTML = distSlider.value;
}
function showInc() {
// Show the inclination angle value in the HTML
incReadout.innerHTML = incSlider.value;
}
function drawAll() {
// Update the object distance from the slider
distToPix();
// Earth position relative to top-left
var xE_tl = xSun + xE
var yE_tl = ySun + yE
// Object position relative to top-left
var xO_tl = xSun + xO
var yO_tl = yO
// Projected horizon position, relative to top-left
var xH_tl = xSun - xE * yO / (yE_tl - yO)
// Projected object motion
R1 = R * yO/ d_pix
var theta_rad = theta_deg * Math.PI / 180
var xP = R1 * Math.sin(-theta_rad)
var yP = R1 * Math.cos(theta_rad) * Math.cos(inc_deg * Math.PI / 180)
var xP_tl = viewCanvas.width/2 + xP
var yP_tl = viewCanvas.height/2 + yP
// Clear both canvas for a new frame
ctx1.clearRect(0, 0, mainCanvas.width, mainCanvas.height);
ctx2.clearRect(0, 0, viewCanvas.width, viewCanvas.height);
// Draw the sun and far Earth orbit
ctx1.fillStyle = "orange";
ctx1.font = "14px Verdana";
ctx1.textAlign="center";
ctx1.fillText("The Sun", xSun, ySun + sunImg.height/2)
drawFarEarthOrbit();
drawSun();
// Draw the angle sweep line
ctx1.beginPath();
ctx1.moveTo(xO_tl, yO_tl);
ctx1.lineTo(xH_tl, 0);
ctx1.moveTo(xO_tl, yO_tl);
ctx1.lineTo(xE_tl, yE_tl);
ctx1.setLineDash([7, 3])
ctx1.lineWidth=2.0;
ctx1.strokeStyle = 'grey';
ctx1.stroke();
// Draw the Earth
ctx1.beginPath();
ctx1.arc(xE_tl, yE_tl, 5, 0, 2*Math.PI);
var grad = ctx1.createRadialGradient(xE_tl-1, yE_tl-2, 1,
xE_tl, yE_tl, 5);
grad.addColorStop(0, "#b3bdf8");
grad.addColorStop(1, "blue");
ctx1.fillStyle = grad;
ctx1.fill();
// Draw the foreground object
var xO_tl = xSun + xO
var yO_tl = yO
ctx1.beginPath();
ctx1.arc(xO_tl, yO_tl, 5, 0, 2*Math.PI);
var grad = ctx1.createRadialGradient(xO_tl-1, yO_tl-2, 1,
xO_tl, yO_tl, 5);
grad.addColorStop(0, "white");
grad.addColorStop(1, "green");
ctx1.fillStyle = grad;
ctx1.fill();
// Draw the angle annotation
var ang = Math.atan(R/d_pix)
var parAngle_asec = 1/(d_pc)
var xArcEnd = xO_tl + annOff * Math.sin(ang)
var yArcEnd = yO_tl- annOff * Math.cos(ang)
ctx1.beginPath();
ctx1.setLineDash([5, 5])
ctx1.strokeStyle = "yellow";
ctx1.lineWidth=1.0;
ctx1.moveTo(xO_tl, yO_tl);
ctx1.lineTo(xArcEnd, yArcEnd);
ctx1.moveTo(xO_tl, yO_tl);
ctx1.lineTo(xO_tl, yO_tl-annOff);
ctx1.stroke();
ctx1.beginPath();
ctx1.setLineDash([1, 0])
ctx1.lineWidth=3.0;
ctx1.arc(xO_tl, yO_tl, annOff, -Math.PI/2, +ang-Math.PI/2);
ctx1.stroke();
//ctx1.stroke();
//ctx1.lineTo(xO_tl, yO_tl);
ctx1.fillStyle = "yellow";
ctx1.font = "14px Verdana";
ctx1.textAlign="start";
var annTxt = "Angle = " + parAngle_asec.toFixed(2) + "''"
ctx1.fillText(annTxt, xO_tl+annOff*Math.sin(ang)+ annOff*0.1,
yO_tl-annOff*Math.cos(ang)/2);
// Draw projected orbit
ctx2.beginPath();
ctx2.setLineDash([7, 3])
ctx2.strokeStyle = 'grey';
ctx2.lineWidth=1.5;
ctx2.ellipse(viewCanvas.width/2, viewCanvas.height/2, R1,
R1 * Math.cos(inc_deg * Math.PI / 180), 0, 0, 2*Math.PI)
ctx2.stroke();
// Draw projected angle line
ctx2.beginPath();
ctx2.setLineDash([3, 3])
ctx2.strokeStyle = 'yellow';
ctx2.lineWidth=1.0;
ctx2.moveTo(viewCanvas.width/2, viewCanvas.height/2);
ctx2.lineTo(viewCanvas.width/2+R1, viewCanvas.height/2);
ctx2.stroke();
// Draw view of object
ctx2.beginPath();
ctx2.arc(xP_tl, yP_tl, 5, 0, 2*Math.PI);
var grad = ctx1.createRadialGradient(xP_tl-1, yP_tl-2, 1,
xP_tl, yP_tl, 5);
grad.addColorStop(0, "white");
grad.addColorStop(1, "green");
ctx2.fillStyle = grad;
ctx2.fill();
// Print the month
var months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November",
"December" ]
ctx2.beginPath();
ctx2.fillStyle = "grey";
ctx2.font = "14px Verdana";
ctx2.textAlign="start";
ctx2.fillText(months[Math.floor(theta_deg/30)], 8,
viewCanvas.height-8)
// Final annotations
ctx1.beginPath();
ctx1.fillStyle = "white";
ctx1.font = "14px Verdana";
ctx1.textAlign="start";
ctx1.fillText("Not to scale.", 8, mainCanvas.height - 8)
ctx1.fillText("Background stars.", 8, 12 + 8)
ctx2.beginPath();
ctx2.fillStyle = "white";
ctx2.font = "14px Verdana";
ctx2.textAlign="center";
ctx2.fillText("View from Earth through a telescope.",
viewCanvas.width/2, 12 + 8)
}
function drawSun() {
ctx1.drawImage(sunImg, xSun-sunImg.width/4, ySun-sunImg.height/4,
sunImg.width/2, sunImg.height/2);
}
function drawNearEarthOrbit() {
ctx1.ellipse(xSun, ySun, R, R * Math.sin(inc_deg * Math.PI / 180.0),
0, 0, Math.PI)
ctx1.setLineDash([7, 3])
ctx1.lineWidth=2;
ctx1.strokeStyle = 'grey';
ctx1.stroke();
}
function drawFarEarthOrbit() {
ctx1.ellipse(xSun, ySun, R, R * Math.sin(inc_deg * Math.PI / 180),
0, Math.PI, 0)
ctx1.setLineDash([7, 3])
ctx1.lineWidth=1.5;
ctx1.strokeStyle = 'grey';
ctx1.stroke();
// Annotate the Earth
ctx1.font = "14px Verdana";
ctx1.fillStyle = '#6b72f8';
ctx1.textAlign="end";
ctx1.fillText("Earth", xSun-R-10, ySun+4)
ctx1.textAlign="start";
}
function animate() {
// Convert polar Earth coords (R, Theta) into (x,y) coordinates
theta_deg += dTheta_deg
theta_deg %= 360
var theta_rad = theta_deg * Math.PI / 180
xE = R * Math.sin(theta_rad)
yE = R * Math.cos(theta_rad) * Math.sin(inc_deg * Math.PI / 180)
// Draw primitives on the canvas and set the animation speed
drawAll();
// Draw the Sun and orbit line in the forground during Earth eclipse
if (theta_deg > 256 || theta_deg < 90) {
drawNearEarthOrbit();
} else {
drawSun();
drawNearEarthOrbit();
}
// Set the animation speed
window.setTimeout(animate, 15);
}
</script>
</body>
</html>