solaris-js
Version:
A reusable component for interactive visualization of the Solar System
112 lines (83 loc) • 3.15 kB
text/coffeescript
import publicize from './helpers/publicizer'
import fastclick from 'fastclick'
import {Scene, AmbientLight, WebGLRenderer, PerspectiveCamera, Vector3} from 'three'
import Model from 'solaris-model'
import Styles from './Styles'
import Scale from './Scale'
import Loader from './loader/Loader'
import CelestialBody from './CelestialBody'
import Background from './Background'
import Controls from './Controls'
wait = (ms, cb) -> setTimeout(cb, ms)
class $Solaris
constructor: (element, options) ->
= options?.root ? 'solaris'
= new Styles(options?.styles, )
#
= new Model
= new Scale(.bodies.pluto.elements.base.a * 2)
= if typeof element is 'string' then document.getElementById(element) else element
.classList.add 'solaris'
= new Loader(, )
fastclick(options?.fastClickElement ? )
= new Scene
.add new AmbientLight(0x222222)
= new Background(.sceneSize, )
.add .mesh
= Object.create(null)
{sun: .bodies.sun}
window.addEventListener 'resize', , false
#
setTime: (time) ->
.setTime(time)
wait 50, => if
#
center: () ->
= [] if typeof is 'string'
.target.copy(.getAbsolutePosition())
.label.select()
#
reset: ->
.reset()
createRenderer: ->
= new WebGLRenderer(antialias: yes, alpha: yes)
.setSize window.innerWidth, window.innerHeight
.setPixelRatio(window.devicePixelRatio)
.appendChild .domElement
createCamera: ->
# fov, aspect, near, far
= new PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.0001, .sceneSize * 40)
.up = new Vector3(0, 0, 1)
.position.set 0, 0, .convert(.bodies.mars.elements.base.a * 2.7)
createControls: ->
= new Controls , .domElement,
# minDistance: .convert(.bodies.sun.radius)
maxDistance: .sceneSize * 5
.enableZoom = true
.target.set(0, 0, 0)
initBodies: (bodies, parent) ->
for k, model of bodies
body = new CelestialBody(model, this, parent, .domElement)
[k] = body
if model.satellites
updateBodies: ->
for k, body of
body.update()
animate: =>
.render ,
.update()
window.requestAnimationFrame()
onWindowResize: =>
.aspect = window.innerWidth / window.innerHeight
.updateProjectionMatrix()
.setSize window.innerWidth, window.innerHeight
export default class Solaris extends publicize $Solaris,
properties: ['model']
methods: ['setTime', 'center', 'reset']