Comps and copies

A comp is a named group of layers that you write once and place as often as you need, each time with its own start and its own values. This page covers comps and their placements, where a placement starts, parameters and roles, copies made from rows of data or from a count, copies made from a template, copies of one element, and the repeater.

A comp and its placements

A comp is a list of layers under a name in comps. A layer with kind: 'comp' places it, which draws the comp's layers with the comp's time 0 at the placement's start. Each use of a comp is called a placement.

comps: {
    card: {
        layers: [
            { id: 'title', kind: 'html', src: '.title', look: { opacity: { at: [0, 0.5], to: [0, 1] } } },
            { id: 'rule', kind: 'html', src: '.rule', look: { style: { width: { at: [0.3, 1], to: [0, 400], unit: 'px' } } } },
        ],
    },
},
layers: [
    { id: 'intro', kind: 'comp', comp: 'card', start: 1.5 },
],

The title fades in from 1.5 to 2 seconds, and the rule under it grows from 1.8 to 2.5 seconds. Every time inside the comp counts from the placement's start, so the comp reads the same wherever it is placed.

A comp lists its layers in drawing order, as the scene's layers does, and a layer's id is unique within its comp. A comp can place another comp, and the starts add up. A comp that places itself, directly or through another comp, stops the scene with KN-167.

A placement's layers find their elements as the scene's own layers do, in the clip first. Two placements of the same comp without repeat therefore draw on the same elements, and the one listed last shows. To show a comp more than once, repeat it, as the rest of this page does.

Where a placement starts

start is where the comp's time 0 falls on the clip's clock, 0 when left out. The clip is the element with the scene's data-component, and its clock counts from its own data-start. It takes any value that gives a time.

{ id: 'intro', kind: 'comp', comp: 'card', start: 2 }

The comp starts 2 seconds into the clip.

{ id: 'intro', kind: 'comp', comp: 'card', start: 'hit1' }

The comp starts at the marker hit1. A clip's id used as a name is the time that clip ends, so start: 'opening' starts the comp when the clip opening ends, and 'opening + 0.5' half a second after.

{ id: 'intro', kind: 'comp', comp: 'card', start: { startOf: 'closing' } }

The comp starts when the clip closing starts.

{ id: 'rows', kind: 'comp', comp: 'row', src: '.rows', start: { stagger: { each: 0.15 } }, repeat: { count: 4 } }

On a repeated comp, start is worked out in each copy, so a { stagger } starts the copies 0.15 seconds apart, the first at 0 and the fourth at 0.45.

A placement's start is worked out once, when the layers are built.

Parameters

Parameters are named values that a comp's layers read. The comp gives its defaults in params, and a placement's own params replace them.

comps: {
    badge: {
        params: { size: 1, lift: 40 },
        layers: [
            { id: 'disc', kind: 'html', src: '.disc', place: { scale: 'size', translate: [0, { at: [0, 0.6], to: ['lift', 0] }] } },
        ],
    },
},
layers: [
    { id: 'big', kind: 'comp', comp: 'badge', params: { size: 1.5 } },
],

The disc rises 40 px into place at one and a half times its size. size comes from the placement, and lift from the comp's default.

A layer reads a number parameter by its name, in a sum too, such as 'lift + 10'. It reads text through a link, { from: 'params.label' }, since a string in content.text is shown as written (Values). A parameter's value can itself be any value form, worked out in the placement above it on that placement's clock, so a nested comp's params: { y: 'x + 1' } reads the x of the comp that places it.

Roles

A role is a color by the job it does, such as accent, which a comp and each placement can set differently. A role names a swatch from the scene's swatches, or a color, or another role.

swatches: { gold: '#ffd166', sea: '#4cc9f0' },
comps: {
    dot: {
        params: { y: 300 },
        roles: { accent: 'gold' },
        layers: [{ id: 'circle', kind: 'svg', src: ':scope', content: { cx: '90i + 145', cy: 'y', r: 30, fill: 'accent' } }],
    },
},
layers: [
    { id: 'warm', kind: 'comp', comp: 'dot', src: '.warm', repeat: { count: 3 } },
    { id: 'cool', kind: 'comp', comp: 'dot', src: '.cool', params: { y: 420 }, roles: { accent: 'sea' }, repeat: { count: 3 } },
],

The three dots the warm placement draws are gold, and the three cool draws below them are sea blue. Each placement makes its copies in its own SVG group, as Each copy from a template shows. A color is looked up in the placement's roles first, then the comp's, then those of the placement above, up to the scene's own roles.

