space-browser / commits / 4507154
feat: camera control
- Panning - Zoom (clamped within reasonable definition).
@@ -1,16 +1,34 @@
11 import { connect, type Remote } from './net';
22 import { makeShip, step, type Ship } from './sim';
3-import { View, CELL, CELL_WORLD } from './view';
3+import { View, CELL, CELL_WORLD, ZOOM_MIN, ZOOM_MAX, type Camera } from './view';
44
5-const view = new View(document.getElementById('view') as HTMLCanvasElement);
5+const canvas = document.getElementById('view') as HTMLCanvasElement;
6+const view = new View(canvas);
67 addEventListener('resize', () => view.resize());
78
89 const own = makeShip(0, '', 0, 0);
910 const others = new Map<number, Ship>();
1011 let marker: { x: number; y: number } | null = null;
11-let cam = { x: 0, y: 0 };
12+const cam: Camera & { follow: boolean } = { x: 0, y: 0, zoom: 1, follow: true };
13+const keys = new Set<string>();
14+let drag: { x: number; y: number } | null = null;
1215 let sendIn = 0;
1316
17+const wpc = () => CELL_WORLD / cam.zoom; // world units per character cell
18+const wpp = () => wpc() / CELL; // world units per css px
19+const pan = (dx: number, dy: number) => {
20+ cam.x += dx;
21+ cam.y += dy;
22+ cam.follow = false;
23+};
24+const at = (e: MouseEvent) => {
25+ const r = canvas.getBoundingClientRect(); // the camera is centered on the canvas, not the window
26+ return {
27+ x: cam.x + (e.clientX - r.left - r.width / 2) * wpp(),
28+ y: cam.y - (e.clientY - r.top - r.height / 2) * wpp(), // screen y points down, world y up
29+ };
30+};
31+
1432 const net = connect(
1533 (w) => {
1634 own.id = w.id;
@@ -36,10 +54,40 @@ const net = connect(
3654 );
3755
3856 addEventListener('pointerdown', (e) => {
39- own.tx = cam.x + (e.clientX - innerWidth / 2) * (CELL_WORLD / CELL);
40- own.ty = cam.y - (e.clientY - innerHeight / 2) * (CELL_WORLD / CELL); // screen y points down, world y up
41- marker = { x: own.tx, y: own.ty };
57+ if (e.button !== 0) {
58+ drag = { x: e.clientX, y: e.clientY };
59+ return;
60+ }
61+ const p = at(e);
62+ own.tx = p.x;
63+ own.ty = p.y;
64+ marker = { x: p.x, y: p.y };
65+});
66+
67+addEventListener('pointermove', (e) => {
68+ if (!drag) return;
69+ pan(-(e.clientX - drag.x) * wpp(), (e.clientY - drag.y) * wpp());
70+ drag = { x: e.clientX, y: e.clientY };
71+});
72+addEventListener('pointerup', () => (drag = null));
73+addEventListener('contextmenu', (e) => e.preventDefault());
74+
75+// Zoom is multiplicative and anchored on the world point under the cursor.
76+addEventListener('wheel', (e) => {
77+ const a = at(e);
78+ const z = Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, cam.zoom * Math.exp(-e.deltaY * 0.0015)));
79+ if (z === cam.zoom) return;
80+ cam.zoom = z;
81+ const b = at(e);
82+ cam.x += a.x - b.x;
83+ cam.y += a.y - b.y;
84+});
85+
86+addEventListener('keydown', (e) => {
87+ keys.add(e.code);
88+ if (e.code === 'Space') cam.follow = true;
4289 });
90+addEventListener('keyup', (e) => keys.delete(e.code));
4391
4492 let last = performance.now();
4593 function frame(): void {
@@ -53,8 +101,18 @@ function frame(): void {
53101 s.x += (s.tx - s.x) * k;
54102 s.y += (s.ty - s.y) * k;
55103 }
56- cam.x += (own.x - cam.x) * (1 - Math.exp(-dt * 8));
57- cam.y += (own.y - cam.y) * (1 - Math.exp(-dt * 8));
104+ // Pan on screen-space speed so it feels the same at every zoom.
105+ const px = (keys.has('KeyD') || keys.has('ArrowRight') ? 1 : 0) - (keys.has('KeyA') || keys.has('ArrowLeft') ? 1 : 0);
106+ const py = (keys.has('KeyW') || keys.has('ArrowUp') ? 1 : 0) - (keys.has('KeyS') || keys.has('ArrowDown') ? 1 : 0);
107+ if (px || py) {
108+ const s = (30 * wpc() * dt) / Math.hypot(px, py); // 30 cells / second
109+ pan(px * s, py * s);
110+ }
111+ if (cam.follow) {
112+ const k = 1 - Math.exp(-dt * 8);
113+ cam.x += (own.x - cam.x) * k;
114+ cam.y += (own.y - cam.y) * k;
115+ }
58116
59117 sendIn += dt;
60118 if (sendIn > 1 / 30) {
@@ -5,9 +5,13 @@ import * as THREE from 'three';
55 import type { Ship } from './sim';
66
77 export const CELL = 12; // css px per character cell
8-export const CELL_WORLD = 2; // world units per character cell
8+export const CELL_WORLD = 2; // world units per character cell at zoom 1
9+export const ZOOM_MIN = 0.6; // furthest out: past this a hull stops reading as a hull
10+export const ZOOM_MAX = 2; // closest in
911 const SHIP_R = 11; // world radius of a hull
1012
13+export type Camera = { x: number; y: number; zoom: number };
14+
1115 const GLYPHS = ' .:-=+*#%@';
1216 const PALETTE = [0x0c0803, 0x241806, 0x3d2709, 0x57370c, 0x754c11, 0x98631a, 0xc78a20, 0xffe9a8];
1317 const ATLAS_CELL = 32;
@@ -153,18 +157,21 @@ export class View {
153157 this.canvas.style.width = `${w}px`;
154158 this.canvas.style.height = `${h}px`;
155159 this.rt.setSize(this.cols, this.rows);
156- const hw = (this.cols / 2) * CELL_WORLD;
157- const hh = (this.rows / 2) * CELL_WORLD;
160+ this.pass.uniforms.uCells.value.set(this.cols, this.rows);
161+ this.pass.uniforms.uCellPx.value = CELL * dpr;
162+ }
163+
164+ draw(cam: Camera, ships: Ship[], ownId: number, marker: { x: number; y: number } | null): void {
165+ // Zoom is world units per character cell: the raster keeps one texel per
166+ // cell, so zooming only changes how much world a cell covers.
167+ const wpc = CELL_WORLD / cam.zoom;
168+ const hw = (this.cols / 2) * wpc;
169+ const hh = (this.rows / 2) * wpc;
158170 this.camera.left = -hw;
159171 this.camera.right = hw;
160172 this.camera.top = hh;
161173 this.camera.bottom = -hh;
162174 this.camera.updateProjectionMatrix();
163- this.pass.uniforms.uCells.value.set(this.cols, this.rows);
164- this.pass.uniforms.uCellPx.value = CELL * dpr;
165- }
166-
167- draw(cam: { x: number; y: number }, ships: Ship[], ownId: number, marker: { x: number; y: number } | null): void {
168175 for (const s of ships) {
169176 let g = this.hulls.get(s.id);
170177 if (!g) {
@@ -183,7 +190,7 @@ export class View {
183190 this.marker.visible = marker !== null;
184191 if (marker) {
185192 // Snap to the rendered cell grid, or a one-cell marker lands between four.
186- const snap = (v: number, c: number) => c + (Math.floor((v - c) / CELL_WORLD) + 0.5) * CELL_WORLD;
193+ const snap = (v: number, c: number) => c + (Math.floor((v - c) / wpc) + 0.5) * wpc;
187194 this.marker.position.set(snap(marker.x, cam.x), snap(marker.y, cam.y), 0.5);
188195 }