-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aae83ce
commit 75fb074
Showing
5 changed files
with
273 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<head> | ||
<style> body { margin: 0; } </style> | ||
|
||
<script src="//unpkg.com/three"></script> | ||
<script src="//unpkg.com/three/examples/js/controls/TrackballControls.js"></script> | ||
<script src="//unpkg.com/three/examples/js/renderers/CSS2DRenderer.js"></script> | ||
|
||
<script src="//unpkg.com/three-globe"></script> | ||
<!-- <script src="../../dist/three-globe.js"></script>--> | ||
</head> | ||
|
||
<body> | ||
<div id="globeViz"></div> | ||
|
||
<script> | ||
const markerSvg = `<svg viewBox="-4 0 36 36"> | ||
<path fill="currentColor" d="M14,0 C21.732,0 28,5.641 28,12.6 C28,23.963 14,36 14,36 C14,36 0,24.064 0,12.6 C0,5.641 6.268,0 14,0 Z"></path> | ||
<circle fill="black" cx="14" cy="14" r="7"></circle> | ||
</svg>`; | ||
|
||
// Gen random data | ||
const N = 30; | ||
const gData = [...Array(N).keys()].map(() => ({ | ||
lat: (Math.random() - 0.5) * 180, | ||
lng: (Math.random() - 0.5) * 360, | ||
size: 7 + Math.random() * 30, | ||
color: ['red', 'white', 'blue', 'green'][Math.round(Math.random() * 3)] | ||
})); | ||
|
||
const Globe = new ThreeGlobe() | ||
.globeImageUrl('//unpkg.com/three-globe/example/img/earth-dark.jpg') | ||
.bumpImageUrl('//unpkg.com/three-globe/example/img/earth-topology.png') | ||
.htmlElementsData(gData) | ||
.htmlElement(d => { | ||
const el = document.createElement('div'); | ||
el.innerHTML = markerSvg; | ||
el.style.color = d.color; | ||
el.style.width = `${d.size}px`; | ||
return el; | ||
}); | ||
|
||
// Setup renderers | ||
const renderers = [new THREE.WebGLRenderer(), new THREE.CSS2DRenderer()]; | ||
renderers.forEach((r, idx) => { | ||
r.setSize(window.innerWidth, window.innerHeight); | ||
if (idx > 0) { | ||
// overlay additional on top of main renderer | ||
r.domElement.style.position = 'absolute'; | ||
r.domElement.style.top = '0px'; | ||
r.domElement.style.pointerEvents = 'none'; | ||
} | ||
document.getElementById('globeViz').appendChild(r.domElement); | ||
}); | ||
|
||
// Setup scene | ||
const scene = new THREE.Scene(); | ||
scene.add(Globe); | ||
scene.add(new THREE.AmbientLight(0xbbbbbb)); | ||
scene.add(new THREE.DirectionalLight(0xffffff, 0.6)); | ||
|
||
// Setup camera | ||
const camera = new THREE.PerspectiveCamera(); | ||
camera.aspect = window.innerWidth/window.innerHeight; | ||
camera.updateProjectionMatrix(); | ||
camera.position.z = 500; | ||
|
||
// Add camera controls | ||
const tbControls = new THREE.TrackballControls(camera, renderers[0].domElement); | ||
tbControls.minDistance = 101; | ||
tbControls.rotateSpeed = 5; | ||
tbControls.zoomSpeed = 0.8; | ||
|
||
// Update pov when camera moves | ||
Globe.setPointOfView(camera.position, Globe.position); | ||
tbControls.addEventListener('change', () => Globe.setPointOfView(camera.position, Globe.position)); | ||
|
||
// Kick-off renderers | ||
(function animate() { // IIFE | ||
// Frame cycle | ||
tbControls.update(); | ||
renderers.forEach(r => r.render(scene, camera)); | ||
requestAnimationFrame(animate); | ||
})(); | ||
</script> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer.js'; | ||
|
||
const THREE = { | ||
...(window.THREE | ||
? window.THREE // Prefer consumption from global THREE, if exists | ||
: {} | ||
), | ||
CSS2DObject | ||
}; | ||
|
||
import Kapsule from 'kapsule'; | ||
import accessorFn from 'accessor-fn'; | ||
import TWEEN from '@tweenjs/tween.js'; | ||
|
||
import { emptyObject } from '../utils/gc'; | ||
import threeDigest from '../utils/digest'; | ||
import { polar2Cartesian } from '../utils/coordTranslate'; | ||
|
||
// | ||
|
||
export default Kapsule({ | ||
props: { | ||
htmlElementsData: { default: [] }, | ||
htmlLat: { default: 'lat' }, | ||
htmlLng: { default: 'lng' }, | ||
htmlAltitude: { default: 0 }, // in units of globe radius | ||
htmlElement: {}, | ||
htmlTransitionDuration: { default: 1000, triggerUpdate: false }, // ms | ||
isBehindGlobe: { onChange() { this.updateObjVisibility() }, triggerUpdate: false } | ||
}, | ||
|
||
methods: { | ||
updateObjVisibility(state, obj) { | ||
// default to all if no obj specified | ||
const objs = obj ? [obj] : state.htmlElementsData.map(d => d.__threeObj).filter(d => d); | ||
// Hide elements on the far side of the globe | ||
objs.forEach(obj => (obj.visible = !state.isBehindGlobe || !state.isBehindGlobe(obj.position))); | ||
} | ||
}, | ||
|
||
init(threeObj, state) { | ||
// Clear the scene | ||
emptyObject(threeObj); | ||
|
||
// Main three object to manipulate | ||
state.scene = threeObj; | ||
}, | ||
|
||
update(state, changedProps) { | ||
// Data accessors | ||
const latAccessor = accessorFn(state.htmlLat); | ||
const lngAccessor = accessorFn(state.htmlLng); | ||
const altitudeAccessor = accessorFn(state.htmlAltitude); | ||
const elemAccessor = accessorFn(state.htmlElement); | ||
|
||
threeDigest(state.htmlElementsData, state.scene, { | ||
// objs need to be recreated if this prop has changed | ||
purge: changedProps.hasOwnProperty('htmlElement'), | ||
createObj: d => { | ||
let elem = elemAccessor(d); | ||
|
||
const obj = new THREE.CSS2DObject(elem); | ||
|
||
obj.__globeObjType = 'html'; // Add object type | ||
|
||
return obj; | ||
}, | ||
updateObj: (obj, d) => { | ||
const applyUpdate = td => { | ||
const { alt, lat, lng } = obj.__currentTargetD = td; | ||
Object.assign(obj.position, polar2Cartesian(lat, lng, alt)); | ||
this.updateObjVisibility(obj); | ||
}; | ||
|
||
const targetD = { | ||
lat: +latAccessor(d), | ||
lng: +lngAccessor(d), | ||
alt: +altitudeAccessor(d) | ||
}; | ||
|
||
if (!state.htmlTransitionDuration || state.htmlTransitionDuration < 0 || !obj.__currentTargetD) { | ||
// set final position | ||
applyUpdate(targetD); | ||
} else { | ||
// animate | ||
new TWEEN.Tween(obj.__currentTargetD) | ||
.to(targetD, state.pointsTransitionDuration) | ||
.easing(TWEEN.Easing.Quadratic.InOut) | ||
.onUpdate(applyUpdate) | ||
.start(); | ||
} | ||
} | ||
}); | ||
} | ||
}); |