54 lines
1.7 KiB
TypeScript
54 lines
1.7 KiB
TypeScript
/**
|
||
* 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 */
|
||
}
|
||
}
|
||
}
|