Text layers
A text layer writes an element's text and can move each letter or word on its own. This page covers writing a line, typing and scrambling it, splitting it into parts that each play their own animation, parts made from a template, captions from a word-timed transcript, type drawn on a canvas, and the values a text layer reports.
A text layer
A text layer is a layer with kind: 'text'.
Its src finds the element, and content.text is what the element shows.
{ id: 'title', kind: 'text', src: 'h1', content: { text: 'Meadowlark' } }
The h1 shows Meadowlark for the layer's whole life, whatever text the HTML gave it.
A string in content.text is shown exactly as written.
To show a value that changes, such as a parameter, a store's field or another layer's report, give a link (Values).
{ id: 'score', kind: 'text', src: '.score', content: { text: { from: 'game.score' } } }
The element shows the store game's score field, and changes when the field does.
The scene lists the store in stores: ['game'].
An html layer's content.text shows a string as written too.
Use a text layer when the text is split, typed, scrambled or captioned.
A text layer also takes life, delay, place and look, as an html layer does (Moving an element), so the whole line can move and fade while its letters move inside it.
Typing a line
content.typewriter types the text a few letters at a time.
{ id: 'note', kind: 'text', src: '.note',
content: { text: 'Please have your boarding pass ready.', typewriter: { start: 2, cps: 18 } } }
The line is empty until 2 seconds, then gains 18 characters a second until the whole sentence shows.
start is a time on the layer's clock (0 when left out), and cps is characters a second (20 when left out).
The letters come from typewriter().
Scrambling a line
content.scramble shows the text as random characters that settle into the real ones from left to right.
{ id: 'gate', kind: 'text', src: '.gate',
content: { text: 'GATE 12 BOARDING', scramble: { start: 0.3, duration: 1.6, seed: 7 } } }
Before 0.3 seconds every letter is scrambled.
From 0.3 the letters settle one after another, and at 1.9 seconds the line reads GATE 12 BOARDING.
The spaces never scramble.
Its options are those of scramble().
startis when the settling begins, 0 when left out.durationis how long it takes, in the scene's unit, 1 second when left out.fpsis how many times a second an unsettled letter changes, 20 when left out.charsis the characters an unsettled letter picks from, such as'01'for binary.seedpicks the random characters, so the same seed shows the same ones on every render.
<div data-composition-id="main" data-start="0" data-duration="5"
data-width="1280" data-height="720" data-fps="30">
<div class="board" data-component="board" data-start="0" data-duration="5">
<p class="gate"></p>
<p class="note"></p>
<p class="tally"><span class="count"></span> characters typed</p>
</div>
</div>
wildflower.kinetic.layers.scene('board', {
layers: [
{
id: 'gate', kind: 'text', src: '.gate',
content: { text: 'GATE 12 BOARDING', scramble: { start: 0.3, duration: 1.6, seed: 7 } },
},
{
id: 'note', kind: 'text', src: '.note',
content: { text: 'Please have your boarding pass ready.', typewriter: { start: 2, cps: 18 } },
},
// Reads how many characters 'note' shows, so it is listed after 'note'.
{ id: 'count', kind: 'html', src: '.count', content: { text: { from: 'note.length' } } },
],
});
html, body { margin: 0; }
[data-composition-id] {
position: relative;
overflow: hidden;
background: #10131c;
color: #fff;
font-family: system-ui, sans-serif;
}
[data-composition-id]:not([data-kinetic-ready]) { visibility: hidden; }
.board {
position: absolute;
inset: 0;
display: flex;
flex-direction: column;
justify-content: center;
padding: 0 120px;
}
.gate {
margin: 0;
font-family: ui-monospace, Menlo, monospace;
font-size: 88px;
color: #ffd166;
white-space: pre;
min-height: 1.2em;
}
.note { margin: 36px 0 0; font-size: 40px; min-height: 1.3em; }
.tally { margin: 48px 0 0; font-size: 24px; color: #8a93a8; }
The count under the line reads the typed layer's length, which a text layer reports.
Splitting into letters or words
content.split breaks the text into parts, one element for each letter ('letter') or each word ('word').
Each space between them becomes a part of its own, so the gaps stay.
{ id: 'name', kind: 'text', src: 'h1', content: { text: 'Meadowlark', split: 'letter' } }
The h1 holds ten spans, one for each letter, and looks the same as before.
Each span is set to inline-block so that it can move.
A part moves when the layer has an animate block, below.
A split line cannot also be typed or scrambled.
Giving split with typewriter or scramble stops the scene with KN-162.
Animating each part
animate gives every part the same tracks, values that change over time such as { at: [0, 0.7], to: [70, 0] }, each read on the part's own clock.
start is when the first part's clock reaches 0, and each is how much later each next part starts.
{
id: 'name', kind: 'text', src: 'h1',
content: { text: 'Meadowlark', split: 'letter' },
animate: {
start: 0.2, each: 0.05,
place: { translate: [0, { at: [0, 0.7], to: [70, 0], ease: 'power3.out' }] },
look: { opacity: { at: [0, 0.3], to: [0, 1] } },
},
}
The M rises 70 px into place and fades in from 0.2 seconds. Each next letter does the same 0.05 seconds later, so the word fills in from left to right and the k lands at about 1.35 seconds.
A part's place takes three keys.
translateis[x, y]in px from where the part sits.rotateis degrees, about the part's center.scaleis a number, 1 for its own size.
animate: { each: 0.05, place: { rotate: { at: [0, 0.7], to: [-14, 0] }, scale: { at: [0, 0.7], to: [0.6, 1] } } }
Each letter turns from -14 degrees to level and grows from 60% to full size over 0.7 seconds of its own clock.
A part's look takes five keys.
opacityis 0 to 1.bluris in px.coloris a color, a swatch or a role, and a track blends between colors.classadds a class to the part next to the classes it already has, such as{ keys: [[0, 'dim'], [1, ['hold', 'lit']]] }.stylesets any CSS property, written with aunitwhen the value has one.
animate: {
each: 0.12,
look: {
blur: { at: [0, 0.5], to: [10, 0] },
color: { at: [0, 0.8], to: ['#ffd166', '#ffffff'] },
style: { letterSpacing: { at: [0, 0.8], to: [6, 0], unit: 'px' } },
},
}
Each word comes into focus over half a second, turns from yellow to white, and its letters close up from 6 px apart.
The order the parts start in
each, total, from, ease, grid, axis and seed are the options of stagger(), which works out each part's delay.
Without each or total, every part starts at start.
animate: { start: 1, total: 0.6, from: 'center', place: { scale: { at: [0, 0.5], to: [0, 1] } } }
The middle letters grow first and the outer ones last, and the last ones start 0.6 seconds after the first.
total spreads the delays over that time whatever the number of parts.
from also takes 'first', 'last', 'edges', 'random' or a part's index, and seed sets the order 'random' gives.
The spaces count in the order but stay still.
spaces: true plays the tracks on the spaces as well.
A part's own i and n
Every part reads i, its index from 0, and n, the number of parts, the spaces included.
A track can use them by name, and so can { stagger }, { sweep } and a function in fns.
animate: { look: { color: { call: 'hue', args: ['i', 'n'] } } }
// fns: { hue: (i, n) => 'hsl(' + Math.round(360 * i / n) + ', 80%, 65%)' }
The letters run through the colors of the rainbow from the first to the last.
<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="title" data-start="0" data-duration="4">
<h1></h1>
<p class="tagline"></p>
</div>
</div>
wildflower.kinetic.layers.scene('title', {
layers: [
{
id: 'name', kind: 'text', src: 'h1',
content: { text: 'Meadowlark', split: 'letter' },
animate: {
start: 0.2, each: 0.05,
place: {
translate: [0, { at: [0, 0.7], to: [70, 0], ease: 'power3.out' }],
rotate: { at: [0, 0.7], to: [-14, 0], ease: 'power3.out' },
},
look: { opacity: { at: [0, 0.3], to: [0, 1] } },
},
},
{
id: 'tagline', kind: 'text', src: '.tagline',
content: { text: 'Field notes from the long grass', split: 'word' },
animate: {
start: 1.4, each: 0.15,
look: {
opacity: { at: [0, 0.5], to: [0, 1] },
blur: { at: [0, 0.5], to: [10, 0] },
},
},
},
],
});
html, body { margin: 0; }
[data-composition-id] {
position: relative;
overflow: hidden;
background: #14231b;
color: #fff;
font-family: Georgia, 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: 150px; font-weight: 400; }
.tagline { margin: 24px 0 0; font-size: 40px; font-style: italic; color: #cfe3c4; }
Parts from a template
A part is a plain span unless the element holds a <template>.
With a template, each part is a copy of the template's first element, with the letter or word written at the start of it.
Anything else inside the template stays after the letter, in every copy.
<h1 class="stamp">
<template><span class="tile"><i class="dot"></i></span></template>
</h1>
{ id: 'stamp', kind: 'text', src: '.stamp', content: { text: 'OPEN', split: 'letter' } }
The h1 holds four span.tile elements, each with its letter followed by an i.dot, and the CSS for .tile and .dot styles every letter.
A part made from a template is not set to inline-block, so give its class display: inline-block when it moves, and white-space: pre so that the spaces keep their width.
Captions
content.captions shows the line of a word-timed transcript that is being spoken at the current time.
A transcript is a list of words, each with its text and its start and end in seconds, which transcription tools write.
const cues = [
{ text: 'Every', start: 0.3, end: 0.6 },
{ text: 'frame', start: 0.6, end: 1.0 },
// ...
];
{ id: 'line', kind: 'text', src: '.line', content: { captions: { cues, maxWords: 4, hold: 0.5 } } }
The element shows up to four words at a time. A line shows from its first word's start until half a second after its last word ends, or until the next line starts if that is sooner. Before the first word and after the last line it is empty.
The words are grouped into lines as captions() groups them, with the same options.
maxWordsandmaxCharsare the most words and characters in a line.maxGapis the seconds of silence that start a new line.sentencesends a line at a word that ends in.,!or?, true when left out.holdis the seconds a line stays after its last word.
The transcript is given one of two ways.
cuesis the list itself, or a link that reads it from a query, such ascues: { from: 'words.rows' }withqueries: ['words'](Captions from a voice track).srtis the text of an SRT or WebVTT file, whichsrt()reads into words.
The transcript is read once, when the layer first draws.
Giving both cues and srt, or neither, stops the scene with KN-162, and so does a captions layer that also has text, split, typewriter or scramble.
Styling each word
Each word of the line is a part of its own, a span or a copy of the element's template.
look.words styles a word by its state: active while it is being spoken, past once it has been, and future before it is.
Each state is an object of CSS properties.
look: {
words: {
active: { color: '#ffd166' },
past: { color: '#ffffff' },
future: { color: '#ffffff', opacity: 0.35 },
},
}
The word being spoken is yellow, the words already spoken are white, and the words still to come are white at 35%.
A word reads i and n, its place in the line, and p, how far through the word the speaker is, from 0 at its start to 1 at its end.
active: { opacity: { add: [0.4, { multiply: [0.6, 'p'] }] } }
The word being spoken brightens from 40% to full as it is said.
The words sit next to each other with no space between them, since each one is its own element.
Space them with CSS, such as display: flex and a gap on the element.
<div data-composition-id="main" data-start="0" data-duration="5.5"
data-width="1280" data-height="720" data-fps="30">
<div class="screen" data-component="subtitles" data-start="0" data-duration="5.5">
<p class="line"></p>
</div>
</div>
const cues = [
{ text: 'Every', start: 0.3, end: 0.6 },
{ text: 'frame', start: 0.6, end: 1.0 },
{ text: 'is', start: 1.0, end: 1.15 },
{ text: 'a', start: 1.15, end: 1.25 },
{ text: 'function', start: 1.25, end: 1.8 },
{ text: 'of', start: 1.8, end: 1.95 },
{ text: 'time.', start: 1.95, end: 2.5 },
{ text: 'Seek', start: 3.0, end: 3.35 },
{ text: 'anywhere,', start: 3.35, end: 3.9 },
{ text: 'and', start: 4.0, end: 4.2 },
{ text: 'it', start: 4.2, end: 4.35 },
{ text: 'matches.', start: 4.35, end: 5.0 },
];
wildflower.kinetic.layers.scene('subtitles', {
layers: [{
id: 'line', kind: 'text', src: '.line',
content: { captions: { cues, maxWords: 4, hold: 0.5 } },
look: {
words: {
active: { color: '#ffd166', opacity: { add: [0.4, { multiply: [0.6, 'p'] }] } },
past: { color: '#ffffff', opacity: 1 },
future: { color: '#ffffff', opacity: 0.35 },
},
},
}],
});
html, body { margin: 0; }
[data-composition-id] {
position: relative;
overflow: hidden;
background: #181226;
color: #fff;
font-family: system-ui, sans-serif;
}
[data-composition-id]:not([data-kinetic-ready]) { visibility: hidden; }
.screen { position: absolute; inset: 0; }
.line {
position: absolute;
left: 80px;
right: 80px;
bottom: 120px;
margin: 0;
display: flex;
justify-content: center;
gap: 0 20px;
font-size: 64px;
font-weight: 700;
}
Type on a canvas
With content.font, a text layer draws its line on a <canvas> from a font's outlines, through text(), and each letter can change its weight or any other axis the font has.
The outlines come from the outline baker, whose file sets a global variable, and content.font is that variable's name.
{
id: 'word', kind: 'text', src: 'canvas.word',
content: { font: 'OUTLINES', text: 'KINETIC', at: [640, 360], size: 160, align: 'center', valign: 'middle', tracking: 0.08, color: '#ffffff' },
animate: {
each: 0.08,
look: { opacity: { at: [0, 0.4], to: [0, 1] } },
axes: { wght: { at: [0, 0.8], to: [100, 900] } },
},
}
The letters of KINETIC fade in one after another, centered on the canvas, and each one thickens from the thinnest weight to the boldest over 0.8 seconds of its own clock.
content.textis a string here, the letters to draw.at,size,align,valign,tracking,spacingandcolorare the line's options, astext()takes them, and each takes any value form.animatetakesstartand the stagger's options as a split line does, and each letter'splace(translate,rotate,scale),look(opacity,blur,color) andaxes, the font's axes by their tags.- A font name that no loaded file has set stops the scene with KN-163, so load the baker's file in a
<script>before the scene's.
Values a text layer reports
A text layer reports what it shows, for other layers to read with a link.
'id.text'is the text shown now: the typed part of a typed line, the current letters of a scrambled one, or the caption line.'id.length'is the number of characters shown, spaces included, or of words in a caption line.'id.index', with captions, is the index of the line shown, from 0.'id.empty', with captions, is true between lines.
{ id: 'number', kind: 'html', src: '.number', content: { text: { add: [{ from: 'line.index' }, 1] } } }
Next to the captions of the example above, this element shows 1 while the first line is up, 2 while the second is, and 0 when no line is up, where index is -1.
A layer that reads a report is listed after the layer it reads, since the reports are worked out in the order the layers are drawn.
Errors
The scene checks a text layer when it is made, and stops with an error that gives the layer and the field.
- KN-162: a
splitthat is not'letter'or'word', a split line that is also typed or scrambled,animatewithoutsplitorfont, a keyanimateor a part'splaceorlookdoes not take, captions with both or neither ofcuesandsrt, an option captions does not take, andlook.wordswithout captions or with a state other thanactive,pastandfuture. - KN-163: a
content.fontthat no loaded outlines file has set.
Lookup
A text layer takes id, kind, life, src, parent, delay, place, look, content and animate.
| Key | Meaning |
|---|---|
content.text | The text.
A string is shown as written, and a link ({ from }) reads a parameter, a store's field or another layer's report. |
content.split | 'letter' or 'word' splits the text into one part per letter or word, and each space becomes a part of its own. |
content.typewriter | { start, cps }, as typewriter() takes them. |
content.scramble | { start, duration, fps, chars, seed }, as scramble() takes them. |
content.captions | { srt | cues, maxWords, maxChars, maxGap, sentences, hold } shows the caption line for the current time, as captions() gives it, with one part per word. |
look.words | { active, past, future }, the CSS of a caption word in each state.
Each word reads i, n and p. |
content.font | On a <canvas>, the baked outlines that text() draws, with at, size, align, valign, tracking, spacing and color as the line's options, and animate.axes (wght, ...) for each letter. |
animate | start, the stagger's each, total, from, ease, grid, axis and seed, spaces: true to move the spaces too, and each part's place and look tracks in its own time.
The parts start together unless each or total is given. |
animate.place | translate, rotate, scale. |
animate.look | opacity, blur, color, class, style. |
Each part is a copy of the element's own <template>, or an inline-block span when the element has none.
| Report | Meaning |
|---|---|
'id.text' | The text shown now. |
'id.length' | The characters shown, or the words in the caption line. |
'id.index' | With captions, the line's index. |
'id.empty' | With captions, true between lines. |
Next
Comps and copies groups layers into a comp and places it many times, once for each row of data.