Your first layers

A Kinetic piece is written as layers, a plain object that says which elements move and how each one changes over time. This page builds a small title card line by line, then covers how layers.scene() finds its clips, functions in fns, layers() without a scene, the top-level keys, and the errors.

Most pieces are written as layers. The HTML and CSS place each element at rest, and each layer moves its element from there. Write a scene in code only for what a layer can't describe: a three.js view drawn through Trillium, or a layout that can only be worked out after something has loaded.

A first piece

Every piece is made of a composition, clips, layers and tracks.

  • The composition is the element with data-composition-id, the whole video at its width, height and length.
  • A clip is an element inside it with data-start and data-duration, a stretch of the video with its own start.
  • A layer is one entry in the list of layers, and it moves one element of the clip.
  • A track is the keyframes of one property, the values it has at given times.

This title card has one clip and three layers. The heading fades in as it rises, a line grows out from its middle, and a subtitle fades in after them.

<div data-composition-id="main" data-start="0" data-duration="5"
     data-width="1280" data-height="720" data-fps="30">
    <div class="card" data-component="title-card" data-start="0" data-duration="5">
        <h1>Meadow</h1>
        <div class="rule"></div>
        <p class="sub">A short film about small things</p>
    </div>
</div>
wildflower.kinetic.layers.scene('title-card', {
    layers: [
        {
            id: 'heading', kind: 'html', src: 'h1',
            place: { translate: [0, { at: [0, 0.8], to: [40, 0], ease: 'power3.out' }] },
            look: { opacity: { at: [0, 0.6], to: [0, 1] } },
        },
        {
            id: 'rule', kind: 'html', src: '.rule',
            place: { scale: [{ at: [0.5, 1.3], to: [0, 1], ease: 'power2.inOut' }, 1] },
        },
        {
            id: 'subtitle', kind: 'html', src: '.sub',
            look: { opacity: { at: [1.2, 1.8], to: [0, 1] } },
        },
    ],
});
html, body { margin: 0; }
[data-composition-id] {
    position: relative;
    overflow: hidden;
    background: #0b0f1a;
    color: #fff;
    font-family: system-ui, sans-serif;
}
[data-composition-id]:not([data-kinetic-ready]) { visibility: hidden; }
.card {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}
.card h1 { margin: 0; font-size: 136px; }
.rule { width: 420px; height: 4px; margin: 28px 0; background: #9d7bff; }
.sub { margin: 0; font-size: 34px; }
A title card

The HTML puts each element where it rests, and the CSS styles it. The layers move the elements from there. Here is the piece again, one part at a time.

wildflower.kinetic.layers.scene('title-card', {

This makes a scene called title-card. The scene draws its layers on every clip whose data-component is title-card, so it draws on the clip in the HTML above.

    layers: [

layers is the list of layers, in drawing order. A layer later in the list draws over an earlier one.

        { id: 'heading', kind: 'html', src: 'h1',

id is the layer's name. No two layers in one list share a name, and an error message gives the layer by this name. kind: 'html' says the layer moves an HTML element. src: 'h1' is a CSS selector for that element, looked for inside the clip, here the card's heading.

            place: { translate: [0, { at: [0, 0.8], to: [40, 0], ease: 'power3.out' }] },

place holds where the element sits. translate is [x, y] in px, an offset from where the CSS put it. x stays 0. y is a track, which goes from 40 px at 0 seconds to 0 px at 0.8 seconds, so the heading starts 40 px low and rises into place. at gives the times, counted in seconds from the clip's start, and to the value at each time. ease: 'power3.out' makes the rise fast at first and slow at the end.

            look: { opacity: { at: [0, 0.6], to: [0, 1] } },

look holds how the element looks. Its opacity goes from 0 to 1 over the first 0.6 seconds, so the heading fades in while it rises. After its last time, a track holds its last value, so the heading stays at full opacity.

        { id: 'rule', kind: 'html', src: '.rule',
            place: { scale: [{ at: [0.5, 1.3], to: [0, 1], ease: 'power2.inOut' }, 1] },

The second layer moves the line under the heading. scale is [x, y] here. x is a track from 0 to 1 between 0.5 and 1.3 seconds, and y stays 1, so the line grows sideways from nothing to its full width. It grows out from its middle, because CSS turns and scales an element about its center unless transform-origin says otherwise. Before 0.5 seconds the track holds its first value, 0, so the line is not there yet.

        { id: 'subtitle', kind: 'html', src: '.sub',
            look: { opacity: { at: [1.2, 1.8], to: [0, 1] } },

The subtitle stays at opacity 0 until 1.2 seconds and fades in by 1.8 seconds, after the heading and the line have arrived.

Layers and keyframes has more on tracks and easing, and First video builds a longer title card step by step. Moving an element covers every key a layer takes.

layers.scene() and its clips

layers.scene(name, { ... }, { fns }) makes a scene called name. At every frame, the scene draws its layers on every clip whose data-component is name. Each clip counts time from its own data-start, so one layers.scene() call can drive several clips.

Here two clips share the scene word. The first starts at 0 and the second at 2, and each plays the same rise from its own start.

<div data-composition-id="main" data-start="0" data-duration="4"
     data-width="1280" data-height="720" data-fps="30">
    <div class="card" data-component="word" data-start="0" data-duration="2">
        <h1>Seed</h1>
    </div>
    <div class="card" data-component="word" data-start="2" data-duration="2">
        <h1>Bloom</h1>
    </div>
</div>
wildflower.kinetic.layers.scene('word', {
    layers: [
        {
            id: 'heading', kind: 'html', src: 'h1',
            place: { translate: [0, { at: [0, 0.8], to: [40, 0], ease: 'power3.out' }] },
            look: { opacity: { at: [0, 0.6, 1.6, 2], to: [0, 1, 1, 0] } },
        },
    ],
});
html, body { margin: 0; }
[data-composition-id] {
    position: relative;
    overflow: hidden;
    background: #0b0f1a;
    color: #fff;
    font-family: system-ui, sans-serif;
}
[data-composition-id]:not([data-kinetic-ready]) { visibility: hidden; }
.card {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}
.card h1 { margin: 0; font-size: 136px; }
One scene, two clips

The opacity track has four keys. It fades in over 0.6 seconds, holds at 1, and fades out over the clip's last 0.4 seconds. Kinetic hides each clip outside its own window (Clips outside their windows), so only one word shows at a time.

Times count from the clip's start

Every time in the layers counts from the start of the clip the scene is on, and so do markers and beats. A marker at 3 seconds in the composition is at 1 second in a clip that starts at 2. A clip's id used as a name is the time that clip ends, also counted from the scene's own clip.

// <div id="intro" data-start="0" data-duration="1">, and this clip has data-start="0.5"
{ id: 'note', kind: 'html', src: '.note', look: { opacity: { keys: [['intro', 0], ['intro + 0.5', 1]] } } }

intro ends at 1 second in the composition, which is 0.5 seconds into this clip, so the note fades in from 0.5 to 1 second of the clip's time. Values covers names and the keys form.

A layer's src looks for its element in the scene's clip first, and in the whole composition when the clip has no match. ':scope' is the clip's own element (src).

Functions in fns

The third argument to layers.scene() can hold fns, an object of functions. A layer calls one by name with { call: 'name', args }, and the value is what the function returns.

<div data-composition-id="main" data-start="0" data-duration="4"
     data-width="1280" data-height="720" data-fps="30">
    <div class="card" data-component="growth" data-start="0" data-duration="4">
        <b class="figure"></b>
        <p class="sub">more wildflowers this spring</p>
    </div>
</div>
const { ramp } = wildflower.kinetic;

wildflower.kinetic.layers.scene('growth', {
    layers: [
        { id: 'figure', kind: 'html', src: '.figure', content: { text: { call: 'percent', args: [{ rate: 1 }] } } },
        { id: 'subtitle', kind: 'html', src: '.sub', look: { opacity: { at: [2.4, 3], to: [0, 1] } } },
    ],
}, {
    fns: {
        // 0% at 0.2 s, rising to 48% at 2.4 s, eased.
        percent: (t) => Math.round(48 * ramp(t, 0.2, 2.4, 'power2.out')) + '%',
    },
});
html, body { margin: 0; }
[data-composition-id] {
    position: relative;
    overflow: hidden;
    background: #0b0f1a;
    color: #fff;
    font-family: system-ui, sans-serif;
}
[data-composition-id]:not([data-kinetic-ready]) { visibility: hidden; }
.card {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}
.figure { font-size: 180px; font-variant-numeric: tabular-nums; color: #9d7bff; }
.sub { margin: 12px 0 0; font-size: 34px; }
A counter from a function

  • content: { text } writes the element's text, here the function's result.
  • { rate: 1 } is a value that grows by 1 each second, so it is the clip's time in seconds. The function gets it as t.
  • ramp(t, from, to, ease) goes from 0 to 1 between the two times (ramp()).

A function gets its values as arguments, the time among them. It must not read the clock or call Math.random(), since every frame has to come out the same however many times it is drawn (Reliability). A function that returns a number can be scaled and offset in place.

{ call: 'wave', args: [{ rate: 1 }], scale: 40, add: 360 }

The value is 40 times what wave returns, plus 360.

A call to a name that fns does not have is KN-166, found when the layers are built.

layers() without a scene

wildflower.kinetic.layers(object, options) builds the same layers without a scene, for a page that seeks them itself. It returns { seek(seconds), refresh(), dispose(), stage }.

const piece = wildflower.kinetic.layers({
    id: 'badge', size: [400, 200], duration: 2,
    layers: [{ id: 'title', kind: 'html', src: 'h1', look: { opacity: { at: [0, 2], to: [0, 1] } } }],
}, { root: document.querySelector('#badge') });

piece.seek(1);   // the h1 inside #badge is at opacity 0.5
  • There is no clip to read, so give id, size and duration yourself.
  • root is the element whose src selectors search, and whose .world a camera moves.
  • fns holds the functions, as in layers.scene().
  • target and canvas are the solids stage's SVG group and grain canvas, for solid layers (kinetic.25d.js loaded after kinetic.wf.js). They take the place of stage.src and stage.grain (Solids, cameras and paths).
  • seek(seconds) draws the layers at that time.
  • refresh() makes the layer calls paint again, for a change that a seek to the same time would not show, and returns true when there are layer calls.
  • dispose() takes what the layers put on the page off it: the copies made from a <template>, and the solids' drawing. A scene made with layers.scene() does this itself when its clip is removed.
  • stage is the solids stage when there are solid layers, and null otherwise.

The top-level keys

The object passed to layers.scene() holds layers and a few settings beside it. Each key below has a short example, and the table at the end lists them all with their defaults.

Size and timing

{ id: 'title-card', size: [1280, 720], duration: 4, fps: 30, layers: [ ... ] }

layers.scene() reads these from the page, so a scene rarely writes them. id is the scene's name, size is the composition's data-width and data-height, duration is the clip's data-duration, and fps is the composition's data-fps, or 30 when it has none. With layers(), give id, size and duration yourself.

{ unit: 'frame', layers: [{ id: 'heading', kind: 'html', src: 'h1', look: { opacity: { at: [0, 30], to: [0, 1] } } }] }

With unit: 'frame', every time in the layers is a frame number, so at 30 fps this heading fades in over its first second.

{ snap: 'frame', layers: [ ... ] }

snap: 'frame' moves each marker and beat to its nearest frame, so a cut on a marker lands on a whole frame. A clip intro that ends at 1.01 seconds is read as 1 second at 30 fps.

{ duration: 2, loop: true, layers: [ ... ] }

loop: true wraps time back to 0 at duration, so in a 4-second clip these layers play their 2 seconds twice. loop: { wrap: true } also wraps a placement that runs past the end, so the part past the end shows again from the start (Comps and copies).

Named values and colors

{
    constants: { rise: 0.6 },
    shared: { fadeIn: { at: [0, 'rise'], to: [0, 1] } },
    layers: [
        { id: 'title', kind: 'html', src: 'h1', place: { translate: [0, { at: [0, 'rise'], to: [30, 0] }] } },
        { id: 'tagline', kind: 'html', src: '.tag', look: { opacity: 'fadeIn' } },
    ],
}

constants are named numbers, and any value can use the name, here 'rise' for 0.6 seconds. shared are named values in any form, read by several layers, here a fade that the tagline uses by its name. Change rise and both the title's move and the tagline's fade take the new length.

{
    swatches: { sky: '#7fb8ff', ink: '#14161c' },
    roles: { accent: 'sky' },
    layers: [{ id: 'dot', kind: 'svg', src: 'circle', content: { fill: 'accent' } }],
}

swatches are named colors. roles name a color by what it is for, and give a swatch's name, so the circle is filled #7fb8ff. A comp or a placement can give a role another color (Values, Comps and copies).

  • presets are animations an HTML layer plays from its own start (Presets).
  • curves are named falloffs for a solid's shading marks, and looks, bands and stage set up the solids (Solids, cameras and paths).

Stores, queries, inputs and props

wildflower.store('dial', { state: { angle: 30 } });

wildflower.kinetic.layers.scene('gauge', {
    stores: ['dial'],
    layers: [{ id: 'needle', kind: 'html', src: '.needle', place: { rotate: { from: 'dial.angle' } } }],
});

stores lists the stores the layers read. The needle turns to the store's angle, and when the angle changes while the clock stands still, the scene draws again, so a slider in a lesson turns it at once. A layer and a store with the same name read as the layer, so give them different names.

{ queries: ['sales'], layers: [{ id: 'bars', kind: 'comp', comp: 'bar', src: '.rows', repeat: { over: 'sales.rows' } }] }

queries lists the wildflower.query queries the layers read. The first frame waits for their rows, and the layers are built once the rows have arrived. Queries need the full tier of WildflowerJS (Data).

{ inputs: { price: { type: Number, default: 10 } }, layers: [{ id: 'price', kind: 'html', src: '.price', content: { text: { from: 'inputs.price' } } }] }

inputs declares the composition variables the layers read, as a scene declares them (inputs), and a layer reads one with { from: 'inputs.name' }. An input that fails its check raises KN-130 once and reads as undefined.

// <div data-component="tag" data-start="2" data-duration="4" data-prop-label="'Spring sale'">
{ props: { label: { type: String, default: 'New' } }, layers: [{ id: 'label', kind: 'html', src: '.label', content: { text: { from: 'props.label' } } }] }

props declares the clip's own data-prop-* attributes, as { name: { type, default } }, and a layer reads one with { from: 'props.name' }. Each clip that uses the scene shows its own value, so this clip shows Spring sale and a clip without the attribute shows New.

Steps, finish and shutter

// <section id="ask" data-step data-start="0" data-duration="6">...</section>
{ steps: { ask: { wait: 2 } }, layers: [] }

steps gives a lesson's steps their options, { id: { wait, until, entry, script } }, as step() takes them. Each step is a clip with an id and data-step (Steps), and here the step ask holds 2 seconds in. For lesson steps where nothing moves, give steps and layers: [].

{ finish: { grain: { amount: 0.06, fps: 30 } }, shutter: { angle: 180, samples: 16 }, layers: [ ... ] }

finish is the finishing pass (grain, vignette, bloom, ...), as config({ finish }) takes it (The finish). shutter is the render's motion blur, in degrees or { angle, samples }, as config({ shutter }) takes it (The shutter).

All three belong to the page, and layers.scene() sets them when it is called, whether or not a clip of that name is on the page. With several records on one page, the finish and shutter of the last one called are the page's.

Layers and comps

layers is the one key every object needs, the list of layers in drawing order. comps holds groups of layers, { name: { layers, params, roles, looks, marks } }, that a kind: 'comp' layer places once or many times (Comps and copies).

Errors

layers() checks the object when you call it, and layers.scene() checks it when the scene starts on its clip. A mistake there is a console error with a KN code, the layer and the field, and the values that field allows, and the scene does not draw.

{ id: 'heading', kind: 'html', src: 'h1', looks: { opacity: 1 } }
// [KN-162] layers: layers.heading.looks: a layer of kind 'html' does not take 'looks';
// it takes id, kind, life, parent, band, src, size, group, stagger, delay, place, look, content, repeat, animate

Some mistakes show only when a frame is drawn, such as a name that matches nothing or a link to a layer that is not there. Those are console errors from the scene's seek, and check() reports a seek threw.

CodeWhat is wrong
KN-161The object itself: a key it does not take, a missing id, size, duration or layers, a layer with no id or an unknown kind, two layers with one id, or a comp that does not exist.
KN-162A field: a key the layer's kind does not take, a missing src, or a repeat or stage in the wrong shape.
KN-163A name that is no parameter, constant, shared value, marker or beat, or a stagger with no i to read.
KN-164A link that reads a layer, store, query, input or prop that is not there.
KN-165A value in no known form, or keys that do not run forward.
KN-166A call to a function that fns does not have.
KN-167An order: a band that is not one of the bands, or a comp placed inside itself.
KN-168An element: a src that finds nothing, or a <template> that a copy needs and is missing.
KN-169Names that lead back to themselves.
KN-100A call with bad arguments, such as solid layers without kinetic.25d.js on the page.

Lookup: the top-level keys

The defaults in this table are the ones layers.scene() reads from the page. With layers(), set id, size and duration yourself.

KeyDefaultMeaning
idthe scene's nameThe piece's name.
sizethe root's data-width and data-height[width, height] in px.
durationthe clip's data-durationThe length, in unit.
fpsthe root's data-fps, else 30Frames a second.
unit'second''second' or 'frame', the unit for every time in the layers.
snapnone'frame' moves each marker and beat to its nearest frame.
loopnonetrue wraps time back to 0 at duration. { wrap: true } also wraps a placement that runs past the end, so the part past the end shows again from the start.
constants{}Named numbers, read by name and in sums.
shared{}Named values that several layers read, in any value form.
swatches{}Named colors.
roles{}Colors and looks by role, which comps and placements can override.
curves{}Named falloffs for a solid's shading marks: an ease name, { power }, { from, ease, power }, or points [[x, y], ...].
presets{}Animations an HTML layer plays from its own start, as { opacity, y, scale } tracks.
looks, bands, stageSolids (Solids, cameras and paths).
stores[]The stores the layers read. The scene draws again when one of them changes while the clock stands still.
queries[]The wildflower.query queries the layers read. The first frame waits for their rows, and the layers are built once the rows have arrived (full tier).
inputsnoneThe composition variables the layers read, declared as a scene declares them (inputs) and read with { from: 'inputs.name' }. An input that fails its check raises KN-130 once and reads as undefined.
propsnoneThe clip's own data-prop-* attributes, as { name: { type, default } }, read with { from: 'props.name' }. Each clip that uses the scene shows its own values.
steps{}A lesson's steps, { id: { wait, until, entry, script } }, as step() takes them. For lesson steps where nothing moves, give steps and layers: [].
finishnoneThe finishing pass (grain, vignette, bloom, ...), as config({ finish }) takes it.
shutternoneThe render's motion blur, in degrees or { angle, samples }, as config({ shutter }) takes it.
layersrequiredThe layers, in drawing order, so a later layer draws over an earlier one.
comps{}Groups of layers, { name: { layers, params, roles, looks, marks } }, that a kind: 'comp' layer places.

Next

Moving an element covers each key a layer takes: src, life, delay, place, look, content, repeat and animate.