diff --git a/frontend/src/arthur/domain/warp.cljs b/frontend/src/arthur/domain/warp.cljs index 1761559..b5ea2bf 100644 --- a/frontend/src/arthur/domain/warp.cljs +++ b/frontend/src/arthur/domain/warp.cljs @@ -28,11 +28,18 @@ (defn- local-field "Kind `kind`'s field about the origin, as (fn [out x y]) writing the point `(x, y)` shows into `out`. nil for a kind there is none of." - [kind a s ph] + [kind a s ph rim] (case kind - ;; Everything (1 + a) times as big. A lens cannot shrink past nothing. - :lens (let [k (/ 1 (max 0.01 (+ 1 a)))] - (fn [^js out x y] (aset out 0 (* x k)) (aset out 1 (* y k)))) + ;; Glass that bulges: (1 + a) times as big in the middle, easing back to + ;; as it is at the rim, so the edge meets what is outside it with no seam + ;; and what is near it is squeezed round the curve — Photoshop's + ;; Spherize. A negative amount pinches. Outside the rim, as it is. + :lens (let [rr (* rim rim)] + (fn [^js out x y] + (let [t2 (/ (+ (* x x) (* y y)) rr) + k (if (< t2 1) (/ 1 (max 0.01 (+ 1 (* a (- 1 t2))))) 1)] + (aset out 0 (* x k)) + (aset out 1 (* y k))))) ;; Each tile of glass shows what is behind its own middle: 1 is one flat ;; colour per tile, 2 the tile's view turned over, as a glass brick does. :tiles (fn [^js out x y] @@ -56,13 +63,30 @@ (aset out 1 y)) nil)) +(defn- rim + "How far shape op `op` reaches from its own middle, in its own space: the + farthest of its points, through `inv`. A lens is back to as it is there, so + its edge is the edge of the glass drawn, at any size." + [{:keys [kind ^js pts n r size]} ^js inv m] + (case kind + :poly (reduce (fn [far i] + (let [x (aget pts (* 2 i)) y (aget pts (inc (* 2 i)))] + (max far (js/Math.hypot + (+ (* (aget inv 0) x) (* (aget inv 2) y) (aget inv 4)) + (+ (* (aget inv 1) x) (* (aget inv 3) y) (aget inv 5)))))) + 0 (range n)) + :disc (/ r (node/mean-scale m)) + :rect (/ (* size js/Math.SQRT1_2) (node/mean-scale m)) + 0)) + (defn field "Warp op `op`'s field in stage space: (fn [out x y]) writing into `out` the stage point that stage point `(x, y)` shows. nil when it has none — a kind there is no field for, or a shape scaled to nothing, which covers nothing." - [{:keys [warp m amount size phase]}] + [{:keys [warp m amount size phase] :as op}] (when-let [^js inv (node/invert m)] - (when-let [f (local-field warp (or amount 0) (max 1 (or size 1)) (or phase 0))] + (when-let [f (local-field warp (or amount 0) (max 1 (or size 1)) (or phase 0) + (when (= :lens warp) (max 1 (rim op inv m))))] (fn [^js out x y] (f out (+ (* (aget inv 0) x) (* (aget inv 2) y) (aget inv 4)) diff --git a/frontend/src/arthur/ui/palette.cljs b/frontend/src/arthur/ui/palette.cljs index a0a3538..9d3c8f5 100644 --- a/frontend/src/arthur/ui/palette.cljs +++ b/frontend/src/arthur/ui/palette.cljs @@ -93,7 +93,7 @@ (def warps "Each warp a shape can be: its kind, its name and what it does." - [[:lens "lens" "lens — magnifies what is under it about its middle. How much is its warp channel."] + [[:lens "lens" "lens — glass that bulges: what is under its middle is magnified, easing back to as it is at its edge. How much is its warp channel; below 0 it pinches."] [:tiles "glass tiles" "glass tiles — each tile shows what is behind it, turned over"] [:wave "wave" "wave — slides what is under it side to side, as water does. Key its phase to run it."] [:ripple "ripple" "ripple — rings out from its middle, as a stone in a pond. Key its phase to run it."] diff --git a/frontend/test/arthur/domain/warp_test.cljs b/frontend/test/arthur/domain/warp_test.cljs index a8bc614..54f36a4 100644 --- a/frontend/test/arthur/domain/warp_test.cljs +++ b/frontend/test/arthur/domain/warp_test.cljs @@ -6,7 +6,8 @@ [arthur.domain.paint :as paint] [arthur.domain.palette :as pal] [arthur.domain.pick :as pick] - [arthur.domain.raster :as raster])) + [arthur.domain.raster :as raster] + [arthur.domain.warp :as warp])) (defn- sq [x0 y0 x1 y1] [x0 y0 x1 y0 x1 y1 x0 y1]) @@ -53,6 +54,25 @@ (is (= [:lens] (pick/hit (:ops d) [12 12] [:lens])) "unless the glass is what is selected, which is how it is dragged"))) +(deftest a-lens-bulges-and-has-no-seam + ;; A round glass of radius 10 about (50, 50): magnified in the middle, as it + ;; is at its rim and outside it, and squeezed in between. + (let [ring (into [] (mapcat #(let [t (* % (/ js/Math.PI 16))] + [(* 10 (js/Math.cos t)) (* 10 (js/Math.sin t))])) + (range 32)) + op (fn [a] {:kind :poly :pts (js/Float64Array. (clj->js (map-indexed #(+ %2 50) ring))) + :n 32 :warp :lens :amount a + :m (js/Float64Array. #js [1 0 0 1 50 50])}) + shows (fn [a x] (aget ((warp/field (op a)) (js/Float64Array. 2) x 50) 0)) + near? (fn [a b] (< (js/Math.abs (- a b)) 1e-9))] + (is (< (js/Math.abs (- 50.5 (shows 1 51))) 0.01) "twice the size at the middle: one pixel out shows half of one") + (is (near? 60 (shows 1 60)) "as it is at the rim, so the edge meets what is outside it") + (is (near? 63 (shows 1 63)) "and outside it") + (is (< (- (shows 1 54) (shows 1 53)) 1) "magnified across its middle two thirds") + (is (> (- (shows 1 59.5) (shows 1 58.5)) 1) "and squeezed round the curve at it") + (is (> (shows -0.5 51) 51) "below 0 it pinches") + (is (near? 55 (shows 0 55)) "and at 0 it is not there"))) + (deftest a-warp-at-amount-zero-is-not-there ;; Amount 0 is untouched for every kind, which is what lets a tween from 0 ;; bring any of them in from nothing. @@ -94,7 +114,8 @@ frame (fn [f] (let [ops ((clip/resolver doc :main nil (pal/compile doc) nil) f)] (raster/draw-ops! (raster/clear! (raster/make 320 200) 0) ops))) wide (fn [f] (count (filter #(= 4 (aget (:buf (frame f)) (+ % (* 21 320)))) (range 10 30))))] - (is (= [2 4 6] (mapv wide [0 1 2])) "1×, 2×, 3× through the tween"))) + (is (= [2 4 5] (mapv wide [0 1 2])) + "wider through the tween: 2× and 3× in the middle, easing off toward the rim"))) (deftest a-lens-in-a-symbol-magnifies-what-is-outside-the-symbol ;; The point of a lens is to go BETWEEN things, so a lens a symbol holds sees