@hiddentao/clockwork-engine
Version:
A TypeScript/PIXI.js game engine for deterministic, replayable games with built-in rendering
36 lines (35 loc) • 1.04 kB
JavaScript
/**
* Coordinate Transform Utilities
*
* Shared functions for world-to-screen and screen-to-world coordinate transformations.
*/
/**
* Transform world coordinates to screen coordinates
*
* @param worldX World X coordinate
* @param worldY World Y coordinate
* @param viewportPosition Viewport position in world space
* @param zoom Viewport zoom level
* @returns Screen coordinates
*/
export function worldToScreen(worldX, worldY, viewportPosition, zoom) {
return {
x: (worldX - viewportPosition.x) * zoom,
y: (worldY - viewportPosition.y) * zoom,
};
}
/**
* Transform screen coordinates to world coordinates
*
* @param screenX Screen X coordinate
* @param screenY Screen Y coordinate
* @param viewportPosition Viewport position in world space
* @param zoom Viewport zoom level
* @returns World coordinates
*/
export function screenToWorld(screenX, screenY, viewportPosition, zoom) {
return {
x: screenX / zoom + viewportPosition.x,
y: screenY / zoom + viewportPosition.y,
};
}