code wiki / _hdl_build / nx_viz_zoom.nx

nx_viz_zoom.nx source

↩ module page · 21 lines · 1208 B

1// nx_viz_zoom.nx -- the ZOOM math core of the sovereign Nishi viz library's INTERACTION layer (the d3-zoom 2// transform, bits-up). Transform state st[0]=k (scale, permil 1000=1x), st[1]=tx, st[2]=ty. screen = data*k/1000+t. 3// Zooming by factor f around screen point (px,py) keeps the data point under the cursor FIXED (the d3-zoom 4// invariant). Pure integer -> emits cleanly to WASM (nx_wat_compiler) for in-browser pan/zoom. Closes interact-zoom 5// (math). license_tier: ORIGINAL 6import "nx_syscalls.nx" 7 8// zoom by factor f (permil) about screen point (px,py); mutates st in place. returns new k. 9func vz_zoom(st: *i64, px: i64, py: i64, f: i64) -> i64 { 10 let tx: i64 = st[1]; let ty: i64 = st[2] 11 st[0] = st[0] * f / 1000 12 st[1] = px - ((px - tx) * f) / 1000 13 st[2] = py - ((py - ty) * f) / 1000 14 return st[0] 15} 16// pan by (dx,dy) screen pixels. 17func vz_pan(st: *i64, dx: i64, dy: i64) -> i64 { st[1] = st[1] + dx; st[2] = st[2] + dy; return 0 } 18// apply the transform: data coord -> screen coord. 19func vz_apply_x(st: *i64, dx: i64) -> i64 { return dx * st[0] / 1000 + st[1] } 20func vz_apply_y(st: *i64, dy: i64) -> i64 { return dy * st[0] / 1000 + st[2] } 21func main() -> i64 { return 0 }