agent-robot-avatar 0.3.2 → 0.6.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.
@@ -1,152 +1,152 @@
1
- import AgentRobotAvatar, { registerAvatarExtension } from './agent-robot-avatar-extension-host.js';
2
- import { DEFAULT_HEAD_HEIGHT_SCALE, flattenHeadPoints } from './agent-robot-avatar-geometry.js';
3
-
4
- const SVG_NS = 'http://www.w3.org/2000/svg';
5
- const ANTENNA_DEFAULTS = Object.freeze({
6
- x: 120,
7
- y: 12,
8
- radius: 15,
9
- stiffness: 180,
10
- damping: 5.6,
11
- });
12
-
13
- const runtimeWindow = typeof window !== 'undefined' ? window : null;
14
- const antennaConfig = (runtimeWindow?.AgentRobotAvatarAntennaConfig && typeof runtimeWindow.AgentRobotAvatarAntennaConfig === 'object')
15
- ? runtimeWindow.AgentRobotAvatarAntennaConfig
16
- : {};
17
- for (const [key, value] of Object.entries(ANTENNA_DEFAULTS)) {
18
- if (!Number.isFinite(Number(antennaConfig[key]))) antennaConfig[key] = value;
19
- }
20
- if (runtimeWindow) {
21
- runtimeWindow.AgentRobotAvatarAntennaConfig = antennaConfig;
22
- runtimeWindow.AgentRobotAvatarAntennaDefaults = ANTENNA_DEFAULTS;
23
- }
24
-
25
- function configNumber(key, fallback, min, max) {
26
- const value = Number(antennaConfig[key]);
27
- if (!Number.isFinite(value)) return fallback;
28
- return Math.max(min, Math.min(max, value));
29
- }
30
-
31
- function antennaHome() {
32
- return {
33
- x: configNumber('x', ANTENNA_DEFAULTS.x, 60, 180),
34
- y: configNumber('y', ANTENNA_DEFAULTS.y, -40, 60),
35
- };
36
- }
37
-
38
- function flattenHead(instance) {
39
- if (instance._headFlattenedR32 || !instance._headShape || !instance._baseHeadPoints?.length) return;
40
-
41
- const flattened = flattenHeadPoints(instance._baseHeadPoints, DEFAULT_HEAD_HEIGHT_SCALE);
42
- const path = typeof instance._pointsToPath === 'function'
43
- ? instance._pointsToPath(flattened)
44
- : `M ${flattened.map((point, index) => `${index ? 'L ' : ''}${point.x.toFixed(2)} ${point.y.toFixed(2)}`).join(' ')} Z`;
45
-
46
- instance._baseHeadPoints = flattened;
47
- instance._baseHeadPathD = path;
48
- instance._headShape.setAttribute('d', path);
49
- instance._headFlattenedR32 = true;
50
- }
51
-
52
- function ensureAntenna(instance, now) {
53
- flattenHead(instance);
54
- if (instance._antennaDot?.isConnected && instance._antennaSpring) return true;
55
-
56
- const svg = instance.shadowRoot?.querySelector('svg');
57
- if (!svg || !instance._headMotion) return false;
58
-
59
- const home = antennaHome();
60
- let dot = instance.shadowRoot.getElementById('antennaDot');
61
- if (!dot) {
62
- dot = document.createElementNS(SVG_NS, 'circle');
63
- dot.id = 'antennaDot';
64
- dot.setAttribute('cx', String(home.x));
65
- dot.setAttribute('cy', String(home.y));
66
- dot.setAttribute('r', String(configNumber('radius', ANTENNA_DEFAULTS.radius, 2, 40)));
67
- dot.setAttribute('fill', instance._head?.getAttribute('fill') || '#08090b');
68
- dot.setAttribute('aria-hidden', 'true');
69
- dot.style.pointerEvents = 'none';
70
- svg.appendChild(dot);
71
- }
72
-
73
- instance._antennaDot = dot;
74
- instance._antennaSpring = {
75
- x: home.x,
76
- y: home.y,
77
- vx: 0,
78
- vy: 0,
79
- last: now,
80
- initialized: false,
81
- };
82
- return true;
83
- }
84
-
85
- function antennaTarget(instance) {
86
- const svg = instance.shadowRoot?.querySelector('svg');
87
- const headMatrix = instance._headMotion?.getScreenCTM?.();
88
- const svgMatrix = svg?.getScreenCTM?.();
89
- const home = antennaHome();
90
- if (!svg || !headMatrix || !svgMatrix || typeof DOMPoint === 'undefined') return home;
91
-
92
- try {
93
- const screenPoint = new DOMPoint(home.x, home.y).matrixTransform(headMatrix);
94
- const localPoint = screenPoint.matrixTransform(svgMatrix.inverse());
95
- return { x: localPoint.x, y: localPoint.y };
96
- } catch (_) {
97
- return home;
98
- }
99
- }
100
-
101
- function updateAntenna(instance, now) {
102
- if (!ensureAntenna(instance, now)) return;
103
-
104
- const spring = instance._antennaSpring;
105
- const dot = instance._antennaDot;
106
- const target = antennaTarget(instance);
107
-
108
- if (instance._isReducedMotion?.()) {
109
- spring.x = target.x;
110
- spring.y = target.y;
111
- spring.vx = 0;
112
- spring.vy = 0;
113
- spring.last = now;
114
- spring.initialized = true;
115
- } else {
116
- if (!spring.initialized) {
117
- spring.x = target.x;
118
- spring.y = target.y;
119
- spring.last = now;
120
- spring.initialized = true;
121
- }
122
-
123
- const dt = Math.max(0.001, Math.min(0.05, (now - spring.last) / 1000 || 0.033));
124
- spring.last = now;
125
-
126
- const stiffness = configNumber('stiffness', ANTENNA_DEFAULTS.stiffness, 5, 300);
127
- const damping = configNumber('damping', ANTENNA_DEFAULTS.damping, 0.5, 40);
128
- const ax = (target.x - spring.x) * stiffness - spring.vx * damping;
129
- const ay = (target.y - spring.y) * stiffness - spring.vy * damping;
130
- spring.vx += ax * dt;
131
- spring.vy += ay * dt;
132
- spring.x += spring.vx * dt;
133
- spring.y += spring.vy * dt;
134
- }
135
-
136
- dot.setAttribute('cx', spring.x.toFixed(2));
137
- dot.setAttribute('cy', spring.y.toFixed(2));
138
- dot.setAttribute('r', configNumber('radius', ANTENNA_DEFAULTS.radius, 2, 40).toFixed(2));
139
- dot.setAttribute('fill', instance._head?.getAttribute('fill') || '#08090b');
140
- }
141
-
142
- function drawAntenna(now) {
143
- updateAntenna(this, now);
144
- }
145
-
146
- registerAvatarExtension({
147
- name: 'antenna',
148
- draw: drawAntenna,
149
- });
150
-
151
- export { AgentRobotAvatar, ANTENNA_DEFAULTS };
152
- export default AgentRobotAvatar;
1
+ import AgentRobotAvatar, { registerAvatarExtension } from './agent-robot-avatar-extension-host.js';
2
+ import { DEFAULT_HEAD_HEIGHT_SCALE, flattenHeadPoints } from './agent-robot-avatar-geometry.js';
3
+
4
+ const SVG_NS = 'http://www.w3.org/2000/svg';
5
+ const ANTENNA_DEFAULTS = Object.freeze({
6
+ x: 120,
7
+ y: 12,
8
+ radius: 15,
9
+ stiffness: 180,
10
+ damping: 5.6,
11
+ });
12
+
13
+ const runtimeWindow = typeof window !== 'undefined' ? window : null;
14
+ const antennaConfig = (runtimeWindow?.AgentRobotAvatarAntennaConfig && typeof runtimeWindow.AgentRobotAvatarAntennaConfig === 'object')
15
+ ? runtimeWindow.AgentRobotAvatarAntennaConfig
16
+ : {};
17
+ for (const [key, value] of Object.entries(ANTENNA_DEFAULTS)) {
18
+ if (!Number.isFinite(Number(antennaConfig[key]))) antennaConfig[key] = value;
19
+ }
20
+ if (runtimeWindow) {
21
+ runtimeWindow.AgentRobotAvatarAntennaConfig = antennaConfig;
22
+ runtimeWindow.AgentRobotAvatarAntennaDefaults = ANTENNA_DEFAULTS;
23
+ }
24
+
25
+ function configNumber(key, fallback, min, max) {
26
+ const value = Number(antennaConfig[key]);
27
+ if (!Number.isFinite(value)) return fallback;
28
+ return Math.max(min, Math.min(max, value));
29
+ }
30
+
31
+ function antennaHome() {
32
+ return {
33
+ x: configNumber('x', ANTENNA_DEFAULTS.x, 60, 180),
34
+ y: configNumber('y', ANTENNA_DEFAULTS.y, -40, 60),
35
+ };
36
+ }
37
+
38
+ function flattenHead(instance) {
39
+ if (instance._headFlattenedR32 || !instance._headShape || !instance._baseHeadPoints?.length) return;
40
+
41
+ const flattened = flattenHeadPoints(instance._baseHeadPoints, DEFAULT_HEAD_HEIGHT_SCALE);
42
+ const path = typeof instance._pointsToPath === 'function'
43
+ ? instance._pointsToPath(flattened)
44
+ : `M ${flattened.map((point, index) => `${index ? 'L ' : ''}${point.x.toFixed(2)} ${point.y.toFixed(2)}`).join(' ')} Z`;
45
+
46
+ instance._baseHeadPoints = flattened;
47
+ instance._baseHeadPathD = path;
48
+ instance._headShape.setAttribute('d', path);
49
+ instance._headFlattenedR32 = true;
50
+ }
51
+
52
+ function ensureAntenna(instance, now) {
53
+ flattenHead(instance);
54
+ if (instance._antennaDot?.isConnected && instance._antennaSpring) return true;
55
+
56
+ const svg = instance.shadowRoot?.querySelector('svg');
57
+ if (!svg || !instance._headMotion) return false;
58
+
59
+ const home = antennaHome();
60
+ let dot = instance.shadowRoot.getElementById('antennaDot');
61
+ if (!dot) {
62
+ dot = document.createElementNS(SVG_NS, 'circle');
63
+ dot.id = 'antennaDot';
64
+ dot.setAttribute('cx', String(home.x));
65
+ dot.setAttribute('cy', String(home.y));
66
+ dot.setAttribute('r', String(configNumber('radius', ANTENNA_DEFAULTS.radius, 2, 40)));
67
+ dot.setAttribute('fill', instance._head?.getAttribute('fill') || '#08090b');
68
+ dot.setAttribute('aria-hidden', 'true');
69
+ dot.style.pointerEvents = 'auto';
70
+ svg.appendChild(dot);
71
+ }
72
+
73
+ instance._antennaDot = dot;
74
+ instance._antennaSpring = {
75
+ x: home.x,
76
+ y: home.y,
77
+ vx: 0,
78
+ vy: 0,
79
+ last: now,
80
+ initialized: false,
81
+ };
82
+ return true;
83
+ }
84
+
85
+ function antennaTarget(instance) {
86
+ const svg = instance.shadowRoot?.querySelector('svg');
87
+ const headMatrix = instance._headMotion?.getScreenCTM?.();
88
+ const svgMatrix = svg?.getScreenCTM?.();
89
+ const home = antennaHome();
90
+ if (!svg || !headMatrix || !svgMatrix || typeof DOMPoint === 'undefined') return home;
91
+
92
+ try {
93
+ const screenPoint = new DOMPoint(home.x, home.y).matrixTransform(headMatrix);
94
+ const localPoint = screenPoint.matrixTransform(svgMatrix.inverse());
95
+ return { x: localPoint.x, y: localPoint.y };
96
+ } catch (_) {
97
+ return home;
98
+ }
99
+ }
100
+
101
+ function updateAntenna(instance, now) {
102
+ if (!ensureAntenna(instance, now)) return;
103
+
104
+ const spring = instance._antennaSpring;
105
+ const dot = instance._antennaDot;
106
+ const target = antennaTarget(instance);
107
+
108
+ if (instance._isReducedMotion?.()) {
109
+ spring.x = target.x;
110
+ spring.y = target.y;
111
+ spring.vx = 0;
112
+ spring.vy = 0;
113
+ spring.last = now;
114
+ spring.initialized = true;
115
+ } else {
116
+ if (!spring.initialized) {
117
+ spring.x = target.x;
118
+ spring.y = target.y;
119
+ spring.last = now;
120
+ spring.initialized = true;
121
+ }
122
+
123
+ const dt = Math.max(0.001, Math.min(0.05, (now - spring.last) / 1000 || 0.033));
124
+ spring.last = now;
125
+
126
+ const stiffness = configNumber('stiffness', ANTENNA_DEFAULTS.stiffness, 5, 300);
127
+ const damping = configNumber('damping', ANTENNA_DEFAULTS.damping, 0.5, 40);
128
+ const ax = (target.x - spring.x) * stiffness - spring.vx * damping;
129
+ const ay = (target.y - spring.y) * stiffness - spring.vy * damping;
130
+ spring.vx += ax * dt;
131
+ spring.vy += ay * dt;
132
+ spring.x += spring.vx * dt;
133
+ spring.y += spring.vy * dt;
134
+ }
135
+
136
+ dot.setAttribute('cx', spring.x.toFixed(2));
137
+ dot.setAttribute('cy', spring.y.toFixed(2));
138
+ dot.setAttribute('r', configNumber('radius', ANTENNA_DEFAULTS.radius, 2, 40).toFixed(2));
139
+ dot.setAttribute('fill', instance._head?.getAttribute('fill') || '#08090b');
140
+ }
141
+
142
+ function drawAntenna(now) {
143
+ updateAntenna(this, now);
144
+ }
145
+
146
+ registerAvatarExtension({
147
+ name: 'antenna',
148
+ draw: drawAntenna,
149
+ });
150
+
151
+ export { AgentRobotAvatar, ANTENNA_DEFAULTS };
152
+ export default AgentRobotAvatar;