skydancer 2.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +185 -0
- package/LICENSE +21 -0
- package/README.md +120 -0
- package/package.json +40 -0
- package/skydancer.js +1658 -0
package/skydancer.js
ADDED
|
@@ -0,0 +1,1658 @@
|
|
|
1
|
+
// <sky-dancer> — an embeddable skydancer widget. Drop into any page with:
|
|
2
|
+
//
|
|
3
|
+
// <script src="https://cdn.jsdelivr.net/gh/marjanb/skydancer@2/skydancer.min.js"></script>
|
|
4
|
+
// <sky-dancer color="#ff3b30" accent="#ffd60a" face="cool" style="height: 500px"></sky-dancer>
|
|
5
|
+
//
|
|
6
|
+
// Every attribute is optional (defaults below). The widget crops to the dancer:
|
|
7
|
+
// 400px tall and 1.4x as wide, plus a bit per extra dancer, unless the page styles it.
|
|
8
|
+
|
|
9
|
+
(() => {
|
|
10
|
+
// There is nothing to register away from a browser. An import while a page
|
|
11
|
+
// is rendered on the server should do nothing rather than throw, so a
|
|
12
|
+
// framework can pull the widget in beside the markup that uses it.
|
|
13
|
+
if (typeof customElements === 'undefined') return;
|
|
14
|
+
if (customElements.get('sky-dancer')) return; // script included twice
|
|
15
|
+
|
|
16
|
+
const DEFAULTS = {
|
|
17
|
+
color: '#ff3b30',
|
|
18
|
+
accent: '#ffd60a',
|
|
19
|
+
girth: 64, // px at REF_HEIGHT; scales with the widget
|
|
20
|
+
armLength: 0.32, // fraction of body height
|
|
21
|
+
speed: 1,
|
|
22
|
+
wildness: 1,
|
|
23
|
+
wind: 1, // 0 = calm; scales pointer wind and gusts
|
|
24
|
+
face: 'happy', // happy | surprised | cool | wink | joy | sleepy | angry
|
|
25
|
+
hair: true,
|
|
26
|
+
count: 1, // how many dancers stand together
|
|
27
|
+
variety: false, // give a crowd a colour and a face each, instead of one look
|
|
28
|
+
paused: false, // hold the pose; the dancer stays on show, it just stops
|
|
29
|
+
pattern: 'none', // markings on the body: none | rings | spiral
|
|
30
|
+
print: '', // words printed down the body, the way a real one carries them
|
|
31
|
+
placard: '', // words on a board staked at the dancer's feet, which hold still
|
|
32
|
+
shout: '', // what it blurts when poked, floating beside the dancer
|
|
33
|
+
href: '', // where a click on the dancer leads; empty for nowhere
|
|
34
|
+
sign: '', // words in a speech bubble by the dancer; empty for none
|
|
35
|
+
bubble: 'drawn', // drawn | box | burst
|
|
36
|
+
hours: '', // when it is worth dancing, as `9-17`; empty for always
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const OFF = ['false', '0', 'no', 'off'];
|
|
40
|
+
|
|
41
|
+
// `9-17` is open from nine until five, and `22-6` runs through midnight.
|
|
42
|
+
// Outside them the fan is off and the dancer lies flat. Anything that is not
|
|
43
|
+
// two numbers is no hours at all, so a typo leaves a dancer that works.
|
|
44
|
+
function openNow(spec, at = new Date()) {
|
|
45
|
+
const m = /^\s*(\d{1,2})\s*-\s*(\d{1,2})\s*$/.exec(String(spec ?? ''));
|
|
46
|
+
if (!m) return true;
|
|
47
|
+
const [from, to] = [+m[1], +m[2]];
|
|
48
|
+
if (from > 24 || to > 24 || from === to) return true;
|
|
49
|
+
// the hour it is now, with the minutes, so it closes on the hour and not
|
|
50
|
+
// at some point during it
|
|
51
|
+
const h = at.getHours() + at.getMinutes() / 60;
|
|
52
|
+
return from < to ? h >= from && h < to : h >= from || h < to;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const attrName = key => key.replace(/[A-Z]/g, c => `-${c.toLowerCase()}`);
|
|
56
|
+
const KEYS = Object.keys(DEFAULTS);
|
|
57
|
+
|
|
58
|
+
function parse(key, value) {
|
|
59
|
+
if (value === null) return DEFAULTS[key];
|
|
60
|
+
if (typeof DEFAULTS[key] === 'boolean') return !OFF.includes(value.trim().toLowerCase());
|
|
61
|
+
if (typeof DEFAULTS[key] === 'number') {
|
|
62
|
+
// Number, not parseFloat: height="500px" should fall back, not mean 500x the canvas
|
|
63
|
+
const n = value.trim() === '' ? NaN : Number(value);
|
|
64
|
+
return Number.isFinite(n) ? n : DEFAULTS[key];
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const BODY_SEGS = 28;
|
|
70
|
+
const ARM_SEGS = 12;
|
|
71
|
+
const HAIR_SEGS = 6;
|
|
72
|
+
// Everything is drawn in units of a REF_HEIGHT-tall widget and scaled to the
|
|
73
|
+
// real height, so the dancer keeps its proportions at any size.
|
|
74
|
+
const REF_HEIGHT = 1000;
|
|
75
|
+
// Width per unit of height: the dancer's own span, measured at the wildest
|
|
76
|
+
// wind, so the embed is a box around it rather than the whole page width.
|
|
77
|
+
const CROP_RATIO = 1.4; // measured: the widest gust reaches 0.663 of the height either side
|
|
78
|
+
const MAX_COUNT = 6;
|
|
79
|
+
// gap between dancers, in widget heights: they stand shoulder to shoulder,
|
|
80
|
+
// so a crowd clusters instead of each one claiming a full box
|
|
81
|
+
const SPACING = 0.55;
|
|
82
|
+
// a crowd looks like a crowd when they aren't all the same size
|
|
83
|
+
const SIZES = [1, 0.84, 0.94, 0.8, 0.9, 0.87];
|
|
84
|
+
|
|
85
|
+
// A hand-drawn speech balloon's silhouette: the oval sits up and to one
|
|
86
|
+
// side, its tail trailing down to the tip at (tipX, tipY) below.
|
|
87
|
+
const BALLOON_D = 'M104.827 4.058C119.434 7.047 128.457 11.168 135.253 18.022C139.087 21.887 140.893 25.185 141.484 29.474C143.457 43.803 125.467 59.372 99.3 65.944C89.6 68.369 82.917 69.188 73.076 69.188C65.04 69.158 61.421 68.82 53.678 67.352C48.236 66.361 47.848 66.252 47.567 65.565C47.436 65.228 47.179 64.969 47.035 64.977C46.866 64.986 44.982 66.218 43.379 67.396C42.205 68.244 41.048 68.984 40.823 69.108C40.339 69.366 39.043 70.231 32.563 74.508C29.897 76.254 27.636 77.743 27.542 77.811C27.433 77.865 28.475 76.268 29.843 74.238C31.21 72.221 32.726 69.207 31.902 67.42C30.668 64.93 29.818 61.117 29.827 59.781L30.18 57.187L27.123 54.302C22.688 49.725 20.771 46.173 20.236 41.8C19.305 34.44 23.563 25.807 31.148 19.658C37.126 14.835 43.189 11.676 52.014 8.714C62.869 5.077 75.108 3.723 94.141 3.947C100.147 4.033 101.529 4.004 101.05 3.721C100.091 3.185 100.908 3.269 104.827 4.058Z';
|
|
88
|
+
// tipX/tipY: where the tail ends, so the balloon can be hung off it
|
|
89
|
+
const BALLOON = { cx: 81, cy: 37, rx: 58, ry: 31, tipX: 27.4, tipY: 77.9 };
|
|
90
|
+
|
|
91
|
+
// Three bubbles in one coordinate frame, so they hang the same way. `fit` is
|
|
92
|
+
// how much of the oval the words may use; a spiky one has less room.
|
|
93
|
+
const BUBBLES = {
|
|
94
|
+
drawn: { fit: 1 }, // the hand-drawn one
|
|
95
|
+
box: { fit: 0.96, r: a => (Math.abs(Math.cos(a)) ** 5 + Math.abs(Math.sin(a)) ** 5) ** -0.2 },
|
|
96
|
+
burst: { fit: 0.74, spikes: 15 },
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const built = {};
|
|
100
|
+
|
|
101
|
+
// one closed contour: round the body, out to the tail's point, and back,
|
|
102
|
+
// so the stroke never crosses itself
|
|
103
|
+
function bubble(name) {
|
|
104
|
+
const key = Object.hasOwn(BUBBLES, name) ? name : 'drawn'; // not 'constructor'
|
|
105
|
+
if (built[key]) return built[key];
|
|
106
|
+
|
|
107
|
+
const { cx, cy, rx, ry, tipX, tipY } = BALLOON;
|
|
108
|
+
const spec = BUBBLES[key];
|
|
109
|
+
let path, w, h;
|
|
110
|
+
|
|
111
|
+
if (key === 'drawn') {
|
|
112
|
+
path = new Path2D(BALLOON_D);
|
|
113
|
+
w = 143;
|
|
114
|
+
h = 78;
|
|
115
|
+
} else {
|
|
116
|
+
const toward = Math.atan2(tipY - cy, tipX - cx);
|
|
117
|
+
const gap = 0.3;
|
|
118
|
+
const steps = spec.spikes ? spec.spikes * 2 : 150;
|
|
119
|
+
const pts = [];
|
|
120
|
+
for (let i = 0; i <= steps; i++) {
|
|
121
|
+
const a = toward + gap + (2 * Math.PI - 2 * gap) * (i / steps);
|
|
122
|
+
const r = spec.spikes ? (i % 2 ? 1.22 : 0.86) : spec.r(a);
|
|
123
|
+
pts.push([cx + Math.cos(a) * rx * r, cy + Math.sin(a) * ry * r]);
|
|
124
|
+
}
|
|
125
|
+
pts.push([tipX, tipY]);
|
|
126
|
+
|
|
127
|
+
path = new Path2D();
|
|
128
|
+
pts.forEach(([x, y], i) => (i ? path.lineTo(x, y) : path.moveTo(x, y)));
|
|
129
|
+
path.closePath();
|
|
130
|
+
w = Math.max(...pts.map(q => q[0])) + 2;
|
|
131
|
+
h = Math.max(...pts.map(q => q[1])) + 2;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
built[key] = { path, w, h, fit: spec.fit };
|
|
135
|
+
return built[key];
|
|
136
|
+
}
|
|
137
|
+
// overshoot, so a new balloon springs past its size and settles back
|
|
138
|
+
const springy = x => 1 + 2.7 * (x - 1) ** 3 + 1.7 * (x - 1) ** 2;
|
|
139
|
+
const OUTLINE = '#1a1a2e';
|
|
140
|
+
|
|
141
|
+
// Printed words get a hand-lettered face rather than the interface one: it
|
|
142
|
+
// is a sign painted on cloth, not a label. All of these ship with an OS, so
|
|
143
|
+
// an embed pulls nothing down. It ends on `sans-serif` rather than
|
|
144
|
+
// `cursive`: `cursive` always resolves to something, and on a machine with
|
|
145
|
+
// none of these that something can be a thin script no one can read at this
|
|
146
|
+
// size, where a plain sans is merely plainer.
|
|
147
|
+
const PRINT_FACE = `'Skia', 'Comic Sans MS', 'Trebuchet MS', sans-serif`;
|
|
148
|
+
|
|
149
|
+
// A tube is stepped across its width in flat bands, u running from -1 at the
|
|
150
|
+
// left edge to 1 at the right: brightest at the lit edge, bare base colour
|
|
151
|
+
// through the middle, deepest at the shaded one. Stepping it rather than
|
|
152
|
+
// ramping it is what keeps the dancer reading as a drawing.
|
|
153
|
+
const TONES = 5; // bands of colour across a tube; keep it odd,
|
|
154
|
+
// so the middle one falls bare on the base colour
|
|
155
|
+
const SPAN = 0.96; // how much of the width they cover, edge to edge
|
|
156
|
+
const OVERLAP = 1.06; // a little, so no seam shows between neighbours
|
|
157
|
+
|
|
158
|
+
// Both zones are painted in the tube's own colour rather than in white and
|
|
159
|
+
// black. A wash of white or black only drains a fill towards grey; swinging
|
|
160
|
+
// the hue instead -- warm into the light, cool into the shade -- and taking
|
|
161
|
+
// the saturation up on the way is what makes a flat colour look lively.
|
|
162
|
+
const WARM = 50; // hue the lit edge leans toward
|
|
163
|
+
const COOL = 265; // hue the shaded side leans toward
|
|
164
|
+
const RIM_LIGHT = 0.38;
|
|
165
|
+
const SHADE = 0.34;
|
|
166
|
+
|
|
167
|
+
// Whatever colour string the page handed us, the canvas resolves it. Reading
|
|
168
|
+
// a 1x1 fill back is the one answer that does not depend on how the browser
|
|
169
|
+
// chooses to serialise a colour -- fillStyle alone can come back as oklch()
|
|
170
|
+
// or color(), which no amount of string picking parses safely.
|
|
171
|
+
const swatch = Object.assign(document.createElement('canvas'), { width: 1, height: 1 })
|
|
172
|
+
.getContext('2d', { willReadFrequently: true });
|
|
173
|
+
const inkCache = new Map();
|
|
174
|
+
|
|
175
|
+
const clamp = (v, lo, hi) => Math.min(hi, Math.max(lo, v));
|
|
176
|
+
|
|
177
|
+
// the short way round the wheel, so a red leans into orange, not into green
|
|
178
|
+
const lean = (h, target, amount) =>
|
|
179
|
+
(h + (((target - h + 540) % 360) - 180) * amount + 360) % 360;
|
|
180
|
+
|
|
181
|
+
// Whether the canvas can make sense of a colour at all. Setting fillStyle to
|
|
182
|
+
// something unreadable leaves the old value, so two different starting
|
|
183
|
+
// points that end up agreeing means it parsed.
|
|
184
|
+
function readable(css) {
|
|
185
|
+
swatch.fillStyle = '#000';
|
|
186
|
+
swatch.fillStyle = css;
|
|
187
|
+
const first = swatch.fillStyle;
|
|
188
|
+
swatch.fillStyle = '#fff';
|
|
189
|
+
swatch.fillStyle = css;
|
|
190
|
+
return first === swatch.fillStyle ? first : null;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function hsl(css) {
|
|
194
|
+
swatch.clearRect(0, 0, 1, 1);
|
|
195
|
+
swatch.fillStyle = '#000'; // a colour the browser can't read leaves this
|
|
196
|
+
swatch.fillStyle = css;
|
|
197
|
+
swatch.fillRect(0, 0, 1, 1);
|
|
198
|
+
const [r, g, b] = [...swatch.getImageData(0, 0, 1, 1).data].map(v => v / 255);
|
|
199
|
+
const max = Math.max(r, g, b), min = Math.min(r, g, b), span = max - min;
|
|
200
|
+
const l = (max + min) / 2;
|
|
201
|
+
if (!span) return { h: 0, s: 0, l: l * 100 };
|
|
202
|
+
const h = max === r ? (g - b) / span + (g < b ? 6 : 0)
|
|
203
|
+
: max === g ? (b - r) / span + 2
|
|
204
|
+
: (r - g) / span + 4;
|
|
205
|
+
return { h: h * 60, s: span / (1 - Math.abs(2 * l - 1)) * 100, l: l * 100 };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// How much of a round cap the head keeps. A line cap can only be a
|
|
209
|
+
// semicircle, so a flatter top than that has to be cut and re-edged.
|
|
210
|
+
const FLAT = 0.55;
|
|
211
|
+
|
|
212
|
+
// A face is a pairing of eyes and a mouth, so the two can be mixed freely.
|
|
213
|
+
const LOOKS = {
|
|
214
|
+
happy: { eyes: 'round', mouth: 'smile' },
|
|
215
|
+
surprised: { eyes: 'round', mouth: 'gape' },
|
|
216
|
+
cool: { eyes: 'shades', mouth: 'smirk' },
|
|
217
|
+
wink: { eyes: 'wink', mouth: 'smile' },
|
|
218
|
+
joy: { eyes: 'arches', mouth: 'grin' },
|
|
219
|
+
sleepy: { eyes: 'shut', mouth: 'small' },
|
|
220
|
+
angry: { eyes: 'brows', mouth: 'frown' },
|
|
221
|
+
};
|
|
222
|
+
const FACES = Object.keys(LOOKS);
|
|
223
|
+
|
|
224
|
+
// Words divided by a pipe are several phrases; a doubled pipe is a literal
|
|
225
|
+
// one. The sign works through them in turn, a poke picks one at a time.
|
|
226
|
+
// Each phrase is a run of anything but a pipe, with `||` allowed inside it,
|
|
227
|
+
// so a doubled pipe never has to be stood in for and put back.
|
|
228
|
+
function phrasesOf(raw) {
|
|
229
|
+
return (String(raw ?? '').match(/(?:\|\||[^|])+/g) ?? [])
|
|
230
|
+
.map(part => part.replaceAll('||', '|').trim())
|
|
231
|
+
.filter(Boolean);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// What a varied crowd differs by, beyond its turn round the hue wheel. The
|
|
235
|
+
// first line is all ones, so the first dancer is exactly what the page asked
|
|
236
|
+
// for and the rest fan out from it.
|
|
237
|
+
const VARIETY = [
|
|
238
|
+
{ girth: 1, arms: 1, flail: 1, hair: true },
|
|
239
|
+
{ girth: 1.24, arms: 0.78, flail: 0.84, hair: true },
|
|
240
|
+
{ girth: 0.82, arms: 1.26, flail: 1.2, hair: false },
|
|
241
|
+
{ girth: 1.12, arms: 0.9, flail: 1.06, hair: true },
|
|
242
|
+
{ girth: 0.88, arms: 1.14, flail: 0.9, hair: false },
|
|
243
|
+
{ girth: 1.3, arms: 1.04, flail: 1.22, hair: true },
|
|
244
|
+
];
|
|
245
|
+
const FATTEST = Math.max(...VARIETY.map(v => v.girth));
|
|
246
|
+
const spinCache = new Map();
|
|
247
|
+
|
|
248
|
+
// the same colour walked round the wheel, for telling a crowd apart
|
|
249
|
+
function spin(css, deg) {
|
|
250
|
+
if (!deg) return css;
|
|
251
|
+
const key = `${css}|${deg}`;
|
|
252
|
+
let out = spinCache.get(key);
|
|
253
|
+
if (!out) {
|
|
254
|
+
const { h, s, l } = hsl(css);
|
|
255
|
+
out = `hsl(${(h + deg) % 360} ${s}% ${l}%)`;
|
|
256
|
+
if (spinCache.size > 64) spinCache.clear();
|
|
257
|
+
spinCache.set(key, out);
|
|
258
|
+
}
|
|
259
|
+
return out;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// the two inks a tube of this colour is lit and shaded with
|
|
263
|
+
function inks(css) {
|
|
264
|
+
let ink = inkCache.get(css);
|
|
265
|
+
if (!ink) {
|
|
266
|
+
const { h, s, l } = hsl(css);
|
|
267
|
+
// A grey has no hue of its own to lean, so it takes the light's outright
|
|
268
|
+
// -- warm into the lit edge, cool into the shade, which is what keeps a
|
|
269
|
+
// grey tube from going flat without turning it into a colour.
|
|
270
|
+
const hue = (target, amount) => s < 5 ? target : lean(h, target, amount);
|
|
271
|
+
// the lit edge never comes out darker than the tube, nor the shade lighter
|
|
272
|
+
const rim = `${hue(WARM, 0.3)} ${clamp(s * 1.2 + 18, 0, 100)}%`
|
|
273
|
+
+ ` ${Math.min(100, l + 15)}%`;
|
|
274
|
+
const shade = `${hue(COOL, 0.28)} ${clamp(s * 1.15 + 10, 0, 100)}%`
|
|
275
|
+
+ ` ${Math.max(0, l - 24)}%`;
|
|
276
|
+
// One ink per band. How far a band sits from the middle is how strongly
|
|
277
|
+
// its ink is laid on, so the steps walk out from the base colour to the
|
|
278
|
+
// lit edge one way and to the shaded one the other.
|
|
279
|
+
ink = [];
|
|
280
|
+
for (let i = 0; i < TONES; i++) {
|
|
281
|
+
const p = (2 * i + 1) / TONES - 1; // -1 at the lit edge, 1 at the shaded
|
|
282
|
+
const a = Math.abs(p);
|
|
283
|
+
ink.push(a < 1e-6 ? null
|
|
284
|
+
: `hsl(${p < 0 ? rim : shade} / ${((p < 0 ? RIM_LIGHT : SHADE) * a).toFixed(3)})`);
|
|
285
|
+
}
|
|
286
|
+
// a colour picker being dragged hands us a new colour every frame, so
|
|
287
|
+
// the cache is there to spare the repeats, not to remember every shade
|
|
288
|
+
if (inkCache.size > 64) inkCache.clear();
|
|
289
|
+
inkCache.set(css, ink);
|
|
290
|
+
}
|
|
291
|
+
return ink;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// The shadow gathering at the foot of the picture is contact shadow, not
|
|
295
|
+
// tube shading, so it keeps its own number -- a flat illustration still
|
|
296
|
+
// wants its subject planted on the ground.
|
|
297
|
+
const GROUNDED = 0.14;
|
|
298
|
+
|
|
299
|
+
const STYLE = `
|
|
300
|
+
/* width follows height, so the embed crops to the dancer */
|
|
301
|
+
:host {
|
|
302
|
+
display: inline-block;
|
|
303
|
+
position: relative; /* the words and the link hang off this */
|
|
304
|
+
vertical-align: middle;
|
|
305
|
+
height: 400px;
|
|
306
|
+
aspect-ratio: calc(${CROP_RATIO} + (var(--count, 1) - 1) * ${SPACING});
|
|
307
|
+
}
|
|
308
|
+
:host([hidden]) { display: none; }
|
|
309
|
+
canvas {
|
|
310
|
+
display: block;
|
|
311
|
+
width: 100%;
|
|
312
|
+
height: 100%;
|
|
313
|
+
/* no backdrop of its own: it stands on whatever the page puts behind it */
|
|
314
|
+
}
|
|
315
|
+
/* Where a click on the dancer leads. The canvas takes the click, but a
|
|
316
|
+
pointer is not the only way in, so the destination is a real link that
|
|
317
|
+
can be tabbed to and read out. */
|
|
318
|
+
.go {
|
|
319
|
+
position: absolute;
|
|
320
|
+
left: 0;
|
|
321
|
+
bottom: 0;
|
|
322
|
+
font: inherit;
|
|
323
|
+
color: inherit;
|
|
324
|
+
}
|
|
325
|
+
.go:not(:focus-visible) {
|
|
326
|
+
width: 1px;
|
|
327
|
+
height: 1px;
|
|
328
|
+
overflow: hidden;
|
|
329
|
+
clip-path: inset(50%);
|
|
330
|
+
white-space: nowrap;
|
|
331
|
+
}
|
|
332
|
+
canvas.leads { cursor: pointer; }
|
|
333
|
+
|
|
334
|
+
/* Poking is a pointer gesture, and a pointer is not the only way in. This
|
|
335
|
+
is the same control as a poke, reachable by tab; a button answers Enter
|
|
336
|
+
and Space by itself, so there is no key handling to get wrong. */
|
|
337
|
+
.poke {
|
|
338
|
+
position: absolute;
|
|
339
|
+
left: 0;
|
|
340
|
+
top: 0;
|
|
341
|
+
font: inherit;
|
|
342
|
+
color: inherit;
|
|
343
|
+
}
|
|
344
|
+
.poke:focus-visible {
|
|
345
|
+
padding: 4px 10px;
|
|
346
|
+
font-size: 13px;
|
|
347
|
+
color: #fffdf6;
|
|
348
|
+
background: ${OUTLINE};
|
|
349
|
+
border: 0;
|
|
350
|
+
border-radius: 999px;
|
|
351
|
+
cursor: pointer;
|
|
352
|
+
}
|
|
353
|
+
.poke:not(:focus-visible) {
|
|
354
|
+
width: 1px;
|
|
355
|
+
height: 1px;
|
|
356
|
+
padding: 0;
|
|
357
|
+
border: 0;
|
|
358
|
+
overflow: hidden;
|
|
359
|
+
clip-path: inset(50%);
|
|
360
|
+
white-space: nowrap;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/* The sign's words, for anything that reads rather than looks: a screen
|
|
364
|
+
reader, the page's own find, a translator. The canvas only paints them. */
|
|
365
|
+
.said {
|
|
366
|
+
position: absolute;
|
|
367
|
+
width: 1px;
|
|
368
|
+
height: 1px;
|
|
369
|
+
margin: -1px;
|
|
370
|
+
overflow: hidden;
|
|
371
|
+
clip-path: inset(50%);
|
|
372
|
+
white-space: nowrap;
|
|
373
|
+
}
|
|
374
|
+
`;
|
|
375
|
+
|
|
376
|
+
// Runs one dancer on a canvas, reading the live cfg object every frame.
|
|
377
|
+
// Returns a function that stops it.
|
|
378
|
+
function animate(canvas, cfg) {
|
|
379
|
+
const ctx = canvas.getContext('2d');
|
|
380
|
+
const events = new AbortController();
|
|
381
|
+
const reduced = matchMedia('(prefers-reduced-motion: reduce)');
|
|
382
|
+
|
|
383
|
+
// A held pose has no loop running, so anything that wants the picture
|
|
384
|
+
// brought up to date has to paint it itself. The page can ask for that
|
|
385
|
+
// with `paused`, and the reader can ask for it through their system.
|
|
386
|
+
const held = () => reduced.matches || cfg.paused;
|
|
387
|
+
let ready = false, visible = false, lastDpr = 0;
|
|
388
|
+
|
|
389
|
+
// ---- canvas sizing ----
|
|
390
|
+
|
|
391
|
+
let W = 0, H = 0;
|
|
392
|
+
|
|
393
|
+
function resize() {
|
|
394
|
+
const dpr = window.devicePixelRatio || 1;
|
|
395
|
+
const scale = canvas.clientHeight / REF_HEIGHT;
|
|
396
|
+
if (!scale) return; // not laid out yet
|
|
397
|
+
W = canvas.clientWidth / scale;
|
|
398
|
+
H = REF_HEIGHT;
|
|
399
|
+
lastDpr = dpr;
|
|
400
|
+
canvas.width = canvas.clientWidth * dpr;
|
|
401
|
+
canvas.height = canvas.clientHeight * dpr;
|
|
402
|
+
ctx.setTransform(dpr * scale, 0, 0, dpr * scale, 0, 0);
|
|
403
|
+
// a still pose has no loop to redraw it after a resize
|
|
404
|
+
if (ready && visible && held()) render();
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const observer = new ResizeObserver(resize);
|
|
408
|
+
observer.observe(canvas);
|
|
409
|
+
resize();
|
|
410
|
+
|
|
411
|
+
// ---- simulation ----
|
|
412
|
+
// Everything is a chain of segments. Each segment adds a small bend to the
|
|
413
|
+
// running angle (0 = straight up, positive = leaning right), so travelling
|
|
414
|
+
// sine waves in the bend read as air rippling up the tube.
|
|
415
|
+
|
|
416
|
+
const count = () => Math.max(1, Math.min(MAX_COUNT, Math.round(cfg.count) || 1));
|
|
417
|
+
|
|
418
|
+
// the thickest tube on stage, as a multiple of the configured girth: the
|
|
419
|
+
// room left for a fan and the margin a tube is held inside both key off it
|
|
420
|
+
const fattest = () => (cfg.variety && count() > 1 ? FATTEST : 1);
|
|
421
|
+
|
|
422
|
+
// height of the strip below the fan's base line: the ground, or just enough to show the whole fan
|
|
423
|
+
const ground = () => Math.ceil(cfg.girth * fattest() * 0.25);
|
|
424
|
+
|
|
425
|
+
// The tube is held inside the canvas the way the ground already holds its
|
|
426
|
+
// feet: a wild gust presses it against the edge instead of running past it,
|
|
427
|
+
// so nothing is ever cut off, whatever the settings.
|
|
428
|
+
function chain(x, y, a, n, segLen, bend) {
|
|
429
|
+
const floor = H - ground();
|
|
430
|
+
const edge = cfg.girth * fattest() * 0.62 + 8; // half the tube, plus its outline
|
|
431
|
+
// The lid takes no allowance for the fattest tube. The tallest dancer's
|
|
432
|
+
// head already sits just under it, and widening it there would press the
|
|
433
|
+
// crown down onto the hair instead of letting it stand up.
|
|
434
|
+
const lid = cfg.girth * 0.62 + 8;
|
|
435
|
+
const pts = [{ x, y, a }];
|
|
436
|
+
for (let i = 1; i <= n; i++) {
|
|
437
|
+
a += bend(i / n, a);
|
|
438
|
+
x = Math.min(W - edge, Math.max(edge, x + Math.sin(a) * segLen));
|
|
439
|
+
y = Math.min(floor, Math.max(lid, y - Math.cos(a) * segLen));
|
|
440
|
+
pts.push({ x, y, a });
|
|
441
|
+
}
|
|
442
|
+
return pts;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// A kink is the tube losing pressure and folding over; the fold travels up
|
|
446
|
+
// and out of the top as the air refills.
|
|
447
|
+
let kink = { start: 2, dur: 1.5, dir: 1, mag: 0.4 };
|
|
448
|
+
|
|
449
|
+
function kinkBend(u, t) {
|
|
450
|
+
const p = (t - kink.start) / kink.dur;
|
|
451
|
+
if (p < 0 || p > 1) return 0;
|
|
452
|
+
const centre = 0.2 + 0.8 * p;
|
|
453
|
+
return kink.dir * kink.mag * cfg.wildness * Math.sin(Math.PI * p) * Math.exp(-(((u - centre) / 0.08) ** 2));
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// Wind is one signed number: negative blows left, positive blows right.
|
|
457
|
+
// It is the pointer (-1 far left .. 1 far right, 0 when it leaves), the
|
|
458
|
+
// page going by underneath, and random gusts, scaled by the wind setting
|
|
459
|
+
// and smoothed.
|
|
460
|
+
let pointerWind = 0, wind = 0;
|
|
461
|
+
let gust = { start: 4, dur: 2, force: 0.7 };
|
|
462
|
+
// The page scrolling past is wind too: the faster it goes, the harder the
|
|
463
|
+
// dancer is blown along with it. Sampled per frame from the scroll
|
|
464
|
+
// position rather than from scroll events, so a smooth scroll, a flick and
|
|
465
|
+
// a jump all read the same, and standing still reads as nothing at all.
|
|
466
|
+
const SCROLL_FULL = 3300; // px a second for all the wind there is
|
|
467
|
+
let scrolledAt = null; // where the page was last frame
|
|
468
|
+
let scrollWind = 0;
|
|
469
|
+
|
|
470
|
+
// where the pointer is, in drawing units, for the eyes to find
|
|
471
|
+
let gaze = null;
|
|
472
|
+
|
|
473
|
+
canvas.addEventListener('pointermove', e => {
|
|
474
|
+
const half = canvas.clientWidth / 2; // offsetX is in CSS px, not drawing units
|
|
475
|
+
pointerWind = Math.max(-1, Math.min(1, (e.offsetX - half) / half));
|
|
476
|
+
const unit = canvas.clientHeight / REF_HEIGHT;
|
|
477
|
+
gaze = unit ? { x: e.offsetX / unit, y: e.offsetY / unit } : null;
|
|
478
|
+
}, { signal: events.signal });
|
|
479
|
+
// Watched on the widget rather than the canvas: the link lies over the
|
|
480
|
+
// canvas, so moving onto it would read as leaving, and moving from it
|
|
481
|
+
// straight out would not read as leaving at all. The host's own bounds
|
|
482
|
+
// are what actually matter.
|
|
483
|
+
canvas.getRootNode().host.addEventListener('pointerleave', () => {
|
|
484
|
+
pointerWind = 0;
|
|
485
|
+
gaze = null; // nothing to watch, so the eyes go back to wandering
|
|
486
|
+
}, { signal: events.signal });
|
|
487
|
+
|
|
488
|
+
// poking it knocks the air out: a kink now, and extra flailing that fades
|
|
489
|
+
let punch = 0;
|
|
490
|
+
|
|
491
|
+
const root = canvas.getRootNode();
|
|
492
|
+
const saidText = root.querySelector('.said');
|
|
493
|
+
const blurted = root.querySelector('.blurted');
|
|
494
|
+
const leadsTo = root.querySelector('.go');
|
|
495
|
+
const pokeButton = root.querySelector('.poke');
|
|
496
|
+
|
|
497
|
+
// Somewhere a link may actually go. `javascript:` is the one that matters:
|
|
498
|
+
// the destination can reach this widget from a page's own templating or
|
|
499
|
+
// from the customizer's shareable link, so it is not always written by
|
|
500
|
+
// someone already able to run script here.
|
|
501
|
+
const GOES = ['http:', 'https:', 'mailto:', 'tel:'];
|
|
502
|
+
|
|
503
|
+
function destination() {
|
|
504
|
+
const raw = String(cfg.href ?? '').trim();
|
|
505
|
+
if (!raw) return '';
|
|
506
|
+
try {
|
|
507
|
+
const url = new URL(raw, location.href); // so a relative path works
|
|
508
|
+
return GOES.includes(url.protocol) ? url.href : '';
|
|
509
|
+
} catch {
|
|
510
|
+
return '';
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// A dancer with somewhere to go says so: the canvas takes the click, and
|
|
515
|
+
// the link carries the destination for a keyboard or a screen reader.
|
|
516
|
+
let aimed = null;
|
|
517
|
+
|
|
518
|
+
function aimLink() {
|
|
519
|
+
const href = destination();
|
|
520
|
+
if (href === aimed) return; // this runs every frame
|
|
521
|
+
aimed = href;
|
|
522
|
+
leadsTo.hidden = !href;
|
|
523
|
+
canvas.classList.toggle('leads', !!href);
|
|
524
|
+
if (!href) return;
|
|
525
|
+
leadsTo.href = href;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// the words on the sign are what the link is pointing at, when there are
|
|
529
|
+
// any; written only when they change, like the hidden copy of them
|
|
530
|
+
let labelled = null;
|
|
531
|
+
|
|
532
|
+
function labelLink(sign) {
|
|
533
|
+
const label = sign || 'Find out more';
|
|
534
|
+
if (!aimed || label === labelled) return;
|
|
535
|
+
labelled = label;
|
|
536
|
+
leadsTo.textContent = label;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// The destination is worked out again rather than read off the element, so
|
|
540
|
+
// nothing can be slipped into it between the frame that set it and this.
|
|
541
|
+
// The link opens a tab by itself; the modifiers still ride along so that
|
|
542
|
+
// shift-click and the rest behave as they would anywhere, and the middle
|
|
543
|
+
// button is heard too.
|
|
544
|
+
function follow(e) {
|
|
545
|
+
if (!destination()) return;
|
|
546
|
+
leadsTo.dispatchEvent(new MouseEvent('click', {
|
|
547
|
+
bubbles: true, cancelable: true, view: window, button: e.button,
|
|
548
|
+
ctrlKey: e.ctrlKey, metaKey: e.metaKey,
|
|
549
|
+
shiftKey: e.shiftKey, altKey: e.altKey,
|
|
550
|
+
}));
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
canvas.addEventListener('click', follow, { signal: events.signal });
|
|
554
|
+
canvas.addEventListener('auxclick', e => { if (e.button === 1) follow(e); },
|
|
555
|
+
{ signal: events.signal });
|
|
556
|
+
|
|
557
|
+
// A poke makes it blurt, and the word pops up beside it rather than going
|
|
558
|
+
// into the balloon: the balloon is what the dancer is there to say, and a
|
|
559
|
+
// poke should not talk over it. Each one lands somewhere else, so a few in
|
|
560
|
+
// a row read as a flurry rather than one word flickering.
|
|
561
|
+
const SHOUTS = 5; // most on screen at once
|
|
562
|
+
const SHOUT_LIFE = 1.15; // seconds each one lasts
|
|
563
|
+
let shouts = [];
|
|
564
|
+
// Centred on the chest rather than the head: the balloon hangs by the head,
|
|
565
|
+
// and a word thrown up there lands on top of what the dancer is saying.
|
|
566
|
+
let lastChest = null;
|
|
567
|
+
let lastTurn = 0;
|
|
568
|
+
let lastShout = -1; // which word the last poke took
|
|
569
|
+
|
|
570
|
+
function burst() {
|
|
571
|
+
// without a running loop nothing would decay the punch, so it would be
|
|
572
|
+
// saved up and spent the moment the dancer came back into view
|
|
573
|
+
if (held() || !visible) return;
|
|
574
|
+
// A poke takes one of them, never the one it took last: a flurry of
|
|
575
|
+
// pokes should be a flurry of different words.
|
|
576
|
+
const choices = phrasesOf(cfg.shout);
|
|
577
|
+
if (choices.length && lastChest) {
|
|
578
|
+
if (choices.length > 1) {
|
|
579
|
+
let pick = lastShout;
|
|
580
|
+
while (pick === lastShout) pick = Math.floor(Math.random() * choices.length);
|
|
581
|
+
lastShout = pick;
|
|
582
|
+
} else lastShout = 0;
|
|
583
|
+
const words = choices[lastShout];
|
|
584
|
+
// at least a third of the way round from the last one, so two in a
|
|
585
|
+
// row never land on top of each other
|
|
586
|
+
lastTurn += Math.PI * (0.7 + Math.random() * 0.6);
|
|
587
|
+
const reach = cfg.girth * (1.5 + Math.random());
|
|
588
|
+
shouts.push({
|
|
589
|
+
words,
|
|
590
|
+
x: lastChest.x + Math.cos(lastTurn) * reach,
|
|
591
|
+
y: lastChest.y + Math.sin(lastTurn) * reach * 0.7,
|
|
592
|
+
at: wall,
|
|
593
|
+
tilt: (Math.random() - 0.5) * 0.45,
|
|
594
|
+
});
|
|
595
|
+
if (shouts.length > SHOUTS) shouts.shift();
|
|
596
|
+
blurted.textContent = words;
|
|
597
|
+
}
|
|
598
|
+
punch = 1;
|
|
599
|
+
kink = { start: t, dur: 0.9, dir: Math.random() < 0.5 ? -1 : 1, mag: 0.45 };
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
canvas.addEventListener('pointerdown', burst, { signal: events.signal });
|
|
603
|
+
pokeButton.addEventListener('click', burst, { signal: events.signal });
|
|
604
|
+
|
|
605
|
+
// Only offered when a poke has something to say, and when a poke would do
|
|
606
|
+
// anything at all. A dancer is decoration, and a decorative one that is
|
|
607
|
+
// merely wiggled has nothing to report to a reader who cannot see it -- so
|
|
608
|
+
// it stays out of the page's tab order rather than costing a stop per
|
|
609
|
+
// embed for a flourish. A held pose swallows a poke, so it offers none.
|
|
610
|
+
let offering = null;
|
|
611
|
+
function offerPoke() {
|
|
612
|
+
const worth = phrasesOf(cfg.shout).length > 0 && !held();
|
|
613
|
+
if (worth === offering) return;
|
|
614
|
+
offering = worth;
|
|
615
|
+
pokeButton.hidden = !worth;
|
|
616
|
+
if (worth) pokeButton.textContent = 'Poke the dancer';
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
// A page can theme the dancer in CSS rather than in attributes, which is
|
|
620
|
+
// how it follows a light and a dark scheme without the embed being
|
|
621
|
+
// rewritten. An attribute still wins: naming a colour outright is the
|
|
622
|
+
// louder ask. Read on the events that can change it, never per frame --
|
|
623
|
+
// getComputedStyle in the loop would force a style recalc every tick.
|
|
624
|
+
const scheme = matchMedia('(prefers-color-scheme: dark)');
|
|
625
|
+
let themed = { color: null, accent: null };
|
|
626
|
+
|
|
627
|
+
function readTheme() {
|
|
628
|
+
const host = root.host;
|
|
629
|
+
const style = getComputedStyle(host);
|
|
630
|
+
const pick = key => {
|
|
631
|
+
if (host.hasAttribute(key)) return null; // the tag said so outright
|
|
632
|
+
const set = style.getPropertyValue(`--sky-dancer-${key}`).trim();
|
|
633
|
+
return set ? readable(set) : null;
|
|
634
|
+
};
|
|
635
|
+
themed = { color: pick('color'), accent: pick('accent') };
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
// The sign works through its phrases in turn. Splitting is cached because
|
|
639
|
+
// this is read every frame; a still pose has no clock to turn the page, so
|
|
640
|
+
// it holds whichever phrase was up when the clock stopped.
|
|
641
|
+
const DWELL = 4; // seconds a phrase stays up
|
|
642
|
+
let split = { raw: null, phrases: [] };
|
|
643
|
+
|
|
644
|
+
function phraseNow(t) {
|
|
645
|
+
const raw = String(cfg.sign ?? '');
|
|
646
|
+
if (raw !== split.raw) split = { raw, phrases: phrasesOf(raw) };
|
|
647
|
+
const { phrases } = split;
|
|
648
|
+
if (phrases.length < 2) return phrases[0] ?? '';
|
|
649
|
+
return phrases[Math.floor(Math.max(0, t) / DWELL) % phrases.length];
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
// The words only reach the DOM when they change: this runs every frame.
|
|
653
|
+
let announced = null;
|
|
654
|
+
function announce(text) {
|
|
655
|
+
if (text === announced) return;
|
|
656
|
+
announced = text;
|
|
657
|
+
saidText.textContent = text;
|
|
658
|
+
saidText.hidden = !text;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
function updateWind(t, dt, since = dt) {
|
|
662
|
+
if (t > gust.start + gust.dur) {
|
|
663
|
+
gust = {
|
|
664
|
+
start: t + 2 + Math.random() * 5,
|
|
665
|
+
dur: 1.5 + Math.random() * 2,
|
|
666
|
+
force: (Math.random() < 0.5 ? -1 : 1) * (0.4 + Math.random() * 0.6),
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
const p = (t - gust.start) / gust.dur;
|
|
670
|
+
const gustForce = p < 0 || p > 1 ? 0 : gust.force * Math.sin(Math.PI * p);
|
|
671
|
+
// Down the page blows it right, up blows it left. The reading is taken
|
|
672
|
+
// once a frame and eased, so it builds while the scroll runs and falls
|
|
673
|
+
// away on its own when it stops.
|
|
674
|
+
const now = window.scrollY;
|
|
675
|
+
// per second rather than per frame: the same flick of the page has to
|
|
676
|
+
// blow the same whether the screen runs at 60Hz or 120
|
|
677
|
+
// Measured against the time that really passed, not the clamped step:
|
|
678
|
+
// the pixels moved cover the whole of a slow frame, so dividing by the
|
|
679
|
+
// clamp would read a janky frame as a gale.
|
|
680
|
+
const moved = scrolledAt === null || since <= 0 ? 0 : (now - scrolledAt) / since;
|
|
681
|
+
scrolledAt = now;
|
|
682
|
+
const blown = Math.max(-1, Math.min(1, moved / SCROLL_FULL));
|
|
683
|
+
// rises with the scroll at once, falls back slowly: a gust, not a switch
|
|
684
|
+
scrollWind += (blown - scrollWind) * Math.min(1, dt * (Math.abs(blown) > Math.abs(scrollWind) ? 18 : 3));
|
|
685
|
+
const together = pointerWind + gustForce + scrollWind;
|
|
686
|
+
const target = Math.max(-1, Math.min(1, together)) * cfg.wind;
|
|
687
|
+
wind += (target - wind) * Math.min(1, dt * 4);
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
// Every so often the air can't keep up and the dancer sags: it dips lower
|
|
691
|
+
// in the body for a moment, then fills again. `lift` is 1 at full height.
|
|
692
|
+
let sag = { start: 6, dur: 1.1, depth: 0.4, side: 1 };
|
|
693
|
+
let lift = 1;
|
|
694
|
+
// `slump` is the sag on its own and `filled` is how much air is in the
|
|
695
|
+
// tube. A sag bows the cloth over without shortening it; air really does
|
|
696
|
+
// change the length. `lift` is the two together, for how slack it all is.
|
|
697
|
+
let slump = 1, filled = 1;
|
|
698
|
+
|
|
699
|
+
// An arrival is the air going in: the tube fills from flat on the way in.
|
|
700
|
+
// Null when nothing is filling or emptying, which is nearly always.
|
|
701
|
+
const FILL = 0.9, EMPTY = 0.55; // seconds
|
|
702
|
+
let air = null; // { from, to, start, dur }
|
|
703
|
+
// Where a finished breath left it, and so also what the page last asked
|
|
704
|
+
// for: an arrival fills to this rather than to full, so a dancer
|
|
705
|
+
// deliberately let down does not stand up again the next time it scrolls
|
|
706
|
+
// into view.
|
|
707
|
+
let resting = 1;
|
|
708
|
+
|
|
709
|
+
function breathe(from, to, dur) {
|
|
710
|
+
air = { from, to, start: wall, dur };
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
// how far the air has got, 0 flat to 1 full
|
|
714
|
+
function pressure() {
|
|
715
|
+
if (!air) return resting; // not 1: a dancer let down stays down
|
|
716
|
+
const p = (wall - air.start) / air.dur;
|
|
717
|
+
if (p >= 1) {
|
|
718
|
+
resting = air.to;
|
|
719
|
+
air = null;
|
|
720
|
+
return resting;
|
|
721
|
+
}
|
|
722
|
+
// eased, so it leaves and arrives without a jolt
|
|
723
|
+
const e = p < 0 ? 0 : 0.5 - Math.cos(Math.PI * p) / 2;
|
|
724
|
+
return air.from + (air.to - air.from) * e;
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
function updateLift(t) {
|
|
728
|
+
if (t > sag.start + sag.dur) {
|
|
729
|
+
sag = {
|
|
730
|
+
start: t + 5 + Math.random() * 9,
|
|
731
|
+
dur: 0.9 + Math.random() * 0.8,
|
|
732
|
+
depth: 0.25 + Math.random() * 0.3,
|
|
733
|
+
side: Math.random() < 0.5 ? -1 : 1, // in calm it falls either way
|
|
734
|
+
};
|
|
735
|
+
}
|
|
736
|
+
const p = (t - sag.start) / sag.dur;
|
|
737
|
+
slump = p < 0 || p > 1 ? 1 : 1 - sag.depth * Math.sin(Math.PI * p); // down and straight back up
|
|
738
|
+
filled = pressure();
|
|
739
|
+
lift = slump * filled; // a half-filled tube is a slack one as well
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
// Whatever is part way in or out finishes at once. A held pose has no
|
|
743
|
+
// frames to breathe with, so without this a dancer paused mid-fill would
|
|
744
|
+
// stay half up.
|
|
745
|
+
function settleAir() {
|
|
746
|
+
if (!air) return;
|
|
747
|
+
resting = filled = air.to;
|
|
748
|
+
air = null;
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
// bend that swings a loose chain (arm, hair) round to point downwind
|
|
752
|
+
function blow(a) {
|
|
753
|
+
return Math.abs(wind) * Math.sin(Math.sign(wind) * 1.4 - a);
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
function simulate(t) {
|
|
757
|
+
if (t > kink.start + kink.dur) {
|
|
758
|
+
kink = {
|
|
759
|
+
start: t + 1 + Math.random() * 3,
|
|
760
|
+
dur: 1.2 + Math.random(),
|
|
761
|
+
dir: Math.random() < 0.5 ? -1 : 1,
|
|
762
|
+
mag: 0.25 + Math.random() * 0.25,
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
const n = count();
|
|
767
|
+
const gap = SPACING * H;
|
|
768
|
+
// smallest first, so they read as standing further back
|
|
769
|
+
return Array.from({ length: n }, (_, i) => {
|
|
770
|
+
const look = lookOf(i, n);
|
|
771
|
+
const x = W / 2 + (i - (n - 1) / 2) * gap;
|
|
772
|
+
return { ...dancerAt(t + i * 3.7, x, SIZES[i % SIZES.length], look), look };
|
|
773
|
+
}).sort((a, b) => a.girth - b.girth);
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
// One crowd, one palette: the colours the page chose stay on the first
|
|
777
|
+
// dancer and the rest are walked evenly round the wheel, so a line-up
|
|
778
|
+
// reads as a set rather than as several unrelated widgets. Faces rotate
|
|
779
|
+
// from the chosen one for the same reason.
|
|
780
|
+
function lookOf(i, n) {
|
|
781
|
+
const base = themed.color ?? cfg.color;
|
|
782
|
+
const accent = themed.accent ?? cfg.accent;
|
|
783
|
+
const plain = { color: base, accent, face: cfg.face,
|
|
784
|
+
girth: 1, arms: 1, flail: 1, hair: cfg.hair };
|
|
785
|
+
if (!cfg.variety || n < 2) return plain;
|
|
786
|
+
const mix = VARIETY[i % VARIETY.length];
|
|
787
|
+
const turn = i * 360 / n;
|
|
788
|
+
const first = Math.max(0, FACES.indexOf(cfg.face));
|
|
789
|
+
return {
|
|
790
|
+
color: spin(base, turn),
|
|
791
|
+
accent: spin(accent, turn),
|
|
792
|
+
face: FACES[(first + i) % FACES.length],
|
|
793
|
+
girth: mix.girth,
|
|
794
|
+
arms: mix.arms,
|
|
795
|
+
flail: mix.flail,
|
|
796
|
+
// a page that asked for no hair gets none, however mixed the crowd
|
|
797
|
+
hair: cfg.hair && mix.hair,
|
|
798
|
+
};
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
// one dancer, `k` times the configured size, standing at x
|
|
802
|
+
function dancerAt(t, x, k, look) {
|
|
803
|
+
// a sagging dancer loses some of its snap
|
|
804
|
+
const limp = 0.55 + 0.45 * lift;
|
|
805
|
+
const w = cfg.wildness * (1 + punch * 2.5) * limp * look.flail;
|
|
806
|
+
const girth = cfg.girth * look.girth * k;
|
|
807
|
+
// cap so tall settings in short widgets don't poke out of the top
|
|
808
|
+
// as tall as the box allows, less room for its rounded head and hair
|
|
809
|
+
const height = (H - ground() - cfg.girth * look.girth * 1.6) * k * filled;
|
|
810
|
+
// A sag is a loss of pressure, not of cloth, so it bows the tube over
|
|
811
|
+
// rather than shortening it: the head drops, the fabric stays. Air
|
|
812
|
+
// going in or out is the other thing, and that does change the length.
|
|
813
|
+
// It falls downwind, but still has to fall somewhere in dead calm.
|
|
814
|
+
// one direction, never a tug-of-war: downwind, or the sag's own way in calm
|
|
815
|
+
const blown = Math.abs(Math.tanh(wind * 4));
|
|
816
|
+
const dir = Math.abs(wind) < 0.02 ? sag.side : Math.sign(wind);
|
|
817
|
+
const falls = dir * (blown + 0.6 * (1 - blown));
|
|
818
|
+
// 0.042 keeps the worst case (deepest sag, full wind) inside a right
|
|
819
|
+
// angle, past which the floor clamp would eat the tube's length
|
|
820
|
+
const droop = (1 - slump) * 0.042 * falls;
|
|
821
|
+
|
|
822
|
+
// start half a girth up so the round end cap stays hidden behind the fan
|
|
823
|
+
const body = chain(x, H - ground() - girth / 2, 0, BODY_SEGS, height / BODY_SEGS, u =>
|
|
824
|
+
w * (0.05 * Math.sin(t * 2.1 - u * 5) + 0.035 * Math.sin(t * 3.7 - u * 9 + 1.3)) * (0.3 + u) + kinkBend(u, t) * limp * look.flail + wind * 0.03 + droop);
|
|
825
|
+
|
|
826
|
+
const shoulder = body[Math.round(BODY_SEGS * 0.66)];
|
|
827
|
+
const arms = [-1, 1].map(side =>
|
|
828
|
+
chain(shoulder.x, shoulder.y,
|
|
829
|
+
shoulder.a + side * (1.1 + w * 0.4 * Math.sin(t * 2.6 + side)),
|
|
830
|
+
ARM_SEGS, height * cfg.armLength * look.arms / ARM_SEGS, (v, a) =>
|
|
831
|
+
w * (0.12 * Math.sin(t * 4.3 - v * 6 + side * 2) + 0.08 * Math.sin(t * 6.1 - v * 10)) * (0.4 + v) + 0.12 * blow(a)));
|
|
832
|
+
|
|
833
|
+
const top = body[BODY_SEGS];
|
|
834
|
+
const hair = !look.hair ? [] : [-2, -1, 0, 1, 2].map(j =>
|
|
835
|
+
chain(top.x, top.y, top.a + j * 0.3, HAIR_SEGS, girth * 0.14, (v, a) =>
|
|
836
|
+
w * 0.25 * Math.sin(t * 9 - v * 4 + j * 1.7) + 0.5 * blow(a)));
|
|
837
|
+
|
|
838
|
+
return { body, arms, hair, girth, x, t };
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
// ---- drawing ----
|
|
842
|
+
|
|
843
|
+
// `grow` runs the line on past both ends, along the chain's own direction.
|
|
844
|
+
// Only the bands use it, and only while clipped to the tube, so what runs
|
|
845
|
+
// past is trimmed back to the silhouette instead of showing.
|
|
846
|
+
function strokeChain(pts, width, style, offset = 0, grow = 0) {
|
|
847
|
+
const side = p => [p.x + Math.cos(p.a) * offset, p.y + Math.sin(p.a) * offset];
|
|
848
|
+
ctx.beginPath();
|
|
849
|
+
if (grow) {
|
|
850
|
+
const [x, y] = side(pts[0]);
|
|
851
|
+
ctx.lineTo(x - Math.sin(pts[0].a) * grow, y + Math.cos(pts[0].a) * grow);
|
|
852
|
+
}
|
|
853
|
+
for (const p of pts) {
|
|
854
|
+
// offset along the segment normal, used for the highlight/shade bands
|
|
855
|
+
const [x, y] = side(p);
|
|
856
|
+
ctx.lineTo(x, y);
|
|
857
|
+
}
|
|
858
|
+
if (grow) {
|
|
859
|
+
const end = pts[pts.length - 1];
|
|
860
|
+
const [x, y] = side(end);
|
|
861
|
+
ctx.lineTo(x + Math.sin(end.a) * grow, y - Math.cos(end.a) * grow);
|
|
862
|
+
}
|
|
863
|
+
ctx.lineWidth = width;
|
|
864
|
+
ctx.strokeStyle = style;
|
|
865
|
+
ctx.stroke();
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
// The tube's own silhouette as a fillable path: along one side, round the
|
|
869
|
+
// far cap, back along the other, round the near one. Clipping the bands to
|
|
870
|
+
// it is what lets them run out to the domed ends instead of stopping short
|
|
871
|
+
// in rounded tips of their own.
|
|
872
|
+
// `flat` squashes the far cap toward the tube, to match a hand-drawn dome
|
|
873
|
+
function tubePath(pts, width, flat = 1) {
|
|
874
|
+
const r = width / 2;
|
|
875
|
+
const first = pts[0], last = pts[pts.length - 1];
|
|
876
|
+
const path = new Path2D();
|
|
877
|
+
pts.forEach((p, i) => {
|
|
878
|
+
const x = p.x + Math.cos(p.a) * r, y = p.y + Math.sin(p.a) * r;
|
|
879
|
+
if (i) path.lineTo(x, y); else path.moveTo(x, y);
|
|
880
|
+
});
|
|
881
|
+
path.ellipse(last.x, last.y, r, r * flat, last.a, 0, -Math.PI, true);
|
|
882
|
+
for (let i = pts.length - 1; i >= 0; i--) {
|
|
883
|
+
const p = pts[i];
|
|
884
|
+
path.lineTo(p.x - Math.cos(p.a) * r, p.y - Math.sin(p.a) * r);
|
|
885
|
+
}
|
|
886
|
+
path.arc(first.x, first.y, r, first.a + Math.PI, first.a, true);
|
|
887
|
+
path.closePath();
|
|
888
|
+
return path;
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
// The flattened cap, painted rather than clipped. Clipping it meant a
|
|
892
|
+
// half-plane in front of the tip, which swallowed whatever part of a
|
|
893
|
+
// bowed-over body curled past that plane, outline and all.
|
|
894
|
+
function fillDome(at, rx, ry, style) {
|
|
895
|
+
ctx.beginPath();
|
|
896
|
+
ctx.ellipse(at.x, at.y, rx, ry, at.a, 0, -Math.PI, true);
|
|
897
|
+
ctx.closePath();
|
|
898
|
+
ctx.fillStyle = style;
|
|
899
|
+
ctx.fill();
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
// Words printed down the tube, the way a real one carries SALE or OPEN.
|
|
903
|
+
// Each letter sits at its own point on the chain and turns with it, so the
|
|
904
|
+
// line of text bends and ripples with the dancer instead of riding on top
|
|
905
|
+
// as a flat label. Drawn under the shading, so it catches the same light.
|
|
906
|
+
const PRINT_TOP = 0.74; // clear of the face
|
|
907
|
+
const PRINT_FOOT = 0.26; // clear of the fan, and of a placard staked in it
|
|
908
|
+
|
|
909
|
+
function printDown(pts, width, text, ink) {
|
|
910
|
+
const n = pts.length - 1;
|
|
911
|
+
const first = Math.round(n * PRINT_TOP), last = Math.round(n * PRINT_FOOT);
|
|
912
|
+
const span = first - last;
|
|
913
|
+
if (span < 1) return;
|
|
914
|
+
// One letter per point on the chain and no more: past that they would
|
|
915
|
+
// land on the same place and stack up, so the rest is simply dropped.
|
|
916
|
+
const letters = [...text].filter(ch => ch !== '\n').slice(0, span + 1);
|
|
917
|
+
if (!letters.length) return;
|
|
918
|
+
|
|
919
|
+
ctx.fillStyle = ink;
|
|
920
|
+
ctx.textAlign = 'center';
|
|
921
|
+
ctx.textBaseline = 'middle';
|
|
922
|
+
// The letters are spread over the whole run rather than packed from the
|
|
923
|
+
// top, so a short word reads down the tube instead of sitting on its
|
|
924
|
+
// shoulders. Their size follows the gap between them, capped by how
|
|
925
|
+
// much tube there is to print across.
|
|
926
|
+
const gaps = Math.max(1, letters.length - 1);
|
|
927
|
+
const step = letters.length > 1 ? span / gaps : 0;
|
|
928
|
+
const segLen = pts[1] ? dist(pts[0], pts[1]) : width;
|
|
929
|
+
const size = Math.min(width * 0.62, Math.max(step, 1) * segLen * 0.85);
|
|
930
|
+
// heavier than the placard and the balloon: those are read close up,
|
|
931
|
+
// while this has a moving, shaded tube under it to carry
|
|
932
|
+
ctx.font = `900 ${size}px ${PRINT_FACE}`;
|
|
933
|
+
|
|
934
|
+
letters.forEach((letter, i) => {
|
|
935
|
+
const p = pts[Math.round(first - i * step)];
|
|
936
|
+
if (!p) return;
|
|
937
|
+
ctx.save();
|
|
938
|
+
ctx.translate(p.x, p.y);
|
|
939
|
+
ctx.rotate(p.a);
|
|
940
|
+
ctx.fillText(letter, 0, 0);
|
|
941
|
+
ctx.restore();
|
|
942
|
+
});
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
const dist = (a, b) => Math.hypot(a.x - b.x, a.y - b.y);
|
|
946
|
+
|
|
947
|
+
// Fabric markings, drawn on the bare colour so the shading goes over them
|
|
948
|
+
// and they catch the same light the tube does. Clipped by the caller.
|
|
949
|
+
// rings bands round the tube, the way a windsock is sewn
|
|
950
|
+
// spiral one stripe winding up it, like a barber's pole
|
|
951
|
+
function drawPattern(pts, width, kind, ink) {
|
|
952
|
+
const n = pts.length - 1;
|
|
953
|
+
ctx.strokeStyle = ink;
|
|
954
|
+
|
|
955
|
+
if (kind === 'rings') {
|
|
956
|
+
// Butt caps: a round one would add half the tube's width at each end
|
|
957
|
+
// of a band, and the rings would swell into blobs that meet.
|
|
958
|
+
ctx.lineCap = 'butt';
|
|
959
|
+
ctx.lineWidth = width;
|
|
960
|
+
for (let i = 3; i < n; i += 4) {
|
|
961
|
+
ctx.beginPath();
|
|
962
|
+
ctx.moveTo(pts[i].x, pts[i].y);
|
|
963
|
+
ctx.lineTo(pts[i + 1].x, pts[i + 1].y);
|
|
964
|
+
ctx.stroke();
|
|
965
|
+
}
|
|
966
|
+
ctx.lineCap = 'round';
|
|
967
|
+
return;
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
// A stripe wound round the tube: the angle climbs with the tube and the
|
|
971
|
+
// offset is its sine, so it sweeps across. Only the half turned toward
|
|
972
|
+
// us is drawn -- the rest is round the back -- so the path breaks every
|
|
973
|
+
// time it goes behind, which is what makes it read as wound rather than
|
|
974
|
+
// as a zigzag painted on the front.
|
|
975
|
+
if (kind !== 'spiral') return; // an unknown marking is no marking
|
|
976
|
+
const TURNS = 3.2;
|
|
977
|
+
ctx.lineWidth = width * 0.26;
|
|
978
|
+
ctx.beginPath();
|
|
979
|
+
let drawing = false;
|
|
980
|
+
for (let i = 0; i <= n; i++) {
|
|
981
|
+
const turn = (i / n) * TURNS * Math.PI * 2;
|
|
982
|
+
const p = pts[i];
|
|
983
|
+
if (Math.cos(turn) <= 0) { drawing = false; continue; } // round the back
|
|
984
|
+
const off = Math.sin(turn) * width * 0.5;
|
|
985
|
+
const x = p.x + Math.cos(p.a) * off, y = p.y + Math.sin(p.a) * off;
|
|
986
|
+
if (drawing) ctx.lineTo(x, y); else ctx.moveTo(x, y);
|
|
987
|
+
drawing = true;
|
|
988
|
+
}
|
|
989
|
+
ctx.stroke();
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
function drawTube(pts, width, color, flat = 1, pattern = 'none', ink = color, print = '') {
|
|
993
|
+
const end = pts[pts.length - 1];
|
|
994
|
+
const r = width / 2;
|
|
995
|
+
const trimming = flat < 1;
|
|
996
|
+
// A butt cap squares off both ends, so the foot would stop short of
|
|
997
|
+
// where its round cap used to reach and the fan would no longer hide
|
|
998
|
+
// the join. The outline is the wider of the two strokes and reached
|
|
999
|
+
// furthest, so the foot is carried down by its half-width.
|
|
1000
|
+
const first = pts[0];
|
|
1001
|
+
const foot = r + 3;
|
|
1002
|
+
const stroke = trimming
|
|
1003
|
+
? [{ x: first.x - Math.sin(first.a) * foot,
|
|
1004
|
+
y: first.y + Math.cos(first.a) * foot, a: first.a }, ...pts]
|
|
1005
|
+
: pts;
|
|
1006
|
+
// A round line cap can only be a semicircle, so a flatter end is drawn
|
|
1007
|
+
// instead of stroked: the tube stops square and the dome goes on by
|
|
1008
|
+
// hand, outline first and fill over it. The far end is the only one that
|
|
1009
|
+
// shows -- the near one stands behind the fan.
|
|
1010
|
+
if (trimming) ctx.lineCap = 'butt';
|
|
1011
|
+
strokeChain(stroke, width + 6, OUTLINE);
|
|
1012
|
+
strokeChain(stroke, width, color);
|
|
1013
|
+
if (trimming) {
|
|
1014
|
+
ctx.lineCap = 'round';
|
|
1015
|
+
fillDome(end, r + 3, r * flat + 3, OUTLINE);
|
|
1016
|
+
fillDome(end, r, r * flat, color);
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
// The bands tile the width: each covers about its own slice, overlapping
|
|
1020
|
+
// its neighbour only enough to leave no seam, so the steps stay crisp.
|
|
1021
|
+
// Tiling SPAN rather than the whole width keeps the outermost band's
|
|
1022
|
+
// edge just inside the silhouette, which matters most on a thin tube.
|
|
1023
|
+
const thickness = SPAN * width / TONES * OVERLAP;
|
|
1024
|
+
|
|
1025
|
+
const tones = inks(color);
|
|
1026
|
+
// clipped to the tube, so each band can overrun both ends and still be
|
|
1027
|
+
// cut off exactly at the edge
|
|
1028
|
+
ctx.save();
|
|
1029
|
+
ctx.clip(tubePath(pts, width, flat));
|
|
1030
|
+
if (pattern !== 'none') drawPattern(pts, width, pattern, ink);
|
|
1031
|
+
if (print) printDown(pts, width, print, ink);
|
|
1032
|
+
for (let i = 0; i < TONES; i++) {
|
|
1033
|
+
const ink = tones[i];
|
|
1034
|
+
if (!ink) continue; // the base colour already stands for the middle
|
|
1035
|
+
const offset = ((2 * i + 1) / TONES - 1) * SPAN * width * 0.5;
|
|
1036
|
+
strokeChain(pts, thickness, ink, offset, width);
|
|
1037
|
+
}
|
|
1038
|
+
ctx.restore();
|
|
1039
|
+
|
|
1040
|
+
// less light reaches the bottom of the tube, which plants it on the ground
|
|
1041
|
+
if (trimming) {
|
|
1042
|
+
ctx.lineCap = 'butt';
|
|
1043
|
+
strokeChain(stroke, width, depthShade);
|
|
1044
|
+
ctx.lineCap = 'round';
|
|
1045
|
+
fillDome(end, r, r * flat, depthShade);
|
|
1046
|
+
} else {
|
|
1047
|
+
strokeChain(pts, width, depthShade);
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
// An eye sits this far either side of the middle, on this line.
|
|
1052
|
+
const EYE_X = 0.2, EYE_Y = -0.1;
|
|
1053
|
+
|
|
1054
|
+
// a lid drawn as a curve: `bulge` up for a smiling eye, down for a shut one
|
|
1055
|
+
function lid(g, side, bulge) {
|
|
1056
|
+
const r = g * 0.12;
|
|
1057
|
+
ctx.beginPath();
|
|
1058
|
+
ctx.arc(side * g * EYE_X, g * EYE_Y - bulge * r * 0.5, r,
|
|
1059
|
+
bulge > 0 ? Math.PI * 1.25 : Math.PI * 0.25,
|
|
1060
|
+
bulge > 0 ? Math.PI * 1.75 : Math.PI * 0.75);
|
|
1061
|
+
ctx.lineWidth = g * 0.05;
|
|
1062
|
+
ctx.strokeStyle = OUTLINE;
|
|
1063
|
+
ctx.stroke();
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
function openEye(g, side, look) {
|
|
1067
|
+
ctx.fillStyle = '#fff';
|
|
1068
|
+
ctx.beginPath();
|
|
1069
|
+
ctx.arc(side * g * EYE_X, g * EYE_Y, g * 0.13, 0, Math.PI * 2);
|
|
1070
|
+
ctx.fill();
|
|
1071
|
+
ctx.fillStyle = OUTLINE;
|
|
1072
|
+
ctx.beginPath();
|
|
1073
|
+
ctx.arc(side * g * EYE_X + look.x, g * EYE_Y + look.y, g * 0.06, 0, Math.PI * 2);
|
|
1074
|
+
ctx.fill();
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
// Which way the pupils sit. With a pointer on the canvas they follow it,
|
|
1078
|
+
// which is the whole of looking alive; with none they wander. The head
|
|
1079
|
+
// turns with the tube, so the direction is taken into its own frame.
|
|
1080
|
+
function eyesOn(head, g, t) {
|
|
1081
|
+
const wander = { x: Math.sin(t * 1.3) * g * 0.03, y: 0 };
|
|
1082
|
+
if (!gaze) return wander;
|
|
1083
|
+
const dx = gaze.x - head.x, dy = gaze.y - head.y;
|
|
1084
|
+
const away = Math.hypot(dx, dy);
|
|
1085
|
+
if (away < 1) return wander; // right on the eyes, so nowhere to look
|
|
1086
|
+
const reach = g * 0.05;
|
|
1087
|
+
return {
|
|
1088
|
+
x: (dx * Math.cos(head.a) + dy * Math.sin(head.a)) / away * reach,
|
|
1089
|
+
y: (-dx * Math.sin(head.a) + dy * Math.cos(head.a)) / away * reach,
|
|
1090
|
+
};
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
function drawEyes(g, t, kind, look) {
|
|
1094
|
+
if (kind === 'shades') {
|
|
1095
|
+
ctx.fillStyle = OUTLINE;
|
|
1096
|
+
ctx.beginPath();
|
|
1097
|
+
ctx.roundRect(-g * 0.4, -g * 0.22, g * 0.36, g * 0.22, g * 0.06);
|
|
1098
|
+
ctx.roundRect(g * 0.04, -g * 0.22, g * 0.36, g * 0.22, g * 0.06);
|
|
1099
|
+
ctx.rect(-g * 0.06, -g * 0.19, g * 0.12, g * 0.05);
|
|
1100
|
+
ctx.fill();
|
|
1101
|
+
return;
|
|
1102
|
+
}
|
|
1103
|
+
for (const side of [-1, 1]) {
|
|
1104
|
+
if (kind === 'arches') lid(g, side, 1);
|
|
1105
|
+
else if (kind === 'shut') lid(g, side, -1);
|
|
1106
|
+
else if (kind === 'wink' && side > 0) lid(g, side, 1);
|
|
1107
|
+
else openEye(g, side, look);
|
|
1108
|
+
}
|
|
1109
|
+
if (kind === 'brows') {
|
|
1110
|
+
// two lines slanting in toward the nose, which is the whole of a scowl
|
|
1111
|
+
ctx.lineWidth = g * 0.055;
|
|
1112
|
+
ctx.strokeStyle = OUTLINE;
|
|
1113
|
+
for (const side of [-1, 1]) {
|
|
1114
|
+
ctx.beginPath();
|
|
1115
|
+
ctx.moveTo(side * g * 0.34, -g * 0.32);
|
|
1116
|
+
ctx.lineTo(side * g * 0.08, -g * 0.2);
|
|
1117
|
+
ctx.stroke();
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
function drawMouth(g, kind) {
|
|
1123
|
+
ctx.fillStyle = OUTLINE;
|
|
1124
|
+
ctx.strokeStyle = OUTLINE;
|
|
1125
|
+
ctx.lineWidth = g * 0.05;
|
|
1126
|
+
ctx.beginPath();
|
|
1127
|
+
if (kind === 'gape') {
|
|
1128
|
+
ctx.ellipse(0, g * 0.22, g * 0.1, g * 0.14, 0, 0, Math.PI * 2);
|
|
1129
|
+
ctx.fill();
|
|
1130
|
+
} else if (kind === 'smirk') {
|
|
1131
|
+
ctx.arc(g * 0.05, g * 0.1, g * 0.2, 0.2, Math.PI * 0.75);
|
|
1132
|
+
ctx.stroke();
|
|
1133
|
+
} else if (kind === 'grin') {
|
|
1134
|
+
ctx.arc(0, g * 0.08, g * 0.3, 0, Math.PI);
|
|
1135
|
+
ctx.fill();
|
|
1136
|
+
} else if (kind === 'small') {
|
|
1137
|
+
ctx.ellipse(0, g * 0.2, g * 0.07, g * 0.05, 0, 0, Math.PI * 2);
|
|
1138
|
+
ctx.fill();
|
|
1139
|
+
} else if (kind === 'frown') {
|
|
1140
|
+
ctx.arc(0, g * 0.34, g * 0.2, Math.PI, Math.PI * 2);
|
|
1141
|
+
ctx.stroke();
|
|
1142
|
+
} else {
|
|
1143
|
+
ctx.arc(0, g * 0.12, g * 0.24, 0, Math.PI);
|
|
1144
|
+
ctx.fill();
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
function drawFace(p, g, t, face) {
|
|
1149
|
+
const { eyes, mouth } = LOOKS[face] || LOOKS.happy;
|
|
1150
|
+
ctx.save();
|
|
1151
|
+
ctx.translate(p.x, p.y);
|
|
1152
|
+
ctx.rotate(p.a);
|
|
1153
|
+
drawEyes(g, t, eyes, eyesOn(p, g, t));
|
|
1154
|
+
drawMouth(g, mouth);
|
|
1155
|
+
ctx.restore();
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
// The blurted words: they rise a little, swell in and fade out. Drawn over
|
|
1159
|
+
// everything, with a pale halo behind them so they read against the dancer
|
|
1160
|
+
// as easily as against the page.
|
|
1161
|
+
function drawShouts(girth) {
|
|
1162
|
+
if (!shouts.length) {
|
|
1163
|
+
// cleared once they have gone, or the same word blurted again would
|
|
1164
|
+
// not read as a change and would go unsaid
|
|
1165
|
+
if (blurted.textContent) blurted.textContent = '';
|
|
1166
|
+
return;
|
|
1167
|
+
}
|
|
1168
|
+
shouts = shouts.filter(s => wall - s.at < SHOUT_LIFE);
|
|
1169
|
+
const size = Math.max(girth * 0.45, H * 0.032);
|
|
1170
|
+
ctx.save();
|
|
1171
|
+
ctx.font = `800 ${size}px system-ui, -apple-system, sans-serif`;
|
|
1172
|
+
ctx.textAlign = 'center';
|
|
1173
|
+
ctx.textBaseline = 'middle';
|
|
1174
|
+
ctx.lineJoin = 'round';
|
|
1175
|
+
for (const s of shouts) {
|
|
1176
|
+
const life = (wall - s.at) / SHOUT_LIFE;
|
|
1177
|
+
const swell = 0.7 + 0.3 * Math.min(1, life / 0.22) + life * 0.1;
|
|
1178
|
+
// up fast, then gone: in over the first fifth, out over the rest
|
|
1179
|
+
const fade = life < 0.2 ? life / 0.2 : 1 - (life - 0.2) / 0.8;
|
|
1180
|
+
ctx.save();
|
|
1181
|
+
ctx.globalAlpha = Math.max(0, fade);
|
|
1182
|
+
ctx.translate(s.x, s.y - life * girth * 0.8);
|
|
1183
|
+
ctx.rotate(s.tilt);
|
|
1184
|
+
ctx.scale(swell, swell);
|
|
1185
|
+
ctx.lineWidth = size * 0.3;
|
|
1186
|
+
ctx.strokeStyle = '#fffdf6';
|
|
1187
|
+
ctx.strokeText(s.words, 0, 0);
|
|
1188
|
+
ctx.fillStyle = OUTLINE;
|
|
1189
|
+
ctx.fillText(s.words, 0, 0);
|
|
1190
|
+
ctx.restore();
|
|
1191
|
+
}
|
|
1192
|
+
ctx.restore();
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
// A board on a stake at the dancer's feet. Everything else here moves, so
|
|
1196
|
+
// this is the one place words can sit and be read: it does not bob, lean
|
|
1197
|
+
// or drift with the tube above it.
|
|
1198
|
+
function drawPlacard(x, girth, text) {
|
|
1199
|
+
const words = text.trim();
|
|
1200
|
+
if (!words) return;
|
|
1201
|
+
// Not a floor, despite the look of it: the width cap below shrinks the
|
|
1202
|
+
// size again when there is a lot to say. It is a starting size, and the
|
|
1203
|
+
// second term is what a thin dancer's board is read at -- below a girth
|
|
1204
|
+
// of about 86 it is the one that wins.
|
|
1205
|
+
let size = Math.max(girth * 0.42, H * 0.036);
|
|
1206
|
+
const face = () => { ctx.font = `700 ${size}px system-ui, -apple-system, sans-serif`; };
|
|
1207
|
+
face();
|
|
1208
|
+
// narrow enough to stay on the canvas, however much it has to say
|
|
1209
|
+
const room = W * 0.86;
|
|
1210
|
+
const pad = () => size * 0.7;
|
|
1211
|
+
let w = ctx.measureText(words).width + pad() * 2;
|
|
1212
|
+
if (w > room) { size *= room / w; face(); w = ctx.measureText(words).width + pad() * 2; }
|
|
1213
|
+
const h = size + pad();
|
|
1214
|
+
// Staked in the dancer's own base and leaning out of it, rather than
|
|
1215
|
+
// standing on a foot of its own: the fan is the only thing on this
|
|
1216
|
+
// canvas that is actually planted in the ground.
|
|
1217
|
+
const LEAN = 0.26; // radians out of upright
|
|
1218
|
+
const root = { x, y: H - ground() - girth * 0.12 };
|
|
1219
|
+
const reach = girth * 1.3 + h * 0.5;
|
|
1220
|
+
const tip = {
|
|
1221
|
+
x: root.x + Math.sin(LEAN) * reach,
|
|
1222
|
+
y: root.y - Math.cos(LEAN) * reach,
|
|
1223
|
+
};
|
|
1224
|
+
|
|
1225
|
+
ctx.save();
|
|
1226
|
+
ctx.lineWidth = 3;
|
|
1227
|
+
ctx.strokeStyle = OUTLINE;
|
|
1228
|
+
ctx.beginPath();
|
|
1229
|
+
ctx.moveTo(root.x, root.y);
|
|
1230
|
+
ctx.lineTo(tip.x, tip.y);
|
|
1231
|
+
ctx.stroke();
|
|
1232
|
+
|
|
1233
|
+
// the board rides the post, so it leans with it
|
|
1234
|
+
ctx.translate(tip.x, tip.y);
|
|
1235
|
+
ctx.rotate(LEAN);
|
|
1236
|
+
ctx.fillStyle = '#fffdf6';
|
|
1237
|
+
ctx.beginPath();
|
|
1238
|
+
ctx.roundRect(-w / 2, -h, w, h, size * 0.28);
|
|
1239
|
+
ctx.fill();
|
|
1240
|
+
ctx.stroke();
|
|
1241
|
+
|
|
1242
|
+
ctx.fillStyle = OUTLINE;
|
|
1243
|
+
ctx.textAlign = 'center';
|
|
1244
|
+
ctx.textBaseline = 'middle';
|
|
1245
|
+
ctx.fillText(words, 0, -h / 2);
|
|
1246
|
+
ctx.restore();
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
function drawFan(x, g) {
|
|
1250
|
+
const w = g * 1.7, h = g * 0.55;
|
|
1251
|
+
ctx.fillStyle = '#3a3f4b';
|
|
1252
|
+
ctx.strokeStyle = OUTLINE;
|
|
1253
|
+
ctx.lineWidth = 3;
|
|
1254
|
+
ctx.beginPath();
|
|
1255
|
+
ctx.roundRect(x - w / 2, H - ground() - h * 0.6, w, h, 8);
|
|
1256
|
+
ctx.fill();
|
|
1257
|
+
ctx.stroke();
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
// A comic speech balloon that floats beside the head. It eases toward the
|
|
1261
|
+
// head rather than tracking it, so the words drift instead of thrashing.
|
|
1262
|
+
// The tail follows the same eased position, so it lags a little when the
|
|
1263
|
+
// balloon is gliding across to the dancer's other side.
|
|
1264
|
+
let drift = null;
|
|
1265
|
+
// for the little pop and the typing when the words change. Both are
|
|
1266
|
+
// arrivals rather than part of the dance, so they go by the wall clock:
|
|
1267
|
+
// at speed="0" the dance stands still, but words still have to appear.
|
|
1268
|
+
let said = null, saidAt = 0;
|
|
1269
|
+
let sided = 1, placedX = null; // which side the balloon sits on, and where it got to
|
|
1270
|
+
|
|
1271
|
+
function drawBubble(at, speakFrom, girth, text) {
|
|
1272
|
+
const shape = bubble(cfg.bubble);
|
|
1273
|
+
|
|
1274
|
+
let size = H * 0.05;
|
|
1275
|
+
const fit = () => { ctx.font = `700 ${size}px system-ui, -apple-system, sans-serif`; };
|
|
1276
|
+
fit();
|
|
1277
|
+
|
|
1278
|
+
// wrap, so a long message fills the balloon instead of stretching it
|
|
1279
|
+
const words = text.split(/\s+/).filter(Boolean);
|
|
1280
|
+
const wrap = max => {
|
|
1281
|
+
const lines = [];
|
|
1282
|
+
let line = '';
|
|
1283
|
+
for (const word of words) {
|
|
1284
|
+
const next = line ? `${line} ${word}` : word;
|
|
1285
|
+
if (line && ctx.measureText(next).width > max) { lines.push(line); line = word; }
|
|
1286
|
+
else line = next;
|
|
1287
|
+
}
|
|
1288
|
+
if (line) lines.push(line);
|
|
1289
|
+
return lines;
|
|
1290
|
+
};
|
|
1291
|
+
|
|
1292
|
+
const lines = wrap(H * 0.3);
|
|
1293
|
+
const widest = Math.max(...lines.map(l => ctx.measureText(l).width));
|
|
1294
|
+
let step = size * 1.2;
|
|
1295
|
+
|
|
1296
|
+
// the balloon scales to its text: an oval holds a box about sqrt(2) smaller
|
|
1297
|
+
let scale = Math.max(
|
|
1298
|
+
widest * 0.71 / (BALLOON.rx * shape.fit),
|
|
1299
|
+
lines.length * step * 0.71 / (BALLOON.ry * shape.fit),
|
|
1300
|
+
H * 0.0017, // never so small it reads as a speck
|
|
1301
|
+
);
|
|
1302
|
+
|
|
1303
|
+
// and shrinks, text and all, rather than overrunning the canvas or
|
|
1304
|
+
// blowing the hand-drawn outline up into a thick black band
|
|
1305
|
+
const cap = Math.min(W * 0.4 / shape.w, H * 0.0024);
|
|
1306
|
+
if (scale > cap) {
|
|
1307
|
+
const f = cap / scale;
|
|
1308
|
+
scale = cap;
|
|
1309
|
+
size *= f;
|
|
1310
|
+
step = size * 1.2;
|
|
1311
|
+
fit();
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
const bw = (shape.w + 2.5) * scale, bh = shape.h * scale; // + the stroke
|
|
1315
|
+
|
|
1316
|
+
// It keeps the side it is on until that side truly runs out of room, so
|
|
1317
|
+
// a dancer wandering across doesn't make it hop back and forth.
|
|
1318
|
+
const fitsRight = speakFrom.x + girth * 0.5 + bw < W;
|
|
1319
|
+
const fitsLeft = speakFrom.x - girth * 0.5 - bw > 0;
|
|
1320
|
+
if (sided > 0 && !fitsRight && fitsLeft) sided = -1;
|
|
1321
|
+
else if (sided < 0 && !fitsLeft && fitsRight) sided = 1;
|
|
1322
|
+
const side = sided;
|
|
1323
|
+
|
|
1324
|
+
// hang the balloon so its tail ends at the dancer
|
|
1325
|
+
const tipX = at.x + side * girth * 0.45;
|
|
1326
|
+
const room = bw * 1.05; // the balloon swells as it springs in and breathes
|
|
1327
|
+
const wanted = side > 0
|
|
1328
|
+
? Math.max(0, Math.min(W - room, tipX - BALLOON.tipX * scale))
|
|
1329
|
+
: Math.max(room, Math.min(W, tipX + BALLOON.tipX * scale));
|
|
1330
|
+
|
|
1331
|
+
// Glide the balloon's own middle, not the corner the shape hangs off:
|
|
1332
|
+
// that corner swaps sides with the mirror, and easing it would still
|
|
1333
|
+
// leave the balloon itself jumping the width of its oval.
|
|
1334
|
+
const middle = wanted + side * BALLOON.cx * scale;
|
|
1335
|
+
if (placedX === null || held()) placedX = middle;
|
|
1336
|
+
else placedX += (middle - placedX) * Math.max(0, Math.min(1, frameDt * 6));
|
|
1337
|
+
const originX = placedX - side * BALLOON.cx * scale;
|
|
1338
|
+
// never flush with the top edge, bob and swell included
|
|
1339
|
+
const originY = Math.max(H * 0.012 + 8 * scale, at.y - BALLOON.tipY * scale);
|
|
1340
|
+
|
|
1341
|
+
// whimsy: it bobs, tilts and breathes, and springs in when the words
|
|
1342
|
+
// change. A still pose gets none of it: no loop, no implied movement.
|
|
1343
|
+
if (text !== said) { said = text; saidAt = wall; }
|
|
1344
|
+
const still = held();
|
|
1345
|
+
const pop = still ? 1 : Math.min(1, (wall - saidAt) / 0.4);
|
|
1346
|
+
// the words arrive a little slower than the balloon does
|
|
1347
|
+
const saying = still ? 1 : Math.min(1, (wall - saidAt) / 0.55);
|
|
1348
|
+
const swell = still ? 1
|
|
1349
|
+
: (pop < 1 ? 0.5 + 0.5 * springy(pop) : 1) * (1 + Math.sin(t * 2.3) * 0.02);
|
|
1350
|
+
const bob = still ? 0 : Math.sin(t * 1.7) * 5 * scale;
|
|
1351
|
+
const tilt = still ? 0 : Math.sin(t * 1.1 + 1) * 0.05;
|
|
1352
|
+
|
|
1353
|
+
// everything hangs off the oval's middle, so the lot bobs together
|
|
1354
|
+
const ax = originX + side * BALLOON.cx * scale;
|
|
1355
|
+
const ay = originY + BALLOON.cy * scale + bob;
|
|
1356
|
+
|
|
1357
|
+
ctx.save();
|
|
1358
|
+
ctx.translate(ax, ay);
|
|
1359
|
+
ctx.rotate(tilt);
|
|
1360
|
+
ctx.scale(swell, swell);
|
|
1361
|
+
|
|
1362
|
+
ctx.save();
|
|
1363
|
+
ctx.scale(side * scale, scale);
|
|
1364
|
+
ctx.translate(-BALLOON.cx, -BALLOON.cy);
|
|
1365
|
+
// the path is the balloon's silhouette, so the outline is a stroke:
|
|
1366
|
+
// one even thickness all the way round, tail included
|
|
1367
|
+
ctx.fillStyle = '#fffdf6';
|
|
1368
|
+
ctx.strokeStyle = OUTLINE;
|
|
1369
|
+
ctx.lineWidth = 3.5;
|
|
1370
|
+
ctx.fill(shape.path);
|
|
1371
|
+
ctx.stroke(shape.path);
|
|
1372
|
+
ctx.restore();
|
|
1373
|
+
|
|
1374
|
+
// the words ride along, but never mirrored with the balloon
|
|
1375
|
+
ctx.fillStyle = OUTLINE;
|
|
1376
|
+
ctx.textAlign = 'center';
|
|
1377
|
+
ctx.textBaseline = 'middle';
|
|
1378
|
+
// Typed out rather than appearing whole, so a rotating sign reads as
|
|
1379
|
+
// being said. The wrap is worked out on the finished words, so nothing
|
|
1380
|
+
// reflows as it arrives; only how much of it shows changes.
|
|
1381
|
+
let left = still ? Infinity : Math.ceil(saying * text.length);
|
|
1382
|
+
lines.forEach((line, i) => {
|
|
1383
|
+
if (left <= 0) return;
|
|
1384
|
+
const shown = left >= line.length ? line : line.slice(0, left);
|
|
1385
|
+
left -= line.length + 1; // the space the wrap ate counts as a beat
|
|
1386
|
+
ctx.fillText(shown, 0, (i - (lines.length - 1) / 2) * step);
|
|
1387
|
+
});
|
|
1388
|
+
|
|
1389
|
+
ctx.restore();
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1392
|
+
function draw(dancers) {
|
|
1393
|
+
ctx.clearRect(0, 0, W, H);
|
|
1394
|
+
ctx.lineCap = 'round';
|
|
1395
|
+
ctx.lineJoin = 'round';
|
|
1396
|
+
|
|
1397
|
+
// one gradient for every tube this frame: it lives in canvas space, so
|
|
1398
|
+
// it darkens whatever part of a tube happens to hang low
|
|
1399
|
+
const floor = H - ground();
|
|
1400
|
+
depthShade = ctx.createLinearGradient(0, floor, 0, floor - H * 0.55);
|
|
1401
|
+
depthShade.addColorStop(0, `rgba(0,0,0,${GROUNDED})`);
|
|
1402
|
+
depthShade.addColorStop(1, 'rgba(0,0,0,0)');
|
|
1403
|
+
|
|
1404
|
+
for (const { body, arms, hair, girth, x, t, look } of dancers) {
|
|
1405
|
+
for (const strand of hair) strokeChain(strand, girth * 0.1 + 4, OUTLINE);
|
|
1406
|
+
for (const strand of hair) strokeChain(strand, girth * 0.1, look.accent);
|
|
1407
|
+
for (const arm of arms) drawTube(arm, girth * 0.38, look.accent);
|
|
1408
|
+
drawTube(body, girth, look.color, FLAT, cfg.pattern, look.accent,
|
|
1409
|
+
String(cfg.print ?? '').trim());
|
|
1410
|
+
// the last drawn is the front-most, since they are sorted by girth
|
|
1411
|
+
lastChest = body[Math.round(BODY_SEGS * 0.55)]; // for blurted words
|
|
1412
|
+
drawFace(body[Math.round(BODY_SEGS * 0.87)], girth, t, look.face);
|
|
1413
|
+
drawFan(x, girth);
|
|
1414
|
+
}
|
|
1415
|
+
|
|
1416
|
+
// The fattest of them sets the scale for everything drawn over the
|
|
1417
|
+
// group, so the board, the balloon and a blurted word agree.
|
|
1418
|
+
const widest = Math.max(...dancers.map(d => d.girth));
|
|
1419
|
+
|
|
1420
|
+
// one board for the lot of them, in front of their feet
|
|
1421
|
+
drawPlacard(W / 2, widest, String(cfg.placard ?? ''));
|
|
1422
|
+
|
|
1423
|
+
const sign = phraseNow(t);
|
|
1424
|
+
if (sign && dancers.length) {
|
|
1425
|
+
// one balloon for the lot of them, floating over the middle of the group
|
|
1426
|
+
const heads = dancers.map(d => d.body[Math.round(BODY_SEGS * 0.9)]);
|
|
1427
|
+
const mid = {
|
|
1428
|
+
x: heads.reduce((sum, h) => sum + h.x, 0) / heads.length,
|
|
1429
|
+
y: heads.reduce((sum, h) => sum + h.y, 0) / heads.length,
|
|
1430
|
+
};
|
|
1431
|
+
if (!drift) drift = { x: mid.x, y: mid.y };
|
|
1432
|
+
// ~1.5s to catch up: a drift, not a chase. A still pose has no loop to ease with.
|
|
1433
|
+
const ease = held() ? 1 : Math.max(0, Math.min(1, frameDt * 0.7));
|
|
1434
|
+
drift.x += (mid.x - drift.x) * ease;
|
|
1435
|
+
drift.y += (mid.y - drift.y) * ease;
|
|
1436
|
+
drawBubble(drift, mid, widest, sign);
|
|
1437
|
+
}
|
|
1438
|
+
// what the canvas just painted, said again for anything that can't see it
|
|
1439
|
+
announce(sign);
|
|
1440
|
+
aimLink();
|
|
1441
|
+
labelLink(sign);
|
|
1442
|
+
offerPoke();
|
|
1443
|
+
// last of all, so a blurted word lands over everything else
|
|
1444
|
+
drawShouts(widest);
|
|
1445
|
+
}
|
|
1446
|
+
|
|
1447
|
+
// ---- loop ----
|
|
1448
|
+
|
|
1449
|
+
// `t` is the dance's own clock and runs at the configured speed; `wall` is
|
|
1450
|
+
// plain seconds. Filling with air is a transition rather than part of the
|
|
1451
|
+
// dance, so it goes by the wall: at speed="0" the dance stands still, but
|
|
1452
|
+
// a dancer still has to finish arriving.
|
|
1453
|
+
let t = 0, wall = 0, last = performance.now(), raf, frameDt = 0, depthShade;
|
|
1454
|
+
|
|
1455
|
+
function render() { draw(simulate(t)); }
|
|
1456
|
+
|
|
1457
|
+
function frame(now) {
|
|
1458
|
+
// clamp dt so a backgrounded tab doesn't jump the animation on return
|
|
1459
|
+
const since = (now - last) / 1000;
|
|
1460
|
+
const dt = Math.min(since, 0.05);
|
|
1461
|
+
frameDt = dt;
|
|
1462
|
+
last = now;
|
|
1463
|
+
t += dt * Math.max(0, cfg.speed); // a negative speed would stall the scheduling
|
|
1464
|
+
wall += dt;
|
|
1465
|
+
punch = Math.max(0, punch - dt * 0.7);
|
|
1466
|
+
// ResizeObserver misses a move to a screen with a different pixel density
|
|
1467
|
+
if ((window.devicePixelRatio || 1) !== lastDpr) resize();
|
|
1468
|
+
updateWind(t, dt, since);
|
|
1469
|
+
updateLift(t);
|
|
1470
|
+
render();
|
|
1471
|
+
raf = requestAnimationFrame(frame);
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
function start() {
|
|
1475
|
+
cancelAnimationFrame(raf);
|
|
1476
|
+
// Whatever stopped the loop -- going off screen, a held pose -- the page
|
|
1477
|
+
// went on scrolling without it. Measuring from where it last looked
|
|
1478
|
+
// would make all of that one gust on the frame it comes back.
|
|
1479
|
+
scrolledAt = null;
|
|
1480
|
+
if (!visible) return;
|
|
1481
|
+
// Held either because the page asked or because the reader did. Either
|
|
1482
|
+
// way there is no loop, so the pose is drawn once and left.
|
|
1483
|
+
if (held()) {
|
|
1484
|
+
punch = 0; // nothing will decay it while the pose is held
|
|
1485
|
+
shouts = []; // nor any clock to fade a blurted word out with
|
|
1486
|
+
blurted.textContent = ''; // which is what would have cleared this
|
|
1487
|
+
settleAir(); // no frames to breathe with, so finish it now
|
|
1488
|
+
updateLift(t); // or the pose could be stuck mid-sag
|
|
1489
|
+
return render();
|
|
1490
|
+
}
|
|
1491
|
+
last = performance.now();
|
|
1492
|
+
raf = requestAnimationFrame(frame);
|
|
1493
|
+
}
|
|
1494
|
+
|
|
1495
|
+
// only animate while on screen, so an embed scrolled out of view costs nothing
|
|
1496
|
+
const visibility = new IntersectionObserver(([entry]) => {
|
|
1497
|
+
const coming = entry.isIntersecting && !visible;
|
|
1498
|
+
visible = entry.isIntersecting;
|
|
1499
|
+
// A held pose has no clock to fill by, so it just stands there. One
|
|
1500
|
+
// already part way in or out carries on from where it stopped: starting
|
|
1501
|
+
// over would snap it flat.
|
|
1502
|
+
if (coming && !held() && !air) breathe(0, resting, FILL);
|
|
1503
|
+
start();
|
|
1504
|
+
});
|
|
1505
|
+
visibility.observe(canvas);
|
|
1506
|
+
reduced.addEventListener('change', start, { signal: events.signal });
|
|
1507
|
+
scheme.addEventListener('change', () => { readTheme(); redrawStill(); },
|
|
1508
|
+
{ signal: events.signal });
|
|
1509
|
+
// Most pages swap themes by putting a class or a data-theme on <html>
|
|
1510
|
+
// rather than by the colour-scheme query, so watch for that as well --
|
|
1511
|
+
// otherwise the dancer keeps the colours it read at startup.
|
|
1512
|
+
const themeWatch = new MutationObserver(() => { readTheme(); redrawStill(); });
|
|
1513
|
+
themeWatch.observe(document.documentElement,
|
|
1514
|
+
{ attributes: true, attributeFilter: ['class', 'style', 'data-theme'] });
|
|
1515
|
+
readTheme();
|
|
1516
|
+
ready = true;
|
|
1517
|
+
|
|
1518
|
+
// a held pose has no loop to pick a change up, so it gets poked
|
|
1519
|
+
function redrawStill() { if (ready && visible && held()) render(); }
|
|
1520
|
+
|
|
1521
|
+
return {
|
|
1522
|
+
// only a held pose needs poking; a running loop picks changes up itself
|
|
1523
|
+
redraw: redrawStill,
|
|
1524
|
+
// Let the air out, or put it back.
|
|
1525
|
+
pump(to) {
|
|
1526
|
+
if (held()) { // no frames to breathe with, so it lands at once
|
|
1527
|
+
air = null;
|
|
1528
|
+
resting = to;
|
|
1529
|
+
updateLift(t);
|
|
1530
|
+
return render();
|
|
1531
|
+
}
|
|
1532
|
+
// Asking for what it is already doing is not a breath. It matters
|
|
1533
|
+
// off screen, where there is no loop to finish one: a pointless
|
|
1534
|
+
// breath would sit there unfinished, and an arrival only fills when
|
|
1535
|
+
// nothing else is breathing, so the dancer would come back already
|
|
1536
|
+
// standing. The hours ask for the same thing every minute.
|
|
1537
|
+
if (to === (air ? air.to : resting)) return;
|
|
1538
|
+
breathe(pressure(), to, to ? FILL : EMPTY);
|
|
1539
|
+
},
|
|
1540
|
+
// starting or stopping is the one change that has to reach the loop
|
|
1541
|
+
hold: start,
|
|
1542
|
+
// a colour attribute coming or going changes whether the page's own
|
|
1543
|
+
// theme gets a say, so the answer has to be looked up again
|
|
1544
|
+
rereadTheme: readTheme,
|
|
1545
|
+
cheer: burst,
|
|
1546
|
+
stop() {
|
|
1547
|
+
visible = false; // a late redraw() must not paint a detached canvas
|
|
1548
|
+
cancelAnimationFrame(raf);
|
|
1549
|
+
observer.disconnect();
|
|
1550
|
+
visibility.disconnect();
|
|
1551
|
+
themeWatch.disconnect();
|
|
1552
|
+
events.abort();
|
|
1553
|
+
},
|
|
1554
|
+
};
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
class SkyDancer extends HTMLElement {
|
|
1558
|
+
static observedAttributes = KEYS.map(attrName);
|
|
1559
|
+
|
|
1560
|
+
constructor() {
|
|
1561
|
+
super();
|
|
1562
|
+
this.cfg = Object.fromEntries(KEYS.map(key => [key, parse(key, this.getAttribute(attrName(key)))]));
|
|
1563
|
+
this.clock = null; // the minute hand for `hours`, when it has any
|
|
1564
|
+
this.kept = false; // whether hours were the thing holding it down
|
|
1565
|
+
const root = this.attachShadow({ mode: 'open' });
|
|
1566
|
+
root.innerHTML = `<style>${STYLE}</style>`
|
|
1567
|
+
// The canvas is decoration, so assistive tech is told to skip it. What
|
|
1568
|
+
// it paints that is actually content -- the words on the sign -- gets a
|
|
1569
|
+
// real element of its own.
|
|
1570
|
+
+ `<canvas aria-hidden="true"></canvas>`
|
|
1571
|
+
+ `<p class="said" hidden></p>`
|
|
1572
|
+
// A blurted word is painted and then gone, so it needs saying as well
|
|
1573
|
+
// as showing. Polite, because it is an aside, not the point.
|
|
1574
|
+
+ `<p class="said blurted" aria-live="polite"></p>`
|
|
1575
|
+
// A new tab, so a click never takes the reader off the page the dancer
|
|
1576
|
+
// is advertising. noopener keeps the opened page from reaching back
|
|
1577
|
+
// through window.opener.
|
|
1578
|
+
+ `<a class="go" target="_blank" rel="noopener noreferrer" hidden></a>`
|
|
1579
|
+
// The poke a pointer gives it, for anything that is not a pointer.
|
|
1580
|
+
+ `<button class="poke" type="button" hidden></button>`;
|
|
1581
|
+
this.canvas = root.querySelector('canvas');
|
|
1582
|
+
}
|
|
1583
|
+
|
|
1584
|
+
attributeChangedCallback(name, _old, value) {
|
|
1585
|
+
const key = KEYS.find(k => attrName(k) === name);
|
|
1586
|
+
this.cfg[key] = parse(key, value);
|
|
1587
|
+
if (key === 'count' && this.isConnected) this.syncCount();
|
|
1588
|
+
// reading the theme forces a style recalc, and only these two can change
|
|
1589
|
+
// what it resolves to
|
|
1590
|
+
if (key === 'color' || key === 'accent') this.anim?.rereadTheme();
|
|
1591
|
+
if (key === 'paused') this.anim?.hold();
|
|
1592
|
+
if (key === 'hours') this.keepHours();
|
|
1593
|
+
this.anim?.redraw();
|
|
1594
|
+
}
|
|
1595
|
+
|
|
1596
|
+
// the host grows sideways with the crowd; a constructor may not touch attributes
|
|
1597
|
+
syncCount() {
|
|
1598
|
+
this.style.setProperty('--count', Math.max(1, Math.min(MAX_COUNT, Math.round(this.cfg.count) || 1)));
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1601
|
+
// the page decides when a dancer is on show
|
|
1602
|
+
hide() { this.hidden = true; }
|
|
1603
|
+
show() { this.hidden = false; }
|
|
1604
|
+
|
|
1605
|
+
// Set it off: the same burst a poke gives it, for a page with something to
|
|
1606
|
+
// celebrate. A still pose has no loop to carry it, so it stays still.
|
|
1607
|
+
cheer() { this.anim?.cheer(); }
|
|
1608
|
+
|
|
1609
|
+
// The other half of that: a dancer can flop when something goes wrong and
|
|
1610
|
+
// stand back up when it comes right. It stays on show either way -- this
|
|
1611
|
+
// is a mood, not hide() and show().
|
|
1612
|
+
deflate() { this.anim?.pump(0); }
|
|
1613
|
+
inflate() { this.anim?.pump(1); }
|
|
1614
|
+
|
|
1615
|
+
// Opening hours, for the shop front this is really a sign for: the air
|
|
1616
|
+
// goes out at closing time and back in when it opens. A page may still
|
|
1617
|
+
// call deflate() or inflate() in between, but the next look at the clock
|
|
1618
|
+
// puts it back where the hours say, within the minute.
|
|
1619
|
+
keepHours() {
|
|
1620
|
+
clearInterval(this.clock);
|
|
1621
|
+
this.clock = null;
|
|
1622
|
+
// An attribute can be set before the element is in a page, and a
|
|
1623
|
+
// timer started then would outlive it: nothing would clear it, and it
|
|
1624
|
+
// would hold the element from being collected.
|
|
1625
|
+
if (!this.isConnected) return;
|
|
1626
|
+
if (!this.cfg.hours) {
|
|
1627
|
+
// Taking the hours away gives the dancer back, rather than leaving it
|
|
1628
|
+
// flat with nothing left to open it again -- but only when there were
|
|
1629
|
+
// hours holding it down. On a first look there is nothing to undo, and
|
|
1630
|
+
// a breath started here would rob the dancer of its arrival: the fill
|
|
1631
|
+
// only runs when nothing else is already breathing.
|
|
1632
|
+
if (this.kept) this.anim?.pump(1);
|
|
1633
|
+
this.kept = false;
|
|
1634
|
+
return;
|
|
1635
|
+
}
|
|
1636
|
+
this.kept = true;
|
|
1637
|
+
const check = () => this.anim?.pump(openNow(this.cfg.hours) ? 1 : 0);
|
|
1638
|
+
check();
|
|
1639
|
+
// a minute is close enough for a thing that changes twice a day
|
|
1640
|
+
this.clock = setInterval(check, 60000);
|
|
1641
|
+
}
|
|
1642
|
+
|
|
1643
|
+
connectedCallback() {
|
|
1644
|
+
this.syncCount();
|
|
1645
|
+
this.anim = animate(this.canvas, this.cfg);
|
|
1646
|
+
this.keepHours();
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1649
|
+
disconnectedCallback() {
|
|
1650
|
+
clearInterval(this.clock);
|
|
1651
|
+
this.clock = null;
|
|
1652
|
+
this.anim.stop();
|
|
1653
|
+
this.anim = null; // the optional calls above cope, and nothing is held
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1657
|
+
customElements.define('sky-dancer', SkyDancer);
|
|
1658
|
+
})();
|