Skip to content

Commit a606331

Browse files
committed
Circle maps aren't regenerated anymore
This is a small optimisation that reduces compute and memory allocations when drawing. While probably not an issue for most cases, there's one device so slow, that the line actually lags behind, I was wondering whether I can do anything to improve this. We'll see if it works.
1 parent 98cf1e3 commit a606331

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

internal/frontend/resources/draw.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,17 @@ function drawBresenhamLine(imageData, x1, y1, x2, y2, color) {
178178
}
179179
}
180180

181+
// We cache them, as we need quite a lot of them, but the pencil size usually
182+
// doesn't change that often. There's also not many sizes, so we don't need to
183+
// worry about invalidating anything.
184+
let cachedCircleMaps = {};
185+
181186
function generateCircleMap(radius) {
187+
const cached = cachedCircleMaps[radius];
188+
if (cached) {
189+
return cached;
190+
}
191+
182192
const diameter = 2 * radius;
183193
const circleData = new Array(diameter);
184194

@@ -196,6 +206,7 @@ function generateCircleMap(radius) {
196206
}
197207
}
198208

209+
cachedCircleMaps[radius] = circleData;
199210
return circleData;
200211
}
201212

0 commit comments

Comments
 (0)