A role or a swatch name works wherever a color goes: an SVG layer's attributes such as fill and stroke, look.color, any property in look.style, a text part's or a caption word's color, the inks and paper of a layer call, and a track between colors.

Copies from rows

repeat on a comp layer places the comp once for each row of a list. Each of these placements is called a copy. With src, each copy is a clone of the <template> inside the element src finds, and the copy's layers find their elements inside its own clone.

<div class="rows">
    <template>
        <div class="row"><span class="label"></span><div class="bar"></div><b class="value"></b></div>
    </template>
</div>
{ id: 'bars', kind: 'comp', comp: 'bar', src: '.rows',
  repeat: { over: [{ label: 'Lupine', value: 82 }, { label: 'Poppy', value: 64 }] } }

The .rows element holds two .row elements, one for Lupine and one for Poppy, each drawn by the bar comp's layers.

What a copy reads

Each copy reads its row's fields and two numbers of its own.

  • i is the copy's index, from 0.
  • n is the number of copies.
  • A number field is read by its name, so '7value' is 7 times the row's value.
  • A text field is read through a link, { from: 'params.label' }. Written as a plain string, 'label', it shows the word label.
  • A row that is not an object, such as each string of ['Lupine', 'Poppy'], is read as item.
{ id: 'row', kind: 'html', src: ':scope', place: { translate: [0, '80i'] } }

Each row sits 80 px below the one before, since '80i' is 0 for the first copy, 80 for the second, and so on. ':scope' is the copy's own element, here the .row.

{ id: 'label', kind: 'html', src: '.label', content: { text: { from: 'params.label' } } }

Each row's .label shows that row's label, Lupine in the first and Poppy in the second.

A link reads a row's field exactly as it is. A field '1.10' shows as 1.10, and a label that happens to match a layer's or a marker's name stays the label.

A row cannot have a field named i or n, since a copy reads those names as its index and the count. A row with either stops the scene with KN-162, so rename the field.

A layer in a copy can link to another layer in the same copy by its id, and reads that copy's layer.

<div data-composition-id="main" data-start="0" data-duration="4"
     data-width="1280" data-height="720" data-fps="30">
    <div class="chart" data-component="chart" data-start="0" data-duration="4">
        <h2>Blooms counted in June</h2>
        <div class="rows">
            <template>
                <div class="row">
                    <span class="label"></span>
                    <div class="bar"></div>
                    <b class="value"></b>
                </div>
            </template>
        </div>
    </div>
