Files
web-cad/frontend/src/interaction/pointer.ts
T
2026-08-26 21:14:46 +02:00

54 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.
/**
* pointer.ts DOM-PointerEvent → ToolPointerEvent Normalisierung (Task A2).
* Konvertiert Bildschirmkoordinaten in Weltkoordinaten über ZoomPanController.
*/
import type { ToolPointerEvent } from '../plugins/types';
import type { ZoomPanController } from '../canvas/ZoomPanController';
/**
* Normalisiert ein Pointer/Mouse-Event zu einem ToolPointerEvent.
* @param e DOM-Event (MouseEvent ist Obermenge von PointerEvent-Feldern)
* @param canvas Das Canvas-Element (für offset-Koordinaten)
* @param zoomPan aktiver Zoom/Pan-Controller
*/
export function toToolPointerEvent(
e: MouseEvent,
canvas: HTMLCanvasElement,
zoomPan: ZoomPanController,
): ToolPointerEvent {
const rect = canvas.getBoundingClientRect();
const sx = e.clientX - rect.left;
const sy = e.clientY - rect.top;
const world = zoomPan.screenToWorld(sx, sy);
return {
world: { x: world.x, y: world.y },
screen: { x: sx, y: sy },
button: e.button,
shift: e.shiftKey,
alt: e.altKey,
ctrl: e.ctrlKey || e.metaKey,
};
}
/** Fangt weitere Pointer-Events auf dem Canvas bis zum release (Pen/Tablet- Komfort). */
export function capturePointer(canvas: HTMLCanvasElement, e: PointerEvent): void {
if (typeof canvas.setPointerCapture === 'function') {
try {
canvas.setPointerCapture(e.pointerId);
} catch {
/* pointerId evtl. schon ungültig — unkritisch */
}
}
}
/** Gibt einen vorherigen Pointer-Capture frei. */
export function releasePointer(canvas: HTMLCanvasElement, e: PointerEvent): void {
if (typeof canvas.releasePointerCapture === 'function') {
try {
canvas.releasePointerCapture(e.pointerId);
} catch {
/* ignorieren */
}
}
}