Re-implement close state to Globe marker

This commit is contained in:
2022-03-01 12:48:10 +01:00
parent f4a4744177
commit 482b62da0e
3 changed files with 55 additions and 44 deletions

View File

@@ -1,5 +1,5 @@
<script lang="ts"> <script lang="ts">
import { onMount, getContext } from 'svelte' import { onMount, onDestroy, getContext } from 'svelte'
import { getPosition, getRandomElement } from '$utils/functions' import { getPosition, getRandomElement } from '$utils/functions'
export let type: string = undefined export let type: string = undefined
@@ -7,50 +7,48 @@
export let scrollSmooth: number = 0.5 export let scrollSmooth: number = 0.5
export let opacity: number = 1 export let opacity: number = 1
const { continents, locations } = getContext('global')
let globe: any
let Globe: any
let globeEl: HTMLElement let globeEl: HTMLElement
let windowHeight: number, windowWidth: number
let containerTop: number = 0, containerHeight: number = 0
let observer: IntersectionObserver let observer: IntersectionObserver
let globe: any
let innerWidth: number
let innerHeight: number
let containerTop = 0
let containerHeight = 0
const globeResolution = windowWidth > 1440 && window.devicePixelRatio > 1 ? '4k' : '2k' $: globeResolution = innerWidth > 1440 && window.devicePixelRatio > 1 ? '4k' : '2k'
const { continents, locations } = getContext('global')
const randomContinent = getRandomElement(continents.filter((cont: any) => cont.countries)) const randomContinent = getRandomElement(continents.filter((cont: any) => cont.countries))
const markers = locations.map(({ name, slug, country, coordinates: { coordinates }}: any) => ({ const markers = locations.map(({ name, slug, country, globe_close: isClose, coordinates: { coordinates }}: any) => ({
name, name,
slug, slug,
countryName: country.name, countryName: country.name,
countrySlug: country.slug, countrySlug: country.slug,
lat: coordinates[1], lat: coordinates[1],
lng: coordinates[0], lng: coordinates[0],
// className: location.close ? 'is-close' : '', className: isClose ? 'is-close' : '',
})) }))
/** /*
* Globe update ** Functions
*/ */
// Globe update
const update = () => { const update = () => {
requestAnimationFrame(update) requestAnimationFrame(update)
globe.update() globe.update()
} }
/** // On scroll
* When scrolling
*/
const handleScroll = () => { const handleScroll = () => {
let scrollDiff = (containerTop + windowHeight + (containerHeight - windowHeight) / 2) - document.documentElement.scrollTop let scrollDiff = (containerTop + innerHeight + (containerHeight - innerHeight) / 2) - document.documentElement.scrollTop
let scrollRatio = (1 - (scrollDiff / windowHeight)) * 2 let scrollRatio = (1 - (scrollDiff / innerHeight)) * 2
if (globe) { if (globe) {
globe.updateCameraPos(scrollRatio, scrollDiff - windowHeight) globe.updateCameraPos(scrollRatio, scrollDiff - innerHeight)
} }
} }
/** // On resize
* When resizing
*/
const handleResize = () => { const handleResize = () => {
if (globeEl) { if (globeEl) {
containerTop = getPosition(globeEl).top containerTop = getPosition(globeEl).top
@@ -64,30 +62,40 @@
} }
/*
** Run code when mounted
*/
onMount(async () => { onMount(async () => {
// Load globe library // Import libraries and code
Globe = await import('$modules/globe') const { default: InteractiveGlobe } = await import('$modules/globe')
// Instantiate globe // Init the globe from library
globe = new Globe.default({ globe = new InteractiveGlobe({
el: globeEl, el: globeEl,
//cameraDistance: size, // Smaller number == larger globe //cameraDistance: size, // Smaller number == larger globe
autoRotationSpeed: autoRotate ? -0.0025 : 0, autoRotationSpeed: autoRotate ? -0.0025 : 0,
rotationStart: randomContinent.rotation, // In degrees rotationStart: randomContinent.rotation, // In degrees
scrollSmoothing: scrollSmooth, scrollSmoothing: scrollSmooth,
opacity, opacity: opacity,
texture: `/images/globe-map-${globeResolution}.png`, texture: `/images/globe-map-${globeResolution}.png`,
markers, markers,
onLinkClicked: () => {} onLinkClicked: () => {}
}) })
// Observe the globe // Run the globe
update()
handleResize()
// Enable/Disable the globe when shown/hidden
observer = new IntersectionObserver(entries => { observer = new IntersectionObserver(entries => {
entries.forEach(entry => { entries.forEach(({ isIntersecting }: IntersectionObserverEntry) => {
if (entry.isIntersecting) { if (isIntersecting) {
globe.enable() globe.enable()
console.log('IO enable globe')
} else { } else {
globe.disable() globe.disable()
console.log('IO disable globe')
} }
}) })
}, { }, {
@@ -96,26 +104,27 @@
}) })
observer.observe(globeEl) observer.observe(globeEl)
// Run the globe // const globeScroll = ScrollOut({
update() // targets: scope,
handleResize() // onShown: () => globe.enable(),
// onHidden: () => globe.disable()
// })
})
// Destroy /*
return () => { ** Destroy when unmounted
if (globe) { */
globe.destroy() onDestroy(() => {
observer.disconnect() globe && globe.destroy()
}
}
}) })
</script> </script>
<svelte:window <svelte:window
bind:innerHeight={windowHeight}
bind:innerWidth={windowWidth}
on:scroll={handleScroll} on:scroll={handleScroll}
on:resize={handleResize} on:resize={handleResize}
bind:innerHeight
bind:innerWidth
/> />

View File

@@ -88,6 +88,7 @@
date_created date_created
} }
has_poster has_poster
globe_close
} }
countries: country (filter: { status: { _eq: "published" }}) { countries: country (filter: { status: { _eq: "published" }}) {

View File

@@ -96,9 +96,10 @@
// Label // Label
&__label { &__label {
position: absolute; position: absolute;
bottom: -16px; bottom: 16px;
left: 16px; left: 0px;
color: transparent; color: transparent;
pointer-events: none;
} }
// Location city // Location city
&__city { &__city {