@kodax-ai/botui 0.1.2 → 0.1.3
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/README.md +32 -6
- package/core/identity.js +46 -0
- package/{themes/solids → core}/motion.js +114 -114
- package/core/registry.js +26 -18
- package/docs/usage.md +202 -0
- package/element.js +5 -0
- package/index.d.ts +2 -0
- package/index.js +11 -9
- package/package.json +33 -2
- package/themes/caret/README.md +43 -0
- package/themes/caret/avatar.js +10 -0
- package/themes/caret/engine.js +143 -0
- package/themes/caret/index.d.ts +24 -0
- package/themes/caret/index.js +35 -0
- package/themes/critter/README.md +43 -0
- package/themes/critter/avatar.js +12 -0
- package/themes/critter/engine.js +113 -0
- package/themes/critter/index.d.ts +28 -0
- package/themes/critter/index.js +35 -0
- package/themes/doodle/README.md +43 -0
- package/themes/doodle/avatar.js +10 -0
- package/themes/doodle/engine.js +149 -0
- package/themes/doodle/index.d.ts +24 -0
- package/themes/doodle/index.js +36 -0
- package/themes/dots/README.md +42 -0
- package/themes/dots/avatar.js +15 -0
- package/themes/dots/engine.js +179 -0
- package/themes/dots/index.d.ts +24 -0
- package/themes/dots/index.js +35 -0
- package/themes/kaomoji/README.md +42 -0
- package/themes/kaomoji/avatar.js +10 -0
- package/themes/kaomoji/engine.js +110 -0
- package/themes/kaomoji/index.d.ts +24 -0
- package/themes/kaomoji/index.js +35 -0
- package/themes/liquid/README.md +47 -0
- package/themes/liquid/avatar.js +10 -0
- package/themes/liquid/engine.js +161 -0
- package/themes/liquid/index.d.ts +24 -0
- package/themes/liquid/index.js +35 -0
- package/themes/matrix/README.md +4 -2
- package/themes/matrix/avatar.js +24 -19
- package/themes/matrix/engine.js +61 -25
- package/themes/matrix/index.d.ts +2 -1
- package/themes/matrix/index.js +9 -15
- package/themes/orb/README.md +44 -0
- package/themes/orb/avatar.js +10 -0
- package/themes/orb/engine.js +130 -0
- package/themes/orb/index.d.ts +24 -0
- package/themes/orb/index.js +36 -0
- package/themes/solids/README.md +1 -1
- package/themes/solids/avatar.js +6 -24
- package/themes/solids/face.js +52 -56
- package/themes/solids/index.js +34 -28
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// The typed face theme's model: a face made of text, on the matrix theme's tile.
|
|
2
|
+
//
|
|
3
|
+
// Every expression is three or four typed characters (o_o, ^_^, >_<), so a chat
|
|
4
|
+
// Bot's face is written in the same medium as its replies. A new face is typed
|
|
5
|
+
// behind a blink: the line folds shut, the text changes, it opens again. Signal
|
|
6
|
+
// states are text too: a rolling ellipsis, a percentage, arrows that advance.
|
|
7
|
+
//
|
|
8
|
+
// Everything else (tile, motion, marks, particles, halo, handling) is the matrix
|
|
9
|
+
// model's; this file only draws the face. Pure, like it: no DOM, no clock, no
|
|
10
|
+
// Math.random, so the same Bot and the same inputs always give the same frames.
|
|
11
|
+
// Sketched in avatar-lab as direction K (字符脸), made a theme on 2026-11-28.
|
|
12
|
+
|
|
13
|
+
import {Matrix} from '../matrix/engine.js';
|
|
14
|
+
import {clamp} from '../../core/core.js';
|
|
15
|
+
|
|
16
|
+
const {sin, abs, max, min, floor, round} = Math;
|
|
17
|
+
const fixed = v => v.toFixed(2);
|
|
18
|
+
const FONT = "ui-monospace, 'Cascadia Mono', 'SF Mono', Menlo, Consolas, 'DejaVu Sans Mono', monospace";
|
|
19
|
+
const HEART = 'oklch(0.6 0.22 15)', BLUSH = 'oklch(0.74 0.17 15)';
|
|
20
|
+
// Spaces would collapse; a no-break space holds its place.
|
|
21
|
+
const keep = text => text.replace(/ /g, ' ');
|
|
22
|
+
const frame = (t, rate, list) => list[floor(t * rate) % list.length];
|
|
23
|
+
|
|
24
|
+
/** The eye character for each of the solids' eye styles: dot (圆点), bar (竖条), tall (长条). */
|
|
25
|
+
const EYE = {dot: '•', bar: 'o', tall: '0'};
|
|
26
|
+
export const kaomojiEyes = Object.freeze(Object.keys(EYE));
|
|
27
|
+
|
|
28
|
+
/** The 21 expressions: (eye character, t) -> text, or {text, fill}. */
|
|
29
|
+
export const FACES = {
|
|
30
|
+
neutral: e => `${e}_${e}`,
|
|
31
|
+
wide: () => 'O_O',
|
|
32
|
+
happy: () => '^_^',
|
|
33
|
+
closed: () => '-_-',
|
|
34
|
+
sleep: () => 'u_u',
|
|
35
|
+
squint: () => '>_<',
|
|
36
|
+
sad: () => ';_;',
|
|
37
|
+
focus: () => 'ò_ó',
|
|
38
|
+
angry: () => 'ÒдÓ',
|
|
39
|
+
calm: () => '˘‿˘',
|
|
40
|
+
sleepy: () => '=_=',
|
|
41
|
+
shy: e => `${e}//${e}`,
|
|
42
|
+
inspecting: e => `${e}_O`,
|
|
43
|
+
confused: () => '?_?',
|
|
44
|
+
wink: () => '^_~',
|
|
45
|
+
skeptic: () => '¬_¬',
|
|
46
|
+
cross: () => 'x_x',
|
|
47
|
+
surprised: () => '°o°',
|
|
48
|
+
dizzy: (e, t) => frame(t, 5, ['@_@', '@_@', '6_9']),
|
|
49
|
+
love: () => ({text: '♥_♥', fill: HEART}),
|
|
50
|
+
sparkle: (e, t) => frame(t, 3.5, ['*_*', '+_+']),
|
|
51
|
+
};
|
|
52
|
+
// Expressions whose eyes are already shut, so a blink leaves them alone.
|
|
53
|
+
const SHUT = new Set(['closed', 'sleep', 'sleepy', 'happy', 'calm', 'cross']);
|
|
54
|
+
|
|
55
|
+
/** What the screen types instead of a face: (eye, t, progress, since) -> text or {text, y, scale, opacity}, or null for the face. */
|
|
56
|
+
export const SCREENS = {
|
|
57
|
+
thinking: (e, t) => frame(t, 4.5, ['• ', '•• ', '•••', ' ••', ' •']),
|
|
58
|
+
streaming: (e, t) => frame(t, 8, [`${e}_${e}`, `${e}o${e}`, `${e}O${e}`, `${e}o${e}`]),
|
|
59
|
+
finished: () => '^o^',
|
|
60
|
+
orbit: (e, t) => `(${frame(t, 8, ['|', '/', '-', '\\'])})`,
|
|
61
|
+
radar: (e, t) => frame(t, 3, [' • ', '(•)', '((•))']),
|
|
62
|
+
progress: (e, t, progress) => round(clamp(progress ?? (t * 0.22) % 1, 0, 1) * 100) + '%',
|
|
63
|
+
loading: (e, t) => frame(t, 5, ['o··', '·o·', '··o', '·o·']),
|
|
64
|
+
spawning: (e, t, progress, since) => since >= 1.2 ? null : ['_', `${e}_`, `${e}_${e}`][min(2, floor(since * 2.6))],
|
|
65
|
+
dictating: (e, t) => frame(t, 6, [`${e}-${e}`, `${e}~${e}`, `${e}=${e}`, `${e}~${e}`]),
|
|
66
|
+
sending: (e, t) => frame(t, 4, ['> ', '>> ', '>>>', ' >>', ' >']),
|
|
67
|
+
receiving: (e, t) => frame(t, 4, [' <', ' <<', '<<<', '<< ', '< ']),
|
|
68
|
+
uploading: (e, t) => { const u = (t * 1.5) % 1; return {text: '^^^', y: 4 - 9 * u, opacity: 1 - 0.7 * u}; },
|
|
69
|
+
writing: (e, t) => 'abc'.slice(0, min(3, floor((t * 3) % 5))) + (floor(t * 3.4) % 2 ? '_' : ' '),
|
|
70
|
+
alerting: (e, t) => ({text: '(!)', scale: 1 + 0.12 * max(0, sin(t * 7))}),
|
|
71
|
+
bouncing: (e, t) => ({text: e, y: 6 - 18 * abs(sin(t * 4))}),
|
|
72
|
+
'powering-down': (e, t, progress, since) => since < 0.5 ? '-_-' : since < 1 ? '._.' : {text: '.', opacity: 0.45 + 0.25 * sin(t * 1.6)},
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
class Kaomoji extends Matrix {
|
|
76
|
+
faceLayout(ink, node) {
|
|
77
|
+
return [
|
|
78
|
+
...[-1, 1].map(() => node('tile', 'ellipse', {rx: 5.5, ry: 3, fill: BLUSH, opacity: 0})),
|
|
79
|
+
node('tile', 'text', {x: 0, y: 0, 'text-anchor': 'middle', 'dominant-baseline': 'central', 'font-family': FONT, 'font-weight': 700, fill: ink.dark, text: ''}),
|
|
80
|
+
];
|
|
81
|
+
}
|
|
82
|
+
/** What to type this frame: {text, fill, y, scale, opacity, face}. */
|
|
83
|
+
typing(facing) {
|
|
84
|
+
if (!facing) return {text: ''};
|
|
85
|
+
const e = EYE[this.eyes] || EYE.bar;
|
|
86
|
+
const screen = !this.forced && SCREENS[this.state]?.(e, this.t, this.progress, this.since);
|
|
87
|
+
if (screen) return typeof screen === 'string' ? {text: screen} : screen;
|
|
88
|
+
let name = this.shown;
|
|
89
|
+
// Halfway through a blink the eyes are shut.
|
|
90
|
+
if (this.p.open < 0.45 && !SHUT.has(name)) name = 'closed';
|
|
91
|
+
const look = (FACES[name] || FACES.neutral)(e, this.t);
|
|
92
|
+
return {face: true, ...(typeof look === 'string' ? {text: look} : look)};
|
|
93
|
+
}
|
|
94
|
+
drawFace(nodes, n, {ink, dim, yaw, pitch, facing}) {
|
|
95
|
+
const p = this.p, typed = this.typing(facing);
|
|
96
|
+
// The face follows the gaze across the screen; a blink folds the line flat and opens it again.
|
|
97
|
+
const ox = typed.face ? clamp((-sin(yaw) * 1.5 + this.gx * 5) * 7, -8, 8) : 0, oy = typed.face ? clamp((pitch * 1.6 + this.gy * 5) * 5, -5, 5) : 0;
|
|
98
|
+
const text = keep(typed.text || ''), length = [...text].length;
|
|
99
|
+
const size = min(33, 105 / max(3, length)) * (this.size < 40 ? 1.12 : 1) * (typed.scale || 1);
|
|
100
|
+
const fold = typed.face ? 0.3 + 0.7 * abs(p.open * 2 - 1) : 1;
|
|
101
|
+
const blush = typed.face ? 0.8 * p.blush : 0;
|
|
102
|
+
for (const side of [-1, 1]) Object.assign(nodes[n++], {cx: fixed(50 + side * 24 + ox), cy: fixed(62 + oy), opacity: fixed(blush)});
|
|
103
|
+
Object.assign(nodes[n++], {text, 'font-size': fixed(size), fill: typed.fill || ink.dark, opacity: fixed((typed.opacity ?? 1) * (0.35 + 0.65 * dim)),
|
|
104
|
+
transform: `translate(${fixed(50 + ox)} ${fixed(50 + oy + (typed.y || 0))}) scale(1 ${fixed(fold)})`});
|
|
105
|
+
return n;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** A typed face model. Options as createMatrix's: identity, hue, chroma, light, dots, edge, shape, eyes, state, progress, size, emphasized. */
|
|
110
|
+
export const createKaomoji = options => new Kaomoji(options);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Types for '@kodax-ai/botui/themes/kaomoji'.
|
|
2
|
+
|
|
3
|
+
import type {ColorName, EyeStyle, ExpressionName, IdentityPool, Theme} from '../../index.js';
|
|
4
|
+
import type {MatrixAvatar, MatrixOptions, MatrixShape, MatrixModel, MatrixTone} from '../matrix/index.js';
|
|
5
|
+
|
|
6
|
+
export type {Avatar, AvatarOptions, ColorName, EyeStyle, ExpressionName, GestureName, IdentityPool, StateName, StateAlias, StateGroup, Theme} from '../../index.js';
|
|
7
|
+
export type {MatrixOptions as KaomojiOptions, MatrixShape as KaomojiShape} from '../matrix/index.js';
|
|
8
|
+
export {setAttention, resolveState, STATES, STATE_GROUPS, ALIASES, COLORS, BIRTH_COLORS, HUES, EYE_STYLES, GESTURES} from '../../index.js';
|
|
9
|
+
export {SHAPES, GROUPS} from '../matrix/index.js';
|
|
10
|
+
|
|
11
|
+
export interface KaomojiAvatar extends MatrixAvatar {}
|
|
12
|
+
export const KaomojiAvatar: {new (options?: MatrixOptions): KaomojiAvatar};
|
|
13
|
+
|
|
14
|
+
export const theme: Theme;
|
|
15
|
+
export function createAvatar(options?: MatrixOptions): KaomojiAvatar;
|
|
16
|
+
export function identityFrom(identity: string, pool?: IdentityPool): {shape: MatrixShape; color: ColorName; eyes: EyeStyle};
|
|
17
|
+
export const EXPRESSIONS: Record<ExpressionName, Function>;
|
|
18
|
+
|
|
19
|
+
// The pure model: the matrix model with a face of typed characters.
|
|
20
|
+
export function createKaomoji(options: MatrixTone & {identity?: string; shape: MatrixShape; eyes?: EyeStyle; state?: string; progress?: number | null; size?: number; emphasized?: boolean}): MatrixModel;
|
|
21
|
+
export const kaomojiShapes: readonly MatrixShape[];
|
|
22
|
+
export const kaomojiEyes: readonly EyeStyle[];
|
|
23
|
+
export const FACES: Record<ExpressionName, Function>;
|
|
24
|
+
export const SCREENS: Record<string, Function>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// BotUI typed face theme: a face made of text, on a coloured tile.
|
|
2
|
+
//
|
|
3
|
+
// import {createAvatar} from './themes/kaomoji/index.js';
|
|
4
|
+
// const bot = createAvatar({identity: 'research-bot', size: 36});
|
|
5
|
+
// sidebar.append(bot.el);
|
|
6
|
+
// bot.setState('thinking');
|
|
7
|
+
|
|
8
|
+
import {KaomojiAvatar} from './avatar.js';
|
|
9
|
+
import {COLORS, EYE_STYLES} from '../../core/identity.js';
|
|
10
|
+
import {STATES} from '../../core/states.js';
|
|
11
|
+
import {registerTheme} from '../../core/registry.js';
|
|
12
|
+
import {GESTURES} from '../../core/motion.js';
|
|
13
|
+
import {FACES} from './engine.js';
|
|
14
|
+
import {SHAPES} from '../matrix/engine.js';
|
|
15
|
+
|
|
16
|
+
export {KaomojiAvatar, identityFrom} from './avatar.js';
|
|
17
|
+
export {COLORS, BIRTH_COLORS, HUES, EYE_STYLES} from '../../core/identity.js';
|
|
18
|
+
export {STATES, STATE_GROUPS, ALIASES, resolveState} from '../../core/states.js';
|
|
19
|
+
export {setAttention} from '../../core/core.js';
|
|
20
|
+
export {GESTURES} from '../../core/motion.js';
|
|
21
|
+
export {SHAPES, GROUPS, matrixShapes as kaomojiShapes} from '../matrix/engine.js';
|
|
22
|
+
export {createKaomoji, kaomojiEyes, FACES, SCREENS} from './engine.js';
|
|
23
|
+
/** The expressions, by name; here each one is a few typed characters (see FACES). */
|
|
24
|
+
export const EXPRESSIONS = FACES;
|
|
25
|
+
|
|
26
|
+
/** Create an avatar. Options as MatrixAvatar's. Append `.el` to the page. */
|
|
27
|
+
export function createAvatar(options) { return new KaomojiAvatar(options); }
|
|
28
|
+
|
|
29
|
+
/** This theme, in the form core/registry.js expects. */
|
|
30
|
+
export const theme = {
|
|
31
|
+
name: 'kaomoji', zh: '字符脸', create: createAvatar,
|
|
32
|
+
catalog: () => ({shapes: SHAPES, colors: Object.keys(COLORS), eyes: Object.keys(EYE_STYLES), states: Object.keys(STATES), gestures: Object.keys(GESTURES), expressions: Object.keys(FACES)}),
|
|
33
|
+
};
|
|
34
|
+
// Importing a theme makes it available to createAvatar({theme}) from the package entry.
|
|
35
|
+
registerTheme(theme);
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# 液面 liquid
|
|
2
|
+
|
|
3
|
+
方块是一只玻璃杯,装着 Bot 颜色的液体。没有眼睛,液面就是脸:开心时两边沿杯壁往上翘,难过时往下垂,生气时沸腾,睡着时又平又低。杯子倾斜时液面保持水平,跳起或转圈时会晃。工作时冒泡,进度就是装了多满。
|
|
4
|
+
|
|
5
|
+
来自 avatar-lab 的草图方向 U(液面),2026-11-28 做成主题。
|
|
6
|
+
|
|
7
|
+
## 用法
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
import {createAvatar} from '@kodax-ai/botui/themes/liquid'; // 或相对路径 ./themes/liquid/index.js
|
|
11
|
+
|
|
12
|
+
const bot = createAvatar({identity: 'research-bot', size: 36});
|
|
13
|
+
sidebar.append(bot.el);
|
|
14
|
+
bot.setState('thinking');
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
从总入口用:先 `import '@kodax-ai/botui/themes/liquid'`,再 `createAvatar({theme: 'liquid', ...})`。自定义元素:同样先加载主题脚本,再写 `<bot-avatar theme="liquid" identity="research-bot"></bot-avatar>`。
|
|
18
|
+
|
|
19
|
+
## 与点阵的关系
|
|
20
|
+
|
|
21
|
+
外形(圆角方、圆、六边形)、颜色、动作、记号、粒子、交互、水印都和 [点阵](../matrix/README.md) 一样,选项和方法也完全相同;同一个 Bot ID 在两个主题里是同一个外形和颜色。不同的是:
|
|
22
|
+
|
|
23
|
+
| 项 | 液面 |
|
|
24
|
+
|---|---|
|
|
25
|
+
| 方块 | 浅色玻璃,液体是 Bot 的颜色,后面一层更深的液面做出厚度 |
|
|
26
|
+
| 眼型 | 没有眼睛;三种眼型变成三种气泡:`dot` 少而大,`bar` 中等,`tall` 多而小 |
|
|
27
|
+
| 眼神(`FACES`) | 21 种都是液面的姿态:弯曲(`happy` 上翘、`sad` 下垂)、波纹高低和碎度、倾斜(`skeptic`、`inspecting`)、沸腾(`angry`)、颜色偏向(`angry` 偏红、`sad` 偏蓝、`shy` / `love` 偏粉) |
|
|
28
|
+
| 状态(`MOODS`) | 液位、气泡多少、波纹速度:工作时冒泡,难过和睡着时液位低,庆祝时满;进度 = 液位;生成时从空慢慢灌满,关机时慢慢流干;思考时气泡绕圈,发送 / 接收时气泡横向流动 |
|
|
29
|
+
| 眨眼 | 液面泛起一圈涟漪 |
|
|
30
|
+
| 背面 | 转到背面仍看得见液体(它是玻璃杯) |
|
|
31
|
+
|
|
32
|
+
小尺寸(24 px 以下)时液面的弯曲不容易看出来,主要靠颜色、液位和气泡区分。
|
|
33
|
+
|
|
34
|
+
裁剪需要 SVG id,每个实例用一个递增计数生成唯一 id。除此之外模型是纯的:不碰 DOM、不用时钟和 `Math.random`。
|
|
35
|
+
|
|
36
|
+
## 纯模型
|
|
37
|
+
|
|
38
|
+
`createLiquid(options)` 与 `createMatrix` 用法相同。节点里有 `defs`、`clipPath` 和一个裁剪用的 `g`,这些节点带 `parent` 指向它所嵌套的节点。
|
|
39
|
+
|
|
40
|
+
## 文件
|
|
41
|
+
|
|
42
|
+
| 文件 | 内容 |
|
|
43
|
+
|---|---|
|
|
44
|
+
| `index.js` | 入口:`createAvatar`、`theme` 和各种表 |
|
|
45
|
+
| `avatar.js` | `LiquidAvatar`:继承点阵的 `MatrixAvatar`,只换模型 |
|
|
46
|
+
| `engine.js` | 液面:姿态表 `FACES`、状态表 `MOODS`、晃动、气泡 |
|
|
47
|
+
| `playground.html` | 试玩页(共用 `../solids/playground.js`) |
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import {MatrixAvatar} from '../matrix/avatar.js';
|
|
2
|
+
import {createLiquid} from './engine.js';
|
|
3
|
+
|
|
4
|
+
export {identityFrom} from '../matrix/avatar.js';
|
|
5
|
+
|
|
6
|
+
/** The liquid avatar: the matrix avatar as a glass of the Bot's colour. Options and methods as MatrixAvatar's. */
|
|
7
|
+
export class LiquidAvatar extends MatrixAvatar {
|
|
8
|
+
static className = 'botui-liquid';
|
|
9
|
+
createModel(options) { return createLiquid(options); }
|
|
10
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// The liquid theme's model: the matrix theme's tile as a glass, filled with the Bot's colour.
|
|
2
|
+
//
|
|
3
|
+
// No eyes: the surface of the liquid is the face. It smiles (the edges climb the
|
|
4
|
+
// glass), droops when sad, boils when angry, lies flat and low when asleep. The
|
|
5
|
+
// liquid stays level as the glass tilts, and sloshes behind when it jumps or
|
|
6
|
+
// spins. Bubbles rise while it works; progress is how full it is.
|
|
7
|
+
//
|
|
8
|
+
// Everything else (outline, motion, marks, particles, halo, handling) is the
|
|
9
|
+
// matrix model's. Pure, like it: no DOM, no clock, no Math.random. The one
|
|
10
|
+
// exception is a counter in the clip id, so two glasses on a page never share one.
|
|
11
|
+
// Sketched in avatar-lab as direction U (液面), made a theme on 2026-11-28.
|
|
12
|
+
|
|
13
|
+
import {Matrix} from '../matrix/engine.js';
|
|
14
|
+
import {TAU, ease, clamp} from '../../core/core.js';
|
|
15
|
+
|
|
16
|
+
const {sin, cos, tan, abs, max, min, floor} = Math;
|
|
17
|
+
const fixed = v => v.toFixed(2);
|
|
18
|
+
const TOP = 12, BOTTOM = 88, LEFT = 10, RIGHT = 90, COLS = 24, BUBBLES = 7, SLOSH = 60, SLOSH_DAMP = 3.2;
|
|
19
|
+
const HEART = 15;
|
|
20
|
+
let made = 0;
|
|
21
|
+
|
|
22
|
+
/** The eye styles become the bubbles: dot (圆点) a few big ones, bar (竖条) medium, tall (长条) many small. */
|
|
23
|
+
const FIZZ = {dot: {r: 3.2, n: 4}, bar: {r: 2.3, n: 6}, tall: {r: 1.6, n: 7}};
|
|
24
|
+
export const liquidEyes = Object.freeze(Object.keys(FIZZ));
|
|
25
|
+
|
|
26
|
+
// ---- the face: the surface ----
|
|
27
|
+
// curve: + smiles (the edges climb), - droops. wave: ripple height. chop: how fast and broken the ripples are.
|
|
28
|
+
// tilt: a lean of the surface. boil: extra bubbles. lean: a hue the colour leans toward.
|
|
29
|
+
const S = (curve = 0, wave = 0.6, extra) => ({curve, wave, chop: 1, tilt: 0, boil: 0, ...extra});
|
|
30
|
+
/** The 21 expressions, each a way the surface sits: (t) -> {curve, wave, chop, tilt, boil, lean}. */
|
|
31
|
+
export const FACES = {
|
|
32
|
+
neutral: () => S(),
|
|
33
|
+
wide: () => S(0, 2.2, {chop: 1.4}),
|
|
34
|
+
happy: () => S(20, 0.8),
|
|
35
|
+
closed: () => S(0, 0.2),
|
|
36
|
+
sleep: () => S(-3, 0.15, {chop: 0.4}),
|
|
37
|
+
squint: () => S(13, 1.6, {chop: 1.8}),
|
|
38
|
+
sad: () => S(-17, 0.3, {chop: 0.5, lean: 250}),
|
|
39
|
+
focus: () => S(0, 0.1),
|
|
40
|
+
angry: () => S(-6, 2.6, {chop: 2.6, boil: 1, lean: 25}),
|
|
41
|
+
calm: () => S(8, 0.3, {chop: 0.5}),
|
|
42
|
+
sleepy: () => S(0, 0.2, {chop: 0.4}),
|
|
43
|
+
shy: () => S(7, 0.5, {lean: HEART}),
|
|
44
|
+
inspecting: () => S(0, 0.4, {tilt: 0.16}),
|
|
45
|
+
confused: t => S(0, 1, {tilt: 0.14 * sin(t * 2.4)}),
|
|
46
|
+
wink: () => S(13, 0.6, {tilt: -0.1}),
|
|
47
|
+
skeptic: () => S(-3, 0.3, {tilt: 0.2}),
|
|
48
|
+
cross: () => S(-2, 1.8, {chop: 3}),
|
|
49
|
+
surprised: () => S(0, 3.4, {chop: 1.6}),
|
|
50
|
+
dizzy: t => S(0, 1.8, {tilt: 0.24 * sin(t * 3.3)}),
|
|
51
|
+
love: () => S(15, 0.8, {boil: 0.6, lean: HEART}),
|
|
52
|
+
sparkle: () => S(15, 1.2, {boil: 1}),
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// ---- the states: how full, and how lively ----
|
|
56
|
+
// level: 0 empty .. 1 full (null keeps the default). bubbles: 0..1. speed: of the ripples.
|
|
57
|
+
const M = (level, bubbles = 0, speed = 1, extra) => ({level, bubbles, speed, ...extra});
|
|
58
|
+
const WORK = M(0.58, 0.8, 1.5);
|
|
59
|
+
export const MOODS = {
|
|
60
|
+
idle: M(0.56, 0.15, 0.8), listening: M(0.6, 0.2, 1),
|
|
61
|
+
thinking: M(0.58, 0.6, 1.2, {swirl: 1}), searching: WORK, reading: WORK, working: WORK, coding: WORK, running: M(0.58, 0.9, 2.2), computing: M(0.58, 1, 1.8), controlling: WORK,
|
|
62
|
+
streaming: M(0.6, 0.5, 2.4), writing: M(0.6, 0.5, 1.6), dictating: M(0.6, 0.4, 2),
|
|
63
|
+
loading: M(0.56, 0.3, 1.2, {pump: 1}), orbit: M(0.58, 0.6, 1.4, {swirl: 1}), radar: M(0.58, 0.4, 1.2, {swirl: 1}),
|
|
64
|
+
progress: M(null, 0.5, 1.2, {fill: 1}), spawning: M(null, 0.6, 1.4, {grow: 1}),
|
|
65
|
+
sending: M(0.56, 0.3, 1.6, {flow: 1}), receiving: M(0.56, 0.3, 1.6, {flow: -1}), uploading: M(0.56, 1, 1.6, {rise: 1}),
|
|
66
|
+
finished: M(0.66, 0.7, 1.2), approving: M(0.64, 0.4, 1), proud: M(0.66, 0.3, 1),
|
|
67
|
+
excited: M(0.7, 1, 2), celebrating: M(0.72, 1, 2.2), laughing: M(0.66, 0.8, 2.4), happy: M(0.64, 0.5, 1.3), playful: M(0.62, 0.5, 1.6),
|
|
68
|
+
angry: M(0.6, 1, 2.4), error: M(0.42, 0, 0.6, {lean: 25}), alerting: M(0.6, 0.6, 2.2, {lean: 45}),
|
|
69
|
+
sad: M(0.38, 0, 0.5), scared: M(0.46, 0.2, 2.6), blocked: M(0.4, 0, 0.3),
|
|
70
|
+
sleeping: M(0.34, 0, 0.35), drowsy: M(0.42, 0, 0.5), bored: M(0.4, 0, 0.5),
|
|
71
|
+
'powering-down': M(0.2, 0, 0.2, {drain: 1}), loving: M(0.62, 0.6, 1, {lean: HEART}),
|
|
72
|
+
};
|
|
73
|
+
const IDLE = MOODS.idle;
|
|
74
|
+
const pseudo = i => { const s = sin(i * 12.9898 + 78.233) * 43758.5453; return s - floor(s); };
|
|
75
|
+
|
|
76
|
+
class Liquid extends Matrix {
|
|
77
|
+
tileLayout(ink, node) {
|
|
78
|
+
const [outline] = super.tileLayout(ink, node), id = `botui-liquid-${++made}-clip`;
|
|
79
|
+
this.fluid = {level: IDLE.level, slope: 0, vslope: 0, curve: 0, wave: 0.6, chop: 1, tilt: 0, boil: 0, bubbles: 0.15, speed: 0.8, mix: 0, lean: null, phase: this.rng() * 10, rise: this.rng() * 10, splash: 0};
|
|
80
|
+
this.lastRoll = 0;
|
|
81
|
+
const defs = node('tile', 'defs'), clip = node('tile', 'clipPath', {id}, defs), inside = node('tile', 'g', {'clip-path': `url(#${id})`});
|
|
82
|
+
return [
|
|
83
|
+
defs, clip, node('tile', 'path', {d: outline.attrs.d}, clip),
|
|
84
|
+
// The glass, the back of the liquid, the liquid, the bright line of its surface, the bubbles, a glint on the glass.
|
|
85
|
+
{...outline, attrs: {...outline.attrs, 'stroke-width': 2.2}}, inside,
|
|
86
|
+
node('tile', 'path', {}, inside), node('tile', 'path', {}, inside),
|
|
87
|
+
node('tile', 'path', {fill: 'none', 'stroke-linecap': 'round', 'stroke-width': 2.2}, inside),
|
|
88
|
+
...Array.from({length: BUBBLES}, () => node('tile', 'circle', {fill: 'none', 'stroke-width': 1.3}, inside)),
|
|
89
|
+
node('tile', 'path', {fill: 'none', stroke: '#fff', 'stroke-linecap': 'round', 'stroke-width': 3, d: 'M24,34Q27,24 36,20'}),
|
|
90
|
+
];
|
|
91
|
+
}
|
|
92
|
+
/** Where the fluid is heading this frame: the state's mood with the expression's surface on top. */
|
|
93
|
+
want() {
|
|
94
|
+
const mood = (!this.forced && MOODS[this.state]) || IDLE, face = (FACES[this.shown] || FACES.neutral)(this.t);
|
|
95
|
+
let level = mood.level ?? IDLE.level;
|
|
96
|
+
if (mood.fill) level = 0.08 + 0.84 * clamp(this.progress ?? (this.t * 0.22) % 1, 0, 1);
|
|
97
|
+
if (mood.grow) level = 0.56 * clamp(this.since / 1.2, 0, 1);
|
|
98
|
+
if (mood.drain) level = max(0.06, 0.56 - this.since * 0.4);
|
|
99
|
+
if (mood.pump) level += 0.1 * sin(this.t * 3);
|
|
100
|
+
return {...face, level, bubbles: clamp(mood.bubbles + face.boil * 0.6, 0, 1), speed: mood.speed * face.chop, lean: face.lean ?? mood.lean ?? null, mood};
|
|
101
|
+
}
|
|
102
|
+
drawTile(nodes, n, {dt, ink, dim}) {
|
|
103
|
+
const want = this.want(), f = this.fluid, tone = this.tone, p = this.p, roll = p.roll + this.g.roll;
|
|
104
|
+
if (dt === null) {
|
|
105
|
+
Object.assign(f, {level: want.level, curve: want.curve, wave: want.wave, chop: want.chop, tilt: want.tilt, bubbles: want.bubbles, speed: want.speed, slope: 0, vslope: 0, splash: 0});
|
|
106
|
+
f.lean = want.lean; f.mix = want.lean == null ? 0 : 1; this.lastRoll = roll;
|
|
107
|
+
} else {
|
|
108
|
+
f.level = ease(f.level, want.level, dt, 2.5);
|
|
109
|
+
for (const key of ['curve', 'wave', 'chop', 'tilt', 'bubbles', 'speed']) f[key] = ease(f[key], want[key], dt, 4);
|
|
110
|
+
if (want.lean != null) f.lean = want.lean;
|
|
111
|
+
f.mix = ease(f.mix, want.lean == null ? 0 : 1, dt, 3);
|
|
112
|
+
// The surface lags the glass: a spring on its slope, kicked by turning and by hops; it settles level.
|
|
113
|
+
const turned = roll - this.lastRoll; this.lastRoll = roll;
|
|
114
|
+
f.vslope += turned * 4 + (-SLOSH * f.slope - SLOSH_DAMP * f.vslope) * dt; f.slope = clamp(f.slope + f.vslope * dt, -0.5, 0.5);
|
|
115
|
+
f.splash = ease(f.splash, 0, dt, 1.6) + abs(this.vel.lift) * dt * 0.02 + abs(this.omega) * dt * 0.004;
|
|
116
|
+
f.phase += dt * f.speed * 3; f.rise += dt * (0.25 + 0.5 * f.speed);
|
|
117
|
+
}
|
|
118
|
+
n += 3;
|
|
119
|
+
// The glass: pale and tinted; the liquid: the Bot's colour, deeper behind.
|
|
120
|
+
const hue = (tone.hue + (((f.lean ?? tone.hue) - tone.hue + 540) % 360 - 180) * f.mix + 360) % 360;
|
|
121
|
+
const chroma = (tone.chroma ?? 0.15) * (0.7 + 0.3 * p.sat), light = tone.light ?? 0.74;
|
|
122
|
+
Object.assign(nodes[n++], {fill: `oklch(${min(0.97, light + 0.2).toFixed(3)} ${(chroma * 0.18).toFixed(3)} ${tone.hue})`, stroke: tone.edge ?? `oklch(${(light - 0.1).toFixed(3)} ${(chroma * 0.8).toFixed(3)} ${tone.hue})`});
|
|
123
|
+
n++;
|
|
124
|
+
// The surface, in the glass's own frame: level, minus the glass's roll so it stays level, plus slosh, the face, ripples.
|
|
125
|
+
const base = BOTTOM - f.level * (BOTTOM - TOP), slope = tan(clamp(-roll / 2 + f.slope + f.tilt, -0.9, 0.9)), blink = (1 - p.open) * 1.6;
|
|
126
|
+
const wave = f.wave + blink + min(6, f.splash * 40);
|
|
127
|
+
const surface = (x, shift = 0) => {
|
|
128
|
+
const u = (x - 50) / 40;
|
|
129
|
+
return base + slope * (x - 50) - f.curve * (u * u - 0.33) + wave * (0.65 * sin(u * 5.2 + f.phase + shift) + 0.35 * sin(u * 11 - f.phase * 1.7 + shift * 2));
|
|
130
|
+
};
|
|
131
|
+
const edge = (shift, lift) => {
|
|
132
|
+
let d = '';
|
|
133
|
+
for (let i = 0; i <= COLS; i++) { const x = LEFT + (RIGHT - LEFT) * i / COLS; d += (i ? 'L' : 'M') + fixed(x) + ',' + fixed(clamp(surface(x, shift) - lift, 2, 98)); }
|
|
134
|
+
return d;
|
|
135
|
+
};
|
|
136
|
+
const fillUnder = d => d + `L${RIGHT + 8},${BOTTOM + 14}L${LEFT - 8},${BOTTOM + 14}Z`;
|
|
137
|
+
Object.assign(nodes[n++], {d: fillUnder(edge(2.1, 2)), fill: `oklch(${max(0.3, light - 0.14).toFixed(3)} ${(chroma * 1.1).toFixed(3)} ${hue.toFixed(1)})`, opacity: fixed(0.9 * dim + 0.1)});
|
|
138
|
+
const top = edge(0, 0);
|
|
139
|
+
Object.assign(nodes[n++], {d: fillUnder(top), fill: `oklch(${light.toFixed(3)} ${chroma.toFixed(3)} ${hue.toFixed(1)})`});
|
|
140
|
+
Object.assign(nodes[n++], {d: top, stroke: `oklch(${min(0.96, light + 0.14).toFixed(3)} ${(chroma * 0.7).toFixed(3)} ${hue.toFixed(1)})`, opacity: fixed(0.8 * dim)});
|
|
141
|
+
// Bubbles rise from the bottom and burst at the surface.
|
|
142
|
+
const fizz = FIZZ[this.eyes] || FIZZ.bar, shown = f.bubbles * fizz.n;
|
|
143
|
+
for (let i = 0; i < BUBBLES; i++) {
|
|
144
|
+
const node = nodes[n++], seen = clamp(shown - i, 0, 1);
|
|
145
|
+
if (i >= fizz.n || seen <= 0.01) { Object.assign(node, {r: 0, opacity: 0}); continue; }
|
|
146
|
+
const u = (f.rise * (0.8 + 0.5 * pseudo(i)) + pseudo(i + 9)) % 1;
|
|
147
|
+
let x = 22 + 56 * pseudo(i + 3) + 2.5 * sin(f.rise * 6 + i);
|
|
148
|
+
if (want.mood.swirl) x = 50 + 22 * cos(f.rise * 3 + i * TAU / fizz.n);
|
|
149
|
+
if (want.mood.flow) x = 50 + want.mood.flow * (-28 + 56 * ((u + pseudo(i)) % 1));
|
|
150
|
+
const y = BOTTOM - 4 - u * (BOTTOM - 4 - surface(x)), r = fizz.r * (0.7 + 0.5 * pseudo(i + 5)) * (0.6 + 0.4 * u);
|
|
151
|
+
Object.assign(node, {cx: fixed(x), cy: fixed(y), r: fixed(r), stroke: `oklch(${min(0.97, light + 0.18).toFixed(3)} ${(chroma * 0.5).toFixed(3)} ${hue.toFixed(1)})`, opacity: fixed(seen * min(1, (1 - u) * 6) * dim)});
|
|
152
|
+
}
|
|
153
|
+
Object.assign(nodes[n++], {opacity: fixed(0.55 * dim)});
|
|
154
|
+
return n;
|
|
155
|
+
}
|
|
156
|
+
faceLayout() { return []; }
|
|
157
|
+
drawFace(nodes, n) { return n; }
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** A liquid model. Options as createMatrix's: identity, hue, chroma, light, dots, edge, shape, eyes, state, progress, size, emphasized. */
|
|
161
|
+
export const createLiquid = options => new Liquid(options);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Types for '@kodax-ai/botui/themes/liquid'.
|
|
2
|
+
|
|
3
|
+
import type {ColorName, EyeStyle, ExpressionName, IdentityPool, Theme} from '../../index.js';
|
|
4
|
+
import type {MatrixAvatar, MatrixOptions, MatrixShape, MatrixModel, MatrixTone} from '../matrix/index.js';
|
|
5
|
+
|
|
6
|
+
export type {Avatar, AvatarOptions, ColorName, EyeStyle, ExpressionName, GestureName, IdentityPool, StateName, StateAlias, StateGroup, Theme} from '../../index.js';
|
|
7
|
+
export type {MatrixOptions as LiquidOptions, MatrixShape as LiquidShape} from '../matrix/index.js';
|
|
8
|
+
export {setAttention, resolveState, STATES, STATE_GROUPS, ALIASES, COLORS, BIRTH_COLORS, HUES, EYE_STYLES, GESTURES} from '../../index.js';
|
|
9
|
+
export {SHAPES, GROUPS} from '../matrix/index.js';
|
|
10
|
+
|
|
11
|
+
export interface LiquidAvatar extends MatrixAvatar {}
|
|
12
|
+
export const LiquidAvatar: {new (options?: MatrixOptions): LiquidAvatar};
|
|
13
|
+
|
|
14
|
+
export const theme: Theme;
|
|
15
|
+
export function createAvatar(options?: MatrixOptions): LiquidAvatar;
|
|
16
|
+
export function identityFrom(identity: string, pool?: IdentityPool): {shape: MatrixShape; color: ColorName; eyes: EyeStyle};
|
|
17
|
+
export const EXPRESSIONS: Record<ExpressionName, Function>;
|
|
18
|
+
|
|
19
|
+
// The pure model: the matrix model with the tile as a glass, the surface of the liquid for a face.
|
|
20
|
+
export function createLiquid(options: MatrixTone & {identity?: string; shape: MatrixShape; eyes?: EyeStyle; state?: string; progress?: number | null; size?: number; emphasized?: boolean}): MatrixModel;
|
|
21
|
+
export const liquidShapes: readonly MatrixShape[];
|
|
22
|
+
export const liquidEyes: readonly EyeStyle[];
|
|
23
|
+
export const FACES: Record<ExpressionName, Function>;
|
|
24
|
+
export const MOODS: Record<string, {level: number | null; bubbles: number; speed: number; [key: string]: unknown}>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// BotUI liquid theme: a glass of the Bot's colour, its surface for a face.
|
|
2
|
+
//
|
|
3
|
+
// import {createAvatar} from './themes/liquid/index.js';
|
|
4
|
+
// const bot = createAvatar({identity: 'research-bot', size: 36});
|
|
5
|
+
// sidebar.append(bot.el);
|
|
6
|
+
// bot.setState('thinking');
|
|
7
|
+
|
|
8
|
+
import {LiquidAvatar} from './avatar.js';
|
|
9
|
+
import {COLORS, EYE_STYLES} from '../../core/identity.js';
|
|
10
|
+
import {STATES} from '../../core/states.js';
|
|
11
|
+
import {registerTheme} from '../../core/registry.js';
|
|
12
|
+
import {GESTURES} from '../../core/motion.js';
|
|
13
|
+
import {FACES} from './engine.js';
|
|
14
|
+
import {SHAPES} from '../matrix/engine.js';
|
|
15
|
+
|
|
16
|
+
export {LiquidAvatar, identityFrom} from './avatar.js';
|
|
17
|
+
export {COLORS, BIRTH_COLORS, HUES, EYE_STYLES} from '../../core/identity.js';
|
|
18
|
+
export {STATES, STATE_GROUPS, ALIASES, resolveState} from '../../core/states.js';
|
|
19
|
+
export {setAttention} from '../../core/core.js';
|
|
20
|
+
export {GESTURES} from '../../core/motion.js';
|
|
21
|
+
export {SHAPES, GROUPS, matrixShapes as liquidShapes} from '../matrix/engine.js';
|
|
22
|
+
export {createLiquid, liquidEyes, FACES, MOODS} from './engine.js';
|
|
23
|
+
/** The expressions, by name; here each one is a way the surface sits (see FACES). */
|
|
24
|
+
export const EXPRESSIONS = FACES;
|
|
25
|
+
|
|
26
|
+
/** Create an avatar. Options as MatrixAvatar's. Append `.el` to the page. */
|
|
27
|
+
export function createAvatar(options) { return new LiquidAvatar(options); }
|
|
28
|
+
|
|
29
|
+
/** This theme, in the form core/registry.js expects. */
|
|
30
|
+
export const theme = {
|
|
31
|
+
name: 'liquid', zh: '液面', create: createAvatar,
|
|
32
|
+
catalog: () => ({shapes: SHAPES, colors: Object.keys(COLORS), eyes: Object.keys(EYE_STYLES), states: Object.keys(STATES), gestures: Object.keys(GESTURES), expressions: Object.keys(FACES)}),
|
|
33
|
+
};
|
|
34
|
+
// Importing a theme makes it available to createAvatar({theme}) from the package entry.
|
|
35
|
+
registerTheme(theme);
|
package/themes/matrix/README.md
CHANGED
|
@@ -14,7 +14,7 @@ sidebar.append(bot.el);
|
|
|
14
14
|
bot.setState('thinking');
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
从总入口用:先 `import '@kodax-ai/botui/themes/matrix'`,再 `createAvatar({theme: 'matrix', ...})`(不写 `theme` 时是默认的立体家族)。自定义元素:同样先加载主题脚本,再写 `<bot-avatar theme="matrix" identity="research-bot"></bot-avatar>`。
|
|
18
18
|
|
|
19
19
|
## 与立体家族的差别
|
|
20
20
|
|
|
@@ -30,7 +30,7 @@ bot.setState('thinking');
|
|
|
30
30
|
| 转身 | 小角度时脸在屏上平移;转过约四分之一圈像卡片一样翻面,背面不显示脸 |
|
|
31
31
|
| 压扁 | 不到立体家族的三分之一。屏幕是硬的,阻塞和按住靠下沉、闭眼、点变暗来表现 |
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
动作和姿态表与立体家族共用 `core/motion.js`,所有主题动起来节奏一致。
|
|
34
34
|
|
|
35
35
|
## 纯模型
|
|
36
36
|
|
|
@@ -44,3 +44,5 @@ bot.setState('thinking');
|
|
|
44
44
|
| `avatar.js` | `MatrixAvatar`:DOM、交互、视线、水印 |
|
|
45
45
|
| `engine.js` | 纯模型:外形、点阵表情、屏幕动画、周边记号和粒子 |
|
|
46
46
|
| `playground.html` | 试玩页(与立体家族共用 `../solids/playground.js`) |
|
|
47
|
+
|
|
48
|
+
活点、字符脸、光标、光晕、液面、像素生物、手绘这七个主题都建在点阵上:继承 `Matrix` 和 `MatrixAvatar`,按需重写 `tileLayout` / `drawTile`(身体)、`faceLayout` / `drawFace`(脸)、`static shapes`(外形表)和 `createModel`。模型节点可以带 `parent` 嵌套(用于 `defs`、`clipPath`、渐变),名为 `text` 的属性是文本内容。改这里的公共部分时,所有主题一起跑 `tests/check.html`。
|
package/themes/matrix/avatar.js
CHANGED
|
@@ -4,29 +4,25 @@
|
|
|
4
4
|
// the solids' own; engine.js turns it into dots. A Bot keeps its solids colour and
|
|
5
5
|
// eye style here, so it looks like itself in either theme.
|
|
6
6
|
|
|
7
|
-
import {clamp,
|
|
7
|
+
import {clamp, pointer, world, join, leave, joined, prefersDark} from '../../core/core.js';
|
|
8
8
|
import {resolveState} from '../../core/states.js';
|
|
9
|
-
import {COLORS,
|
|
10
|
-
import {EYE_STYLES} from '../solids/face.js';
|
|
9
|
+
import {COLORS, EYE_STYLES, bornLook, shapeFor, WATERMARK, WATERMARK_FROM, CREDIT} from '../../core/identity.js';
|
|
11
10
|
import {createMatrix, matrixShapes} from './engine.js';
|
|
12
11
|
|
|
13
12
|
const NS = 'http://www.w3.org/2000/svg';
|
|
14
13
|
function el(name, attrs, parent) {
|
|
15
14
|
const node = document.createElementNS(NS, name);
|
|
16
|
-
|
|
15
|
+
// `text` is the node's text content; everything else is an attribute.
|
|
16
|
+
for (const key in attrs) if (key === 'text') node.textContent = attrs[key]; else node.setAttribute(key, attrs[key]);
|
|
17
17
|
if (parent) parent.append(node);
|
|
18
18
|
return node;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
|
-
* The look a Bot ID maps to:
|
|
23
|
-
* the
|
|
22
|
+
* The look a Bot ID maps to: one of `shapes` (the tile outlines by default), and
|
|
23
|
+
* the same colour and eye style as in every theme. `pool` can limit the choice: {shapes, colors, eyes}.
|
|
24
24
|
*/
|
|
25
|
-
export
|
|
26
|
-
const born = solidIdentity(identity, {colors: pool?.colors, eyes: pool?.eyes});
|
|
27
|
-
const kept = (pool?.shapes || []).filter(name => matrixShapes.includes(name)), shapes = kept.length ? kept : matrixShapes;
|
|
28
|
-
return {shape: shapes[hash(identity + '/shape') % shapes.length], color: born.color, eyes: born.eyes};
|
|
29
|
-
}
|
|
25
|
+
export const identityFrom = (identity, pool, shapes = matrixShapes) => ({shape: shapeFor(identity, shapes, pool), ...bornLook(identity, pool)});
|
|
30
26
|
|
|
31
27
|
// How a tile shows each colour: the saturated ones bright, the neutrals as themselves.
|
|
32
28
|
// A tile that would fade into the page gets an edge.
|
|
@@ -42,6 +38,10 @@ function toneOf(color, spec, dark) {
|
|
|
42
38
|
}
|
|
43
39
|
|
|
44
40
|
export class MatrixAvatar {
|
|
41
|
+
/** The host element's theme class; a theme built on this one sets its own. */
|
|
42
|
+
static className = 'botui-matrix';
|
|
43
|
+
/** The shapes this theme draws; a theme built on this one with other bodies sets its own. */
|
|
44
|
+
static shapes = matrixShapes;
|
|
45
45
|
/**
|
|
46
46
|
* Options as SolidAvatar's, except: `shape` is a tile outline from matrixShapes
|
|
47
47
|
* (squircle, circle, hex); `signals` and `eyeColor` do not apply, the screen
|
|
@@ -50,9 +50,9 @@ export class MatrixAvatar {
|
|
|
50
50
|
constructor(options = {}) {
|
|
51
51
|
this.identity = options.identity ?? 'bot';
|
|
52
52
|
this.pool = options.pool ?? null;
|
|
53
|
-
const born = identityFrom(this.identity
|
|
53
|
+
const born = this.identityFrom(this.identity);
|
|
54
54
|
this.size = Number(options.size ?? 36);
|
|
55
|
-
this.shape =
|
|
55
|
+
this.shape = this.constructor.shapes.includes(options.shape) ? options.shape : born.shape;
|
|
56
56
|
this.eyes = EYE_STYLES[options.eyes] ? options.eyes : born.eyes;
|
|
57
57
|
this.scheme = options.scheme ?? 'light'; this.emphasized = !!options.emphasized;
|
|
58
58
|
this.state = resolveState(options.state) || 'idle'; this.progress = options.progress ?? null;
|
|
@@ -64,7 +64,7 @@ export class MatrixAvatar {
|
|
|
64
64
|
this.paint(options.color, options.hue, born.color);
|
|
65
65
|
|
|
66
66
|
this.el = document.createElement('span');
|
|
67
|
-
this.el.className = 'botui-avatar
|
|
67
|
+
this.el.className = 'botui-avatar ' + this.constructor.className; this.el.botAvatar = this; this.el.dataset.state = this.state;
|
|
68
68
|
this.el.style.cssText = 'display:inline-block;line-height:0;vertical-align:middle;user-select:none;-webkit-user-select:none';
|
|
69
69
|
this.name(options.label);
|
|
70
70
|
this.svg = el('svg', {viewBox: '0 0 100 100', width: this.size, height: this.size, overflow: 'visible'}, this.el);
|
|
@@ -84,11 +84,11 @@ export class MatrixAvatar {
|
|
|
84
84
|
update(options) {
|
|
85
85
|
let rebuild = false;
|
|
86
86
|
if (options.identity !== undefined && options.identity !== this.identity) {
|
|
87
|
-
this.identity = options.identity; const born = identityFrom(this.identity
|
|
87
|
+
this.identity = options.identity; const born = this.identityFrom(this.identity);
|
|
88
88
|
Object.assign(this, {shape: born.shape, eyes: born.eyes}); this.paint(born.color); rebuild = true;
|
|
89
89
|
}
|
|
90
90
|
if (options.color !== undefined || options.hue !== undefined) { this.paint(options.color, options.hue, this.color); rebuild = true; }
|
|
91
|
-
if (
|
|
91
|
+
if (this.constructor.shapes.includes(options.shape) && options.shape !== this.shape) { this.shape = options.shape; rebuild = true; }
|
|
92
92
|
if (EYE_STYLES[options.eyes] && options.eyes !== this.eyes) { this.eyes = options.eyes; rebuild = true; }
|
|
93
93
|
if (options.scheme !== undefined && options.scheme !== this.scheme) { this.scheme = options.scheme; this.paint(this.color, this.spec.hue, this.color); rebuild = true; }
|
|
94
94
|
for (const key of ['track', 'playing', 'maxFrameRate', 'watermark']) if (options[key] !== undefined) this[key] = options[key];
|
|
@@ -151,6 +151,7 @@ export class MatrixAvatar {
|
|
|
151
151
|
settle() { this.draw(this.model.settle()); this.settled = true; }
|
|
152
152
|
|
|
153
153
|
// ---- internals ----
|
|
154
|
+
identityFrom(identity) { return identityFrom(identity, this.pool, this.constructor.shapes); }
|
|
154
155
|
name(label) {
|
|
155
156
|
if (label) { this.el.setAttribute('role', 'img'); this.el.setAttribute('aria-label', label); this.el.removeAttribute('aria-hidden'); }
|
|
156
157
|
else { this.el.removeAttribute('role'); this.el.removeAttribute('aria-label'); this.el.setAttribute('aria-hidden', 'true'); }
|
|
@@ -168,11 +169,15 @@ export class MatrixAvatar {
|
|
|
168
169
|
if (this.watermark !== false && this.size >= WATERMARK_FROM) { this.markNode.setAttribute('font-size', (clamp(this.size * 0.075, 5.5, 10) * 100 / this.size).toFixed(2)); this.markNode.removeAttribute('display'); }
|
|
169
170
|
else this.markNode.setAttribute('display', 'none');
|
|
170
171
|
}
|
|
172
|
+
/** The model behind the drawing; a theme built on this one returns its own. */
|
|
173
|
+
createModel(options) { return createMatrix(options); }
|
|
171
174
|
/** A fresh model and fresh nodes for the current look. The state carries over; the pose starts at rest. */
|
|
172
175
|
build() {
|
|
173
|
-
this.model =
|
|
176
|
+
this.model = this.createModel({identity: this.identity, ...this.tone, shape: this.shape, eyes: this.eyes, state: this.state, progress: this.progress, size: this.size, emphasized: this.emphasized});
|
|
174
177
|
for (const layer of Object.values(this.layers)) layer.replaceChildren();
|
|
175
|
-
|
|
178
|
+
// A layout entry may name an earlier entry as its `parent` (a gradient's stops, a clip's path).
|
|
179
|
+
const made = new Map();
|
|
180
|
+
this.nodes = this.model.layout.map(entry => { const node = el(entry.tag, entry.attrs, entry.parent ? made.get(entry.parent) : this.layers[entry.layer]); made.set(entry, node); return node; });
|
|
176
181
|
this.seen = this.nodes.map(() => ({}));
|
|
177
182
|
this.el.dataset.shape = this.shape;
|
|
178
183
|
this.marked();
|
|
@@ -182,7 +187,7 @@ export class MatrixAvatar {
|
|
|
182
187
|
this.layers.tile.setAttribute('transform', transform);
|
|
183
188
|
nodes.forEach((attrs, i) => {
|
|
184
189
|
const seen = this.seen[i];
|
|
185
|
-
for (const name in attrs) if (seen[name] !== attrs[name]) { seen[name] = attrs[name]; this.nodes[i].setAttribute(name, attrs[name]); }
|
|
190
|
+
for (const name in attrs) if (seen[name] !== attrs[name]) { seen[name] = attrs[name]; if (name === 'text') this.nodes[i].textContent = attrs[name]; else this.nodes[i].setAttribute(name, attrs[name]); }
|
|
186
191
|
});
|
|
187
192
|
}
|
|
188
193
|
listen() {
|