diff --git a/frontend/src/arthur/domain/outline.cljs b/frontend/src/arthur/domain/outline.cljs index ea8037b..bf33f5f 100644 --- a/frontend/src/arthur/domain/outline.cljs +++ b/frontend/src/arthur/domain/outline.cljs @@ -36,9 +36,7 @@ "The brush, `size` pixels across, from `[ax ay]` to `[bx by]`: a disc every half radius along the way, so a fast stroke is still one stroke." [m [ax ay] [bx by] size] - (let [[ox oy] (or (:origin m) [0 0]) - ax (- ax ox) ay (- ay oy) bx (- bx ox) by (- by oy) - r (max 0.5 (/ size 2)) + (let [r (max 0.5 (/ size 2)) steps (max 1 (js/Math.ceil (/ (js/Math.hypot (- bx ax) (- by ay)) (max 0.5 (/ r 2)))))] (dotimes [i (inc steps)] (let [t (/ i steps)] diff --git a/frontend/src/arthur/domain/raster.cljs b/frontend/src/arthur/domain/raster.cljs index 79fac57..38cd5ba 100644 --- a/frontend/src/arthur/domain/raster.cljs +++ b/frontend/src/arthur/domain/raster.cljs @@ -15,13 +15,18 @@ blitted inside one frame, and never stored in app-db. No DOM here. `->rgba` returns plain bytes; wrapping them in an ImageData is - ui/canvas's job, which is also what lets every assertion below run in node.") + ui/canvas's job, which is also what lets every assertion below run in node. + + A raster may have an `:origin`, the stage point its first pixel is, and ops + stay in stage pixels either way: the editor rasterises whatever part of the + work area is on screen, which need not be the stage.") (defn make [w h] {:w w :h h :buf (js/Uint8Array. (* w h))}) -(defn clear! [{:keys [buf] :as r} index] +(defn clear! [{:keys [buf cov] :as r} index] (.fill buf index) + (when cov (.fill cov 0)) r) ;; --------------------------------------------------------------------------- @@ -84,10 +89,11 @@ `pts` may be a CLJS vector or any typed array; scanline crossings are collected into a plain JS array and sorted in place. `ink` is as `plot!`'s." ([r pts n index] (fill-poly-buf! r pts n index nil)) - ([{:keys [w h buf cov] :as r} pts n index ink] + ([{:keys [w h buf cov origin] :as r} pts n index ink] (when (>= n 3) - (let [px (fn [i] (if (vector? pts) (-nth pts (* 2 i)) (aget pts (* 2 i)))) - py (fn [i] (if (vector? pts) (-nth pts (inc (* 2 i))) (aget pts (inc (* 2 i))))) + (let [[ox oy] (or origin [0 0]) + px (fn [i] (- (if (vector? pts) (-nth pts (* 2 i)) (aget pts (* 2 i))) ox)) + py (fn [i] (- (if (vector? pts) (-nth pts (inc (* 2 i))) (aget pts (inc (* 2 i)))) oy)) xs (array) ys (map py (range n)) y0 (max 0 (js/Math.ceil (- (reduce min ys) 0.5))) @@ -149,8 +155,10 @@ clamp that would flatten the performance at the extremes." ([r cx cy rad index] (fill-disc! r cx cy rad index nil nil)) ([r cx cy rad index over] (fill-disc! r cx cy rad index over nil)) - ([{:keys [w h buf cov] :as r} cx cy rad index over ink] - (let [rr (* rad rad) + ([{:keys [w h buf cov origin] :as r} cx cy rad index over ink] + (let [[ox oy] (or origin [0 0]) + cx (- cx ox) cy (- cy oy) + rr (* rad rad) y0 (max 0 (js/Math.floor (- cy rad))) y1 (min (dec h) (js/Math.ceil (+ cy rad))) x0 (max 0 (js/Math.floor (- cx rad))) @@ -183,8 +191,10 @@ breathing." ([r cx cy size index] (fill-rect! r cx cy size index nil nil)) ([r cx cy size index over] (fill-rect! r cx cy size index over nil)) - ([{:keys [w h buf cov] :as r} cx cy size index over ink] - (let [size (js/Math.round size)] + ([{:keys [w h buf cov origin] :as r} cx cy size index over ink] + (let [[ox oy] (or origin [0 0]) + cx (- cx ox) cy (- cy oy) + size (js/Math.round size)] (when (>= size 1) (let [x0 (js/Math.round (- cx (/ size 2))) y0 (js/Math.round (- cy (/ size 2))) @@ -286,13 +296,13 @@ ;; reuses its point buffers. (defonce ^:private layers (atom {})) -(defn- layer-at [depth w h] +(defn- layer-at [depth {:keys [w h origin]}] (let [l (get @layers depth)] (if (and l (= w (:w l)) (= h (:h l))) - l + (assoc l :origin origin) (let [l (layer w h)] (swap! layers assoc depth l) - l)))) + (assoc l :origin origin))))) (defn draw-ops! "Paint a list of draw ops, in the order given, into the raster. Stage 7. @@ -305,13 +315,13 @@ `:begin` and `:end` bracket the ops of a symbol drawn into a layer of its own; `:knock` on an op makes it a knockout and `:lut` a remap. See `plot!`." - [{:keys [w h] :as r} ops] + [r ops] (reduce (fn [stack {:keys [kind pts n color stencil cx cy size] :as op}] (let [top (peek stack) ink (or (:knock op) (:lut op))] (case kind - :begin (let [l (layer-at (count stack) w h)] + :begin (let [l (layer-at (count stack) r)] (.fill (:cov l) 0) (conj stack l)) :end (if (< 1 (count stack)) diff --git a/frontend/src/arthur/ui/canvas.cljs b/frontend/src/arthur/ui/canvas.cljs index 8add87d..6e81f76 100644 --- a/frontend/src/arthur/ui/canvas.cljs +++ b/frontend/src/arthur/ui/canvas.cljs @@ -30,10 +30,42 @@ (swap! cache assoc el img) img)))) +(defn- pasteboard! + "Clear every pixel of `data` off a stage `sw` by `sh` that nothing drew on, + so the work area shows through round what is drawn there. Never into the + stage's own span of a row, which is always the picture." + [^js data {:keys [w h ^js cov] [ox oy] :origin} [sw sh]] + (let [px (js/Uint32Array. (.-buffer data) (.-byteOffset data) (* w h)) + x0 (- ox) y0 (- oy) x1 (+ x0 sw) y1 (+ y0 sh) + clear! (fn [from to] + (loop [o from] + (when (< o to) + (when (zero? (aget cov o)) (aset px o 0)) + (recur (inc o)))))] + (dotimes [y h] + (let [row (* y w)] + (if (and (<= y0 y) (< y y1)) + (do (clear! row (+ row (min w (max 0 x0)))) (clear! (+ row (min w (max 0 x1))) (+ row w))) + (clear! row (+ row w))))))) + +(defn place! + "Show canvas `el` over `[x0 y0 x1 y1]` of the work area round a stage `[sw sh]`, + in stage pixels: in proportion to the stage, so zooming does not move it." + [^js el [x0 y0 x1 y1] [sw sh]] + (let [style (.-style el) pc #(str (/ (* 100 %1) %2) "%")] + (set! (.-display style) "") + (set! (.-left style) (pc x0 sw)) (set! (.-top style) (pc y0 sh)) + (set! (.-width style) (pc (- x1 x0) sw)) (set! (.-height style) (pc (- y1 y0) sh)))) + (defn blit! - "Expand an indexed raster through the palette and put it on the canvas." + "Expand an indexed raster through the palette and put it on the canvas. + + With `stage`, `[w h]`, the raster is some part of the work area round a stage + that size — its `:origin` says which — and the canvas is placed over that + part, in proportion to the stage, so zooming does not move it." ([el r] (blit! el r pal/rgb)) - ([^js el {:keys [w h] :as r} palette-rgb] + ([el r palette-rgb] (blit! el r palette-rgb nil)) + ([^js el {:keys [w h cov] :as r} palette-rgb stage] (when el ;; Guarded: assigning width reallocates the backing store, so doing it ;; unconditionally would throw a canvas away thirty times a second. @@ -42,7 +74,17 @@ (let [ctx (.getContext el "2d") img (image-data-for ctx el w h)] (raster/->rgba r palette-rgb 1 (.-data img)) - (.putImageData ctx img 0 0))))) + (when cov (pasteboard! (.-data img) r stage)) + (.putImageData ctx img 0 0)) + (when stage + (let [[ox oy] (:origin r)] + (place! el [ox oy (+ ox w) (+ oy h)] stage)))))) + +(defn hide! + "Show nothing on canvas `el`: the part of the work area on screen has nothing + in it." + [^js el] + (set! (.. el -style -display) "none")) (defn ->png "An indexed raster as a PNG data URL, expanded through `palette-rgb`. @@ -83,8 +125,9 @@ (.clearRect (.getContext el "2d") 0 0 (.-width el) (.-height el)))) (defn tint-layer! - "Composite a covered raster in one editor tint, keeping uncovered pixels clear." - [el {:keys [w h cov]} [red green blue] opacity] + "Composite a covered raster in one editor tint, keeping uncovered pixels clear, + with its first pixel at `[x y]` on `el`." + [el {:keys [w h cov]} [x y] [red green blue] opacity] (when el (let [scratch (or @onion-surface (reset! onion-surface (js/document.createElement "canvas"))) @@ -100,4 +143,4 @@ (aset data (+ p 2) blue) (aset data (+ p 3) (if (pos? (aget cov i)) (js/Math.round (* 255 opacity)) 0)))) (.putImageData ctx img 0 0) - (.drawImage (.getContext el "2d") scratch 0 0)))) + (.drawImage (.getContext el "2d") scratch x y)))) diff --git a/frontend/src/arthur/ui/layout.cljs b/frontend/src/arthur/ui/layout.cljs index 556fff7..456af52 100644 --- a/frontend/src/arthur/ui/layout.cljs +++ b/frontend/src/arthur/ui/layout.cljs @@ -210,6 +210,17 @@ [slider "Opacity" 0.05 0.6 opacity #(rf/dispatch [::onion :opacity %])] [:small "Before: red · after: blue. Only the selection, when there is one. Hidden during playback."]])])) +(defn centre-stage! + "Scroll the work area so the stage is in the middle of the view." + [] + (when-let [^js area (.querySelector js/document ".stage-area")] + (when-let [wrap (.querySelector area ".stage-wrap")] + (let [a (.getBoundingClientRect area) s (.getBoundingClientRect wrap)] + (set! (.-scrollLeft area) (+ (.-scrollLeft area) + (- (+ (.-left s) (/ (.-width s) 2)) (+ (.-left a) (/ (.-clientWidth area) 2))))) + (set! (.-scrollTop area) (+ (.-scrollTop area) + (- (+ (.-top s) (/ (.-height s) 2)) (+ (.-top a) (/ (.-clientHeight area) 2))))))))) + (defn fit-stage! [] (when-let [area (.querySelector js/document ".stage-area")] (when-let [wrap (.querySelector area ".stage-wrap")] @@ -217,7 +228,7 @@ h (js/parseFloat (.getAttribute wrap "data-height"))] (rf/dispatch-sync [::set :stage (min (/ (- (.-clientWidth area) 32) w) (/ (- (.-clientHeight area) 32) h))]) - (js/requestAnimationFrame #(do (set! (.-scrollLeft area) 0) (set! (.-scrollTop area) 0))))))) + (js/requestAnimationFrame centre-stage!))))) (defn stage-controls "The stage's own view, after the tracing switch: what else is drawn over it, diff --git a/frontend/src/arthur/ui/player.cljs b/frontend/src/arthur/ui/player.cljs index 6931491..b2d5c17 100644 --- a/frontend/src/arthur/ui/player.cljs +++ b/frontend/src/arthur/ui/player.cljs @@ -99,9 +99,18 @@ (defonce ^:private snapshot (atom {})) (defonce ^:private tracker (atom nil)) -(defn- repaint! [] +(defn repaint! + "Paint the frame again: something other than the frame changed what is seen, + such as the work area scrolling." + [] (swap! state assoc :last -1)) +;; The work area, `{:stage [w h] :rect [x0 y0 x1 y1]}` in stage pixels: the stage, +;; everything drawn so far, and a stage's width and height round both. It grows +;; as things are drawn further out, so there is always room to draw further, and +;; never shrinks under the view. A Reagent atom: the stage lays itself out by it. +(defonce board (ratom/atom nil)) + (rf/reg-sub ::snapshot ;; ONE SUB, so the tracker below derefs one input that is never dirty when it @@ -186,35 +195,112 @@ (swap! state assoc :onion-canvas el) (when el (repaint!))) -(declare op-path) +(declare op-path reach) (defn- paint-onions! "Ghost the picture `onion/frames` either side of `f`: the selected node's ops - when a node is selected, everything otherwise." - [f] + when a node is selected, everything otherwise. Each ghost is rasterised as + far as it reaches inside `[vx vy wx wy]`, what is on screen, as the picture + is, and the canvas covers them all." + [f [vx vy wx wy]] (let [{:keys [onion-canvas]} @state {:keys [onion playing? resolver frames selection width height]} @snapshot - path (if (= :node (first selection)) (or (nth selection 3 nil) [(nth selection 2)]) [])] - (canvas/clear-overlay! onion-canvas) - (when (and onion-canvas (:on? onion) (not playing?)) - (doseq [{:keys [frame direction]} (onion/frames f frames onion)] - ;; Consume each result before asking the resolver for another frame: - ;; its geometry buffers are shared with the current picture. - (let [ops (filter (fn [op] - (and (not= :trace (:kind op)) - (= path (vec (take (count path) (op-path op)))))) - (resolver frame)) - layer (raster/layer width height)] - (raster/draw-ops! layer ops) - (canvas/tint-layer! onion-canvas layer - (if (= :before direction) [235 75 75] [65 145 245]) - (:opacity onion))))))) + path (if (= :node (first selection)) (or (nth selection 3 nil) [(nth selection 2)]) []) + ghosts (when (and onion-canvas (:on? onion) (not playing?)) + (into [] + (keep (fn [{:keys [frame direction]}] + ;; Consume each result before asking the resolver for + ;; another frame: its geometry buffers are shared + ;; with the current picture. + (let [ops (into [] (filter (fn [op] + (and (not= :trace (:kind op)) + (= path (vec (take (count path) (op-path op))))))) + (resolver frame)) + [ax ay bx by] (reach ops width height) + x0 (max ax vx) y0 (max ay vy) x1 (min bx wx) y1 (min by wy)] + (when (and (< x0 x1) (< y0 y1)) + {:layer (raster/draw-ops! (assoc (raster/layer (- x1 x0) (- y1 y0)) :origin [x0 y0]) ops) + :tint (if (= :before direction) [235 75 75] [65 145 245])})))) + (onion/frames f frames onion)))] + (when onion-canvas + (if (empty? ghosts) + (canvas/clear-overlay! onion-canvas) + (let [[x0 y0 x1 y1] (->> ghosts + (map (fn [{{[ox oy] :origin :keys [w h]} :layer}] [ox oy (+ ox w) (+ oy h)])) + (reduce (fn [[a b c d] [e f g h]] [(min a e) (min b f) (max c g) (max d h)])))] + (when (not= (- x1 x0) (.-width onion-canvas)) (set! (.-width onion-canvas) (- x1 x0))) + (when (not= (- y1 y0) (.-height onion-canvas)) (set! (.-height onion-canvas) (- y1 y0))) + (canvas/clear-overlay! onion-canvas) + (canvas/place! onion-canvas [x0 y0 x1 y1] [width height]) + (doseq [{:keys [layer tint]} ghosts] + (let [[ox oy] (:origin layer)] + (canvas/tint-layer! onion-canvas layer [(- ox x0) (- oy y0)] tint (:opacity onion))))))))) -(defn- raster-for [w h] - (let [{:keys [raster]} @state] - (if (and raster (= w (:w raster)) (= h (:h raster))) - raster - (:raster (swap! state assoc :raster (raster/make w h)))))) +(defn- reach + "`[x0 y0 x1 y1]` round stage `w` by `h` and everything `ops` draw." + [ops w h] + (let [b #js [0 0 w h] + grow! (fn [x0 y0 x1 y1] + (aset b 0 (min (aget b 0) x0)) (aset b 1 (min (aget b 1) y0)) + (aset b 2 (max (aget b 2) x1)) (aset b 3 (max (aget b 3) y1)))] + (doseq [{:keys [kind ^js pts n cx cy r size]} ops] + (case kind + :poly (dotimes [i n] + (let [x (aget pts (* 2 i)) y (aget pts (inc (* 2 i)))] (grow! x y x y))) + :disc (grow! (- cx r) (- cy r) (+ cx r) (+ cy r)) + :rect (let [s (/ size 2)] (grow! (- cx s) (- cy s) (+ cx s) (+ cy s))) + nil)) + [(js/Math.floor (aget b 0)) (js/Math.floor (aget b 1)) + (js/Math.ceil (aget b 2)) (js/Math.ceil (aget b 3))])) + +(defn- grow-board! + "Make the work area hold `[x0 y0 x1 y1]` and a stage round it, in steps of 64 + so a shape moving out does not lay the stage out again every frame." + [[x0 y0 x1 y1] w h] + (let [step #(* 64 (% (/ %2 64))) + want [(step js/Math.floor (- x0 w)) (step js/Math.floor (- y0 h)) + (step js/Math.ceil (+ x1 w)) (step js/Math.ceil (+ y1 h))] + {:keys [stage rect]} @board + rect (if (= stage [w h]) (mapv #(%1 %2 %3) [min min max max] rect want) want)] + (when (not= rect (:rect @board)) + (reset! board {:stage [w h] :rect rect})))) + +(defn- view-of + "`{:rect [x0 y0 x1 y1] :zoom z}`: the part of the work area on screen, in + stage pixels, and screen pixels to a stage pixel, for a stage `w` pixels wide. + Nil before the stage is laid out." + [w] + (when-let [^js c (:canvas @state)] + (let [wrap (.-parentElement c) + area (.closest c ".stage-area") + s (.getBoundingClientRect wrap) + a (.getBoundingClientRect area) + z (/ (.-width s) w)] + (when (pos? z) + {:zoom z + :rect [(js/Math.floor (/ (- (.-left a) (.-left s)) z)) + (js/Math.floor (/ (- (.-top a) (.-top s)) z)) + (js/Math.ceil (/ (- (+ (.-left a) (.-clientWidth area)) (.-left s)) z)) + (js/Math.ceil (/ (- (+ (.-top a) (.-clientHeight area)) (.-top s)) z))]})))) + +(defn visible + "The part of the work area on screen, `[x0 y0 x1 y1]` in stage pixels, for a + stage `w` pixels wide. Nil before the stage is laid out." + [w] + (:rect (view-of w))) + +(defn- raster-for + "A raster `w` by `h` whose first pixel is stage point `origin`, its buffers + reused while the size holds: scrolling moves the origin and nothing else. With + coverage when it reaches off the stage, so what is there and drawn on by + nothing can be left out of the blit." + [w h origin off?] + (let [k [w h off?] + r (if (= k (:raster-key @state)) + (:raster @state) + (:raster (swap! state assoc :raster-key k + :raster (if off? (raster/layer w h) (raster/make w h)))))] + (assoc r :origin origin))) (defn at "The topmost row path drawn at stage point `point` on the frame last painted, @@ -319,8 +405,8 @@ ;; THE STAGE IS THE CLIP'S, not a constant. Project dimensions are ;; independent of the footage, so the size the frame is rasterised at comes ;; out of the document like everything else. - (paint-onions! f) - (let [ras (raster-for width height) + (let [{:keys [rect zoom]} (or (view-of width) {:rect [0 0 width height] :zoom 1}) + _ (paint-onions! f rect) {traces true picture false} (group-by #(= :trace (:kind %)) (resolver f)) ;; A layer switched off, or all of them, is not on the stage at all: ;; not painted and not there to be clicked. @@ -329,12 +415,22 @@ active (clip/active-palette resolver) bg (pal/background-index palette active)] (swap! state assoc :ops (into (vec picture) traces)) - (-> ras - (raster/clear! bg) - (raster/draw-ops! (with-previews picture palette active))) - (js/performance.mark "arthur/blit:start") - (canvas/blit! canvas ras (pal/effective-ramp palette active)) - (tracing/paint! traces (assoc tracing :width width :origin 0 :playing? playing?) repaint!)) + ;; What is drawn, cut to what is on screen: a frame with nothing off the + ;; stage costs what the stage does, and a drawing a dozen stages wide + ;; costs no more than the pane it is seen through. + (let [ops (with-previews picture palette active) + [ax ay bx by :as all] (reach ops width height) + [vx vy wx wy] rect + x0 (max ax vx) y0 (max ay vy) x1 (min bx wx) y1 (min by wy)] + (grow-board! all width height) + (js/performance.mark "arthur/blit:start") + (if (and (< x0 x1) (< y0 y1)) + (let [off? (or (neg? x0) (neg? y0) (< width x1) (< height y1)) + ras (raster-for (- x1 x0) (- y1 y0) [x0 y0] off?)] + (-> ras (raster/clear! bg) (raster/draw-ops! ops)) + (canvas/blit! canvas ras (pal/effective-ramp palette active) [width height])) + (canvas/hide! canvas))) + (tracing/paint! traces (assoc tracing :rect rect :zoom zoom :stage [width height] :playing? playing?) repaint!)) (js/performance.measure "arthur/resolve+draw" "arthur/paint:start" "arthur/blit:start") (js/performance.measure "arthur/paint" "arthur/paint:start") ;; User Timing entries otherwise accumulate forever in the browser's diff --git a/frontend/src/arthur/ui/stage.cljs b/frontend/src/arthur/ui/stage.cljs index 2a29b8d..61b06e5 100644 --- a/frontend/src/arthur/ui/stage.cljs +++ b/frontend/src/arthur/ui/stage.cljs @@ -511,7 +511,9 @@ (when (and (some? c) (not (symbol/knockout? c)) (or all? (= c start))) q)))) (get-in document [:symbols sid :nodes])))) - m (outline/stamp! (outline/mask (:w ctx) (:h ctx)) p p size) + ;; Over what is on screen, which is everywhere the pointer can paint. + m (let [[x0 y0 x1 y1] (or (player/visible (:w ctx)) [0 0 (:w ctx) (:h ctx)])] + (outline/stamp! (assoc (outline/mask (- x1 x0) (- y1 y0)) :origin [x0 y0]) p p size)) s (cond ;; The brush's target is the creation target, which the player ;; reads for itself. @@ -594,7 +596,7 @@ [:circle {:class (str "first" (when (and hover (closes? draft hover)) " closing")) :cx fx :cy fy :r 1.8}]]))) -(defn- overlay [w h zoom opacity] +(defn- overlay [w h zoom [x0 y0 x1 y1]] (let [tool @(rf/subscribe [::sub/tool]) draft @(rf/subscribe [::sub/draft]) hover @(rf/subscribe [::sub/hover]) @@ -623,8 +625,8 @@ mark @marquee] [:svg {:class (str "paint-overlay tool-" (name tool) (when (and pen? hover (closes? draft hover)) " closing")) - :width (* zoom 3 w) :height (* zoom 3 h) - :view-box (str (- w) " " (- h) " " (* 3 w) " " (* 3 h)) + :width (* zoom (- x1 x0)) :height (* zoom (- y1 y0)) + :view-box (str x0 " " y0 " " (- x1 x0) " " (- y1 y0)) :tab-index -1 :on-pointer-down (fn [^js event] @@ -799,8 +801,12 @@ (defn- navigation [] {:title "Two-finger scroll to pan · Pinch or ⌘/Ctrl-wheel to zoom · Space-drag or middle-drag to pan" + ;; The picture is what is on screen, so moving or resizing the view paints + ;; it again. + :on-scroll player/repaint! :ref (fn [el] (when el + (.observe (js/ResizeObserver. player/repaint!) el) (set! (.-onwheel el) (fn [e] (.preventDefault e) @@ -843,10 +849,26 @@ :on-pointer-up-capture (fn [e] (when @pan (.stopPropagation e) (reset! pan nil))) :on-pointer-cancel-capture (fn [_] (reset! pan nil))}) +;; How the work area was last laid out, to keep what is on screen still when it +;; grows up or to the left — which moves the stage in it — and to centre a new +;; stage. +(defonce ^:private laid (atom nil)) + +(defn- relaid! + "After the work area is laid out as `now`, from `was`." + [was now] + (let [^js area (.querySelector js/document ".stage-area")] + (cond + (not= (:stage was) (:stage now)) (layout/centre-stage!) + (= (:zoom was) (:zoom now)) + (let [[ax ay] (:rect was) [bx by] (:rect now) z (:zoom now)] + (set! (.-scrollLeft area) (+ (.-scrollLeft area) (* z (- ax bx)))) + (set! (.-scrollTop area) (+ (.-scrollTop area) (* z (- ay by)))))) + (player/repaint!))) + (defn view [] ;; Reactive on the clip's dimensions, so selecting a clip of another size - ;; resizes the canvas. `ui/canvas` guards the width assignment — which - ;; reallocates the backing store — so this re-rendering costs nothing per frame. + ;; lays the stage out again; the player sizes the picture for itself. (let [w @(rf/subscribe [::playback/width]) h @(rf/subscribe [::playback/height]) ;; A drop lands at the playhead IN THE OPEN SYMBOL's frames, the same @@ -858,7 +880,17 @@ (clip/symbol-name clip sid)) zoom @(rf/subscribe [::layout/zoom :stage]) {:keys [on? opacity]} @(rf/subscribe [::layout/mat]) - opacity (if on? opacity 0)] + opacity (if on? opacity 0) + ;; The work area round the stage: as far as anything is drawn, and a + ;; stage further. Margins and not an element round it, so it scrolls to + ;; the left and the top as well. See `player/board`. + {:keys [stage rect]} @player/board + [x0 y0 x1 y1 :as rect] (if (= stage [w h]) rect [(- w) (- h) (* 2 w) (* 2 h)]) + now {:stage [w h] :zoom zoom :rect rect} + was @laid] + (when (not= was now) + (reset! laid now) + (r/after-render #(relaid! was now))) [:div.stage-area (navigation) [:div.stage-wrap @@ -868,6 +900,9 @@ ;; accept on BOTH, and the element under the pointer changes whenever the ;; preview re-renders beneath it, which fires a fresh `dragenter`. {:data-width w :data-height h + :style {:width (* zoom w) :height (* zoom h) + :margin (str (* zoom (- y0)) "px " (* zoom (- x1 w)) "px " + (* zoom (- y1 h)) "px " (* zoom (- x0)) "px")} :on-drag-enter (fn [^js event] (when (drag/accepts?) (.preventDefault event))) :on-drag-over (fn [^js event] (when (drag/accepts?) @@ -879,18 +914,14 @@ :on-drop (fn [^js event] (.preventDefault event) (drag/land! frame (stage-point event w h)))} - [:canvas.stage {:ref #(player/set-canvas! %) - :width w :height h - :style {:width (str (* zoom w) "px") - :height (str (* zoom h) "px")}}] - [:canvas.onion-skin {:ref #(player/set-onion-canvas! %) - :width w :height h - :style {:width (str (* zoom w) "px") - :height (str (* zoom h) "px")}}] - [:canvas.tracing {:ref #(tracing/set-canvas! %) - :width (* zoom w) :height (* zoom h)}] - [:div.stage-shade {:style {:box-shadow (str "0 0 0 100vmax rgba(0,0,0," opacity ")")}}] - [:div.stage-overlay-wrap [overlay w h zoom opacity]]] - (when destination-name - [:div.stage-target "creating in " [:strong destination-name]]) + ;; Sized and placed by the player, to what is drawn and on screen. + [:canvas.stage {:ref #(player/set-canvas! %)}] + ;; These too: see `player/paint-onions!` and `tracing/paint!`. + [:canvas.onion-skin {:ref #(player/set-onion-canvas! %)}] + [:canvas.tracing {:ref #(tracing/set-canvas! %)}] + [:div.stage-shade {:style {:box-shadow (str "0 0 0 " (* zoom (max (- x1 x0) (- y1 y0))) "px rgba(0,0,0," opacity ")")}}] + [:div.stage-overlay-wrap {:style {:left (* zoom x0) :top (* zoom y0)}} + [overlay w h zoom rect]] + (when destination-name + [:div.stage-target "creating in " [:strong destination-name]])] [tools/adjust-last]])) diff --git a/frontend/src/arthur/ui/tracing.cljs b/frontend/src/arthur/ui/tracing.cljs index 64076c3..d6c04ba 100644 --- a/frontend/src/arthur/ui/tracing.cljs +++ b/frontend/src/arthur/ui/tracing.cljs @@ -6,8 +6,9 @@ raster refuses and only the stage's resolver asks for (see `clip/trace-op`), so it cannot reach an export. It is drawn OVER the picture at one opacity rather than under it, because the raster clears to an opaque ground. The canvas is the - stage's size on screen, not the raster's, so a 1280px still is not squeezed - through a 320px stage on its way to being seen. + part of the work area on screen at screen resolution, not the raster's, so a + 1280px still is not squeezed through a 320px stage on its way to being seen, + and footage reaching off the stage is seen there as the picture is. The op says where the image goes and which of its frames to show, and nothing else: a face's footage is registered to the face because it is placed inside @@ -21,7 +22,8 @@ separate cause of the same symptom — the tracing blinking out for a frame or two — and none of them covers the others: the hold is what survives a seek, the reading ahead is what keeps playback from being a frame behind for good." - (:require [arthur.flow.ingest :as ingest])) + (:require [arthur.flow.ingest :as ingest] + [arthur.ui.canvas :as canvas])) (defonce ^:private state (atom {:canvas nil :urls {} :images {} :order [] :last {}})) @@ -121,16 +123,21 @@ (image (url-of media p on-ready) on-ready)))) (defn paint! - "Draw trace ops `ops`, in draw order, at `opacity`, over a stage `width` wide. - `on-ready` is called when a still or a manifest that was missing arrives, to - paint again." - [ops {:keys [opacity width playing? origin]} on-ready] - (when-let [^js canvas (:canvas @state)] - (let [ctx (.getContext canvas "2d") - zoom (/ (.-width canvas) width) + "Draw trace ops `ops`, in draw order, at `opacity`, over `rect` of the work + area round a stage `stage` — what is on screen, in stage pixels — at `zoom` + screen pixels to one. `on-ready` is called when a still or a manifest that was + missing arrives, to paint again." + [ops {:keys [opacity playing? rect zoom stage]} on-ready] + (when-let [^js el (:canvas @state)] + (let [[x0 y0 x1 y1] rect + cw (js/Math.round (* zoom (- x1 x0))) ch (js/Math.round (* zoom (- y1 y0))) + _ (when (not= cw (.-width el)) (set! (.-width el) cw)) + _ (when (not= ch (.-height el)) (set! (.-height el) ch)) + _ (canvas/place! el rect stage) + ctx (.getContext el "2d") was (:last @state)] (.setTransform ctx 1 0 0 1 0 0) - (.clearRect ctx 0 0 (.-width canvas) (.-height canvas)) + (.clearRect ctx 0 0 cw ch) ;; What each layer is showing, forgotten for the layers that are gone. ;; Before the draw, so switching them all off forgets all of them. (swap! state update :last select-keys (map :node ops)) @@ -148,7 +155,7 @@ (.setTransform ctx (* zoom (aget m 0)) (* zoom (aget m 1)) (* zoom (aget m 2)) (* zoom (aget m 3)) - (* zoom (+ (or origin 0) (aget m 4))) (* zoom (+ (or origin 0) (aget m 5)))) + (* zoom (- (aget m 4) x0)) (* zoom (- (aget m 5) y0))) (.drawImage ctx img 0 0 w h)) (when (and playing? (not= frame (get-in was [node :frame]))) (warm! op on-ready))))))) diff --git a/frontend/test/arthur/domain/raster_test.cljs b/frontend/test/arthur/domain/raster_test.cljs index 6e86af0..defbe03 100644 --- a/frontend/test/arthur/domain/raster_test.cljs +++ b/frontend/test/arthur/domain/raster_test.cljs @@ -250,3 +250,19 @@ (is (= 2 (count-index (-> (r/make 20 10) (r/clear! 0) (r/draw-ops! [{:kind :mask :mask m :color 4}])) 4))))) + +(deftest a-raster-with-an-origin-is-that-part-of-the-stage + ;; Ops stay in stage pixels: a raster whose first pixel is stage point + ;; (-10, -10) holds what is drawn there, layers included, 10 further on. + (let [ops [{:kind :begin} + {:kind :poly :pts (sq -5 -5 5 5) :n 4 :color 1} + {:kind :end} + {:kind :disc :cx -5 :cy 15 :r 2 :color 2} + {:kind :rect :cx 15 :cy -5 :size 3 :color 3}] + moved (-> (r/make 30 30) (assoc :origin [-10 -10]) (r/clear! 0) (r/draw-ops! ops)) + plain (-> (r/make 30 30) (r/clear! 0) + (r/draw-ops! [{:kind :poly :pts (sq 5 5 15 15) :n 4 :color 1} + {:kind :disc :cx 5 :cy 25 :r 2 :color 2} + {:kind :rect :cx 25 :cy 5 :size 3 :color 3}]))] + (is (= 100 (count-index moved 1))) + (is (= (vec (array-seq (:buf plain))) (vec (array-seq (:buf moved))))))) diff --git a/static/arthur/app.css b/static/arthur/app.css index 2304c80..82d7ea1 100644 --- a/static/arthur/app.css +++ b/static/arthur/app.css @@ -848,8 +848,15 @@ button.share-button:hover, button.share-button.on { filter: brightness(1.1); } padding: 14px; } +/* Under the stage, wherever the work area round it reaches. */ .stage-target { - flex: 0 0 auto; + position: absolute; + top: 100%; + left: 50%; + transform: translateX(-50%); + white-space: nowrap; + line-height: normal; + pointer-events: none; margin-top: -1px; padding: 3px 9px 4px; border: 1px solid var(--line); @@ -871,14 +878,13 @@ button.share-button:hover, button.share-button.on { filter: brightness(1.1); } outline: 1px solid rgba(0, 0, 0, .45); } -.stage { display: block; background: var(--stage); } +/* Placed by the player over whatever part of the work area is drawn on and on + screen, which may be more than the stage or less. */ +.stage { position: absolute; } .paint-overlay { position: absolute; inset: 0; touch-action: none; } -.stage-overlay-wrap { - position: absolute; - left: -100%; top: -100%; - width: 300%; height: 300%; -} +/* Over the whole work area, placed by the stage. */ +.stage-overlay-wrap { position: absolute; } .stage-shade { position: absolute; inset: 0; @@ -886,7 +892,7 @@ button.share-button:hover, button.share-button.on { filter: brightness(1.1); } outline: 1px solid #aaa; } /* The footage traced over: a reference above the picture, never part of it. */ -.tracing { position: absolute; inset: 0; pointer-events: none; } +.tracing { position: absolute; pointer-events: none; } /* Where a drag out of the pool would land. Dashed and unfilled, so it reads as "not there yet" over whatever is already drawn. */ @@ -1809,7 +1815,7 @@ button.share-button:hover, button.share-button.on { filter: brightness(1.1); } .stage-wrap { flex: 0 0 auto; } -.onion-skin { position: absolute; inset: 0; pointer-events: none; image-rendering: pixelated; } +.onion-skin { position: absolute; pointer-events: none; image-rendering: pixelated; } /* Expanded automation lanes expose editable keyframe dots. */