Repository navigation
Expand file tree
/
Copy pathcomp.js
More file actions
171 lines (158 loc) · 10.8 KB
/
Copy pathcomp.js
File metadata and controls
171 lines (158 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// The raw-motion demo, made with raw-motion. Every pixel below is a function of t.
const W = 1920, H = 1080, DUR = 12, FPS = 60;
const { $, set, vis, text, spring, impulse, kf, ease, lerp, inv, clamp, rng, noise1, typeOn, ev, hi, finish } = RM;
$('stage').style.width = W + 'px'; $('stage').style.height = H + 'px';
// ── timeline (seconds). Keeping every cue in one table is what makes re-timing painless. ──────
const A = { type: .2, strike: 1.15, no: 1.55, lib: 1.75, out: 2.7 };
const B = { head: 3.05, panel: 3.15, type: 3.35, go: 4.6, out: 5.9 };
const C = { in: 6.05, formed: 7.3, gather: 8.15, out: 8.55 };
const D = { ball: 8.5, whip: 8.8, land: 9.1, ghosts: 9.25, out: 9.95 };
const E = { logo: 10.2, tag: 10.7, url: 11.0 };
// ── A · "No libraries." ────────────────────────────────────────────────────────────────────────
const IMP = 'import { animate } from "some-library"';
const impEl = $('imp'), impText = document.createTextNode(''); impEl.prepend(impText);
const bigWords = [...$('big').children];
// ── B · the code that is literally moving the box next to it ───────────────────────────────────
const CODE = 'function seek(t) {\n const u = spring(t - 4.6)\n box.x = lerp(-160, 280, u)\n box.rotate = 180 * u\n}';
const CPS = 55;
const hl = s => s.replace(/&/g, '&').replace(/</g, '<')
.replace(/-?\b\d+(\.\d+)?\b/g, m => `<span class="n">${m}</span>`)
.replace(/\b(function|const)\b/g, '<span class="k">$1</span>')
.replace(/\b(seek|spring|lerp)\b/g, '<span class="f">$1</span>');
let codeHTML = '';
// ── C · a seeded swarm that settles into the word seek(t) ──────────────────────────────────────
const cv = $('swarm'); cv.width = W; cv.height = H; const cx = cv.getContext('2d');
const P = (() => {
const off = document.createElement('canvas'); off.width = W; off.height = H;
const o = off.getContext('2d');
o.fillStyle = '#fff'; o.font = '700 300px "SF Mono", Menlo, "DejaVu Sans Mono", monospace';
o.textAlign = 'center'; o.textBaseline = 'middle'; o.fillText('seek(t)', W / 2, H / 2 - 40);
const px = o.getImageData(0, 0, W, H).data, pts = [];
for (let y = 0; y < H; y += 7) for (let x = 0; x < W; x += 7) if (px[(y * W + x) * 4 + 3] > 128) pts.push([x, y]);
const r = rng(42);
for (let i = pts.length - 1; i > 0; i--) { const j = Math.floor(r() * (i + 1)); [pts[i], pts[j]] = [pts[j], pts[i]]; }
return pts.slice(0, 2200).map(([tx, ty]) => {
const a = r() * Math.PI * 2, d = 700 + r() * 900;
return { tx, ty, sx: W / 2 + Math.cos(a) * d, sy: H / 2 + Math.sin(a) * d * .7, delay: r() * .7, hot: r() < .22, n: noise1(Math.floor(r() * 1e6)) };
});
})();
const GX = W / 2 - 640, GY = H / 2; // where the swarm collapses into the ball
// ── D · the ball + its four "sub-frame" ghosts ─────────────────────────────────────────────────
const ghosts = [0, 1, 2, 3].map(i => $('g' + i)), ball = $('ball');
const BX0 = -640, BX1 = 640;
const ballX = t => kf(t, [[D.whip, BX0], [D.land, BX1, ease.inOutCubic]]);
// ── E · logo ───────────────────────────────────────────────────────────────────────────────────
const logoEl = $('logo');
const logoChars = [...'raw-motion'].map(c => { const s = document.createElement('span'); s.textContent = c; if (c === '-') s.className = 'dash'; logoEl.appendChild(s); return s; });
// ── the cue sheet ──────────────────────────────────────────────────────────────────────────────
for (let i = 0; i < IMP.length; i += 2) ev(A.type + i / 45, 'key');
ev(A.strike, 'click'); ev(A.strike + .02, 'glitch');
ev(A.no - .05, 'whoosh', .4); ev(A.no, 'hit'); ev(A.lib, 'drop');
ev(A.out, 'whoosh_out', .4);
ev(B.head, 'whoosh', .45);
for (let i = 0; i < CODE.length; i += 2) if (CODE[i] !== ' ' && CODE[i] !== '\n') ev(B.type + i / CPS, 'key');
ev(B.go, 'pop'); ev(B.go + .35, 'blip', 3);
ev(B.out, 'whoosh_out', .45);
ev(C.in, 'riser', C.formed - C.in); ev(C.formed, 'chime');
ev(C.out, 'swell', .5);
ev(D.ball, 'pop'); ev(D.land, 'whoosh', .35); ev(D.land, 'hit');
for (let i = 0; i < 4; i++) ev(D.ghosts + i * .09, 'tick', i * 2);
ev(D.out, 'whoosh_out', .4);
ev(E.logo, 'impact'); ev(E.url, 'chime', 5);
hi(D.whip - .05, D.land + .08, 24); // the whip is ~140 px/frame: 24 blur samples
hi(C.gather, C.out + .05, 6);
function seek(t) {
// A ──────────────────────────────────────────────────────────────────────────────────────────
const aOut = ease.inCubic(inv(A.out, A.out + .35, t));
const aOn = t < A.out + .4;
vis(impEl, aOn); vis($('big'), aOn);
if (aOn) {
const s = typeOn(IMP, t, A.type, 45);
if (impText.data !== s) impText.data = s;
set(impEl, { y: -aOut * 120, o: (1 - aOut) * (t > A.strike ? .55 + .45 * (1 - inv(A.strike, A.strike + .3, t)) : 1), blur: aOut * 14 });
set($('str'), { sx: ease.outExpo(inv(A.strike, A.strike + .3, t)), o: 1 });
bigWords.forEach((w, i) => {
const u = spring(t - [A.no, A.lib][i], 2.1, .42);
const kick = impulse(t - A.lib, 4, .3) * (i === 0 ? -14 : 14);
set(w, { y: (1 - u) * 60 - aOut * 160, x: kick, s: lerp(1.9, 1, clamp(u)) + (u - clamp(u)) * .5, o: clamp(u * 2) * (1 - aOut), blur: (1 - clamp(u * 1.5)) * 18 + aOut * 18 });
});
}
// B ──────────────────────────────────────────────────────────────────────────────────────────
const bOut = ease.inCubic(inv(B.out, B.out + .35, t));
const bOn = t > B.head - .05 && t < B.out + .4;
for (const id of ['head', 'code', 'box', 'tread']) vis($(id), bOn);
if (bOn) {
const hu = spring(t - B.head, 2, .6);
set($('head'), { y: (1 - hu) * 50 - bOut * 90, o: clamp(hu) * (1 - bOut), blur: (1 - clamp(hu)) * 10 + bOut * 12 });
const pu = spring(t - B.panel, 1.7, .62);
set($('code'), { x: (1 - pu) * -260 - bOut * 200, o: clamp(pu * 1.5) * (1 - bOut), blur: bOut * 12 });
const typed = typeOn(CODE, t, B.type, CPS);
const blink = typed.length < CODE.length || Math.floor((t - B.type) * 2.4) % 2 === 0;
const html = hl(typed) + (blink ? '<span id="cursor"></span>' : '');
if (html !== codeHTML) { $('code').innerHTML = html; codeHTML = html; }
// the box runs the exact expression typed on the left — spring(t - 4.6)
const u = spring(t - B.go, 1.6, .38);
const bi = spring(t - (B.panel + .2), 2, .6);
set($('box'), { x: lerp(-160, 280, u) + bOut * 220, r: 180 * u, s: clamp(bi) * (1 - bOut * .4), o: clamp(bi * 2) * (1 - bOut) });
text($('tread'), `t = ${t.toFixed(2)} s u = ${u.toFixed(3)}`);
set($('tread'), { o: clamp(bi) * (1 - bOut) * .9 });
}
// C ──────────────────────────────────────────────────────────────────────────────────────────
cx.clearRect(0, 0, W, H);
const cOn = t > C.in && t < C.out + .1;
vis($('cap'), cOn);
if (cOn) {
const g = ease.inCubic(inv(C.gather, C.out, t)); // collapse into the ball
for (const p of P) {
const u = spring(t - C.in - p.delay, .9, .7);
const drift = (1 - clamp(u)) * 60 + 3;
let x = lerp(p.sx, p.tx, u) + p.n(t * 1.3) * drift, y = lerp(p.sy, p.ty, u) + p.n(t * 1.3 + 50) * drift;
x = lerp(x, GX, g); y = lerp(y, GY, g);
cx.globalAlpha = clamp(u * 1.6) * (1 - g * .5);
cx.fillStyle = p.hot ? '#ff6a45' : '#f4f4f5';
const sz = 3.4 * (1 - g * .6);
cx.fillRect(x - sz / 2, y - sz / 2, sz, sz);
}
cx.globalAlpha = 1;
const cu = spring(t - (C.formed - .3), 2, .6), cOut = inv(C.gather, C.gather + .25, t);
set($('cap'), { y: (1 - cu) * 40, o: clamp(cu) * (1 - cOut), blur: cOut * 10 });
}
// D ──────────────────────────────────────────────────────────────────────────────────────────
const dOut = ease.inCubic(inv(D.out, D.out + .3, t));
const dOn = t > D.ball - .02 && t < D.out + .35;
vis($('blurwrap'), dOn); vis($('cap2'), dOn);
if (dOn) {
const bu = spring(t - D.ball, 3, .45), land = t - D.land;
const sq = impulse(land, 3.2, .32) * .35;
set(ball, { x: ballX(t), sx: clamp(bu, 0, 1.3) * (1 + sq), sy: clamp(bu, 0, 1.3) * (1 - sq), o: 1 - dOut });
// the 4 samples one frame's shutter would have caught, frozen along the path
ghosts.forEach((gEl, i) => {
const at = D.ghosts + i * .09, gu = spring(t - at, 2.2, .55);
const gx = lerp(BX0, BX1, ease.inOutCubic((i + .5) / 4));
vis(gEl, t > at);
set(gEl, { x: gx, s: .6 + .4 * clamp(gu), o: clamp(gu) * .22 * (1 - dOut) });
});
const cu = spring(t - (D.ghosts + .15), 2, .6);
set($('cap2'), { y: (1 - cu) * 40, o: clamp(cu) * (1 - dOut), blur: dOut * 10 });
}
// E ──────────────────────────────────────────────────────────────────────────────────────────
const eOn = t > E.logo - .05;
for (const id of ['logo', 'tag', 'url']) vis($(id), eOn);
if (eOn) {
logoChars.forEach((c, i) => {
const u = spring(t - (E.logo + i * .035), 2.3, .45);
set(c, { y: (1 - u) * 140, o: clamp(u * 1.8), blur: (1 - clamp(u * 1.3)) * 16 });
});
const tu = spring(t - E.tag, 2, .6), uu = spring(t - E.url, 2.2, .5);
set($('tag'), { y: (1 - tu) * 36, o: clamp(tu) });
set($('url'), { y: (1 - uu) * 30, s: .9 + .1 * clamp(uu), o: clamp(uu) });
set(logoEl, { s: 1 + (t - E.logo) * .006 }); // a slow push-in on the hold
}
}
finish(seek, DUR, {
bpm: 120,
chords: [[0, 3, [57, 60, 64], 33], [3, 6, [53, 57, 60], 29], [6, 8.5, [48, 55, 60, 64], 36],
[8.5, 10.2, [55, 59, 62], 31], [10.2, DUR, [53, 57, 60, 64], 29]],
groove: [3, 10.2],
fadeOut: .6,
});