13 lines
623 B
TypeScript
13 lines
623 B
TypeScript
export interface Point { x: number; y: number }
|
|
export interface Size { width: number; height: number }
|
|
export interface PreviewRect extends Size { left: number; top: number }
|
|
/** The image rectangle includes zoom, pane resizing, and scroll offsets. */
|
|
export function mapPoint(clientX: number, clientY: number, rect: PreviewRect, screen: Size): Point {
|
|
const x = (clientX - rect.left) / rect.width
|
|
const y = (clientY - rect.top) / rect.height
|
|
return {
|
|
x: Math.max(0, Math.min(screen.width - 1, Math.floor(x * screen.width))),
|
|
y: Math.max(0, Math.min(screen.height - 1, Math.floor(y * screen.height))),
|
|
}
|
|
}
|