Macaw.Draw.IR.Motion (macaw_draw v0.1.0)

Copy Markdown View Source

The motion values every target writes (spec/10 §1.5, §5), copied from visualize's Visualize.IR.Motion (visualize spec/14 §3.10–§3.12, D-176).

A style may carry :transition, a list of {property, duration, easing} (property :fill, :stroke or :opacity, duration in milliseconds, easing a Macaw.Draw.Ease name); :animation, %{name, duration, easing, iterations, delay}; and :enter and :exit, %{name, duration, easing}. A node that moves carries "data-mw-id", the identity motion is keyed by.

SVG writes motion as CSS (css_easing/1, css_transition/1, css_properties/1, css_keyframes/1, css_animation/1, oklch/1), a canvas as a span the hooks run (span/3, json/1, transitions/1), a leaving node as exit_attribute/2; all of them ease along the same 21 samples of one curve, samples/1, so one curve moves every target.

iex> alias Macaw.Draw.IR.Motion
iex> Motion.css_easing(:linear)
"linear"
iex> Motion.samples(:quad_in) |> Enum.take(3)
[0, 0.0025, 0.01]
iex> Motion.css_properties(:stroke)
["stroke", "stroke-width", "stroke-opacity"]
iex> Motion.oklch("#ffffff")
"oklch(1 0 0)"
iex> Motion.oklch("var(--mw-red, #c23238)")
"oklch(from var(--mw-red, #c23238) l c h / alpha)"

An easing that names none is {[], {:unknown, :ease, spelling}}, a part of the wrong shape a type fault: every function answers a value.

Summary

Types

A refusal: faults with their paths.

A property a transition moves.

A resolved transition: what moves, for how long, along which curve.

Functions

The CSS animation value of a resolved animation: the keyframes (mw-<name> for a built-in, a host's name as it is), the duration, the easing, the delay and the iterations.

The easing as CSS: linear, or linear() of samples/1.

A built-in's keyframes as CSS: @keyframes mw-<name> { … } (mw-enter-<name> for an entrance), each offset a percentage, opacity as itself, x and y as translate, rotate and scale as the individual transform properties, which compose with a node's transform.

The CSS properties a property moves (spec/10 §1.5): fill → fill, fill-opacity; stroke → stroke, stroke-width, stroke-opacity; opacity → opacity; never all.

The CSS transition value of a resolved transition: each CSS property a property moves, its duration and its easing, joined by commas.

The text of data-mw-exit for node leaving with exit (spec/10 §5): the name, the duration, the samples, the node's box as drawn and a built-in's keyframes, as JSON.

A span's arguments as JSON text: lists, strings, numbers and nil, numbers formatted as path data is, every string escaped; the text the :json command and the binary record both carry. A term of no JSON form here (a map, a tuple) is written as its inspected string.

A built-in's keyframes: offsets from 0 to 1, each with the values there of :opacity, :x, :y (a translation in user units), :rotate (degrees, about the center of the node's box as drawn) and :scale. The animations are :pulse, :blink, :shake and :spin; the entrances {:enter, :fade} and {:enter, :grow}; the exits {:exit, :fade} and {:exit, :shrink} ({:exit, :explode} is drawn by the hooks). Any other name, a host's, is {:error, [{[], {:unknown, :keyframes, spelling}}]}.

A color as oklch(), so CSS interpolates it in OKLab (spec/10 §1.5): a color term, or a literal Macaw.Layers.Color.parse/1 reads, as its coordinates; an oklch(…) as it is; any other string, a theme's var(--mw-…, literal) or a transform's color-mix(…), as the relative color oklch(from <value> l c h / alpha). :none, :current_color, a paint and any other term pass through.

The easing's 21 values at t = 0, 0.05, …, 1, each rounded to 4 decimals; [0, 1] for :linear, which two points draw exactly. A name is an atom or its string.

