export interface CameraAxialPoint { q: number; r: number; } export interface CameraStateLike { center: CameraAxialPoint; zoom: number; viewportWidth: number; viewportHeight: number; } export interface CameraWorldBounds { minX: number; maxX: number; minY: number; maxY: number; width: number; height: number; } export interface CameraViewportInsets { top?: number; right?: number; bottom?: number; left?: number; } export function createCameraWorldSignature(params: { radius: number; bounds: CameraWorldBounds; nodes: Array; }) { const source = JSON.stringify({ radius: params.radius, bounds: [params.bounds.minX, params.bounds.maxX, params.bounds.minY, params.bounds.maxY] .map((value) => Math.round(value * 1000) / 1000), nodes: [...params.nodes] .sort((left, right) => left.id.localeCompare(right.id)) .map((node) => [node.id, node.q, node.r]), }); let hash = 2166136261; for (let index = 0; index < source.length; index += 1) { hash ^= source.charCodeAt(index); hash = Math.imul(hash, 16777619); } return (hash >>> 0).toString(36); } export function cameraAxialToPixel(point: CameraAxialPoint, radius: number) { return { x: radius * Math.sqrt(3) * (point.q + point.r / 2), y: radius * 1.5 * point.r, }; } export function cameraPixelToAxial(point: { x: number; y: number }, radius: number) { const r = (2 * point.y) / (3 * radius); const q = point.x / (Math.sqrt(3) * radius) - r / 2; return { q, r }; } export function cameraToViewBox(camera: CameraStateLike, radius: number) { const zoom = Math.max(Number(camera.zoom) || 1, 0.0001); const width = Math.max(Number(camera.viewportWidth) || 1, 1) / zoom; const height = Math.max(Number(camera.viewportHeight) || 1, 1) / zoom; const center = cameraAxialToPixel(camera.center || { q: 0, r: 0 }, radius); return { x: center.x - width / 2, y: center.y - height / 2, width, height, }; } export function clampCameraToWorld( camera: CameraStateLike, bounds: CameraWorldBounds, radius: number, ) { const zoom = Math.min(Math.max(Number(camera.zoom) || 1, 0.5), 2.4); const viewportWidth = Math.max(Number(camera.viewportWidth) || 1, 1); const viewportHeight = Math.max(Number(camera.viewportHeight) || 1, 1); const centerPx = cameraAxialToPixel(camera.center || { q: 0, r: 0 }, radius); const boundsWidth = Math.max(bounds.width, radius * 8); const boundsHeight = Math.max(bounds.height, radius * 8); const minZoom = Math.max(viewportWidth / boundsWidth, viewportHeight / boundsHeight, 0.0001); const safeZoom = Math.max(zoom, minZoom); const halfWidth = viewportWidth / (2 * safeZoom); const halfHeight = viewportHeight / (2 * safeZoom); const centerX = Math.min(Math.max(centerPx.x, bounds.minX + halfWidth), bounds.maxX - halfWidth); const centerY = Math.min(Math.max(centerPx.y, bounds.minY + halfHeight), bounds.maxY - halfHeight); return { center: cameraPixelToAxial({ x: centerX, y: centerY }, radius), zoom: safeZoom, viewportWidth, viewportHeight, }; } export function ensureCameraTargetVisible(params: { camera: CameraStateLike; target: CameraAxialPoint; radius: number; safeAreaRatio: number; viewportInsets?: CameraViewportInsets; }) { const { camera, target, radius } = params; const viewBox = cameraToViewBox(camera, radius); const targetPosition = cameraAxialToPixel(target, radius); const ratio = Math.min(0.35, Math.max(0, params.safeAreaRatio)); const insetX = Math.min(viewBox.width * 0.4, Math.max(radius * 2, viewBox.width * ratio)); const insetY = Math.min(viewBox.height * 0.4, Math.max(radius * 2, viewBox.height * ratio)); const zoom = Math.max(Number(camera.zoom) || 1, 0.0001); const viewportInsets = params.viewportInsets ?? {}; const leftInset = Math.min(viewBox.width * 0.45, Math.max(insetX, (viewportInsets.left ?? 0) / zoom)); const rightInset = Math.min(viewBox.width * 0.45, Math.max(insetX, (viewportInsets.right ?? 0) / zoom)); const topInset = Math.min(viewBox.height * 0.45, Math.max(insetY, (viewportInsets.top ?? 0) / zoom)); const bottomInset = Math.min(viewBox.height * 0.45, Math.max(insetY, (viewportInsets.bottom ?? 0) / zoom)); const safeLeft = viewBox.x + leftInset; const safeRight = viewBox.x + viewBox.width - rightInset; const safeTop = viewBox.y + topInset; const safeBottom = viewBox.y + viewBox.height - bottomInset; const shiftX = targetPosition.x < safeLeft ? targetPosition.x - safeLeft : targetPosition.x > safeRight ? targetPosition.x - safeRight : 0; const shiftY = targetPosition.y < safeTop ? targetPosition.y - safeTop : targetPosition.y > safeBottom ? targetPosition.y - safeBottom : 0; if (Math.abs(shiftX) < 0.5 && Math.abs(shiftY) < 0.5) return null; const center = cameraAxialToPixel(camera.center, radius); return { ...camera, center: cameraPixelToAxial({ x: center.x + shiftX, y: center.y + shiftY, }, radius), }; } export function fitCameraToEntities(params: { entities: CameraAxialPoint[]; viewportWidth: number; viewportHeight: number; radius: number; padding?: number; }) { const { entities, viewportWidth, viewportHeight, radius, padding = radius * 4 } = params; if (!entities.length) { return { center: { q: 0, r: 0 }, zoom: 1, viewportWidth, viewportHeight }; } const points = entities.map((entity) => cameraAxialToPixel(entity, radius)); const minX = Math.min(...points.map((point) => point.x)); const maxX = Math.max(...points.map((point) => point.x)); const minY = Math.min(...points.map((point) => point.y)); const maxY = Math.max(...points.map((point) => point.y)); const spanX = Math.max(maxX - minX + padding * 2, radius * 8); const spanY = Math.max(maxY - minY + padding * 2, radius * 8); const zoomX = viewportWidth > 0 ? viewportWidth / spanX : 1; const zoomY = viewportHeight > 0 ? viewportHeight / spanY : 1; return { center: { q: Math.round(entities.reduce((sum, entity) => sum + entity.q, 0) / entities.length), r: Math.round(entities.reduce((sum, entity) => sum + entity.r, 0) / entities.length), }, zoom: Math.min(zoomX, zoomY, 1), viewportWidth, viewportHeight, }; }