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

Copy Markdown View Source

The motion sheet of a drawing (spec/10 §5), copied from visualize's Visualize.Chart.Frame.Motion, taking resolved styles rather than a chart frame: SVG moves through CSS, and under the strict CSP that CSS is a generated stylesheet, never a <style> in the page (macaw spec/10 §1, rule 3).

sheet(styles) answers {css, classes} for the motion parts of the resolved styles a document can draw: every distinct :transition, :animation and :enter part.

  • Classes. Each distinct part is one class, mw-transition-<h>, mw-animation-<h> or mw-enter-<h>, <h> the base-36 :erlang.phash2/1 of the resolved part, so the same part has the same class in every sheet that holds it, a theme's included.
  • Rules. A transition: .c, :where(.c) * { transition: … }, each property as the CSS properties it moves, never all, the easing linear() of the samples. An animation: .c { animation: <keyframes> <duration>ms <easing> <delay>ms <iterations>; transform-box: fill-box; transform-origin: center; }, on a wrapper group of its own. An enter: .c { animation: mw-enter-<name> <duration>ms <easing> 0ms 1; … } on an outer wrapper. The built-ins' @keyframes for each one drawn; a host's keyframes name as it is, held to a CSS identifier.
  • Reduced motion ends every sheet, which a document cannot turn off.

The SVG renderer (Macaw.Draw.Render.SVG) writes the classes in :css resolution: a node's transition class on the node, its fill and stroke as oklch() where a transition moves them, so CSS interpolates in OKLab; an animation and an enter as wrapper groups, so a spin turns about the node as drawn; and, on a node that names its identity, data-mw-exit for its exit (Macaw.Draw.IR.Motion.exit_attribute/2), which the motion hook plays. In :literal resolution nothing is written and the node draws its end state.

Summary

Types

A refusal: faults with their paths.

A motion part: its kind and its resolved value.

Functions

The class of one part: mw-<kind>-<h>, <h> the base-36 :erlang.phash2/1 of the resolved value.

The motion classes a resolved style carries, in the order the renderer writes them: its transition's, its animation's, its enter's. A style without motion has none.

Whether a host's keyframes name is a CSS identifier, -?[_a-zA-Z][_a-zA-Z0-9-]*.

The generated stylesheet of styles and the class of each distinct motion part: {css, %{part => class}}. Styles that carry no motion answer {"", %{}}, a document that declares no sheet. A part of the wrong shape, or a host's keyframes name that is not a CSS identifier, is {:error, [{[i, kind], reason}]} at the style's index.

Types

error()

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

A refusal: faults with their paths.

part()

@type part() :: {:transition | :animation | :enter, term()}

A motion part: its kind and its resolved value.

Functions

class(arg1)

@spec class(part()) :: String.t() | error()

The class of one part: mw-<kind>-<h>, <h> the base-36 :erlang.phash2/1 of the resolved value.

iex> Macaw.Draw.Render.Motion.class({:transition, [{:opacity, 200, :linear}]}) ==
...>   "mw-transition-" <> Macaw.Draw.IR.Element.hash([{:opacity, 200, :linear}])
true

classes(style)

@spec classes(map()) :: [String.t()] | error()

The motion classes a resolved style carries, in the order the renderer writes them: its transition's, its animation's, its enter's. A style without motion has none.

iex> Macaw.Draw.Render.Motion.classes(%{fill: "red"})
[]

identifier?(name)

@spec identifier?(term()) :: boolean()

Whether a host's keyframes name is a CSS identifier, -?[_a-zA-Z][_a-zA-Z0-9-]*.

iex> {Macaw.Draw.Render.Motion.identifier?("wobble-2"), Macaw.Draw.Render.Motion.identifier?("2x;}")}
{true, false}

sheet(styles)

@spec sheet([map()]) :: {String.t(), %{required(part()) => String.t()}} | error()

The generated stylesheet of styles and the class of each distinct motion part: {css, %{part => class}}. Styles that carry no motion answer {"", %{}}, a document that declares no sheet. A part of the wrong shape, or a host's keyframes name that is not a CSS identifier, is {:error, [{[i, kind], reason}]} at the style's index.

iex> {css, classes} = Macaw.Draw.Render.Motion.sheet([%{transition: [{:opacity, 200, :linear}]}])
iex> [class] = Map.values(classes)
iex> String.starts_with?(css, ".#{class}, :where(.#{class}) * { transition: opacity 200ms linear; }")
true