Ease between character poses in the agent header

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
TonioandPaperclip committed 2026-12-03 23:43:34 -08:00
1 parent 3c7e46509c
commit 3d4b46fcb8
4 files changed
+54 -7

No files matched your search

+29 -3
View File
@@ -1,7 +1,7 @@
// ClipLab runtime adapted for Paperclip; geometry/animation model remain upstream.
// See PROVENANCE.md and LICENSE.
import { CharacterRenderer } from './renderer.js'
import { sampleDefinition, animationDuration, type Definition } from './model.js'
import { sampleDefinition, animationDuration, faceLayers, mixFaceLayers, mixPose, type Sample, type Definition } from './model.js'
import { centeredGaze, easeGaze, pointerGaze } from './gaze.js'
export interface CharacterOptions {
animation?: string; followCursor?: boolean; followRotation?: boolean;
@@ -22,7 +22,27 @@ export function createCharacter(target: HTMLElement, definition: Definition, opt
const reduced = window.matchMedia('(prefers-reduced-motion: reduce)')
const coarse = window.matchMedia('(pointer: coarse)')
const canRun = () => !destroyed && visible && !document.hidden && !reduced.matches
const render = () => renderer.render({ ...definition.character, followCursor: options.followCursor !== false, followRotation: options.followRotation !== false }, sampleDefinition(definition, animation, elapsed), { cursor: gaze, reducedMotion: reduced.matches }, gaze)
let transition: { from: Sample; seconds: number } | undefined
function currentSample(): Sample {
const next = sampleDefinition(definition, animation, elapsed)
if (!transition) return next
if (elapsed >= transition.seconds || reduced.matches) {
transition = undefined
return next
}
const { from, seconds } = transition
const progress = elapsed / seconds
const t = progress * progress * (3 - 2 * progress)
return {
...next,
pose: mixPose(from.pose, next.pose, t),
faceLayers: mixFaceLayers(from.faceLayers ?? faceLayers(from.pose), next.faceLayers ?? faceLayers(next.pose), t),
blink: from.blink + (next.blink - from.blink) * t,
bob: from.bob + (next.bob - from.bob) * t,
breathe: from.breathe + (next.breathe - from.breathe) * t,
}
}
const render = () => renderer.render({ ...definition.character, followCursor: options.followCursor !== false, followRotation: options.followRotation !== false }, currentSample(), { cursor: gaze, reducedMotion: reduced.matches }, gaze)
const pointer = (event: PointerEvent) => { if (event.pointerType !== 'touch') goal = pointerGaze(event, target.getBoundingClientRect()) }
const leave = () => { goal = centeredGaze() }
function track(enabled: boolean) {
@@ -75,7 +95,13 @@ export function createCharacter(target: HTMLElement, definition: Definition, opt
} catch (error) { destroy(); throw error }
return {
setAnimation(id: string) { if (animation === id) return; animation = id; elapsed = 0; renderSafely() },
setAnimation(id: string, transitionSeconds = 0) {
if (animation === id) return
transition = transitionSeconds > 0 && playing && canRun()
? { from: currentSample(), seconds: transitionSeconds }
: undefined
animation = id; elapsed = 0; renderSafely()
},
setDefinition(value: Definition) { definition = value; renderSafely() },
seek(seconds: number) { elapsed = Math.max(0, seconds); renderSafely() },
pause() { playing = false; sync() },
+8 -3
View File
@@ -1,5 +1,6 @@
import { useEffect, useMemo, useRef, useState, useSyncExternalStore, type RefObject } from "react";
import { resolveAgentAppearance, type CharacterState } from "@paperclipai/shared";
import { parseCssTimeMs } from "@/components/task-chat/motion-tokens";
import { cn } from "@/lib/utils";
import { characterSlot, orgChartCharacterSlots } from "@/lib/agent-character-slot";
import { AgentAvatar, avatarSizeClasses, type AgentAvatarProps } from "./AgentAvatar";
@@ -16,6 +17,7 @@ function loadCharacterModules() {
export interface AgentCharacterProps extends Omit<AgentAvatarProps, "pose"> {
state?: CharacterState;
motion?: "auto" | "still";
transitionStates?: boolean;
trackingRegion?: RefObject<HTMLElement | null>;
trackingScope?: "region" | "page";
followCursor?: boolean;
@@ -23,7 +25,7 @@ export interface AgentCharacterProps extends Omit<AgentAvatarProps, "pose"> {
/** Org leadership cards may animate together, within a bounded canvas budget. */
animationGroup?: "hero" | "org-chart";
}
export function AgentCharacter({ agent, appearance, size = 256, state = "idle", muted = false, motion = "auto", trackingRegion, trackingScope = "region", followCursor = true, followRotation = true, animationGroup = "hero", className, label, name }: AgentCharacterProps) {
export function AgentCharacter({ agent, appearance, size = 256, state = "idle", muted = false, motion = "auto", transitionStates = false, trackingRegion, trackingScope = "region", followCursor = true, followRotation = true, animationGroup = "hero", className, label, name }: AgentCharacterProps) {
const identity = useMemo(() => resolveAgentAppearance(appearance ?? agent?.appearance, agent?.id), [appearance, agent?.appearance, agent?.id]);
const root = useRef<HTMLSpanElement>(null), host = useRef<HTMLSpanElement>(null), player = useRef<Player | null>(null);
const slotId = useRef(Symbol("agent-character"));
@@ -63,9 +65,12 @@ export function AgentCharacter({ agent, appearance, size = 256, state = "idle",
if (!player.current) return;
void loadCharacterModules().then(([, library]) => {
player.current?.setDefinition(library.characterDefinition(identity, muted));
player.current?.setAnimation(library.animationId(state));
const duration = transitionStates && root.current
? parseCssTimeMs(getComputedStyle(root.current).getPropertyValue("--motion-duration-fast")) / 1000
: 0;
player.current?.setAnimation(library.animationId(state), duration);
});
}, [identity, muted, state, ready]);
}, [identity, muted, state, ready, transitionStates]);
return <span ref={root} role={label ? "img" : undefined} aria-label={label} aria-hidden={label ? undefined : true}
className={cn("relative inline-block shrink-0", avatarSizeClasses[size], className)}>
<AgentAvatar agent={agent} appearance={identity} size={size < 256 ? 256 : size} name={name} pose={state} muted={muted} className={cn("size-full", ready && !identity.customAvatarAssetId && "invisible")} />
@@ -83,3 +83,19 @@ it("can follow the whole page while measuring gaze from the character and cleani
player.destroy();
expect(remove.mock.calls.some(([event]) => event === "pointermove")).toBe(true);
});
it("blends from the displayed pose and can reverse a sleep transition mid-frame", () => {
const player = create();
intersect([{ isIntersecting: true }]);
const pose = () => renderer.render.mock.calls.at(-1)![1].pose;
const awake = pose();
player.setAnimation("sleepy", 0.16);
expect(pose()).toEqual(awake);
const [id, frame] = [...frames][0];
frames.delete(id);
frame(performance.now() + 80);
const midway = pose();
expect(midway).not.toEqual(awake);
player.setAnimation("idle", 0.16);
expect(pose()).toEqual(midway);
});
+1 -1
View File
@@ -876,7 +876,7 @@ export function AgentDetail() {
{/* Header */}
<header className="agent-character-header flex items-center gap-2">
<div role="img" aria-label={`${agent.name} avatar`} className="relative shrink-0">
<AgentCharacter agent={agent} state={characterState === "rest" ? "idle" : characterState} size={128} className="agent-header-character" trackingScope="page" followCursor />
<AgentCharacter agent={agent} state={characterState === "rest" ? "idle" : characterState} size={128} className="agent-header-character" transitionStates trackingScope="page" followCursor />
{sleeping && !agent.appearance?.customAvatarAssetId && <SleepingZs />}
</div>
<div className="min-w-0 flex-1">