Locally scoped buf

master
David Volovskiy 2025-05-03 15:19:32 -04:00
parent 9f0af38cb6
commit 5f837fbf0b
1 changed files with 14 additions and 11 deletions

View File

@ -296,14 +296,14 @@ function drawGaugeImage(date) {
ring_fill = (hh * 60) + mm; ring_fill = (hh * 60) + mm;
ring_max = 1440; ring_max = 1440;
break; break;
case 'Steps': case 'Steps':
ring_fill = getSteps(); ring_fill = getSteps();
ring_max = settings.step_target; ring_max = settings.step_target;
break; break;
case 'Battery': case 'Battery':
ring_fill = E.getBattery(); ring_fill = E.getBattery();
break; break;
case 'Sun': case 'Sun':
var dayMin = getMinutesFromDate(date); var dayMin = getMinutesFromDate(date);
if (dayMin >= sunEnd && dayMin <= night) ring_fill = 100; if (dayMin >= sunEnd && dayMin <= night) ring_fill = 100;
else { else {
@ -311,7 +311,7 @@ function drawGaugeImage(date) {
if (ring_fill > 100) { // If we're now past a sunrise of sunset if (ring_fill > 100) { // If we're now past a sunrise of sunset
updateSunRiseSunSet(date, location.lat, location.lon, true); updateSunRiseSunSet(date, location.lat, location.lon, true);
ring_fill = 100 * (date - sunStart) / sunFull; ring_fill = 100 * (date - sunStart) / sunFull;
} }
} }
invertRing = !isDaytime; invertRing = !isDaytime;
break; break;
@ -422,34 +422,38 @@ function polyArray(start, end, max) {
var array = [g.getHeight()/2, g.getHeight()/2]; var array = [g.getHeight()/2, g.getHeight()/2];
var pt = addPoint(start, max); var pt = addPoint(start, max);
array.push(pt[0], pt[1]); array.push(pt[0], pt[1]);
for (let i = start+1; i < end; i++) { for (let i = start+1; i < end; i++) {
if (((i - start)) % 13 < 1) { // Add a point every 8th of the circle if (((i - start)) % 13 < 1) { // Add a point every 8th of the circle
pt = addPoint(i, max); pt = addPoint(i, max);
array.push(pt[0], pt[1]); array.push(pt[0], pt[1]);
} }
} }
pt = addPoint(end, max); pt = addPoint(end, max);
array.push(pt[0], pt[1]); array.push(pt[0], pt[1]);
log_debug("Poly Arr: " + array); log_debug("Poly Arr: " + array);
return array; return array;
} }
buf = Graphics.createArrayBuffer(w, h, 2, { msb: true });
function drawRing(start, end, max) { function drawRing(start, end, max) {
const edge = 4; const edge = 4;
const thickness = 6; const thickness = 6;
// Create persistent `buf` inside the function scope
if (!drawRing._buf) {
drawRing._buf = Graphics.createArrayBuffer(w, h, 2, { msb: true });
}
const buf = drawRing._buf;
let img = { width: w, height: h, transparent: 0, let img = { width: w, height: h, transparent: 0,
bpp: 2, palette: pal1, buffer: buf.buffer }; bpp: 2, palette: pal1, buffer: buf.buffer };
buf.clear(); buf.clear();
buf.setColor(1).fillEllipse(edge,edge,w-edge,h-edge); buf.setColor(1).fillEllipse(edge,edge,w-edge,h-edge);
buf.setColor(0).fillEllipse(edge+thickness,edge+thickness,w-edge-thickness,h-edge-thickness); buf.setColor(0).fillEllipse(edge+thickness,edge+thickness,w-edge-thickness,h-edge-thickness);
img.palette = pal2; img.palette = pal2;
g.drawImage(img, 0, 0); // Draws an unfilled circle g.drawImage(img, 0, 0); // Draws an unfilled circle
buf.clear(); buf.clear();
buf.setColor(1).fillEllipse(edge,edge,w-edge,h-edge); buf.setColor(1).fillEllipse(edge,edge,w-edge,h-edge);
buf.setColor(0).fillEllipse(edge+thickness,edge+thickness,w-edge-thickness,h-edge-thickness); buf.setColor(0).fillEllipse(edge+thickness,edge+thickness,w-edge-thickness,h-edge-thickness);
buf.setColor(0).fillPoly(polyArray(start, end, max)); buf.setColor(0).fillPoly(polyArray(start, end, max));
img.palette = pal1; img.palette = pal1;
g.drawImage(img, 0, 0); // Draws the filled-in segment g.drawImage(img, 0, 0); // Draws the filled-in segment
@ -597,7 +601,6 @@ var drawTimeout;
function queueDraw() { function queueDraw() {
let now = Date.now(); let now = Date.now();
let delay = settings.ring == 'Seconds' ? sec_update - (now % sec_update) : 60000 - (now % 60000); let delay = settings.ring == 'Seconds' ? sec_update - (now % sec_update) : 60000 - (now % 60000);
print(delay);
if (drawTimeout) clearTimeout(drawTimeout); if (drawTimeout) clearTimeout(drawTimeout);
drawTimeout = setTimeout(function() { drawTimeout = setTimeout(function() {
drawTimeout = undefined; drawTimeout = undefined;