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:
parent
bebc2daa95
commit
a40480a464
3 changed files with 81 additions and 19 deletions
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue