craft-library 0.7.2 → 0.9.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/dist/orb.js ADDED
@@ -0,0 +1,381 @@
1
+ /*! Sara Lobo Library · craft-library 0.9.0 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
2
+ // Fold orb (Dobra) · Craft Library, by Sara Lobo. orb.css and orb.js from the library, unchanged, around a mount.
3
+ import { css } from './style.js';
4
+ const CSS = "/* Orb · Sara Lobo Library\n orb.js draws the orb and its ground on a canvas that fills the element. Give the element a size (it is a block,\n 100% wide; the height is yours to set, or --orb-height). */\n\n.orb {\n position: relative;\n display: block;\n width: 100%;\n height: var(--orb-height, 420px);\n overflow: hidden;\n}\n.orb-canvas {\n position: absolute;\n inset: 0;\n width: 100%;\n height: 100%;\n display: block;\n}\n";
5
+ let api;
6
+ function engine() {
7
+ if (api) return api;
8
+ const window = {};
9
+ /* Orb · Sara Lobo Library
10
+ An AI assistant orb that pays attention without a face. A sphere of clear glass with two twisted inner folds of
11
+ light, ray-traced in one WebGL shader (no dependencies). It turns its fold and its rim toward the caret while you
12
+ type, its folds ripple with a voice, it defocuses into a soft spectral blur while it thinks.
13
+ Three tones, siblings in different colours: 'steel' (mid grey, the orb glows white), 'mist' (a light ground, the orb
14
+ is clear glass that refracts it) and 'blue' (night blue, the orb glows blue). It fills its box and paints its own
15
+ ground.
16
+ var api = orb(element, { tone: 'steel', state: 'idle' });
17
+ api.set('typing' | 'listening' | 'thinking' | 'speaking' | 'idle'); api.tone('mist');
18
+ api.follow(inputElement); // looks at its caret while it has focus, reacts to each keystroke
19
+ api.level(0.6); // drive listening / speaking from your own audio (0–1); api.level(null): built-in voice
20
+ api.pulse(); api.lookAt(clientX, clientY); api.destroy();
21
+ Every motion runs on its own dt-based spring, so any change can interrupt any other without a jump. */
22
+ (function () {
23
+ 'use strict';
24
+
25
+ var TONES = {
26
+ steel: { top: [0.215, 0.22, 0.232], bot: [0.062, 0.064, 0.07], floor: [0.13, 0.135, 0.145], shadow: 0, vig: 0.32, ink: 0, disp: 0.65, halo: 0.75, orb: [0.84, 0.86, 0.90], rim: [1, 1, 1], haloC: [0.42, 0.43, 0.47] },
27
+ mist: { top: [0.948, 0.95, 0.958], bot: [0.795, 0.80, 0.815], floor: [0.05, 0.05, 0.055], shadow: 0.17, vig: 0.12, ink: 1, disp: 1.0, halo: 0, orb: [0.66, 0.70, 0.78], rim: [1, 1, 1], haloC: [0, 0, 0] },
28
+ blue: { top: [0.008, 0.016, 0.046], bot: [0.003, 0.006, 0.018], floor: [0.03, 0.14, 0.58], shadow: 0, vig: 0, ink: 0, disp: 1.0, halo: 1, orb: [0.46, 0.64, 1.0], rim: [0.5, 0.74, 1.0], haloC: [0.10, 0.30, 1.0] }
29
+ };
30
+ var STATES = { idle: 1, typing: 1, listening: 1, thinking: 1, speaking: 1 };
31
+
32
+ var VS = 'attribute vec2 p;void main(){gl_Position=vec4(p,0.0,1.0);}';
33
+ var FS = [
34
+ 'precision highp float;',
35
+ 'uniform vec2 uRes,uC;uniform float uR;',
36
+ 'uniform vec3 uTop,uBot,uFloor;uniform float uShadow,uVig;',
37
+ 'uniform float uT,uAttend,uLevel,uPulse,uBlur,uPhase,uFill,uSpeak,uInk,uDispK,uHaloK;',
38
+ 'uniform vec3 uBands,uGaze,uLight,uOrbCol,uRimCol,uHaloCol;uniform mat3 uRot;',
39
+ 'float sq(float x){return x*x;}',
40
+ 'float hash12(vec2 p){vec3 p3=fract(vec3(p.xyx)*.1031);p3+=dot(p3,p3.yzx+33.33);return fract((p3.x+p3.y)*p3.z);}',
41
+ // the ground: a vertical gradient, a soft floor light, a vignette and, on light grounds, a contact shadow
42
+ 'vec3 ground(vec2 f){vec2 uv=f/uRes;vec3 c=mix(uBot,uTop,smoothstep(0.0,0.85,uv.y));float bx=uv.x-0.5;',
43
+ ' c+=uFloor*exp(-sq(uv.y/0.2))*(0.55+0.45*exp(-sq(bx/0.45)));c*=1.0-uVig*sq(bx*1.5);',
44
+ ' vec2 q=(f-uC)/uR;c*=1.0-uShadow*exp(-sq((q.y+1.5)/0.17)-sq(q.x/0.72));return c;}',
45
+ 'vec3 tm(vec3 x){return 1.0-exp(-max(x,0.0)*1.1);}',
46
+ 'float Fp(float f,float S){return sign(f)*(1.0-exp(-S*abs(f)))/S;}',
47
+ 'mat2 rot(float a){float c=cos(a),s=sin(a);return mat2(c,-s,s,c);}',
48
+ // the two folds: the sphere's skin turned into itself, twisted along an axis
49
+ 'float sheetA(vec3 q){vec2 xz=rot(2.3*q.y+uPhase)*q.xz;vec3 r=vec3(xz.x,q.y,xz.y);',
50
+ ' float rr=0.80+0.05*sin(3.0*r.y+uT*0.4)+0.06*uBands.x*uLevel;',
51
+ ' return length(r-vec3(0.46+0.14*sin(2.2*q.y+0.6),0.0,0.0))-rr+uLevel*0.035*sin(11.0*r.y-8.0*uT)*(0.4+uBands.y);}',
52
+ 'float sheetB(vec3 q){vec2 yz=rot(-2.0*q.x-0.7*uPhase+2.2)*q.yz;vec3 r=vec3(q.x,yz.x,yz.y);',
53
+ ' return length(r-vec3(-0.52,0.12+0.12*sin(2.0*q.x+1.4),0.0))-(0.76+0.04*sin(2.4*r.z+uT*0.3))+uLevel*0.03*sin(9.0*r.x+7.0*uT)*(0.4+uBands.z);}',
54
+ 'void main(){',
55
+ ' vec2 f=gl_FragCoord.xy;vec3 bg=ground(f);vec3 col=bg,light=vec3(0.0);vec2 p=(f-uC)/uR;',
56
+ ' if(dot(p,p)<9.0){',
57
+ ' const float K=0.3535534;vec3 ro=vec3(0.0,0.0,3.0),rd=normalize(vec3(p*K,-1.0));',
58
+ ' float rad=1.0+0.028*uLevel;float b=dot(ro,rd),c=dot(ro,ro)-rad*rad,h=b*b-c;',
59
+ ' float dmin=sqrt(max(dot(ro,ro)-b*b,0.0)),e=max(dmin-rad,0.0);',
60
+ ' light+=uHaloCol*uHaloK*(0.16*exp(-e*14.0)+0.06*exp(-e*3.2))*(0.6+0.9*uFill+0.8*uLevel+0.5*uAttend);',
61
+ // the silhouette is blended over ~1.6 device pixels: never a stair-stepped cut
62
+ ' float ps=tan(asin(rad/3.0))/K,pl=length(p);float cov=smoothstep(ps+0.8/uR,ps-0.8/uR,pl);',
63
+ ' if(cov>0.0){',
64
+ ' float sh=sqrt(max(h,0.0)),t0=-b-sh,t1=-b+sh;vec3 n0=normalize(ro+rd*t0);',
65
+ ' float cosv=max(dot(n0,-rd),0.0),fres=1.0-cosv;float gl=max(dot(n0,uGaze),0.0),ll=max(dot(n0,uLight),0.0);',
66
+ ' float dl=max(ps-pl,0.0)/ps;float lip=0.55+0.45*smoothstep(0.0,0.016,dl);',
67
+ ' vec3 band=vec3(exp(-dl/0.030),exp(-dl/0.037),exp(-dl/0.046))*lip;',
68
+ // march the folds; each step integrates the sheet glow exactly for a linear field, one offset per colour channel
69
+ ' const int N=44;float dt=(t1-t0)/float(N);float S=mix(56.0,11.0,uBlur),dsp=(0.016+0.085*uBlur)*uDispK;',
70
+ ' vec3 x=ro+rd*t0,q=uRot*x;float fa=sheetA(q),fb=sheetB(q);',
71
+ ' vec3 FA=vec3(Fp(fa+dsp,S),Fp(fa,S),Fp(fa-dsp,S)),FB=vec3(Fp(fb+dsp,S),Fp(fb,S),Fp(fb-dsp,S));',
72
+ ' float pfa=fa,pfb=fb;vec3 acc=vec3(0.0);',
73
+ ' for(int i=1;i<=N;i++){',
74
+ ' x=ro+rd*(t0+float(i)*dt);q=uRot*x;fa=sheetA(q);fb=sheetB(q);',
75
+ ' vec3 nFA=vec3(Fp(fa+dsp,S),Fp(fa,S),Fp(fa-dsp,S)),nFB=vec3(Fp(fb+dsp,S),Fp(fb,S),Fp(fb-dsp,S));',
76
+ ' float da=fa-pfa,db=fb-pfb;',
77
+ ' vec3 ia=abs(da)>1e-4?(nFA-FA)/da:exp(-S*abs(vec3(fa+dsp,fa,fa-dsp)));',
78
+ ' vec3 ib=abs(db)>1e-4?(nFB-FB)/db:exp(-S*abs(vec3(fb+dsp,fb,fb-dsp)));',
79
+ ' vec3 xn=x/rad;float lit=0.3+1.3*smoothstep(-0.3,1.0,dot(xn,uLight))+0.6*uAttend*smoothstep(0.0,1.0,dot(xn,uGaze));',
80
+ ' float ga=1.0-min(abs(da)/dt,1.0),gb=1.0-min(abs(db)/dt,1.0);',
81
+ ' float wa=mix(0.035,1.0,ga*ga*mix(1.0,0.3,uBlur)+uBlur*0.7),wb=mix(0.035,1.0,gb*gb*mix(1.0,0.3,uBlur)+uBlur*0.7);',
82
+ ' acc+=(ia*wa+0.75*ib*wb)*lit;FA=nFA;FB=nFB;pfa=fa;pfb=fb;',
83
+ ' }',
84
+ ' acc*=dt*S*0.11*(1.0+0.8*uLevel+0.35*uSpeak);float thick=t1-t0;',
85
+ ' if(uInk<0.5){',
86
+ // a dark ground: the ball emits light
87
+ ' float rimW=0.14+3.2*ll*ll*ll+1.3*uAttend*gl*gl+1.4*uPulse*gl+0.9*uLevel;',
88
+ ' vec3 inner=(0.72*band+0.28*vec3(pow(fres,4.0),pow(fres,3.4),pow(fres,2.9)))*uRimCol*rimW*1.9;',
89
+ ' inner+=uOrbCol*0.09*pow(ll,3.0)*(0.3+0.7*fres);inner+=acc*uOrbCol+acc*acc*vec3(0.30,0.32,0.36);',
90
+ ' inner+=uOrbCol*thick*0.08*uFill;light+=inner*cov;col=bg*mix(1.0,exp(-thick*0.9),cov);',
91
+ ' }else{',
92
+ // a light ground: clear glass refracting the ground, darker toward its edge, folds read as ink, a white crescent
93
+ ' float k=0.55*fres+0.10;vec2 off=-n0.xy*uR*k;float dk=0.05*uDispK;',
94
+ ' vec3 tr=vec3(ground(f+off*(1.0-dk)).r,ground(f+off).g,ground(f+off*(1.0+dk)).b);',
95
+ ' tr*=mix(vec3(1.0),uOrbCol,clamp(thick*0.32,0.0,0.6));tr=mix(tr,vec3(0.96),uFill*0.5*smoothstep(0.0,1.2,thick));',
96
+ ' tr*=1.0-0.45*band;tr*=1.0-clamp(acc*(0.40+0.3*uLevel),0.0,0.85);',
97
+ ' float spec=(0.6*band.g+0.4*pow(fres,3.0))*pow(ll,3.0)*(1.0+1.5*uAttend*gl*gl+uPulse*gl)*smoothstep(0.004,0.02,dl);',
98
+ ' tr+=vec3(1.0)*spec*0.95;tr+=clamp(acc*acc*0.05,0.0,0.5);col=mix(bg,tr,cov);',
99
+ ' }',
100
+ ' }',
101
+ ' }',
102
+ ' gl_FragColor=vec4(col+tm(light)+(hash12(f)-0.5)*(1.5/255.0),1.0);',
103
+ '}'
104
+ ].join('\n');
105
+
106
+ // ---------- motion ----------
107
+ function Spring(x, k, zeta) { this.x = x; this.v = 0; this.target = x; this.k = k; this.c = 2 * zeta * Math.sqrt(k); }
108
+ Spring.prototype.step = function (dt) {
109
+ var n = Math.max(1, Math.ceil(dt * 240)), h = dt / n;
110
+ for (var i = 0; i < n; i++) { this.v += (-this.k * (this.x - this.target) - this.c * this.v) * h; this.x += this.v * h; }
111
+ return this.x;
112
+ };
113
+ function damp(x, t, l, dt) { return x + (t - x) * (1 - Math.exp(-l * dt)); }
114
+ function clamp(x, a, b) { return Math.min(b, Math.max(a, x)); }
115
+ function smooth(a, b, x) { var t = clamp((x - a) / (b - a), 0, 1); return t * t * (3 - 2 * t); }
116
+ function hash1(n) { var s = Math.sin(n * 127.1 + 311.7) * 43758.5453; return s - Math.floor(s); }
117
+ function vnoise(x) { var i = Math.floor(x), f = x - i, u = f * f * (3 - 2 * f); return hash1(i) * (1 - u) + hash1(i + 1) * u; }
118
+ function norm(v) { var n = Math.hypot(v[0], v[1], v[2]) || 1; v[0] /= n; v[1] /= n; v[2] /= n; return v; }
119
+ // the rotation that takes +z to the unit vector g, inverted (world → the orb's own frame), as a column-major mat3
120
+ function frameTo(g, out) {
121
+ var z = [0, 0, 1], d = g[2];
122
+ if (d > 0.99999) { out.set([1, 0, 0, 0, 1, 0, 0, 0, 1]); return; }
123
+ var ax = [-g[1], g[0], 0]; norm(ax); // z × g
124
+ var a = Math.acos(clamp(d, -1, 1)), c = Math.cos(-a), s = Math.sin(-a), t = 1 - c, x = ax[0], y = ax[1], w = ax[2];
125
+ out[0] = t * x * x + c; out[3] = t * x * y - s * w; out[6] = t * x * w + s * y;
126
+ out[1] = t * x * y + s * w; out[4] = t * y * y + c; out[7] = t * y * w - s * x;
127
+ out[2] = t * x * w - s * y; out[5] = t * y * w + s * x; out[8] = t * w * w + c;
128
+ }
129
+ // a voice: lobes at the speech rate, gated into phrases; the assistant's own voice is steadier than a person's
130
+ function simVoice(t, ai, bands) {
131
+ var rate = ai ? 3.4 : 4.2;
132
+ var syl = Math.pow(Math.abs(Math.sin(Math.PI * rate * t + 2.6 * vnoise(t * 0.8 + (ai ? 40 : 0)))), ai ? 1.3 : 1.8);
133
+ var gate = ai ? smooth(0.18, 0.4, vnoise(t * 0.55 + 7)) : smooth(0.32, 0.52, vnoise(t * 0.7 + 11));
134
+ var lv = syl * gate * (0.5 + 0.5 * vnoise(t * 2.1 + 5));
135
+ bands[0] = lv * (0.75 + 0.5 * vnoise(t * 6.5 + 1)); bands[1] = lv * (0.45 + 0.5 * vnoise(t * 6.5 + 9)); bands[2] = lv * (0.2 + 0.4 * vnoise(t * 6.5 + 17));
136
+ return lv;
137
+ }
138
+
139
+ // host APIs are read as globals, not off window: the package runs this file with a window of its own
140
+ var reduceMQ = typeof matchMedia === 'function' ? matchMedia('(prefers-reduced-motion: reduce)') : null;
141
+ var measureCtx = null;
142
+
143
+ function orb(host, o) {
144
+ o = o || {};
145
+ var tone = TONES[o.tone] ? o.tone : 'steel', state = STATES[o.state] ? o.state : 'idle';
146
+ host.classList.add('orb');
147
+ if (!host.hasAttribute('role')) host.setAttribute('role', 'img');
148
+ if (!host.hasAttribute('aria-label')) host.setAttribute('aria-label', o.label || 'AI assistant');
149
+ var canvas = document.createElement('canvas');
150
+ canvas.className = 'orb-canvas';
151
+ canvas.setAttribute('aria-hidden', 'true');
152
+ host.appendChild(canvas);
153
+ var gl = canvas.getContext('webgl', { antialias: false, alpha: false, depth: false, stencil: false, premultipliedAlpha: false, powerPreference: 'high-performance' });
154
+ var noop = function () { return api; };
155
+ var api = { set: noop, tone: noop, level: noop, pulse: noop, lookAt: noop, follow: noop, destroy: function () { canvas.remove(); } };
156
+ if (!gl) { host.classList.add('orb-nogl'); return api; }
157
+
158
+ function sh(type, src) { var s = gl.createShader(type); gl.shaderSource(s, src); gl.compileShader(s); return s; }
159
+ var prog = gl.createProgram();
160
+ gl.attachShader(prog, sh(gl.VERTEX_SHADER, VS)); gl.attachShader(prog, sh(gl.FRAGMENT_SHADER, FS));
161
+ gl.bindAttribLocation(prog, 0, 'p'); gl.linkProgram(prog);
162
+ if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) { host.classList.add('orb-nogl'); return api; }
163
+ gl.useProgram(prog);
164
+ var buf = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, buf);
165
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
166
+ gl.enableVertexAttribArray(0); gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
167
+ var U = {};
168
+ ['uRes', 'uC', 'uR', 'uTop', 'uBot', 'uFloor', 'uShadow', 'uVig', 'uT', 'uAttend', 'uLevel', 'uPulse', 'uBlur', 'uPhase', 'uFill', 'uSpeak',
169
+ 'uInk', 'uDispK', 'uHaloK', 'uBands', 'uGaze', 'uLight', 'uOrbCol', 'uRimCol', 'uHaloCol', 'uRot'].forEach(function (n) { U[n] = gl.getUniformLocation(prog, n); });
170
+
171
+ // ---------- state ----------
172
+ var W = 0, H = 0, dpr = 1, cx = 0, cy = 0, R = 1;
173
+ var t = 0, prev = 0, raf = 0, onScreen = true, dead = false;
174
+ var gx = new Spring(0, 26, 1), gy = new Spring(0, 26, 1), gz = new Spring(1, 26, 1);
175
+ var attend = new Spring(0, 46, 1), pulseS = new Spring(0, 150, 1), think = new Spring(0, 22, 1), speak = new Spring(0, 34, 1);
176
+ var blur = new Spring(0.3, 24, 1), fill = new Spring(0, 22, 1);
177
+ var phase = 0, pvel = 0.14, kick = 0, lastKey = -99, level = 0, bands = [0, 0, 0], rawB = [0, 0, 0];
178
+ var gaze = [0, 0, 1], light = norm([0.62, 0.55, 0.35]), lightT = [0, 0, 0], rotM = new Float32Array(9);
179
+ var cur = JSON.parse(JSON.stringify(TONES[tone])), want = TONES[tone];
180
+ var extLevel = null, followEl = null, focused = false, look = null;
181
+ var caret = { x: 0, y: 0 }, sim = { x: -1.7, next: 0.45, pause: 0 };
182
+ var mic = { on: false, an: null, ctx: null, stream: null, td: null, fd: null, asked: false };
183
+ var SPEED = { idle: 0.14, typing: 0.2, listening: 0.32, thinking: 1.15, speaking: 0.42 };
184
+
185
+ function size() {
186
+ var r = host.getBoundingClientRect();
187
+ dpr = Math.min(typeof devicePixelRatio === 'number' && devicePixelRatio > 0 ? devicePixelRatio : 1, 2);
188
+ W = Math.max(1, Math.round(r.width * dpr)); H = Math.max(1, Math.round(r.height * dpr));
189
+ if (canvas.width !== W || canvas.height !== H) { canvas.width = W; canvas.height = H; }
190
+ cx = r.width / 2; cy = r.height * 0.46; R = Math.max(8, Math.min(r.width, r.height) * 0.3);
191
+ }
192
+ var ro = typeof ResizeObserver === 'function' ? new ResizeObserver(size) : null;
193
+ if (ro) ro.observe(host); else addEventListener('resize', size);
194
+ var io = typeof IntersectionObserver === 'function' ? new IntersectionObserver(function (es) { onScreen = es[0].isIntersecting; }, { rootMargin: '80px' }) : null;
195
+ if (io) io.observe(host);
196
+ size();
197
+
198
+ function key() { pulseS.v += 15; kick += 0.55; lastKey = t; }
199
+ function mode() { return followEl && focused ? 'typing' : state; }
200
+
201
+ // the caret of the followed field, in the orb's own coordinates
202
+ function readCaret() {
203
+ if (!followEl) return;
204
+ var r = host.getBoundingClientRect(), fr = followEl.getBoundingClientRect(), cs = getComputedStyle(followEl);
205
+ measureCtx = measureCtx || document.createElement('canvas').getContext('2d');
206
+ measureCtx.font = cs.fontWeight + ' ' + cs.fontSize + ' ' + cs.fontFamily;
207
+ var v = followEl.value || '', at = followEl.selectionStart == null ? v.length : followEl.selectionStart;
208
+ var w = measureCtx.measureText(v.slice(0, at)).width - (followEl.scrollLeft || 0) + (parseFloat(cs.paddingLeft) || 0);
209
+ caret.x = fr.left - r.left + clamp(w, 0, fr.width); caret.y = fr.top - r.top + fr.height / 2;
210
+ }
211
+ function onInput() { readCaret(); key(); }
212
+ function onFocus() { focused = true; readCaret(); }
213
+ function onBlur() { focused = false; }
214
+
215
+ function startMic() {
216
+ if (mic.asked || !navigator.mediaDevices) return; mic.asked = true;
217
+ navigator.mediaDevices.getUserMedia({ audio: { echoCancellation: true, noiseSuppression: true } }).then(function (s) {
218
+ if (dead) { s.getTracks().forEach(function (k) { k.stop(); }); return; }
219
+ var C = new (typeof AudioContext === 'function' ? AudioContext : webkitAudioContext)(), an = C.createAnalyser();
220
+ an.fftSize = 1024; an.smoothingTimeConstant = 0.5; C.createMediaStreamSource(s).connect(an);
221
+ mic.on = true; mic.ctx = C; mic.an = an; mic.stream = s; mic.td = new Float32Array(an.fftSize); mic.fd = new Uint8Array(an.frequencyBinCount);
222
+ }).catch(function () {});
223
+ }
224
+ function stopMic() {
225
+ if (mic.stream) mic.stream.getTracks().forEach(function (k) { k.stop(); });
226
+ if (mic.ctx) mic.ctx.close();
227
+ mic.on = false; mic.asked = false; mic.stream = mic.ctx = mic.an = null;
228
+ }
229
+ function readMic(b) {
230
+ mic.an.getFloatTimeDomainData(mic.td);
231
+ var s = 0, i; for (i = 0; i < mic.td.length; i++) s += mic.td[i] * mic.td[i];
232
+ var db = 20 * Math.log10(Math.sqrt(s / mic.td.length) + 1e-7);
233
+ mic.an.getByteFrequencyData(mic.fd);
234
+ var hz = mic.ctx.sampleRate / mic.an.fftSize, band = function (lo, hi) { var a = 0, n = 0; for (var k = Math.floor(lo / hz); k <= Math.ceil(hi / hz); k++) { a += mic.fd[k] || 0; n++; } return clamp((a / n / 255 - 0.28) / 0.5, 0, 1); };
235
+ b[0] = band(90, 300); b[1] = band(300, 2000); b[2] = band(2000, 6500);
236
+ return clamp((db + 54) / 34, 0, 1);
237
+ }
238
+
239
+ function step(dt) {
240
+ t += dt;
241
+ var m = mode(), reduce = reduceMQ && reduceMQ.matches, sw = reduce ? 0 : 1;
242
+ // where it attends
243
+ var tx = 0, ty = 0, tz = 1, att = 0;
244
+ if (m === 'typing') {
245
+ if (followEl && focused) readCaret();
246
+ else {
247
+ // a simulated line of typing below the orb: the caret runs left to right, a key at a time, then starts over
248
+ if (t >= sim.next) {
249
+ if (sim.x >= 1.7) { sim.x = -1.7; sim.next = t + 1.1; }
250
+ else { sim.x += 0.11 + 0.06 * hash1(t * 9.1); key(); sim.next = t + 0.07 + 0.095 * hash1(t * 3.7) + (hash1(t * 5.3) > 0.82 ? 0.11 : 0); }
251
+ }
252
+ caret.x = cx + sim.x * R; caret.y = cy + 3.1 * R;
253
+ }
254
+ att = t - lastKey < 1.3 ? 1 : 0.55;
255
+ tx = (caret.x - cx) / R; ty = -(caret.y - cy) / R; tz = 1.7;
256
+ } else if (look) { tx = (look[0] - cx) / R; ty = -(look[1] - cy) / R; tz = 1.7; }
257
+ else if (m === 'listening') ty = -0.08;
258
+ else if (m === 'thinking') { tx = 0.34 * Math.cos(t * 0.75); ty = 0.24 * Math.sin(t * 0.97); }
259
+ else if (m === 'speaking') ty = 0.04;
260
+ else { tx = sw * 0.14 * Math.sin(t / 4.3); ty = sw * 0.09 * Math.sin(t / 5.7 + 1); }
261
+ var n = Math.hypot(tx, ty, tz) || 1;
262
+ gx.target = tx / n; gy.target = ty / n; gz.target = tz / n;
263
+ gx.step(dt); gy.step(dt); gz.step(dt);
264
+ gaze[0] = gx.x; gaze[1] = gy.x; gaze[2] = gz.x; norm(gaze);
265
+ attend.target = att; attend.step(dt); pulseS.step(dt);
266
+ think.target = m === 'thinking' ? 1 : 0; think.step(dt);
267
+ speak.target = m === 'speaking' ? 1 : 0; speak.step(dt);
268
+ // the voice
269
+ var lv = 0; rawB[0] = rawB[1] = rawB[2] = 0;
270
+ if (m === 'listening' || m === 'speaking') {
271
+ if (extLevel != null) { lv = clamp(extLevel, 0, 1); rawB[0] = lv * 0.9; rawB[1] = lv * 0.6; rawB[2] = lv * 0.3; }
272
+ else if (m === 'listening' && mic.on) lv = readMic(rawB);
273
+ else lv = simVoice(t, m === 'speaking', rawB);
274
+ }
275
+ level = damp(level, lv, lv > level ? 28 : 6, dt);
276
+ for (var i = 0; i < 3; i++) bands[i] = damp(bands[i], rawB[i], rawB[i] > bands[i] ? 24 : 7, dt);
277
+ // the folds turn, faster when it thinks; a key gives them a small push
278
+ kick = damp(kick, 0, 2.2, dt);
279
+ pvel = damp(pvel, (reduce ? 0.3 : 1) * SPEED[m] + kick, 5, dt); phase += pvel * dt;
280
+ blur.target = m === 'thinking' ? 1 : m === 'typing' ? 0 : m === 'listening' ? 0.12 : m === 'speaking' ? 0.18 : 0.3; blur.step(dt);
281
+ fill.target = m === 'thinking' ? 1 : m === 'speaking' ? 0.12 : 0; fill.step(dt);
282
+ // the tone cross-fades
283
+ var k = 1 - Math.exp(-5 * dt);
284
+ for (var key2 in cur) {
285
+ if (Array.isArray(cur[key2])) for (var j = 0; j < 3; j++) cur[key2][j] += (want[key2][j] - cur[key2][j]) * k;
286
+ else cur[key2] += (want[key2] - cur[key2]) * k;
287
+ }
288
+ // the key light: top left on a light ground, top right on a dark one; it comes round to what the orb attends to
289
+ var lx = cur.ink > 0.5 ? -0.55 : 0.62;
290
+ if (m === 'thinking') { lightT[0] = Math.cos(t * 0.9); lightT[1] = Math.sin(t * 0.9); lightT[2] = 0.3; }
291
+ else { lightT[0] = lx + sw * 0.22 * Math.sin(t / 6.1); lightT[1] = 0.58 + sw * 0.18 * Math.sin(t / 7.3 + 2); lightT[2] = 0.38; }
292
+ norm(lightT);
293
+ var a = 0.85 * attend.x;
294
+ for (i = 0; i < 3; i++) lightT[i] += (gaze[i] - lightT[i]) * a;
295
+ norm(lightT);
296
+ var kl = 1 - Math.exp(-2.6 * dt);
297
+ for (i = 0; i < 3; i++) light[i] += (lightT[i] - light[i]) * kl;
298
+ norm(light);
299
+ }
300
+
301
+ function draw() {
302
+ gl.viewport(0, 0, W, H);
303
+ gl.uniform2f(U.uRes, W, H); gl.uniform2f(U.uC, cx * dpr, H - cy * dpr); gl.uniform1f(U.uR, R * dpr);
304
+ gl.uniform3fv(U.uTop, cur.top); gl.uniform3fv(U.uBot, cur.bot); gl.uniform3fv(U.uFloor, cur.floor);
305
+ gl.uniform1f(U.uShadow, cur.shadow); gl.uniform1f(U.uVig, cur.vig); gl.uniform1f(U.uInk, cur.ink); gl.uniform1f(U.uDispK, cur.disp); gl.uniform1f(U.uHaloK, cur.halo);
306
+ gl.uniform3fv(U.uOrbCol, cur.orb); gl.uniform3fv(U.uRimCol, cur.rim); gl.uniform3fv(U.uHaloCol, cur.haloC);
307
+ gl.uniform1f(U.uT, t); gl.uniform1f(U.uAttend, attend.x); gl.uniform1f(U.uLevel, level); gl.uniform1f(U.uPulse, clamp(pulseS.x, 0, 1.2));
308
+ gl.uniform1f(U.uBlur, blur.x); gl.uniform1f(U.uPhase, phase); gl.uniform1f(U.uFill, fill.x); gl.uniform1f(U.uSpeak, speak.x);
309
+ gl.uniform3f(U.uBands, bands[0], bands[1], bands[2]); gl.uniform3fv(U.uGaze, gaze); gl.uniform3fv(U.uLight, light);
310
+ frameTo(gaze, rotM); gl.uniformMatrix3fv(U.uRot, false, rotM);
311
+ gl.drawArrays(gl.TRIANGLES, 0, 3);
312
+ }
313
+
314
+ function frame(now) {
315
+ if (dead) return;
316
+ raf = requestAnimationFrame(frame);
317
+ var dt = prev ? Math.min(1 / 30, Math.max(0, (now - prev) / 1000)) : 0; prev = now;
318
+ if (!onScreen || document.hidden) return;
319
+ step(dt); draw();
320
+ }
321
+ step(0); draw();
322
+ raf = requestAnimationFrame(frame);
323
+
324
+ api = {
325
+ set: function (s) { if (STATES[s]) { state = s; if (s === 'listening' && o.mic) startMic(); if (s !== 'listening') stopMic(); } return api; },
326
+ tone: function (n) { if (TONES[n]) { tone = n; want = TONES[n]; } return api; },
327
+ level: function (v) { extLevel = v == null ? null : +v; return api; },
328
+ pulse: function () { key(); return api; },
329
+ lookAt: function (x, y) {
330
+ if (x == null) { look = null; return api; }
331
+ var r = host.getBoundingClientRect(); look = [x - r.left, y - r.top]; return api;
332
+ },
333
+ follow: function (el) {
334
+ if (followEl) { followEl.removeEventListener('input', onInput); followEl.removeEventListener('focus', onFocus); followEl.removeEventListener('blur', onBlur); followEl.removeEventListener('keyup', readCaret); followEl.removeEventListener('click', readCaret); }
335
+ followEl = el || null; focused = !!(followEl && document.activeElement === followEl);
336
+ if (followEl) { followEl.addEventListener('input', onInput); followEl.addEventListener('focus', onFocus); followEl.addEventListener('blur', onBlur); followEl.addEventListener('keyup', readCaret); followEl.addEventListener('click', readCaret); }
337
+ return api;
338
+ },
339
+ get state() { return state; },
340
+ get toneName() { return tone; },
341
+ destroy: function () {
342
+ dead = true; cancelAnimationFrame(raf); api.follow(null); stopMic();
343
+ if (ro) ro.disconnect(); else removeEventListener('resize', size);
344
+ if (io) io.disconnect();
345
+ var lose = gl.getExtension('WEBGL_lose_context'); if (lose) lose.loseContext();
346
+ canvas.remove(); host.classList.remove('orb');
347
+ }
348
+ };
349
+ if (o.follow) api.follow(typeof o.follow === 'string' ? document.querySelector(o.follow) : o.follow);
350
+ if (o.level != null) api.level(o.level);
351
+ api.set(state);
352
+ return api;
353
+ }
354
+
355
+ orb.tones = Object.keys(TONES);
356
+ orb.states = Object.keys(STATES);
357
+ window.orb = orb;
358
+ })();
359
+
360
+ return (api = window);
361
+ }
362
+
363
+ // host: the element the orb fills. o: { tone ('steel', 'mist' or 'blue'), state ('idle', 'typing', 'listening',
364
+ // 'thinking' or 'speaking'), height (px or any CSS length, default 420), follow (a text field, or a selector: the orb
365
+ // watches its caret while it has focus), level (0–1 from your own audio; leave it out for the built-in voice),
366
+ // mic (true: "listening" uses the microphone), label (its accessible name, default "AI assistant") }
367
+ export function mountFoldOrb(host, o = {}) {
368
+ css('craft-orb', CSS);
369
+ const E = engine();
370
+ host.innerHTML = '<div class="orb"></div>';
371
+ const el = host.firstChild;
372
+ const tall = (h) => { if (h != null && h !== '') el.style.setProperty('--orb-height', typeof h === 'number' || /^\d+(\.\d+)?$/.test(h) ? h + 'px' : String(h)); };
373
+ const field = (f) => typeof f === 'string' ? document.querySelector(f) : f && 'current' in f ? f.current : f;
374
+ tall(o.height);
375
+ const m = E.orb(el, { tone: o.tone, state: o.state, follow: field(o.follow), level: o.level, mic: o.mic, label: o.label });
376
+ return {
377
+ element: el, set: (s) => m.set(s), tone: (t) => m.tone(t), level: (v) => m.level(v), pulse: () => m.pulse(),
378
+ lookAt: (x, y) => m.lookAt(x, y), follow: (f) => m.follow(field(f)), height: tall,
379
+ destroy() { m.destroy(); host.innerHTML = ''; }
380
+ };
381
+ }
@@ -1,3 +1,3 @@
1
- /*! Sara Lobo Library · craft-library 0.7.2 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
1
+ /*! Sara Lobo Library · craft-library 0.9.0 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
2
2
  // where the Panorama's own clip and pictures are: jsDelivr, at this version of the package
3
- export const ASSETS = 'https://cdn.jsdelivr.net/npm/craft-library@0.7.2/dist/panorama/';
3
+ export const ASSETS = 'https://cdn.jsdelivr.net/npm/craft-library@0.9.0/dist/panorama/';
@@ -1,4 +1,4 @@
1
- /*! Sara Lobo Library · craft-library 0.7.2 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
1
+ /*! Sara Lobo Library · craft-library 0.9.0 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
2
2
  // Panorama: the mount around the library's panorama.js. With no video and no stills it shows the library's own clip
3
3
  // and pictures, which ship with the package in dist/panorama/ (see panorama-assets.js for where they are read from).
4
4
  import { mountPanorama } from './panorama.js';
package/dist/panorama.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! Sara Lobo Library · craft-library 0.7.2 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
1
+ /*! Sara Lobo Library · craft-library 0.9.0 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
2
2
  // Panorama · Sara Lobo Library
3
3
  // A hero for moving pictures. A clip opens full-bleed under letterbox bars while the title tracks in; then the clip shrinks
4
4
  // into one screen of a dome of monitors that surrounds you, and the other screens arrive in a wave around it. The pointer
@@ -1,3 +1,3 @@
1
- /*! Sara Lobo Library · craft-library 0.7.2 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
1
+ /*! Sara Lobo Library · craft-library 0.9.0 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
2
2
  // where the piggy bank comes from: for bundlers, the package's piggy.js (it imports the `three` dependency)
3
3
  export const loadPiggy = () => import('./piggy.js');
@@ -1,4 +1,4 @@
1
- /*! Sara Lobo Library · craft-library 0.7.2 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
1
+ /*! Sara Lobo Library · craft-library 0.9.0 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
2
2
  // Glass piggy bank: the mount around the library's piggy.js. piggy.js, and three.js with it, loads only when a piggy
3
3
  // bank is mounted, so the other pieces stay light.
4
4
  import { loadPiggy } from './piggy-load.js';
package/dist/piggy.cdn.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! Sara Lobo Library · craft-library 0.7.2 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
1
+ /*! Sara Lobo Library · craft-library 0.9.0 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
2
2
  // Piggy bank · Sara Lobo Library
3
3
  // A congratulations moment in 3D: a frosted glass piggy bank hops in from the right with a full turn, lands on its hind
4
4
  // feet, nearly tips over and clacks back down, rocking like the rigid glass it is. Coins pop into the air above it and
package/dist/piggy.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! Sara Lobo Library · craft-library 0.7.2 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
1
+ /*! Sara Lobo Library · craft-library 0.9.0 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
2
2
  // Piggy bank · Sara Lobo Library
3
3
  // A congratulations moment in 3D: a frosted glass piggy bank hops in from the right with a full turn, lands on its hind
4
4
  // feet, nearly tips over and clacks back down, rocking like the rigid glass it is. Coins pop into the air above it and
package/dist/pin-load.js CHANGED
@@ -1,3 +1,3 @@
1
- /*! Sara Lobo Library · craft-library 0.7.2 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
1
+ /*! Sara Lobo Library · craft-library 0.9.0 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
2
2
  // where the map pin comes from: for bundlers, the package's pin.js (it imports the `three` dependency)
3
3
  export const loadPin = () => import('./pin.js');
package/dist/pin-mount.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! Sara Lobo Library · craft-library 0.7.2 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
1
+ /*! Sara Lobo Library · craft-library 0.9.0 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
2
2
  // Map pin: the mount around the library's pin.js. pin.js, and three.js with it, loads only when a map pin is mounted,
3
3
  // so the other pieces stay light.
4
4
  import { loadPin } from './pin-load.js';
package/dist/pin.cdn.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! Sara Lobo Library · craft-library 0.7.2 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
1
+ /*! Sara Lobo Library · craft-library 0.9.0 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
2
2
  /* Pin de mapa · Sara Lobo Library (saralobolibrary.com). A city map in perspective with a glass map pin: a click or a tap
3
3
  on a street sends the pin flying to the nearest curb, where it sticks its tip in; the map is in focus there and soft
4
4
  further away, the streets carry their names, and a card under the map names the address and confirms it.
package/dist/pin.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! Sara Lobo Library · craft-library 0.7.2 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
1
+ /*! Sara Lobo Library · craft-library 0.9.0 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
2
2
  /* Pin de mapa · Sara Lobo Library (saralobolibrary.com). A city map in perspective with a glass map pin: a click or a tap
3
3
  on a street sends the pin flying to the nearest curb, where it sticks its tip in; the map is in focus there and soft
4
4
  further away, the streets carry their names, and a card under the map names the address and confirms it.
package/dist/sign.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! Sara Lobo Library · craft-library 0.7.2 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
1
+ /*! Sara Lobo Library · craft-library 0.9.0 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
2
2
  // Where the pieces come from: one quiet line in the browser console, once per page, for the people who build with them
3
3
  // (nothing shows on the page itself). It waits for the page to finish loading, so a page can set
4
4
  // window.CRAFT_LIBRARY_QUIET = true (in any script, or right after importing the package) to skip it.
package/dist/style.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! Sara Lobo Library · craft-library 0.7.2 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
1
+ /*! Sara Lobo Library · craft-library 0.9.0 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
2
2
  // each piece's CSS goes into the page once, the first time one is mounted
3
3
  const done = new Set();
4
4
  export function css(id, text) {
package/dist/thinking.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! Sara Lobo Library · craft-library 0.7.2 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
1
+ /*! Sara Lobo Library · craft-library 0.9.0 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
2
2
  // Magic sphere and Sweep · Craft Library, by Sara Lobo. thinking.css and thinking.js from the library, unchanged,
3
3
  // around a mount.
4
4
  import { css } from './style.js';
@@ -1,3 +1,3 @@
1
- /*! Sara Lobo Library · craft-library 0.7.2 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
1
+ /*! Sara Lobo Library · craft-library 0.9.0 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
2
2
  // where the token wheel comes from: for bundlers, the package's tokens.js (it imports the `three` dependency)
3
3
  export const loadTokens = () => import('./tokens.js');
@@ -1,4 +1,4 @@
1
- /*! Sara Lobo Library · craft-library 0.7.2 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
1
+ /*! Sara Lobo Library · craft-library 0.9.0 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
2
2
  // Token wheel: the mount around the library's tokens.js. tokens.js, and three.js with it, loads only when a token wheel
3
3
  // is mounted, so the other pieces stay light.
4
4
  import { loadTokens } from './tokens-load.js';
@@ -1,4 +1,4 @@
1
- /*! Sara Lobo Library · craft-library 0.7.2 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
1
+ /*! Sara Lobo Library · craft-library 0.9.0 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
2
2
  /* Roda de fichas · Sara Lobo Library (saralobolibrary.com). An onboarding screen for an account: eight silver tokens
3
3
  (Pix, card, Bitcoin, insurance, investments, exchange, cashback, boleto) turn slowly on a wheel, and each one, as it
4
4
  passes the top, comes forward and makes a full turn to show what it is. Turn the phone on its side and the tokens let
package/dist/tokens.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! Sara Lobo Library · craft-library 0.7.2 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
1
+ /*! Sara Lobo Library · craft-library 0.9.0 · interface pieces by Sara Lobo · https://saralobolibrary.com · MIT */
2
2
  /* Roda de fichas · Sara Lobo Library (saralobolibrary.com). An onboarding screen for an account: eight silver tokens
3
3
  (Pix, card, Bitcoin, insurance, investments, exchange, cashback, boleto) turn slowly on a wheel, and each one, as it
4
4
  passes the top, comes forward and makes a full turn to show what it is. Turn the phone on its side and the tokens let
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "craft-library",
3
- "version": "0.7.2",
4
- "description": "Sara Lobo Library: a liquid glass nav bar, a bursting heart, a magnetic icon, an iridescent 3D card, a glass piggy bank, two thinking indicators (Magic sphere, Sweep), the Panorama hero, the Map pin address picker and the Token wheel onboarding, for React or plain HTML",
3
+ "version": "0.9.0",
4
+ "description": "Sara Lobo Library: a liquid glass nav bar, a bursting heart, a magnetic icon, an iridescent 3D card, a glass piggy bank, two thinking indicators (Magic sphere, Sweep), the Panorama hero, the Map pin address picker, the Token wheel onboarding, the Thumbs reaction like/dislike and the Fold orb AI assistant orb, for React or plain HTML",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.js",
@@ -72,7 +72,14 @@
72
72
  "threejs",
73
73
  "ai-prompts",
74
74
  "claude-code",
75
- "cursor"
75
+ "cursor",
76
+ "thumbs-up",
77
+ "like-dislike",
78
+ "reaction",
79
+ "ai-orb",
80
+ "voice-assistant",
81
+ "shader",
82
+ "glass-orb"
76
83
  ],
77
84
  "author": "Sara Lobo",
78
85
  "license": "MIT",