Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get the current phase of the moon, and adjust a sprite for it
const phases=[
{"sprite-x":123, "sprite-y":987, phase:"new"},
{"sprite-x":123, "sprite-y":987, phase:"wax-crescent-1"},
{"sprite-x":123, "sprite-y":987, phase:"wax-crescent-2"},
{"sprite-x":123, "sprite-y":987, phase:"wax-crescent-3"},
{"sprite-x":123, "sprite-y":987, phase:"wax-crescent-4"},
{"sprite-x":123, "sprite-y":987, phase:"wax-crescent-5"},
{"sprite-x":123, "sprite-y":987, phase:"wax-crescent-6"},
{"sprite-x":123, "sprite-y":987, phase:"quarter-1"},
{"sprite-x":123, "sprite-y":987, phase:"wax-gibbous-1"},
{"sprite-x":123, "sprite-y":987, phase:"wax-gibbous-2"},
{"sprite-x":123, "sprite-y":987, phase:"wax-gibbous-3"},
{"sprite-x":123, "sprite-y":987, phase:"wax-gibbous-4"},
{"sprite-x":123, "sprite-y":987, phase:"full"},
{"sprite-x":123, "sprite-y":987, phase:"wane-gibbous-1"},
{"sprite-x":123, "sprite-y":987, phase:"wane-gibbous-2"},
{"sprite-x":123, "sprite-y":987, phase:"wane-gibbous-3"},
{"sprite-x":123, "sprite-y":987, phase:"wane-gibbous-4"},
{"sprite-x":123, "sprite-y":987, phase:"wane-gibbous-5"},
{"sprite-x":123, "sprite-y":987, phase:"wane-gibbous-6"},
{"sprite-x":123, "sprite-y":987, phase:"quarter-3"},
{"sprite-x":123, "sprite-y":987, phase:"wane-crescent-1"},
{"sprite-x":123, "sprite-y":987, phase:"wane-crescent-2"},
{"sprite-x":123, "sprite-y":987, phase:"wane-crescent-3"},
{"sprite-x":123, "sprite-y":987, phase:"wane-crescent-4"},
{"sprite-x":123, "sprite-y":987, phase:"wane-crescent-5"},
{"sprite-x":123, "sprite-y":987, phase:"new"}
]
let newMoonBaseline = new Date('November 26, 2019 15:05:00')
let millisecondsInADay = 86400000
let lunarSynodicPeriod = 29.53049
let daysSinceNewMoon = ((new Date()) - newMoonBaseline) / millisecondsInADay
let pctOfCurrentCycle = (daysSinceNewMoon % lunarSynodicPeriod) / lunarSynodicPeriod
var phaseIndex = Math.round((phases.length - 1) * pctOfCurrentCycle)
console.log(phases[phaseIndex].phase)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment