Solar Clock: Memory footprint reduction. It appears that each object created is extremely expensive memorywise. Have merged together the night and data strategy which reduces the footprint by 5%

master
adrian w kirk 2021-08-08 16:16:05 +01:00
parent 91a957adeb
commit 8bcf8887cc
1 changed files with 23 additions and 45 deletions

View File

@ -120,25 +120,9 @@ class SolarMode {
throw "sun drawing undefined"; throw "sun drawing undefined";
} }
} }
class NightMode extends SolarMode { class DayNightMode extends SolarMode {
test(time, day_info, screen_info, img_info) {
return (time < day_info.sunrise_date || time > day_info.sunset_date);
}
draw(time, day_info, screen_info, img_info){
draw_night_sun(screen_info.sun_x,screen_info.sun_y,screen_info.sun_radius, img_info);
}
}
class DayLightMode extends SolarMode {
test(time, day_info, screen_info){ test(time, day_info, screen_info){
var sun_height = screen_info.sunrise_y - screen_info.sun_y; return true;
/*console.log("DayLightMode " +
"time=" + time.toISOString() +
" sunset_date=" + day_info.sunset_date.toISOString() +
" sunrise_date=" + day_info.sunrise_date.toISOString()
);*/
return time < day_info.sunset_date &&
time > day_info.sunrise_date &&
sun_height >= screen_info.sun_radius * 2 + SUNSET_START_HEIGHT;
} }
// The corona is larger the closer you are to solar noon // The corona is larger the closer you are to solar noon
_calc_corona_radius(now, day_info){ _calc_corona_radius(now, day_info){
@ -169,20 +153,24 @@ class DayLightMode extends SolarMode {
} }
} }
draw(now, day_info, screen_info, img_info){ draw(now, day_info, screen_info, img_info){
var sun_color = daytime_sun_color(now,day_info); if(now < day_info.sunrise_date || now > day_info.sunset_date){
var corona_radius = this._calc_corona_radius(now, day_info); draw_night_sun(screen_info.sun_x,screen_info.sun_y,screen_info.sun_radius, img_info);
var draw_info = GraphicUtils.draw_info(img_info); } else {
GraphicUtils.set_color(sun_color,draw_info.buff); var sun_color = daytime_sun_color(now, day_info);
if(corona_radius > screen_info.sun_radius){ var corona_radius = this._calc_corona_radius(now, day_info);
this._drawCorona(corona_radius, var draw_info = GraphicUtils.draw_info(img_info);
screen_info.sun_x, GraphicUtils.set_color(sun_color, draw_info.buff);
screen_info.sun_y, if (corona_radius > screen_info.sun_radius) {
screen_info.sun_radius, this._drawCorona(corona_radius,
draw_info); screen_info.sun_x,
screen_info.sun_y,
screen_info.sun_radius,
draw_info);
}
draw_info.buff.fillCircle(screen_info.sun_x - draw_info.offset_x,
screen_info.sun_y - draw_info.offset_y,
screen_info.sun_radius);
} }
draw_info.buff.fillCircle(screen_info.sun_x - draw_info.offset_x,
screen_info.sun_y - draw_info.offset_y,
screen_info.sun_radius);
} }
} }
class TwiLightMode extends SolarMode { class TwiLightMode extends SolarMode {
@ -222,32 +210,22 @@ class TwiLightMode extends SolarMode {
} }
class SolarControllerImpl { class SolarControllerImpl {
constructor(){ constructor(){
this.solar_modes = [new TwiLightMode(), new DayLightMode()]; this.solar_modes = [new TwiLightMode()];
this.default_mode = new NightMode(); this.default_mode = new DayNightMode()
this.last = null;
} }
// The mode method is responsible for selecting the // The mode method is responsible for selecting the
// correct mode to the time given. // correct mode to the time given.
mode(time, day_info, screen_info){ mode(time, day_info, screen_info){
// first we test the last selection
// to see if its still valid
if(this.last != null){
if(this.last.test(time,day_info,screen_info)){
return this.last;
}
}
// next we step through the different modes and test then // next we step through the different modes and test then
// one by one. // one by one.
for(var i=0; i<this.solar_modes.length; i++){ for(var i=0; i<this.solar_modes.length; i++){
if(this.solar_modes[i].test(time,day_info,screen_info) ){ if(this.solar_modes[i].test(time,day_info,screen_info) ){
this.last = this.solar_modes[i]; return this.solar_modes[i];
return this.last;
} }
} }
// Otherwise we use the default // Otherwise we use the default
//console.log("defaulting"); //console.log("defaulting");
this.last = this.default_mode; return this.default_mode;
return this.last;
} }
} }