-
Notifications
You must be signed in to change notification settings - Fork 6
/
globe.js
378 lines (303 loc) · 10.7 KB
/
globe.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
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
368
369
370
371
372
373
374
375
376
377
378
class Globe {
constructor() {
this.renderer = null
this.scene = null
this.camera = null
this.earth = null
this.spikes = []
this.animationId = null
this.stars = null
// Camera control properties
this.isDragging = false
this.previousMousePosition = {
x: 0,
y: 0
}
// Camera orbit properties
this.cameraDistance = 1
this.cameraRotation = {
x: Math.PI * 0.1, // slightly above equator
y: Math.PI * 0.6 // west of prime meridian
}
// Zoom properties
this.minZoom = 0.1
this.maxZoom = 10
this.currentZoom = 1
this.zoomSpeed = 0.02
}
updateCameraPosition() {
// Convert spherical coordinates to Cartesian
const phi = this.cameraRotation.x // vertical angle
const theta = this.cameraRotation.y // horizontal angle
const radius = this.cameraDistance / this.currentZoom
this.camera.position.x = radius * Math.cos(phi) * Math.cos(theta)
this.camera.position.y = radius * Math.sin(phi)
this.camera.position.z = radius * Math.cos(phi) * Math.sin(theta)
// Always look at the center
this.camera.lookAt(0, 0, 0)
this.camera.up.set(0, 1, 0) // Keep "up" direction consistent
}
createStarField() {
const starsGeometry = new THREE.BufferGeometry()
const starsMaterial = new THREE.PointsMaterial({
color: 0xffffff,
size: 0.02,
transparent: true,
opacity: 0.8,
sizeAttenuation: true
})
const starsVertices = []
const radius = 10 // Radius of our star sphere
const starsCount = 2000 // Number of stars
for (let i = 0; i < starsCount; i++) {
// Create random spherical coordinates
const theta = 2 * Math.PI * Math.random()
const phi = Math.acos(2 * Math.random() - 1)
const x = radius * Math.sin(phi) * Math.cos(theta)
const y = radius * Math.sin(phi) * Math.sin(theta)
const z = radius * Math.cos(phi)
starsVertices.push(x, y, z)
}
starsGeometry.setAttribute("position", new THREE.Float32BufferAttribute(starsVertices, 3))
this.stars = new THREE.Points(starsGeometry, starsMaterial)
this.scene.add(this.stars)
}
createGlobe() {
// Ensure any previous globe and animation is removed
this.removeGlobe()
// Initialize scene, camera, renderer, etc.
this.scene = new THREE.Scene()
const height = window.innerHeight - 100
this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / height, 0.1, 1000)
// Set initial camera position
this.cameraDistance = 1
this.updateCameraPosition()
// Set up renderer with alpha and better quality
this.renderer = new THREE.WebGLRenderer({ antialias: true })
this.renderer.setSize(window.innerWidth, height)
this.renderer.setClearColor(0x000000) // Set background to black
document.body.prepend(this.renderer.domElement)
// Create starry background
this.createStarField()
// Create Earth with better lighting
const geometry = new THREE.SphereGeometry(0.5, 32, 32)
const texture = new THREE.TextureLoader().load("earth_atmos_2048.jpg")
// Use PhongMaterial for better lighting
const material = new THREE.MeshPhongMaterial({
map: texture,
specular: 0x333333,
shininess: 5
})
this.earth = new THREE.Mesh(geometry, material)
this.scene.add(this.earth)
// Add ambient light
const ambientLight = new THREE.AmbientLight(0xfffffff)
this.scene.add(ambientLight)
// Add directional light
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.4)
directionalLight.position.set(5, 3, 5)
this.scene.add(directionalLight)
// Add mouse and zoom controls
this.setupMouseControls()
this.setupZoomControls()
// Start the animation loop
this.animate()
return this
}
setupMouseControls() {
const canvas = this.renderer.domElement
canvas.addEventListener("mousedown", e => {
this.isDragging = true
this.previousMousePosition = {
x: e.clientX,
y: e.clientY
}
})
canvas.addEventListener("mousemove", e => {
if (!this.isDragging) return
const deltaMove = {
x: e.clientX - this.previousMousePosition.x,
y: e.clientY - this.previousMousePosition.y
}
// Adjust rotation speed based on movement
const rotationSpeed = 0.005
// Update camera orbit angles
this.cameraRotation.y += deltaMove.x * rotationSpeed
this.cameraRotation.x += deltaMove.y * rotationSpeed
// Limit vertical rotation to prevent flipping
this.cameraRotation.x = Math.max(-Math.PI / 2 + 0.1, Math.min(Math.PI / 2 - 0.1, this.cameraRotation.x))
// Update camera position
this.updateCameraPosition()
this.previousMousePosition = {
x: e.clientX,
y: e.clientY
}
})
window.addEventListener("mouseup", () => {
this.isDragging = false
})
canvas.addEventListener("selectstart", e => {
e.preventDefault()
})
}
setupZoomControls() {
const canvas = this.renderer.domElement
canvas.addEventListener("wheel", e => {
e.preventDefault()
const zoomDelta = e.deltaY > 0 ? -this.zoomSpeed : this.zoomSpeed
this.currentZoom = Math.max(this.minZoom, Math.min(this.maxZoom, this.currentZoom + zoomDelta))
this.updateCameraPosition()
})
let touchDistance = 0
canvas.addEventListener("touchstart", e => {
if (e.touches.length === 2) {
const touch1 = e.touches[0]
const touch2 = e.touches[1]
touchDistance = Math.hypot(touch2.clientX - touch1.clientX, touch2.clientY - touch1.clientY)
}
})
canvas.addEventListener("touchmove", e => {
if (e.touches.length === 2) {
e.preventDefault()
const touch1 = e.touches[0]
const touch2 = e.touches[1]
const newDistance = Math.hypot(touch2.clientX - touch1.clientX, touch2.clientY - touch1.clientY)
const delta = newDistance - touchDistance
const zoomDelta = delta * 0.01 * this.zoomSpeed
this.currentZoom = Math.max(this.minZoom, Math.min(this.maxZoom, this.currentZoom + zoomDelta))
this.updateCameraPosition()
touchDistance = newDistance
}
})
}
animate() {
if (!this.earth) return
const { earth, spikes, renderer, scene, camera } = this
this.animationId = requestAnimationFrame(this.animate.bind(this))
const AUTO_ROTATE_SPEED = 0.0003
// Auto-rotate camera when not dragging
if (this.shouldRotate && !this.isDragging) {
this.cameraRotation.y += AUTO_ROTATE_SPEED
this.updateCameraPosition()
}
// Update spikes
spikes.forEach((spike, index) => {
spike.scale.y *= 0.95
if (spike.scale.y < 0.01) {
earth.remove(spike)
spikes.splice(index, 1)
}
})
renderer.render(scene, camera)
}
removeGlobe() {
// Cancel the previous animation frame to stop multiple animations
if (this.animationId) {
cancelAnimationFrame(this.animationId)
}
// Remove event listeners if they exist
if (this.renderer) {
const canvas = this.renderer.domElement
canvas.removeEventListener("mousedown", this.onMouseDown)
canvas.removeEventListener("mousemove", this.onMouseMove)
canvas.removeEventListener("selectstart", this.onSelectStart)
}
// Dispose of the renderer, scene, and event listeners
if (this.renderer) {
this.renderer.dispose()
this.renderer.domElement.remove()
}
if (this.scene) {
while (this.scene.children.length > 0) {
const object = this.scene.children[0]
if (object.geometry) object.geometry.dispose()
if (object.material) object.material.dispose()
this.scene.remove(object)
}
}
this.spikes = []
this.renderer = null
this.scene = null
this.earth = null
}
latLongToVector3(lat, lon) {
const phi = (90 - lat) * (Math.PI / 180)
const theta = (lon + 180) * (Math.PI / 180)
const x = -0.5 * Math.sin(phi) * Math.cos(theta)
const y = 0.5 * Math.cos(phi)
const z = 0.5 * Math.sin(phi) * Math.sin(theta)
return new THREE.Vector3(x, y, z)
}
visualizeHit(request) {
const { lat, long, type } = request
const position = this.latLongToVector3(lat, long)
const spikeGeometry = new THREE.ConeGeometry(0.005, 0.3, 8)
spikeGeometry.translate(0, 0.05, 0)
spikeGeometry.rotateX(Math.PI / 2)
const color = type === "read" ? 0xffffff : 0x00ff00
const spikeMaterial = new THREE.MeshBasicMaterial({ color })
const spike = new THREE.Mesh(spikeGeometry, spikeMaterial)
spike.position.set(position.x, position.y, position.z)
spike.lookAt(new THREE.Vector3(0, 0, 0))
this.earth.add(spike) // Add spike to earth instead of scene
this.spikes.push(spike)
}
listenToResize() {
window.addEventListener("resize", () => {
this.createGlobe() // Recreate the globe on resize
})
return this
}
listenForClicks() {
document.body.addEventListener("dblclick", () => {
if (!document.fullscreenElement) {
document.body.requestFullscreen().catch(err => {
console.log(`Error trying to go fullscreen: ${err.message} (${err.name})`)
})
} else {
document.exitFullscreen()
}
})
// Pause/Resume on single-click
document.body.addEventListener("click", () => (this.shouldRotate = !this.shouldRotate))
return this
}
shouldRotate = true
bindToSSE() {
const logContainer = document.getElementById("log-container")
const urlParams = new URLSearchParams(window.location.search)
const folderName = urlParams.get("folderName")
const queryString = folderName ? `?folderName=${folderName}` : ""
if (folderName) document.querySelector("#summaryLink").href = "summarizeRequests.htm?folderName=" + folderName
else document.querySelector("#summaryLink").href = "requests.html"
const eventSource = new EventSource(`/requests.htm${queryString}`)
eventSource.onmessage = event => {
const data = JSON.parse(event.data)
const logEntry = document.createElement("div")
logEntry.textContent = data.log
logContainer.appendChild(logEntry)
logContainer.scrollTop = logContainer.scrollHeight
const parts = data.log.split(" ")
const long = parseFloat(parts.pop())
const lat = parseFloat(parts.pop())
const type = parts.shift()
const page = parts.shift()
const request = {
lat,
long,
type,
page
}
this.visualizeHit(request)
}
eventSource.onerror = error => {
console.error("EventSource failed:", error)
eventSource.close()
}
eventSource.onopen = event => {
console.log("SSE connection opened")
}
return this
}
}
window.globe = new Globe().createGlobe().bindToSSE().listenToResize().listenForClicks()