Update Globe and add comments

This commit is contained in:
2022-07-19 11:54:50 +02:00
parent 5d7d4896fe
commit a4176fed3b
2 changed files with 27 additions and 37 deletions

View File

@@ -4,7 +4,7 @@
<script lang="ts"> <script lang="ts">
import { getContext, onMount } from 'svelte' import { getContext, onMount } from 'svelte'
import { fade, fly } from 'svelte/transition' import { fly } from 'svelte/transition'
import { quartOut } from 'svelte/easing' import { quartOut } from 'svelte/easing'
import { Globe, type Marker } from '$modules/globe2' import { Globe, type Marker } from '$modules/globe2'
import { getRandomItem } from '$utils/functions' import { getRandomItem } from '$utils/functions'
@@ -12,7 +12,6 @@
import Image from '$components/atoms/Image.svelte' import Image from '$components/atoms/Image.svelte'
let innerWidth: number let innerWidth: number
let offsetWidth: number, offsetHeight: number
let globeParentEl: HTMLElement, globeEl: HTMLElement let globeParentEl: HTMLElement, globeEl: HTMLElement
let globe: any let globe: any
let observer: IntersectionObserver let observer: IntersectionObserver
@@ -27,11 +26,7 @@
const markers = locations.map(({ name, slug, country, coordinates: { coordinates }}): Marker => ({ const markers = locations.map(({ name, slug, country, coordinates: { coordinates }}): Marker => ({
name, name,
slug, slug,
country: { country: { ...country },
name: country.name,
slug: country.slug,
flag: country.flag,
},
lat: coordinates[1], lat: coordinates[1],
lng: coordinates[0], lng: coordinates[0],
})) }))
@@ -41,22 +36,18 @@
globe = new Globe({ globe = new Globe({
el: globeEl, el: globeEl,
parent: globeParentEl, parent: globeParentEl,
width: offsetWidth,
height: offsetHeight,
mapFile: `/images/globe-map-${globeResolution}.png`, mapFile: `/images/globe-map-${globeResolution}.png`,
dpr: Math.min(Math.round(window.devicePixelRatio), 2), dpr: Math.min(Math.round(window.devicePixelRatio), 2),
autoRotate: true, autoRotate: true,
speed: 0.0035, speed: 0.003,
rotationStart: randomContinent.rotation, rotationStart: randomContinent.rotation,
markers, markers,
pane: import.meta.env.DEV, pane: import.meta.env.DEV,
}) })
// Define cluster locations // TODO: Define cluster locations and position it
clusterLocations = locations.filter((loc: any) => loc.country.slug === 'france') clusterLocations = locations.filter((loc: any) => loc.country.slug === 'france')
// console.log(globe)
resize() resize()
// Render only if in viewport // Render only if in viewport
@@ -100,10 +91,7 @@
// Resize // Resize
const resize = () => { const resize = () => {
globe.resize({ globe.resize()
width: offsetWidth,
height: offsetHeight,
})
} }
// Destroy // Destroy
@@ -119,10 +107,7 @@
<div class="globe" bind:this={globeParentEl}> <div class="globe" bind:this={globeParentEl}>
<div class="globe__inner"> <div class="globe__inner">
<div class="globe__canvas" <div class="globe__canvas" bind:this={globeEl} />
bind:this={globeEl}
bind:offsetWidth bind:offsetHeight
/>
</div> </div>
<ul class="globe__markers"> <ul class="globe__markers">

View File

