Add per-symbol onion skinning controls and cel ghosts
This commit is contained in:
parent
f2fc261221
commit
606055382b
8 changed files with 355 additions and 3 deletions
54
frontend/src/arthur/domain/onion.cljs
Normal file
54
frontend/src/arthur/domain/onion.cljs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
(ns arthur.domain.onion
|
||||
"Editor-only neighboring cel events, in output frames."
|
||||
(:require [arthur.domain.clip :as clip]
|
||||
[arthur.domain.nest :as nest]
|
||||
[arthur.domain.node :as node]
|
||||
[arthur.domain.symbol :as symbol]))
|
||||
|
||||
(def defaults {:on? false :before 1 :after 1 :opacity 0.25 :scope :selected})
|
||||
|
||||
(defn neighbors [events f before after]
|
||||
(concat
|
||||
(map #(assoc % :direction :before)
|
||||
(take before (reverse (sort-by :start (filter #(<= (:end %) f) events)))))
|
||||
(map #(assoc % :direction :after)
|
||||
(take after (sort-by :start (filter #(> (:start %) f) events))))))
|
||||
|
||||
(defn samples [document store open selection f settings]
|
||||
(let [own (clip/shown-frame document open f)
|
||||
path (if (= :node (first selection)) (or (nth selection 3 nil) [(nth selection 2)]) [])
|
||||
locations (for [n (range (inc (count path)))
|
||||
:let [p (vec (take n path))
|
||||
sid (if (empty? p) open (:sid (nest/inside document store open p own)))]
|
||||
:when (symbol/lane? (clip/symbol document sid))]
|
||||
{:sid sid :path p})
|
||||
selected (last locations)
|
||||
;; Whole-symbol scope includes visible lane placements, recursively.
|
||||
walk (fn walk [sid p seen]
|
||||
(when-not (contains? seen sid)
|
||||
(let [sym (clip/symbol document sid)]
|
||||
(if (symbol/lane? sym)
|
||||
[{:sid sid :path p}]
|
||||
(mapcat (fn [[id n]]
|
||||
(when (node/source n)
|
||||
(let [q (conj p id)
|
||||
at (nest/inside document store open q own)]
|
||||
(when (:sid at) (walk (:sid at) q (conj seen sid))))))
|
||||
(:nodes sym))))))
|
||||
lanes (if (= :symbol (:scope settings)) (walk open [] #{}) (when selected [selected]))]
|
||||
(mapcat
|
||||
(fn [{:keys [sid path]}]
|
||||
(let [events (keep (fn [n]
|
||||
(let [id (:id n)
|
||||
q (conj path id)
|
||||
t (nest/own-time document store open q own)]
|
||||
(when (and (node/source n) (:span n) t (pos? (:rate t)))
|
||||
(let [[lo hi] (:span n)
|
||||
start (+ (:at t) (/ lo (:rate t)))
|
||||
end (+ (:at t) (/ hi (:rate t)))]
|
||||
{:start start :end end :path q
|
||||
:frame (clip/first-output-frame document open (js/Math.ceil start))}))))
|
||||
(symbol/children (:nodes (clip/symbol document sid))))]
|
||||
(filter #(< -1 (:frame %) (clip/output-frames document open))
|
||||
(neighbors events own (:before settings) (:after settings)))))
|
||||
lanes)))
|
||||
|
|
@ -74,3 +74,30 @@
|
|||
(raster/->rgba r palette-rgb 1 (.-data img))
|
||||
(.putImageData ctx img 0 0))
|
||||
(.toDataURL el "image/png")))
|
||||
|
||||
|
||||
(defonce ^:private onion-surface (atom nil))
|
||||
|
||||
(defn clear-overlay! [el]
|
||||
(when el
|
||||
(.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]
|
||||
(when el
|
||||
(let [scratch (or @onion-surface
|
||||
(reset! onion-surface (js/document.createElement "canvas")))
|
||||
_ (when (not= w (.-width scratch)) (set! (.-width scratch) w))
|
||||
_ (when (not= h (.-height scratch)) (set! (.-height scratch) h))
|
||||
ctx (.getContext scratch "2d")
|
||||
img (image-data-for ctx scratch w h)
|
||||
data (.-data img)]
|
||||
(dotimes [i (* w h)]
|
||||
(let [p (* i 4)]
|
||||
(aset data p red)
|
||||
(aset data (+ p 1) green)
|
||||
(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))))
|
||||
|
|
|
|||
|
|
@ -20,7 +20,8 @@
|
|||
drawers over the stage rather than columns beside it, which is the stylesheet's
|
||||
half of this (see `@media` in app.css), and a drawer that is shut should not be
|
||||
holding video thumbnails live."
|
||||
(:require [re-frame.core :as rf]))
|
||||
(:require [arthur.domain.onion :as onion]
|
||||
[re-frame.core :as rf]))
|
||||
|
||||
(def ^:private narrow?
|
||||
"A phone-shaped window, read ONCE at load. It decides what the layout opens
|
||||
|
|
@ -167,6 +168,38 @@
|
|||
(rf/reg-sub ::mat (fn [db _] (merge {:opacity 0.55} (get-in db [:ui :passepartout]))))
|
||||
(rf/reg-event-db ::mat (fn [db [_ k v]] (assoc-in db [:ui :passepartout k] v)))
|
||||
|
||||
(rf/reg-sub ::onion
|
||||
(fn [db _]
|
||||
(merge onion/defaults (get-in db [:ui :onion (:clip/current db) (get-in db [:ui :open])]))))
|
||||
(rf/reg-event-db ::onion
|
||||
(fn [db [_ k v]]
|
||||
(assoc-in db [:ui :onion (:clip/current db) (get-in db [:ui :open]) k] v)))
|
||||
|
||||
(defn onion-controls []
|
||||
(let [{:keys [on? before after opacity scope]} @(rf/subscribe [::onion])]
|
||||
[:div.group.onion-controls
|
||||
[:button {:class (when on? "on") :aria-pressed (boolean on?)
|
||||
:title "Onion skin — neighboring cels of the selected lane"
|
||||
:on-click #(rf/dispatch [::onion :on? (not on?)])} "onion"]
|
||||
[:details
|
||||
[:summary {:aria-label "Onion skin settings" :title "Onion skin settings"} "▾"]
|
||||
[:div.view-popout
|
||||
[:label "Scope"
|
||||
[:select {:value (name scope)
|
||||
:on-change #(rf/dispatch [::onion :scope (keyword (.. % -target -value))])}
|
||||
[:option {:value "selected"} "Selected lane"]
|
||||
[:option {:value "symbol"} "Whole symbol"]]]
|
||||
(for [[k label v] [[:before "Previous cels" before] [:after "Next cels" after]]]
|
||||
^{:key k} [:label label
|
||||
[:input {:type "number" :min 0 :max 5 :value v
|
||||
:on-change #(let [n (js/parseInt (.. % -target -value) 10)]
|
||||
(when-not (js/isNaN n)
|
||||
(rf/dispatch [::onion k (max 0 (min 5 n))])))}]])
|
||||
[:label "Opacity"
|
||||
[:input {:type "range" :min 0.05 :max 0.6 :step 0.05 :value opacity
|
||||
:on-change #(rf/dispatch [::onion :opacity (js/parseFloat (.. % -target -value))])}]]
|
||||
[:small "Previous: red · Next: blue. Select a lane or one of its cels. Hidden during playback."]]]]))
|
||||
|
||||
(defn fit-stage! []
|
||||
(when-let [area (.querySelector js/document ".stage-area")]
|
||||
(when-let [wrap (.querySelector area ".stage-wrap")]
|
||||
|
|
@ -179,6 +212,7 @@
|
|||
(defn stage-controls []
|
||||
(let [{:keys [opacity]} @(rf/subscribe [::mat])]
|
||||
[:<>
|
||||
[onion-controls]
|
||||
[:button {:on-click fit-stage! :title "Fit stage in view"} "fit"]
|
||||
[:details.passepartout-controls
|
||||
[:summary "passepartout"]
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@
|
|||
[arthur.domain.clip :as clip]
|
||||
[arthur.domain.cut :as cut]
|
||||
[arthur.domain.outline :as outline]
|
||||
[arthur.domain.onion :as onion]
|
||||
[arthur.domain.palette :as pal]
|
||||
[arthur.domain.pick :as pick]
|
||||
[arthur.domain.raster :as raster]
|
||||
|
|
@ -133,7 +134,12 @@
|
|||
:tracing t
|
||||
:frame @(rf/subscribe [::sub/frame])
|
||||
:playing? @(rf/subscribe [::sub/playing?])
|
||||
:pen pen})
|
||||
:pen pen
|
||||
:onion @(rf/subscribe [::layout/onion])
|
||||
:document @(rf/subscribe [::render/clip])
|
||||
:store @(rf/subscribe [::render/store])
|
||||
:open @(rf/subscribe [::render/open])
|
||||
:selection @(rf/subscribe [::ui-sub/selection])})
|
||||
;; A new resolver means a new scene or a new palette, and neither
|
||||
;; moves the playhead — so nothing else would ask for a redraw.
|
||||
;;
|
||||
|
|
@ -146,7 +152,10 @@
|
|||
;; Comparing the whole map is as cheap as picking fields out of it:
|
||||
;; the document and the store it carries are the same OBJECTS unless
|
||||
;; the resolver changed too, and that is tested first.
|
||||
(when-not (and (identical? was now) (= was-t t) (= pen (:pen before)) (= (:mat before) (:mat @snapshot)))
|
||||
(when-not (and (identical? was now) (= was-t t) (= pen (:pen before)) (= (:mat before) (:mat @snapshot))
|
||||
(= (:onion before) (:onion @snapshot))
|
||||
(= (:selection before) (:selection @snapshot))
|
||||
(= (:playing? before) (:playing? @snapshot)))
|
||||
(repaint!))))))
|
||||
|
||||
;; The brush or eraser stroke being painted: its mask, mutated in place as the
|
||||
|
|
@ -170,6 +179,30 @@
|
|||
;; — a blank canvas under a transport reading perfectly correct.
|
||||
(when el (repaint!)))
|
||||
|
||||
(defn set-onion-canvas! [el]
|
||||
(swap! state assoc :onion-canvas el)
|
||||
(when el (repaint!)))
|
||||
|
||||
(declare op-path)
|
||||
|
||||
(defn- paint-onions! [f]
|
||||
(let [{:keys [onion-canvas]} @state
|
||||
{:keys [onion playing? resolver document store open selection width height]} @snapshot]
|
||||
(canvas/clear-overlay! onion-canvas)
|
||||
(when (and onion-canvas (:on? onion) (not playing?))
|
||||
(doseq [{:keys [frame path direction]} (onion/samples document store open selection f 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)))))))
|
||||
|
||||
(defn- raster-for [w h]
|
||||
(let [{:keys [raster]} @state]
|
||||
(if (and raster (= w (:w raster)) (= h (:h raster)))
|
||||
|
|
@ -276,6 +309,7 @@
|
|||
;; 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)
|
||||
{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:
|
||||
|
|
|
|||
|
|
@ -763,6 +763,10 @@
|
|||
: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 ")")}}]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue