Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- `@botharness/pixel-avatar` 0.2.0: opt-in `mouthLayers` adds saved/closed/half-open/open speech features to every head turn. Existing recipes, static snapshots and default golden output remain unchanged; text pacing stays with the consumer.
- `@botharness/pixel-morph`: one morph that throws no longer stops the shared animation loop for the others, and a morph started from inside a frame no longer starts a second frame chain.

## 0.2.0
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ The engine does not know what it is drawing. Here it morphs one generated face s

## Custom avatars

Pass `{ mouthLayers: true }` to `pixelAvatarSvg` or `pixelFigure` to opt into speech features. Each head pose contains four `[data-avatar-mouth]` groups: `saved`, `closed`, `half-open`, and `open`. Show one state in every pose at a time; return to `saved` when speech ends or is cancelled. Layers include shared brows, nose and cheeks so their paint order stays exact. Eyes, blink, glasses and head turns remain independent. Text pacing and reduced-motion policy belong to the consuming app. Without the option, every existing output stays byte-for-byte unchanged; recipes and saved snapshots do not change.

A recipe is plain JSON (`head`, `hair`, `eyes`, `outfit`, `accessory`, colours…). `AVATAR_PARTS`, `AVATAR_SWATCHES` and `AVATAR_PRESETS` list every option, so an app can build its own avatar editor on top; `isPixelAvatarRecipe` validates what users save.

Hosts add their own state symbols with `symbolArtCells(rows, color)`, which draws 24×24 character art (`A` body, `H` highlight) in the built-in style.
Expand Down
4 changes: 4 additions & 0 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@

用法、原理和贡献方式见 [README.md](README.md)。

## 说话嘴型

给 `pixelAvatarSvg` 或 `pixelFigure` 传入 `{ mouthLayers: true }`,即可按需生成嘴型图层。每个转头角度都包含四组 `[data-avatar-mouth]`:`saved`(原本的表情)、`closed`(闭合)、`half-open`(半开)、`open`(张开)。每个角度同时只显示一个状态;说话结束或取消时恢复 `saved`。图层保留共享的眉毛、鼻子和脸颊,以维持原有像素绘制顺序;眼神、眨眼、眼镜和转头保持独立。文本节奏与减少动效策略由使用方负责。不启用该选项时,现有输出逐字节保持不变,配方与已保存的快照不变。

