Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(glyphgrid): camera math

enes69e44db0d6f2
diff --git a/src/renderer/glyphgrid/camera.test.ts b/src/renderer/glyphgrid/camera.test.ts
new file mode 100644
index 000000000..0bf289b06
--- /dev/null
+++ b/src/renderer/glyphgrid/camera.test.ts
@@ -0,0 +1,36 @@
+import { describe, expect, it } from 'vitest'
+import {
+ rectsIntersect,
+ screenToWorld,
+ visibleWorldRect,
+ worldToScreen,
+ type Camera
+} from './camera'
+
+describe('glyphgrid camera', () => {
+ const cam: Camera = { x: 100, y: -50, zoom: 0.5 }
+
+ it('worldToScreen matches the React Flow viewport convention (screen = world*zoom + pan)', () => {
+ expect(worldToScreen(0, 0, cam)).toEqual({ x: 100, y: -50 })
+ expect(worldToScreen(200, 300, cam)).toEqual({ x: 200, y: 100 })
+ })
+
+ it('screenToWorld inverts worldToScreen exactly', () => {
+ const s = worldToScreen(123.5, -77.25, cam)
+ const w = screenToWorld(s.x, s.y, cam)
+ expect(w.x).toBeCloseTo(123.5, 10)
+ expect(w.y).toBeCloseTo(-77.25, 10)
+ })
+
+ it('visibleWorldRect maps the screen viewport into world space', () => {
+ const r = visibleWorldRect(cam, 800, 600)
+ expect(r).toEqual({ x: -200, y: 100, w: 1600, h: 1200 })
+ })
+
+ it('rectsIntersect: overlapping, touching and disjoint', () => {
+ const a = { x: 0, y: 0, w: 10, h: 10 }
+ expect(rectsIntersect(a, { x: 5, y: 5, w: 10, h: 10 })).toBe(true)
+ expect(rectsIntersect(a, { x: 10, y: 0, w: 5, h: 5 })).toBe(false) // edge-touch = not visible
+ expect(rectsIntersect(a, { x: 20, y: 20, w: 1, h: 1 })).toBe(false)
+ })
+})
diff --git a/src/renderer/glyphgrid/camera.ts b/src/renderer/glyphgrid/camera.ts
new file mode 100644
index 000000000..3d57959a5
--- /dev/null
+++ b/src/renderer/glyphgrid/camera.ts
@@ -0,0 +1,33 @@
+/** Pan/zoom camera, IDENTICAL in convention to React Flow's Viewport: x/y are the pan offsets
+ * in screen pixels, zoom is the scale. screen = world * zoom + pan. Pure module. */
+export interface Camera {
+ x: number
+ y: number
+ zoom: number
+}
+
+export interface Rect {
+ x: number
+ y: number
+ w: number
+ h: number
+}
+
+export function worldToScreen(wx: number, wy: number, cam: Camera): { x: number; y: number } {
+ return { x: wx * cam.zoom + cam.x, y: wy * cam.zoom + cam.y }
+}
+
+export function screenToWorld(sx: number, sy: number, cam: Camera): { x: number; y: number } {
+ return { x: (sx - cam.x) / cam.zoom, y: (sy - cam.y) / cam.zoom }
+}
+
+/** The world-space rectangle the screen viewport currently shows — the culling input. */
+export function visibleWorldRect(cam: Camera, viewW: number, viewH: number): Rect {
+ const tl = screenToWorld(0, 0, cam)
+ return { x: tl.x, y: tl.y, w: viewW / cam.zoom, h: viewH / cam.zoom }
+}
+
+/** Strict overlap (shared edge does not count — an edge-touching grid draws zero pixels). */
+export function rectsIntersect(a: Rect, b: Rect): boolean {
+ return a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h
+}