The arguments of a motion span: [id, box, transitions, animation, enter, exit] for a node that names its identity, drawn with style, its effective style (its own over its groups'), trailing absent parts left out. A node whose style moves nothing has a span too, with no transitions: a hook then knows what it shows when a later stream moves it.

The resolved transition as the canvas encoders write it: [[property, duration, samples], …].

Types

error()

@type error() :: {:error, [Macaw.Base.Fault.t()]}

A refusal: faults with their paths.

property()

@type property() :: :fill | :stroke | :opacity

A property a transition moves.

transition()

@type transition() :: [{property(), number(), atom()}]

A resolved transition: what moves, for how long, along which curve.

Functions

css_animation(arg1)

@spec css_animation(map()) :: String.t() | error()

The CSS animation value of a resolved animation: the keyframes (mw-<name> for a built-in, a host's name as it is), the duration, the easing, the delay and the iterations.

iex> Macaw.Draw.IR.Motion.css_animation(%{name: :pulse, duration: 1200, easing: :linear, iterations: :infinite, delay: 0})
"mw-pulse 1200ms linear 0ms infinite"

css_easing(easing)

@spec css_easing(atom() | String.t()) :: String.t() | error()

The easing as CSS: linear, or linear() of samples/1.

css_keyframes(name)

@spec css_keyframes(atom() | {:enter | :exit, atom()}) :: String.t() | error()

A built-in's keyframes as CSS: @keyframes mw-<name> { … } (mw-enter-<name> for an entrance), each offset a percentage, opacity as itself, x and y as translate, rotate and scale as the individual transform properties, which compose with a node's transform.

iex> Macaw.Draw.IR.Motion.css_keyframes(:spin)
"@keyframes mw-spin { 0% { rotate: 0deg; } 100% { rotate: 360deg; } }"

css_properties(p)

@spec css_properties(property()) :: [String.t()] | error()

The CSS properties a property moves (spec/10 §1.5): fill → fill, fill-opacity; stroke → stroke, stroke-width, stroke-opacity; opacity → opacity; never all.

css_transition(transition)

@spec css_transition(transition()) :: String.t() | error()

The CSS transition value of a resolved transition: each CSS property a property moves, its duration and its easing, joined by commas.

iex> Macaw.Draw.IR.Motion.css_transition([{:opacity, 300, :linear}])
"opacity 300ms linear"

exit_attribute(exit, node)

@spec exit_attribute(map(), Macaw.Draw.IR.Element.t()) :: String.t() | error()

The text of data-mw-exit for node leaving with exit (spec/10 §5): the name, the duration, the samples, the node's box as drawn and a built-in's keyframes, as JSON.

iex> exit = %{name: :fade, duration: 300, easing: :linear}
iex> Macaw.Draw.IR.Motion.exit_attribute(exit, Macaw.Draw.IR.Element.rect(0, 0, 4, 2))
~s(["fade",300,[0,1],[0,0,4,2],[[0,1,null,null,null,null],[1,0,null,null,null,null]]])

json(value)

@spec json(term()) :: String.t()

A span's arguments as JSON text: lists, strings, numbers and nil, numbers formatted as path data is, every string escaped; the text the :json command and the binary record both carry. A term of no JSON form here (a map, a tuple) is written as its inspected string.

iex> Macaw.Draw.IR.Motion.json(["a\"b", 1.5, nil, [0, 1]])
~S(["a\"b",1.5,null,[0,1]])

keyframes(name)

@spec keyframes(atom() | {:enter | :exit, atom()}) :: [{number(), map()}] | error()

A built-in's keyframes: offsets from 0 to 1, each with the values there of :opacity, :x, :y (a translation in user units), :rotate (degrees, about the center of the node's box as drawn) and :scale. The animations are :pulse, :blink, :shake and :spin; the entrances {:enter, :fade} and {:enter, :grow}; the exits {:exit, :fade} and {:exit, :shrink} ({:exit, :explode} is drawn by the hooks). Any other name, a host's, is {:error, [{[], {:unknown, :keyframes, spelling}}]}.

iex> Macaw.Draw.IR.Motion.keyframes(:pulse)
[{0, %{opacity: 1}}, {0.5, %{opacity: 0.35}}, {1, %{opacity: 1}}]

oklch(color)

@spec oklch(term()) :: term()

A color as oklch(), so CSS interpolates it in OKLab (spec/10 §1.5): a color term, or a literal Macaw.Layers.Color.parse/1 reads, as its coordinates; an oklch(…) as it is; any other string, a theme's var(--mw-…, literal) or a transform's color-mix(…), as the relative color oklch(from <value> l c h / alpha). :none, :current_color, a paint and any other term pass through.

samples(easing)

@spec samples(atom() | String.t()) :: [number()] | error()

The easing's 21 values at t = 0, 0.05, …, 1, each rounded to 4 decimals; [0, 1] for :linear, which two points draw exactly. A name is an atom or its string.

span(id, node, style)

@spec span(String.t(), Macaw.Draw.IR.Element.t(), map()) :: list() | error()

The arguments of a motion span: [id, box, transitions, animation, enter, exit] for a node that names its identity, drawn with style, its effective style (its own over its groups'), trailing absent parts left out. A node whose style moves nothing has a span too, with no transitions: a hook then knows what it shows when a later stream moves it.

iex> rect = Macaw.Draw.IR.Element.rect(0, 0, 4, 2)
iex> Macaw.Draw.IR.Motion.span("f.1.#1", rect, %{transition: [{:opacity, 200, :linear}]})
["f.1.#1", [0, 0, 4, 2], [["opacity", 200, [0, 1]]]]

transitions(transition)

@spec transitions(transition()) :: [list()] | error()

The resolved transition as the canvas encoders write it: [[property, duration, samples], …].