Skip to content

Commit 7ea854f

Browse files
authored
Update turtles.js (#5044)
Summary Optimizes window and turtle resizing behavior to feel smoother on high-end devices while preserving performance on lower-end hardware. The change adapts dynamically based on device capability and avoids introducing overhead where it could cause lag. Changes 1. Smooth Turtle Decoration Resize (js/turtle.js) High-end devices (>4 cores): Turtle decoration resize is animated using a short tween (~200ms) instead of snapping instantly. Low-end devices: Retains the original instant resize behavior to avoid performance overhead. 2. Optimized Window Resize Handling (js/turtles.js) High-end devices: Window resize events are throttled using requestAnimationFrame, allowing continuous, smooth canvas updates during resize. Low-end devices: Keeps the existing 150ms debounce logic so updates occur only after resizing ends. 3. Flicker Handling An experimental bitmap scaling approach to reduce canvas flicker during resize was reverted. The current implementation accepts native browser repaint behavior to preserve rendering correctness and simplicity. Impact Smoother, more responsive resizing on modern hardware. No regression or added overhead on lower-end devices. Improves perceived UI quality without changing existing behavior for beginners or constrained systems.
1 parent d72d0dd commit 7ea854f

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

js/turtles.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,15 +1248,32 @@ Turtles.TurtlesView = class {
12481248
__makeBoundary();
12491249
}
12501250

1251-
// Debounce the resize event to prevent performance issues
1251+
// Debounce or throttle the resize event based on device capability
12521252
let resizeTimeout;
1253+
let ticking = false;
1254+
12531255
window.addEventListener("resize", () => {
1254-
clearTimeout(resizeTimeout);
1255-
resizeTimeout = setTimeout(() => {
1256-
handleCanvasResize();
1257-
__makeBoundary();
1258-
__makeBoundary2();
1259-
}, 150); // Wait 150ms after the last resize event to execute
1256+
const isHighEndDevice =
1257+
navigator.hardwareConcurrency && navigator.hardwareConcurrency >= 4;
1258+
1259+
if (isHighEndDevice) {
1260+
if (!ticking) {
1261+
window.requestAnimationFrame(() => {
1262+
handleCanvasResize();
1263+
__makeBoundary();
1264+
__makeBoundary2();
1265+
ticking = false;
1266+
});
1267+
ticking = true;
1268+
}
1269+
} else {
1270+
clearTimeout(resizeTimeout);
1271+
resizeTimeout = setTimeout(() => {
1272+
handleCanvasResize();
1273+
__makeBoundary();
1274+
__makeBoundary2();
1275+
}, 150); // Wait 150ms after the last resize event to execute
1276+
}
12601277
});
12611278

12621279
return this;

0 commit comments

Comments
 (0)