Files
tidefront/apps/web/src/map/render-loop.ts
T

36 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Karte 2: Zeichnen nach Bedarf statt in jedem Bild. Pixi zeichnet sonst 40-mal je Sekunde die ganze Karte – auch
// wenn sich nichts bewegt. Auf Geräten ohne Grafikbeschleunigung (kopflose Browser, ältere Rechner) blockierte das den
// Hauptthread (je Bild 300 ms bei 1440 × 900 @2), auf Handys kostet es Akku. Jetzt: volle Bildrate nach Eingaben und
// solange sich Ausschnitt, Zoom oder Überblendungen ändern (1,5 s nachlaufend), sonst 4 Bilder je Sekunde (die
// Verbände, das Wetter und neue Daten aktualisieren ohnehin nur alle 250 ms).
import type { Application } from "pixi.js";
const HOT_MS = 1500;
const IDLE_MS = 250;
export interface RenderLoop {
/** Am Ende jedes Ticks: zeichnet, wenn nötig. `changed`: Ausschnitt/Zoom/Überblendung hat sich geändert. */
frame: (changed: boolean) => void;
/** Für die nächsten 1,5 s mit voller Rate zeichnen. */
kick: () => void;
dispose: () => void;
}
export function renderOnDemand(app: Application, canvas: HTMLCanvasElement): RenderLoop {
app.ticker.remove(app.render, app); // kein automatisches Zeichnen in jedem Bild
let hotUntil = performance.now() + HOT_MS;
let last = 0;
const kick = () => { hotUntil = performance.now() + HOT_MS; };
const events = ["pointerdown", "pointermove", "wheel", "touchstart"] as const;
for (const e of events) canvas.addEventListener(e, kick, { passive: true });
const frame = (changed: boolean) => {
const now = performance.now();
if (changed) hotUntil = now + HOT_MS;
if (now < hotUntil || now - last >= IDLE_MS) {
last = now;
app.render();
}
};
return { frame, kick, dispose: () => { for (const e of events) canvas.removeEventListener(e, kick); } };
}