First, I should mention that I'm pretty new to JS, so forgive me if this is a rookie mistake. I'm trying to make a custom clock for my bangle.js 2, and it involves inverting the colors of portions of the screen. I wrote a function that iterates over the pixels in a given circle and inverts them:
// swaps background and foreground colors inside any given circle
function invertCircle(x, y, rad) {
rad = Math.round(rad);
sw = g.getWidth() - 1;
bg = g.getBgColor();
fg = g.getColor();
// iterate over every onscreen pixel within the radius
let iEnd = Math.min(g.getHeight() - 1, y + rad);
for (let i = Math.max(0, y - rad); i <= iEnd; i ++) {
let chord = Math.round(Math.sqrt(Math.pow(rad, 2) - Math.pow(Math.abs(i - y), 2)));
let jEnd = Math.min(sw, x + chord);
for (let j = Math.max(0, x - chord); j <= jEnd; j ++) {
// invert pixel color
if (g.getPixel(j, i) == bg) {
g.setPixel(j, i, fg);
} else {
g.setPixel(j, i, bg);
}
}
}
}
// draw test image
g.clear();
g.setFont("Vector", 16);
g.drawLine(0, g.getHeight() / 2, g.getWidth(), g.getHeight() / 2);
g.drawString("Test\nImage", g.getWidth() / 2, g.getHeight() / 2 - 15);
invertCircle(g.getWidth() / 2, g.getHeight() / 2, 30);
When inverting larger areas, execution gets halted a few hundred milliseconds into the drawing process.
Execution Interrupted
at line 1 col 319
...(j,i)==bg){g.setPixel(j,i,fg);}else{g.setPixel(j,i,bg);}}}
^
in function "invertCircle" called from line 1 col 53
invertCircle(g.getWidth() / 2, g.getHeight() / 2, 30);
^
>echo(1)
=undefined
>
I'm guessing that this has something to do with hogging the CPU for too long, but I'm not really sure. Any advice on how to avoid halting execution and / or make this more efficient would be appreciated.
First, I should mention that I'm pretty new to JS, so forgive me if this is a rookie mistake. I'm trying to make a custom clock for my bangle.js 2, and it involves inverting the colors of portions of the screen. I wrote a function that iterates over the pixels in a given circle and inverts them:
When inverting larger areas, execution gets halted a few hundred milliseconds into the drawing process.
I'm guessing that this has something to do with hogging the CPU for too long, but I'm not really sure. Any advice on how to avoid halting execution and / or make this more efficient would be appreciated.