add interpolation to cursor reticles

cursor reticles are now interpolated to the update interval, so they should be smooth at > 60 fps
This commit is contained in:
りき萌 2025-06-26 18:48:16 +02:00
parent bebc2daa95
commit a40480a464
3 changed files with 81 additions and 19 deletions

View file

@ -20,18 +20,34 @@ export function listen(...listenerSpecs) {
}
export function debounce(time, fn) {
// This function is kind of tricky, but basically:
//
// - we want to guarantee `fn` is called at most once every `time` milliseconds
// - at the same time, in case debounced `fn` is called during an ongoing timeout, we want to
// queue up another run, and run it immediately after `time` passes
// - at the same time, in case this catch-up condition occurs, we must also ensure there's a
// delay after `fn` is called
//
// yielding the recursive solution below.
let timeout = null;
let queued = null;
const callFn = (args) => {
fn(...args);
timeout = setTimeout(() => {
timeout = null;
if (queued != null) {
callFn(queued);
queued = null;
}
}, time);
};
return (...args) => {
if (timeout == null) {
fn(...args);
timeout = setTimeout(() => {
timeout = null;
if (queued != null) {
fn(...queued);
queued = null;
}
}, time);
callFn(args);
} else {
queued = args;
}