已保存的头像不能变:`packages/avatar/test/fixtures/botharness-golden.json` 锁住了每个选项和种子的输出。
2 changes: 1 addition & 1 deletion packages/avatar/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@botharness/pixel-avatar",
"version": "0.1.0",
"version": "0.2.0",
"description": "Seeded, editable 32×32 chibi pixel avatars with state symbols that morph in and out",
"keywords": [
"agent-ui",
Expand Down
58 changes: 41 additions & 17 deletions packages/avatar/src/figure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const TONGUE = '#ef6f84';
const GOLD = '#efb93f';

export type PixelGrid = Grid;
export type PixelMouthState = 'saved' | 'closed' | 'half-open' | 'open';
export interface PixelFigureOptions {
mouthLayers?: boolean;
}
const blank = (): Grid => Array.from({ length: N }, () => Array<Cell>(N).fill(undefined));
const ellipse =
(cx: number, cy: number, rx: number, ry: number): Mask =>
Expand Down Expand Up @@ -685,6 +689,7 @@ export function pixelTileColor(hair: string): string {
export function pixelFigure(
recipe: Recipe,
yawDeg: number,
options: PixelFigureOptions = {},
): { tile: string; body: string; head: string; cells: PixelCell[] } {
const yaw = (yawDeg * Math.PI) / 180;
const sinY = Math.sin(yaw);
Expand Down Expand Up @@ -848,22 +853,41 @@ export function pixelFigure(
if (recipe.nose === 'dot') sprite(features, ['N'], cx, 18 + h, { N: shade(skin, 0.78) });
if (recipe.nose === 'button') sprite(features, ['N'], cx, 18 + h, { N: shade(skin, 0.88) });
if (recipe.nose === 'line') sprite(features, ['N', 'N'], cx, 17 + h, { N: shade(skin, 0.82) });
const mouth = MOUTHS[recipe.mouth];
const mx = C - 0.5 + fs * 0.6;
sprite(features, mouth, Math.round(mx - (mouth[0]!.length - 1) / 2), 20 + h, {
K: MOUTH,
M: MOUTH_INSIDE,
T: TONGUE,
W: WHITE,
});
if (recipe.cheeks === 'blush') {
sprite(features, ['PP'], left.x - 1, 19 + h, { P: mix(BLUSH, skin, 0.25) });
sprite(features, ['PP'], right.x + right.w - 1, 19 + h, { P: mix(BLUSH, skin, 0.25) });
}
if (recipe.cheeks === 'freckles') {
sprite(features, ['F.F'], left.x, 19 + h, { F: shade(skin, 0.7) });
sprite(features, ['F.F'], right.x + 1, 19 + h, { F: shade(skin, 0.7) });
}
const withMouth = (mouth: readonly string[]) => {
const layer = features.map((row) => [...row]);
sprite(layer, mouth, Math.round(mx - (mouth[0]!.length - 1) / 2), 20 + h, {
K: MOUTH,
M: MOUTH_INSIDE,
T: TONGUE,
W: WHITE,
});
if (recipe.cheeks === 'blush') {
sprite(layer, ['PP'], left.x - 1, 19 + h, { P: mix(BLUSH, skin, 0.25) });
sprite(layer, ['PP'], right.x + right.w - 1, 19 + h, { P: mix(BLUSH, skin, 0.25) });
}
if (recipe.cheeks === 'freckles') {
sprite(layer, ['F.F'], left.x, 19 + h, { F: shade(skin, 0.7) });
sprite(layer, ['F.F'], right.x + 1, 19 + h, { F: shade(skin, 0.7) });
}
return layer;
};
const savedFeatures = withMouth(MOUTHS[recipe.mouth]);
const featureMarkup = options.mouthLayers
? (
[
['saved', MOUTHS[recipe.mouth]],
['closed', ['KKK']],
['half-open', ['KKK', 'MMM']],
['open', ['.K.', 'KMK', '.K.']],
] satisfies readonly [PixelMouthState, readonly string[]][]
)
.map(
([state, rows]) =>
`<g data-avatar-mouth="${state}" opacity="${state === 'saved' ? 1 : 0}">${rects(withMouth(rows))}</g>`,
)
.join('')
: rects(savedFeatures);
const frame = (fx: number, w: number, round: boolean) =>
paint(
glasses,
Expand Down Expand Up @@ -908,7 +932,7 @@ export function pixelFigure(
const cells: PixelCell[] = [];
for (let y = 0; y < N; y++)
for (let x = 0; x < N; x++) {
const c = glasses[y]![x] ?? eyes[y]![x] ?? features[y]![x] ?? full[y]![x];
const c = glasses[y]![x] ?? eyes[y]![x] ?? savedFeatures[y]![x] ?? full[y]![x];
if (c) cells.push({ x, y, c });
}
return {
Expand All @@ -917,7 +941,7 @@ export function pixelFigure(
body: `<g class="bh-illustrated-body">${rects(bodyCells)}</g>`,
head: [
`<g class="bh-illustrated-head">${rects(headCells)}`,
`<g class="bh-illustrated-face">${rects(features)}`,
`<g class="bh-illustrated-face">${featureMarkup}`,
`<g class="bh-illustrated-gaze">${rects(eyes)}</g>`,
`<g class="bh-illustrated-blink" opacity="0">${rects(closed)}</g>`,
`${rects(glasses)}</g></g>`,
Expand Down
8 changes: 7 additions & 1 deletion packages/avatar/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
export type { PixelCell } from '@botharness/pixel-morph';
export { pixelFigure, pixelTileColor, type PixelGrid } from './figure.js';
export {
pixelFigure,
pixelTileColor,
type PixelGrid,
type PixelFigureOptions,
type PixelMouthState,
} from './figure.js';
export {
AVATAR_COLORS,
AVATAR_HAIR_PARTS,
Expand Down
5 changes: 3 additions & 2 deletions packages/avatar/src/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const YAW = { front: 0, left: -25, right: 25 } as const;
export const AVATAR_TURNS = [-14, -7, 7, 14] as const;

export interface PixelAvatarSvgOptions {
mouthLayers?: boolean;
/** Extra yaw offsets to pre-render as hidden `[data-avatar-turn]` layers. */
turns?: readonly number[];
/** Class prefix for the rig layers (`-body`, `-head`, `-face`, `-gaze`, `-blink`). */
Expand All @@ -25,10 +26,10 @@ export function pixelAvatarSvg(
options: PixelAvatarSvgOptions = {},
): string {
if (!isPixelAvatarRecipe(recipe)) throw new Error('invalid Avatar recipe');
const base = pixelFigure(recipe, YAW[recipe.pose]);
const base = pixelFigure(recipe, YAW[recipe.pose], options);
const turns = (options.turns ?? [])
.map((delta) => {
const figure = pixelFigure(recipe, YAW[recipe.pose] + delta);
const figure = pixelFigure(recipe, YAW[recipe.pose] + delta, options);
const layers = `${figure.body}${figure.head}`.replaceAll(
'class="bh-illustrated-',
'data-turn-part="',
Expand Down
44 changes: 44 additions & 0 deletions packages/avatar/test/speech.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, expect, it } from 'vitest';
import {
AVATAR_PARTS,
AVATAR_PRESETS,
AVATAR_TURNS,
DEFAULT_RECIPE,
detailedRecipe,
pixelAvatarSvg,
type PixelAvatarRecipe,
} from '../src/index.js';

describe('optional speech mouth rig', () => {
it('provides saved, closed, half-open and open feature layers at every head turn', () => {
const svg = pixelAvatarSvg(DEFAULT_RECIPE, { turns: AVATAR_TURNS, mouthLayers: true });
for (const state of ['saved', 'closed', 'half-open', 'open']) {
expect(svg.match(new RegExp(`data-avatar-mouth="${state}"`, 'gu'))).toHaveLength(5);
}
expect(svg).toContain('data-avatar-mouth="saved" opacity="1"');
expect(svg).toContain('data-avatar-mouth="open" opacity="0"');
expect(pixelAvatarSvg(DEFAULT_RECIPE)).not.toContain('data-avatar-mouth');
});

it('restores exact saved markup across catalog faces, poses and detailed geometry', () => {
const recipes: PixelAvatarRecipe[] = [
...AVATAR_PRESETS,
...AVATAR_PARTS.mouth.flatMap((mouth) =>
AVATAR_PARTS.pose.flatMap((pose) => [
{ ...DEFAULT_RECIPE, mouth, pose },
{ ...detailedRecipe(DEFAULT_RECIPE), mouth, pose, height: 1, spacing: -1 },
]),
),
];
for (const recipe of recipes) {
const snapshot = JSON.stringify(recipe);
const normal = pixelAvatarSvg(recipe, { turns: AVATAR_TURNS });
const speaking = pixelAvatarSvg(recipe, { turns: AVATAR_TURNS, mouthLayers: true });
const restored = speaking
.replace(/<g data-avatar-mouth="(?:closed|half-open|open)" opacity="0">.*?<\/g>/gu, '')
.replace(/<g data-avatar-mouth="saved" opacity="1">(.*?)<\/g>/gu, '$1');
expect(restored).toBe(normal);
expect(JSON.stringify(recipe)).toBe(snapshot);
}
});
});
Loading