@@ -11,6 +11,8 @@ export class Globe {
this.options = options this.options = options
this.el = options.el this.el = options.el
this.parent = options.parent this.parent = options.parent
this.width = this.el.offsetWidth
this.height = this.el.offsetHeight
this.markers = options.markers || [] this.markers = options.markers || []
// Parameters // Parameters
@@ -28,10 +30,7 @@ export class Globe {
// Run globe after check for WebGL support // Run globe after check for WebGL support
if (this.webgl) { if (this.webgl) {
this.build() this.build()
this.resize({ this.resize()
width: this.options.width,
height: this.options.height,
})
this.render() this.render()
} }
@@ -59,6 +58,7 @@ export class Globe {
// Create camera // Create camera
this.camera = new Camera(this.gl) this.camera = new Camera(this.gl)
// TODO: Why 1.315? Is there a way to calculate this number?
this.camera.position.set(0, 0, 1.315) this.camera.position.set(0, 0, 1.315)
// Create controls // Create controls
@@ -84,8 +84,9 @@ export class Globe {
}) })
// Create light // Create light
// TODO: How to create a nicer light that doesn't fade to 0? Just creating a "dark" area where you still can read markers and see countries/continents
// this.light = new Vec3(0, 50, 150) // this.light = new Vec3(0, 50, 150)
this.light = new Vec3(0, 0, 1000) this.light = new Vec3(0, 0, 15)
// Add map texture // Add map texture
const map = new Texture(this.gl) const map = new Texture(this.gl)
@@ -113,7 +114,7 @@ export class Globe {
}) })
this.mesh.setParent(this.scene) this.mesh.setParent(this.scene)
// Define random continent position // Start globe angle with a random continent's position
if (this.options.rotationStart) { if (this.options.rotationStart) {
this.mesh.rotation.y = degToRad(this.options.rotationStart * -1) || 0 this.mesh.rotation.y = degToRad(this.options.rotationStart * -1) || 0
} }
@@ -177,6 +178,11 @@ export class Globe {
// Scale marker position to fit globe size // Scale marker position to fit globe size
marker.position = [position[0] *= 0.5, position[1] *= 0.5, position[2] *= 0.5] marker.position = [position[0] *= 0.5, position[1] *= 0.5, position[2] *= 0.5]
// Position marker
const posX = (marker.position[0] + 1) * (this.width / 1.315)
const posY = (1 - marker.position[1]) * (this.height / 1.315)
markerEl.style.transform = `translate3d(${posX}px, ${posY}px, 0)`
console.log(marker) console.log(marker)
return marker return marker
}) })
@@ -185,13 +191,14 @@ export class Globe {
// Update markers // Update markers
updateMarkers () { updateMarkers () {
this.markers.forEach((marker: Marker) => { this.markers.forEach((marker: Marker) => {
const markerEl = this.getMarker(marker.slug) // const markerEl = this.getMarker(marker.slug)
const screenVector = new Vec3(0,0,0) // const screenVector = new Vec3(0,0,0)
screenVector.copy(marker.position) // screenVector.copy(marker.position)
this.camera.project(screenVector) // this.camera.project(screenVector)
// let posX = (screenVector.x + 1) * (this.options.width / 1.315) // let posX = (screenVector.x + 1) * (this.options.width / 1.315)
// // posX /= this.mesh.rotation.y // // // posX /= this.mesh.rotation.y
// let posY = (1 - screenVector.y) * (this.options.height / 1.315) // let posY = (1 - screenVector.y) * (this.options.height / 1.315)
// markerEl.style.transform = `translate3d(${posX}px, ${posY}px, 0)` // markerEl.style.transform = `translate3d(${posX}px, ${posY}px, 0)`
}) })
@@ -201,9 +208,9 @@ export class Globe {
/** /**
* Resize method * Resize method
*/ */
resize (options: any) { resize () {
// this.renderer.setSize(window.innerWidth, window.innerHeight) // this.renderer.setSize(window.innerWidth, window.innerHeight)
this.renderer.setSize(options.width || this.options.width, options.height || this.options.height) this.renderer.setSize(this.width, this.height)
this.camera.perspective({ this.camera.perspective({
aspect: this.gl.canvas.width / this.gl.canvas.height aspect: this.gl.canvas.width / this.gl.canvas.height
}) })
@@ -229,7 +236,7 @@ export class Globe {
camera: this.camera, camera: this.camera,
}) })
// Update light // TODO: Update light
// this.light.set(this.camera.position) // this.light.set(this.camera.position)
// this.program.uniforms.u_lightWorldPosition.value = [this.mesh.rotation.y * 1, 50, 150] // this.program.uniforms.u_lightWorldPosition.value = [this.mesh.rotation.y * 1, 50, 150]
@@ -264,8 +271,6 @@ export class Globe {
type Options = { type Options = {
el: HTMLElement el: HTMLElement
parent: HTMLElement parent: HTMLElement
width: number
height: number
mapFile: string mapFile: string
dpr: number dpr: number
autoRotate: boolean autoRotate: boolean