I don't think there are packages that come with SVGs, so you will need to source / produce those yourself.
As for getting SunCalc to give you a nice number for the phase that actually sits on 0
, 0.25
, 0.5
, or 0.75
for a bit, the easiest is to do some rounding:
If you want to round directly to those numbers, you can round to the nearest quarter:
var date = new Date();
Math.round(SunCalc.getMoonIllumination(date).phase * 4) / 4;
Or (as you specified in comments) if you want it to still go through the waxing/waning sections and stick to Full Moon / New Moon / Quarters for around about a day, you can round to the nearest 28th:
var date = new Date();
Math.round(SunCalc.getMoonIllumination(date).phase * 28) / 28;
Why does this work? and why 28?
So the simplest way to explain is to look at the quarters example again. We want the rounded number to match one of the options (0, 0.25, 0.5, 0.75
).
Because Math.round
, rounds to the nearest integer, we want these results to become integers we can round to. By multiplying them by 4 we get:
0 * 4 = 0
0.25 * 4 = 1
0.5 * 4 = 2
0.75 * 4 = 3
Everything we get in between these ends up rounding to one of them (except some round to 5, which we would need to wrap around ourselves).
Then we divide the result by the same multiplier to get the numbers back in the expected range.
Now 28 works because for two reasons:
- 4 is a factor of 28. So some numbers will come back as multiples of .25
- Because it is close to the length of the lunar cycle (~29.53) in days
This means that we end up with the result rounding to each value for approximately a day