</div>
wildflower.kinetic.layers.scene('chart', {
    comps: {
        // One row of the chart: its place, its label, its bar and its number.
        bar: {
            layers: [
                { id: 'row', kind: 'html', src: ':scope', place: { translate: [0, '80i'] }, look: { opacity: { at: [0, 0.3], to: [0, 1] } } },
                { id: 'label', kind: 'html', src: '.label', content: { text: { from: 'params.label' } } },
                { id: 'bar', kind: 'html', src: '.bar', look: { style: { width: { at: [0, 1], to: [0, '7value'], ease: 'power2.out', unit: 'px' } } } },
                { id: 'value', kind: 'html', src: '.value', content: { text: ['value'] } },
            ],
        },
    },
    layers: [{
        id: 'bars', kind: 'comp', comp: 'bar', src: '.rows',
        start: { stagger: { each: 0.2 } },
        repeat: {
            over: [
                { label: 'Lupine', value: 82 },
                { label: 'Poppy', value: 64 },
                { label: 'Aster', value: 47 },
                { label: 'Yarrow', value: 31 },
            ],
        },
    }],
});
html, body { margin: 0; }
[data-composition-id] {
    position: relative;
    overflow: hidden;
    background: #101820;
    color: #fff;
    font-family: system-ui, sans-serif;
}
[data-composition-id]:not([data-kinetic-ready]) { visibility: hidden; }
.chart { position: absolute; inset: 0; }
.chart h2 { position: absolute; left: 120px; top: 110px; margin: 0; font-size: 44px; font-weight: 600; }
.rows { position: absolute; left: 120px; right: 120px; top: 230px; }
.row { position: absolute; left: 0; right: 0; top: 0; height: 60px; display: flex; align-items: center; }
.label { width: 160px; padding-right: 24px; text-align: right; font-size: 28px; color: #c9d4e0; }
.bar { height: 44px; border-radius: 6px; background: #8bd17c; }
.value { margin-left: 16px; font-size: 28px; }
A bar chart from rows

Each row fades in and its bar grows to 7 px for each bloom, 0.2 seconds after the row above it. The number at the end of each bar is the row's value, read by name.

Where the rows come from

repeat takes one of four forms.

repeat: { over: [{ label: 'Lupine', value: 82 }, { label: 'Poppy', value: 64 }] }

The rows are written in the scene, one copy for each.

repeat: { over: 'sales.rows' }

The rows are a field of a query or a store the scene reads, here the rows of the query sales, listed in queries: ['sales']. The path can also read 'inputs.name' or 'props.name'. A chart from data builds a bar chart race this way.

repeat: { over: { call: 'top', args: [{ from: 'sales.rows' }, 3] } }
// fns: { top: (rows, k) => rows.slice(0, k) }

The rows are what a function in fns returns, here the first three of the query's rows. An argument that is a link reads a query's, a store's, an input's or a prop's field, and any other argument is passed as written.

repeat: { count: 12 }

Twelve copies, each reading its i from 0 to 11 and n as 12. count is a whole number above 0.

The rows are read once, when the layers are built. With queries, the layers are built once the query's rows have arrived, so the copies match the data.

Each copy from a template

With src, the copies are clones of the first element in the container's <template>, added to the container in order. This also works inside an <svg>, where each copy is an SVG element.

<svg viewBox="0 0 1280 720">
    <g class="top"><template><circle r="0"></circle></template></g>
</svg>
{ id: 'top', kind: 'comp', comp: 'dot', src: '.top', repeat: { count: 12 } }

The group .top holds twelve circles, and each copy's layers draw on its own circle.

Without src, the copies have no elements of their own, so every copy draws on the same elements and the last copy shows. A src whose element has no <template>, or that finds no element, stops the scene with KN-168. A src on a comp layer without repeat stops it with KN-162.

<div data-composition-id="main" data-start="0" data-duration="3"
     data-width="1280" data-height="720" data-fps="30">
    <div class="sky" data-component="dots" data-start="0" data-duration="3">
        <svg viewBox="0 0 1280 720">
            <g class="top"><template><circle r="0"></circle></template></g>
            <g class="bottom"><template><circle r="0"></circle></template></g>
        </svg>
    </div>
</div>
wildflower.kinetic.layers.scene('dots', {
    swatches: { gold: '#ffd166', sea: '#4cc9f0' },
    comps: {
        // One dot: placed along the row by its index, at the height y, in the accent color.
        dot: {
            params: { y: 260 },
            roles: { accent: 'gold' },
            layers: [{
                id: 'circle', kind: 'svg', src: ':scope',
                content: {
                    cx: '90i + 145',
                    cy: 'y',
                    r: { at: [0, 0.6], to: [0, 30], ease: 'back.out' },
                    fill: 'accent',
                },
            }],
        },
    },
    layers: [
        { id: 'top', kind: 'comp', comp: 'dot', src: '.top', start: { stagger: { each: 0.08 } }, repeat: { count: 12 } },
        {
            id: 'bottom', kind: 'comp', comp: 'dot', src: '.bottom',
            start: { add: [1, { stagger: { each: 0.08, from: 'last' } }] },
            params: { y: 460 }, roles: { accent: 'sea' }, repeat: { count: 12 },
        },
    ],
});
html, body { margin: 0; }
[data-composition-id] {
    position: relative;
    overflow: hidden;
    background: #0e1424;
}
[data-composition-id]:not([data-kinetic-ready]) { visibility: hidden; }
.sky { position: absolute; inset: 0; }
.sky svg { position: absolute; inset: 0; width: 100%; height: 100%; }
Copies inside an SVG

A row of gold dots pops in from the left. From 1 second a row of blue dots pops in below it from the right, since that placement gives its own y and its own accent, and its stagger starts from: 'last'.

Copies of one element

An html layer has a repeat of its own, for many copies of one element that each play the same tracks. It makes the copies from the <template> inside the layer's element, and animate gives each copy its place and look. Each copy reads its i and n.

{
    id: 'bars', kind: 'html', src: '.bars', repeat: { count: 24 },
    animate: { look: { style: { height: { call: 'level', args: ['i', { rate: 1 }], unit: 'px' } } } },
}
// fns: { level: (i, t) => 40 + 160 * (0.5 + 0.5 * Math.sin(t * 4 - i * 0.45)) }

Twenty-four bars rise and fall in a wave that runs from left to right. count takes any value that gives a number, so a knob or a store's field can change how many copies there are.

Use a comp with repeat when each copy has several elements or its own row of data, and an html layer's repeat when each copy is one element moved by one set of tracks. { every, life } is the html layer's other form, a copy born every so often that lives for a while (Moving an element). A layer's animate keys are the same as a text part's (Animating each part).

<div data-composition-id="main" data-start="0" data-duration="4"
     data-width="1280" data-height="720" data-fps="30">
    <div class="meter" data-component="meter" data-start="0" data-duration="4">
        <div class="bars">
            <template><i class="bar"></i></template>
        </div>
    </div>
</div>
wildflower.kinetic.layers.scene('meter', {
    layers: [{
        id: 'bars', kind: 'html', src: '.bars', repeat: { count: 24 },
        animate: { look: { style: { height: { call: 'level', args: ['i', { rate: 1 }], unit: 'px' } } } },
    }],
}, {
    fns: {
        // A bar's height from its index and the time: a wave that runs along the row.
        level: (i, t) => 40 + 160 * (0.5 + 0.5 * Math.sin(t * 4 - i * 0.45)),
    },
});
html, body { margin: 0; }
[data-composition-id] {
    position: relative;
    overflow: hidden;
    background: #16121f;
}
[data-composition-id]:not([data-kinetic-ready]) { visibility: hidden; }
.meter { position: absolute; inset: 0; }
.bars {
    position: absolute;
    left: 160px;
    right: 160px;
    bottom: 200px;
    height: 240px;
    display: flex;
    align-items: flex-end;
    gap: 12px;
}
.bar { flex: 1; height: 40px; border-radius: 4px 4px 0 0; background: #b48cff; }
Copies of one element

The repeater

look.repeat on an html layer makes copies of the element's <template>, each one moved, turned and scaled once more than the one before, as repeat() makes them.

{
    id: 'fan', kind: 'html', src: '.fan',
    look: { repeat: { count: 12, rotate: { at: [0.2, 1.6], to: [0, 15], ease: 'power2.out' }, opacity: [1, 0.35], origin: '50% 100%' } },
}

Twelve leaves fan out about their bottom edge from 0.2 to 1.6 seconds, each turned 15 degrees more than the one before, and fade toward the last, which is at 35%.

  • count is the number of copies, and 0 or less makes none.
  • translate is [x, y] in px, rotate is degrees, and scale is a factor, each the step from one copy to the next.
  • opacity is [first, last], mixed across the copies, or the last copy's alone.
  • offset shifts the series, so copy k takes copy k + offset's step, and origin is every copy's transform-origin.
  • each: { scale } scales every copy about its own origin after its step.

Every option takes any value form. The layer reports 'fan.count', the number of copies, and the last copy's 'fan.transform' and 'fan.transformOrigin', for layers listed after it to read with a link.

Errors

The scene checks its comps and copies when it is made, and stops with an error that gives the layer and the field.

  • KN-161: a comp layer's comp names no comp in comps.
  • KN-162: a repeat that is not { over } or { count }, a count that is not a whole number above 0, both over and count, a src on a comp layer without repeat, and a row with a field named i or n. On an html layer, a repeat that is not { count } or { every, life }.
  • KN-164: repeat.over reads a path whose first part is no query, store, inputs or props the scene reads.
  • KN-165: what repeat.over reads or a function returns is not a list.
  • KN-166: repeat.over calls a function that is not in fns.
  • KN-167: a comp that places itself.
  • KN-168: a repeat's element that has no <template>, or a src that finds no element.

Lookup

The scene's comps is { name: { layers, params, roles, looks, marks } }. looks and marks are for solids (Solids, cameras and paths).

Comp layer keyMeaning
compThe name of the comp it places.
startWhere the comp's time 0 falls: a time, a marker, a sum, { startOf: 'clipId' }, or a value worked out in each copy, such as { stagger: { each: 0.15 } }.
paramsValues its layers read by name, over the comp's own params.
rolesColors by role, over the comp's own roles.
repeat{ over: [rows] }, { over: 'query.path' }, { over: { call, args } } or { count }. Each copy is a placement of its own and reads i, n and its row's fields.
srcWith repeat, the container whose <template> each copy clones, also inside an <svg>.
parentA null or group the placement moves with (Solids, cameras and paths).
A copy readsAs
i, nIts index from 0, and the number of copies.
A number fieldIts name, such as 'value' or '7value'.
A text fieldA link, { from: 'params.label' }.
A row that is not an objectitem, such as { from: 'params.item' }.
CopiesWhereEach copy
repeat on a comp layerThe comp, once for each row or up to countA clone of src's template, drawn by all the comp's layers, reading i, n and its row.
repeat: { count } on an html layerThe layer's elementA clone of the element's template, moved by animate, reading i and n.
repeat: { every, life } on an html layerThe layer's elementA clone of the element's template, born every every and shown while its age is under life.
look.repeatThe layer's elementA clone of the element's template, a step on from the one before.

Next

Layer calls runs Kinetic's effects, shading, drawing and transitions on a layer's element.