aboutsummaryrefslogtreecommitdiff
path: root/static/src/mouse_controller.ts
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth@simponic.xyz>2024-09-02 20:10:35 -0700
committerElizabeth Hunt <elizabeth@simponic.xyz>2024-09-02 20:10:35 -0700
commitd35ab6cc851d3fc85f55cfb5676f8dc3552fab24 (patch)
treebef9784e90e2e8f8f5b9e05300238747b775e618 /static/src/mouse_controller.ts
parenta0a2068b66204d7d06eb3c3b4aa61b28469121f6 (diff)
downloadkennel.hatecomputers.club-d35ab6cc851d3fc85f55cfb5676f8dc3552fab24.tar.gz
kennel.hatecomputers.club-d35ab6cc851d3fc85f55cfb5676f8dc3552fab24.zip
a bit more progress
Diffstat (limited to 'static/src/mouse_controller.ts')
-rw-r--r--static/src/mouse_controller.ts42
1 files changed, 21 insertions, 21 deletions
diff --git a/static/src/mouse_controller.ts b/static/src/mouse_controller.ts
index b8849b4..c7ae304 100644
--- a/static/src/mouse_controller.ts
+++ b/static/src/mouse_controller.ts
@@ -1,21 +1,23 @@
import { Vec2 } from "./vector";
-
export class MouseController {
- private readonly debounce_ms = 400;
- private readonly movement_threshold = 40;
private last_event_time = Date.now();
- private movement_queue: Vec2[] = [];
- private interval_id: number | null = null;
+ private last_movement: Vec2 | undefined;
+ private interval_id: number | undefined;
- constructor(private readonly callback: (new_movement: Vec2) => void) {}
+ constructor(
+ private readonly publisher: (new_movement: Vec2) => void | Promise<void>,
+ private readonly debounce_ms = 200,
+ private readonly l2_norm_threshold = 40,
+ ) {}
public start() {
- if (this.interval_id !== null) {
+ if (typeof this.interval_id !== "undefined") {
return;
}
- this.interval_id = setInterval(() => {
- this.publish_movement();
- }, this.debounce_ms);
+ this.interval_id = setInterval(
+ () => this.publish_movement(),
+ this.debounce_ms,
+ );
}
public stop() {
@@ -23,32 +25,30 @@ export class MouseController {
return;
}
clearInterval(this.interval_id);
- this.interval_id = null;
+ delete this.interval_id;
}
public move(x: number, y: number) {
const new_movement = new Vec2(x, y);
- const last_movement = this.movement_queue.at(-1);
- this.movement_queue.push(new_movement);
if (
- typeof last_movement === "undefined" ||
- new_movement.distance_to(last_movement) < this.movement_threshold
+ typeof this.last_movement !== "undefined" &&
+ new_movement.distance_to(this.last_movement) >= this.l2_norm_threshold
) {
- return;
+ this.publish_movement();
}
- this.publish_movement();
+ this.last_movement = new_movement;
}
private publish_movement() {
if (
- Date.now() - this.last_event_time < this.debounce_ms ||
- this.movement_queue.length === 0
+ typeof this.last_movement === "undefined" ||
+ Date.now() - this.last_event_time < this.debounce_ms
) {
return;
}
this.last_event_time = Date.now();
- this.callback(this.movement_queue.at(-1)!);
- this.movement_queue = [];
+ this.publisher(this.last_movement.copy());
+ delete this.last_movement;
}
}