2025-03-14 23:40:43 +05:30
|
|
|
import { BROWSER_DEFAULT_HEIGHT, BROWSER_DEFAULT_WIDTH } from "../constants/const";
|
2025-03-14 12:41:44 +05:30
|
|
|
|
|
|
|
|
export class CoordinateMapper {
|
|
|
|
|
private canvasWidth: number;
|
|
|
|
|
private canvasHeight: number;
|
|
|
|
|
private browserWidth: number;
|
|
|
|
|
private browserHeight: number;
|
|
|
|
|
|
|
|
|
|
constructor(
|
2025-03-15 03:57:54 +05:30
|
|
|
canvasWidth: number = window.innerWidth * 0.7,
|
2025-03-17 23:20:19 +05:30
|
|
|
canvasHeight: number = window.innerHeight * 0.64,
|
2025-03-14 12:41:44 +05:30
|
|
|
browserWidth: number = BROWSER_DEFAULT_WIDTH,
|
|
|
|
|
browserHeight: number = BROWSER_DEFAULT_HEIGHT
|
|
|
|
|
) {
|
|
|
|
|
this.canvasWidth = canvasWidth;
|
|
|
|
|
this.canvasHeight = canvasHeight;
|
|
|
|
|
this.browserWidth = browserWidth;
|
|
|
|
|
this.browserHeight = browserHeight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mapCanvasToBrowser(coord: { x: number, y: number }): { x: number, y: number } {
|
|
|
|
|
return {
|
|
|
|
|
x: (coord.x / this.canvasWidth) * this.browserWidth,
|
|
|
|
|
y: (coord.y / this.canvasHeight) * this.browserHeight
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mapBrowserToCanvas(coord: { x: number, y: number }): { x: number, y: number } {
|
|
|
|
|
return {
|
|
|
|
|
x: (coord.x / this.browserWidth) * this.canvasWidth,
|
|
|
|
|
y: (coord.y / this.browserHeight) * this.canvasHeight
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mapBrowserRectToCanvas(rect: DOMRect): DOMRect {
|
|
|
|
|
const topLeft = this.mapBrowserToCanvas({ x: rect.left, y: rect.top });
|
|
|
|
|
const bottomRight = this.mapBrowserToCanvas({ x: rect.right, y: rect.bottom });
|
|
|
|
|
|
|
|
|
|
const width = bottomRight.x - topLeft.x;
|
|
|
|
|
const height = bottomRight.y - topLeft.y;
|
|
|
|
|
|
|
|
|
|
return new DOMRect(
|
|
|
|
|
topLeft.x,
|
|
|
|
|
topLeft.y,
|
|
|
|
|
width,
|
|
|
|
|
height
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mapCanvasRectToBrowser(rect: DOMRect): DOMRect {
|
|
|
|
|
const topLeft = this.mapCanvasToBrowser({ x: rect.left, y: rect.top });
|
|
|
|
|
const bottomRight = this.mapCanvasToBrowser({ x: rect.right, y: rect.bottom });
|
|
|
|
|
|
|
|
|
|
const width = bottomRight.x - topLeft.x;
|
|
|
|
|
const height = bottomRight.y - topLeft.y;
|
|
|
|
|
|
|
|
|
|
return new DOMRect(
|
|
|
|
|
topLeft.x,
|
|
|
|
|
topLeft.y,
|
|
|
|
|
width,
|
|
|
|
|
height
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateDimensions(canvasWidth?: number, canvasHeight?: number, browserWidth?: number, browserHeight?: number) {
|
|
|
|
|
if (canvasWidth) this.canvasWidth = canvasWidth;
|
|
|
|
|
if (canvasHeight) this.canvasHeight = canvasHeight;
|
|
|
|
|
if (browserWidth) this.browserWidth = browserWidth;
|
|
|
|
|
if (browserHeight) this.browserHeight = browserHeight;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const coordinateMapper = new CoordinateMapper();
|