morphico 0.3.0 → 0.4.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/morphico.mjs CHANGED
@@ -1,4 +1,4 @@
1
- /*! morphico v0.3.0 | code: MIT License, Copyright (c) 2026 Raycho Ivanov (see LICENSE)
1
+ /*! morphico v0.4.0 | code: MIT License, Copyright (c) 2026 Raycho Ivanov (see LICENSE)
2
2
  * Icons based on Lucide (https://lucide.dev), some of them from Feather (https://feathericons.com):
3
3
  *
4
4
  * ISC License
@@ -3126,7 +3126,7 @@ return { EMPTY_STATE, DEFAULT_DURATION, shapesOf, hasState, prefersReducedMotion
3126
3126
  // sets/shapes.ts
3127
3127
  const { lucideShapeOutlines, clipShapes } = (() => {
3128
3128
  // Shapes for clip(): closed outlines that cut out an element. From Lucide 1.61.0 (ISC, see LICENSE-lucide), imported
3129
- // by `node tools/import-lucide.ts <name>`; tag without its dot. The states are the Lucide outlines made comparable for
3129
+ // by `node tools/import-lucide.ts <name>`; tag without its dot, club without its stem. The states are the Lucide outlines made comparable for
3130
3130
  // morphing: clockwise, starting where the outline crosses the vertical middle at the top, scaled evenly to fill the
3131
3131
  // 24×24 box and centred (computed on first use, not at import). Roundness 1 = Lucide's own corners … 0 = sharp.
3132
3132
  const DEGREE = Math.PI / 180;
@@ -3337,16 +3337,6 @@ const lucideShapeOutlines = {
3337
3337
  line(2, 16, 2, 8),
3338
3338
  arc(4, 8, 2, 180, 270),
3339
3339
  ],
3340
- 'rectangle-vertical': [
3341
- line(8, 2, 16, 2),
3342
- arc(16, 4, 2, -90, 0),
3343
- line(18, 4, 18, 20),
3344
- arc(16, 20, 2, 0, 90),
3345
- line(16, 22, 8, 22),
3346
- arc(8, 20, 2, 90, 180),
3347
- line(6, 20, 6, 4),
3348
- arc(8, 4, 2, 180, 270),
3349
- ],
3350
3340
  'message-circle': [
3351
3341
  arc(1.1328, 17.079, 2, -21.6245, 12.4142),
3352
3342
  line(3.086, 17.509, 2.021, 20.799),
@@ -3367,16 +3357,6 @@ const lucideShapeOutlines = {
3367
3357
  arc(20, 5, 2, -90, 0),
3368
3358
  line(22, 5, 22, 17),
3369
3359
  ],
3370
- diamond: [
3371
- arc(4.4033, 12.005, 2.41, -134.9707, -225.0293),
3372
- line(2.7, 13.71, 10.29, 21.3),
3373
- arc(11.995, 19.5967, 2.41, 135.0293, 44.9707),
3374
- line(13.7, 21.3, 21.29, 13.71),
3375
- arc(19.5867, 12.005, 2.41, 45.0293, -45.0293),
3376
- line(21.29, 10.3, 13.7, 2.71),
3377
- arc(11.995, 4.4133, 2.41, -44.9707, -135.0293),
3378
- line(10.29, 2.71, 2.7, 10.3),
3379
- ],
3380
3360
  cloud: [
3381
3361
  line(17.5, 19, 9, 19),
3382
3362
  arc(9.0018, 12, 7, 90.0147, 343.3984),
@@ -3409,6 +3389,13 @@ const lucideShapeOutlines = {
3409
3389
  arc(12, 4.4139, 2.414, -135.0015, -44.9985),
3410
3390
  line(13.707, 2.707, 15.39, 4.39),
3411
3391
  ],
3392
+ // without its stem line (Lucide's second piece)
3393
+ club: [
3394
+ arc(12, 7.51, 5.5, 16.2602, -196.2602),
3395
+ arc(7.5026, 14.494, 5.5, -98.1803, -324.8563),
3396
+ arc(16.5052, 14.5052, 5.5, 144.9978, -81.9018),
3397
+ line(17.28, 9.06, 17.28, 9.05),
3398
+ ],
3412
3399
  };
3413
3400
  // ---- making the outlines comparable ----
3414
3401
  const pointOnStroke = (stroke, progress) => strokePiece(stroke, 0, progress)[3];
@@ -3600,6 +3587,8 @@ const GRID_CENTER = 12;
3600
3587
  const DEGREE = Math.PI / 180;
3601
3588
  /** Masks and their clip regions reach far past the element, so bands and turned shapes are never cut by them. */
3602
3589
  const FAR = 100000;
3590
+ /** In objectBoundingBox units (1 = the element's width / height): far around the element. */
3591
+ const INVERTED_SURROUNDING = 'M-1000 -1000H1001V1001H-1000Z';
3603
3592
  let clipCount = 0;
3604
3593
  const lengthToPixels = (length, reference, name) => {
3605
3594
  if (typeof length === 'number' && Number.isFinite(length))
@@ -3622,7 +3611,7 @@ const checkedOutline = (outline) => {
3622
3611
  };
3623
3612
  /** Cuts `element` out by a morphing shape. */
3624
3613
  const clip = (element, options = {}) => {
3625
- const { set = clipShapes, state: initialState = 'circle', x = 50, y = 50, size, rotation = 0, roundness = 1, outline = false, ...defaultMorphOptions } = options;
3614
+ const { set = clipShapes, state: initialState = 'circle', x = 50, y = 50, size, rotation = 0, roundness = 1, outline = false, inverted = false, ...defaultMorphOptions } = options;
3626
3615
  const checkedState = (shapeSet, state) => {
3627
3616
  if (!(state in shapeSet.strokes)) {
3628
3617
  throw new Error(`morphico: unknown shape "${state}" (known: ${Object.keys(shapeSet.strokes).join(', ')})`);
@@ -3636,7 +3625,7 @@ const clip = (element, options = {}) => {
3636
3625
  /** Where the change goes (or what shows when nothing moves). */
3637
3626
  const target = {
3638
3627
  state: checkedState(set, initialState), set: resolveIconSet(set, roundness), roundness,
3639
- x, y, size, rotation, outline: checkedOutline(outline),
3628
+ x, y, size, rotation, outline: checkedOutline(outline), inverted,
3640
3629
  };
3641
3630
  const pieceLength = pieceLengthOf(set);
3642
3631
  let frame = null; // null = resting at the target
@@ -3658,16 +3647,22 @@ const clip = (element, options = {}) => {
3658
3647
  helper.remove();
3659
3648
  maskElement.replaceChildren();
3660
3649
  bandPaths = [];
3650
+ clipShape.setAttribute('clip-rule', target.inverted ? 'evenodd' : 'nonzero');
3661
3651
  if (!target.outline)
3662
3652
  return;
3663
3653
  const { width, band = 'inside' } = target.outline;
3654
+ // inverted: everything shows (white), the band is the hole (black)
3655
+ const bandColor = target.inverted ? 'black' : 'white';
3656
+ if (target.inverted) {
3657
+ maskElement.append(createSvgElement('rect', { x: -FAR, y: -FAR, width: 2 * FAR, height: 2 * FAR, fill: 'white' }));
3658
+ }
3664
3659
  const pixelPath = (attributes) => {
3665
3660
  const path = createSvgElement('path', attributes);
3666
3661
  bandPaths.push(path);
3667
3662
  return path;
3668
3663
  };
3669
3664
  // inside = a line twice as wide, clipped to the shape; outside = a line twice as wide, the shape cut out of it
3670
- const edgeLine = (lineWidth) => pixelPath({ fill: 'none', stroke: 'white', 'stroke-width': lineWidth, 'stroke-linejoin': 'miter' });
3665
+ const edgeLine = (lineWidth) => pixelPath({ fill: 'none', stroke: bandColor, 'stroke-width': lineWidth, 'stroke-linejoin': 'miter' });
3671
3666
  if (band === 'centre') {
3672
3667
  maskElement.append(edgeLine(width));
3673
3668
  }
@@ -3727,7 +3722,9 @@ const clip = (element, options = {}) => {
3727
3722
  else {
3728
3723
  // objectBoundingBox: 0…1 of the element's width / height
3729
3724
  const boxUnits = pixels.map((value, index) => value / (index % 2 ? elementHeight : elementWidth));
3730
- clipShape.setAttribute('d', toPathData(boxUnits, pieceLength));
3725
+ // inverted: a rectangle far around the element with the shape as a hole in it (clip-rule evenodd)
3726
+ const surrounding = target.inverted ? INVERTED_SURROUNDING : '';
3727
+ clipShape.setAttribute('d', surrounding + toPathData(boxUnits, pieceLength));
3731
3728
  }
3732
3729
  };
3733
3730
  const measure = (width, height) => {
@@ -3766,8 +3763,11 @@ const clip = (element, options = {}) => {
3766
3763
  target.size = changes.size;
3767
3764
  if (changes.rotation !== undefined)
3768
3765
  target.rotation = changes.rotation;
3769
- if (changes.outline !== undefined) {
3770
- target.outline = checkedOutline(changes.outline);
3766
+ if (changes.outline !== undefined || changes.inverted !== undefined) {
3767
+ if (changes.outline !== undefined)
3768
+ target.outline = checkedOutline(changes.outline);
3769
+ if (changes.inverted !== undefined)
3770
+ target.inverted = changes.inverted;
3771
3771
  buildBand();
3772
3772
  applyStyle();
3773
3773
  }
@@ -3864,6 +3864,645 @@ const clip = (element, options = {}) => {
3864
3864
  return { clip };
3865
3865
  })();
3866
3866
 
3867
+ // sets/faces.ts
3868
+ const { OUTLINE_POINTS, PIECE_NAMES, faceExpressionNames, faceExpressionPieces, interpolateFace, blinked, smoothClosedPath, facePaths } = (() => {
3869
+ const OUTLINE_POINTS = 24;
3870
+ const PIECE_NAMES = ['leftBrow', 'rightBrow', 'leftEye', 'rightEye', 'leftPupil', 'rightPupil', 'mouth'];
3871
+ const DENSE_SAMPLES = 360;
3872
+ const toRadians = (degrees) => (degrees * Math.PI) / 180;
3873
+ const rotatePoint = ([x, y], degrees) => {
3874
+ const angle = toRadians(degrees);
3875
+ return [x * Math.cos(angle) - y * Math.sin(angle), x * Math.sin(angle) + y * Math.cos(angle)];
3876
+ };
3877
+ const denseBlobOutline = (options) => {
3878
+ const { centre, width, height, rotation = 0, squareness = 0, topScale = 1 } = options;
3879
+ const exponent = 2 + squareness * 6;
3880
+ const halfWidth = width / 2;
3881
+ const halfHeight = height / 2;
3882
+ const outline = [];
3883
+ for (let index = 0; index < DENSE_SAMPLES; index++) {
3884
+ const angle = toRadians(-90 + (360 * index) / DENSE_SAMPLES);
3885
+ const cosine = Math.cos(angle);
3886
+ const sine = Math.sin(angle);
3887
+ const y = halfHeight * Math.sign(sine) * Math.abs(sine) ** (2 / exponent);
3888
+ const widthScale = 1 + ((topScale - 1) * (halfHeight - y)) / (2 * halfHeight);
3889
+ const x = halfWidth * widthScale * Math.sign(cosine) * Math.abs(cosine) ** (2 / exponent);
3890
+ const [turnedX, turnedY] = rotatePoint([x, y], rotation);
3891
+ outline.push([centre[0] + turnedX, centre[1] + turnedY]);
3892
+ }
3893
+ const xs = outline.map(([x]) => x);
3894
+ const left = Math.min(...xs);
3895
+ const right = Math.max(...xs);
3896
+ const lineY = ([leftY, rightY], x) => leftY + ((rightY - leftY) * (x - left)) / (right - left);
3897
+ return outline.map(([x, y]) => {
3898
+ let cutY = y;
3899
+ if (options.cutTop)
3900
+ cutY = Math.max(cutY, lineY(options.cutTop, x));
3901
+ if (options.cutBottom)
3902
+ cutY = Math.min(cutY, lineY(options.cutBottom, x));
3903
+ return [x, cutY];
3904
+ });
3905
+ };
3906
+ /** OUTLINE_POINTS points of a star-shaped outline, on rays at even angles from its box's centre (measured in the box
3907
+ * stretched to a square, so a long flat shape still gets points on its round ends). */
3908
+ const resampleByAngle = (outline) => {
3909
+ const xs = outline.map(([x]) => x);
3910
+ const ys = outline.map(([, y]) => y);
3911
+ const centreX = (Math.min(...xs) + Math.max(...xs)) / 2;
3912
+ const centreY = (Math.min(...ys) + Math.max(...ys)) / 2;
3913
+ const halfX = Math.max((Math.max(...xs) - Math.min(...xs)) / 2, 1e-9);
3914
+ const halfY = Math.max((Math.max(...ys) - Math.min(...ys)) / 2, 1e-9);
3915
+ const normalized = outline.map(([x, y]) => [(x - centreX) / halfX, (y - centreY) / halfY]);
3916
+ const points = [];
3917
+ for (let index = 0; index < OUTLINE_POINTS; index++) {
3918
+ const angle = toRadians(-90 + (360 * index) / OUTLINE_POINTS);
3919
+ const directionX = Math.cos(angle);
3920
+ const directionY = Math.sin(angle);
3921
+ let farthest = 0;
3922
+ normalized.forEach((start, edgeIndex) => {
3923
+ const end = normalized[(edgeIndex + 1) % normalized.length];
3924
+ const edgeX = end[0] - start[0];
3925
+ const edgeY = end[1] - start[1];
3926
+ const denominator = directionX * edgeY - directionY * edgeX;
3927
+ if (Math.abs(denominator) < 1e-12)
3928
+ return;
3929
+ const distance = (start[0] * edgeY - start[1] * edgeX) / denominator;
3930
+ const along = (start[0] * directionY - start[1] * directionX) / denominator;
3931
+ // a little slack: a ray passing exactly through a corner of the dense outline must still hit one of its edges
3932
+ if (along >= -1e-9 && along <= 1 + 1e-9 && distance > farthest)
3933
+ farthest = distance;
3934
+ });
3935
+ points.push([centreX + directionX * farthest * halfX, centreY + directionY * farthest * halfY]);
3936
+ }
3937
+ return points;
3938
+ };
3939
+ const blob = (options) => resampleByAngle(denseBlobOutline(options));
3940
+ const band = (options) => {
3941
+ const { from, to, bend, thickness, endShare = 1 } = options;
3942
+ const chordX = to[0] - from[0];
3943
+ const chordY = to[1] - from[1];
3944
+ const chordLength = Math.hypot(chordX, chordY);
3945
+ // the quadratic's control point sits twice as far out as the curve's middle
3946
+ const control = [
3947
+ (from[0] + to[0]) / 2 + ((2 * bend * chordY) / chordLength),
3948
+ (from[1] + to[1]) / 2 - ((2 * bend * chordX) / chordLength),
3949
+ ];
3950
+ const pointAt = (along) => {
3951
+ const rest = 1 - along;
3952
+ return [
3953
+ rest * rest * from[0] + 2 * rest * along * control[0] + along * along * to[0],
3954
+ rest * rest * from[1] + 2 * rest * along * control[1] + along * along * to[1],
3955
+ ];
3956
+ };
3957
+ const tangentAt = (along) => {
3958
+ const tangentX = 2 * (1 - along) * (control[0] - from[0]) + 2 * along * (to[0] - control[0]);
3959
+ const tangentY = 2 * (1 - along) * (control[1] - from[1]) + 2 * along * (to[1] - control[1]);
3960
+ const length = Math.hypot(tangentX, tangentY);
3961
+ return [tangentX / length, tangentY / length];
3962
+ };
3963
+ const thicknessAt = (along) => thickness * (endShare + (1 - endShare) * Math.sin(Math.PI * along));
3964
+ // side 1 = the upper edge (the left normal of a rightward curve), -1 = the lower edge
3965
+ const edge = (along, side) => {
3966
+ const [x, y] = pointAt(along);
3967
+ const [tangentX, tangentY] = tangentAt(along);
3968
+ const offset = (side * thicknessAt(along)) / 2;
3969
+ return [x + tangentY * offset, y - tangentX * offset];
3970
+ };
3971
+ // a point on the half circle around an end: turn -90° = on the upper edge, 0 = the tip, 90° = on the lower edge
3972
+ const capPoint = (along, turn) => {
3973
+ const [x, y] = pointAt(along);
3974
+ const [tangentX, tangentY] = tangentAt(along);
3975
+ const radius = thicknessAt(along) / 2;
3976
+ const outward = along === 1 ? 1 : -1;
3977
+ const alongTangent = outward * Math.cos(turn) * radius;
3978
+ const towardsLower = outward * Math.sin(turn) * radius;
3979
+ return [x + tangentX * alongTangent - tangentY * towardsLower, y + tangentY * alongTangent + tangentX * towardsLower];
3980
+ };
3981
+ // clockwise from the top middle at the same angles as a blob's points (a quarter = the right end, half = the bottom
3982
+ // middle, three quarters = the left end); the points within 30° of an end lie on its half circle, the rest along the
3983
+ // edges
3984
+ const capShare = Math.sin(Math.PI / 3);
3985
+ return Array.from({ length: OUTLINE_POINTS }, (_, index) => {
3986
+ const angle = (2 * Math.PI * index) / OUTLINE_POINTS;
3987
+ const sine = Math.sin(angle);
3988
+ if (Math.abs(sine) < capShare - 1e-9)
3989
+ return edge(0.5 + (0.5 * sine) / capShare, Math.cos(angle) > 0 ? 1 : -1);
3990
+ // the angle's distance past the cap's start, stretched from ±30° around the end to ±90° around the half circle
3991
+ const endAngle = sine > 0 ? Math.PI / 2 : (3 * Math.PI) / 2;
3992
+ return capPoint(sine > 0 ? 1 : 0, (angle - endAngle) * 3);
3993
+ });
3994
+ };
3995
+ /** A piece that isn't there: every point on one spot (draws nothing). */
3996
+ const hidden = (at) => Array.from({ length: OUTLINE_POINTS }, () => [at[0], at[1]]);
3997
+ /** The piece mirrored onto the other half of the face, still clockwise from the top. */
3998
+ const mirrored = (points) => points.map((_, index) => {
3999
+ const [x, y] = points[(OUTLINE_POINTS - index) % OUTLINE_POINTS];
4000
+ return [24 - x, y];
4001
+ });
4002
+ /** A face from its left half (as seen) + the mouth. */
4003
+ const symmetricFace = ({ brow, eye, pupil, mouth }) => ({
4004
+ leftBrow: brow,
4005
+ rightBrow: mirrored(brow),
4006
+ leftEye: eye,
4007
+ rightEye: mirrored(eye),
4008
+ leftPupil: pupil,
4009
+ rightPupil: mirrored(pupil),
4010
+ mouth,
4011
+ });
4012
+ /** The whole face mirrored: left and right pieces swap sides. */
4013
+ const mirroredFace = (face) => ({
4014
+ leftBrow: mirrored(face.rightBrow),
4015
+ rightBrow: mirrored(face.leftBrow),
4016
+ leftEye: mirrored(face.rightEye),
4017
+ rightEye: mirrored(face.leftEye),
4018
+ leftPupil: mirrored(face.rightPupil),
4019
+ rightPupil: mirrored(face.leftPupil),
4020
+ mouth: mirrored(face.mouth),
4021
+ });
4022
+ // ---- the expressions (names from the user's pictures) ----
4023
+ const faceExpressionNames = [
4024
+ 'normal',
4025
+ 'smiling',
4026
+ 'pleading',
4027
+ 'wonder',
4028
+ 'focused',
4029
+ 'fright',
4030
+ 'angry',
4031
+ 'asleep',
4032
+ 'sad',
4033
+ 'surprised',
4034
+ 'laughing',
4035
+ 'tired',
4036
+ 'wink-left',
4037
+ 'wink-right',
4038
+ 'suspicious',
4039
+ 'confused',
4040
+ 'smug',
4041
+ 'irritated',
4042
+ 'bored',
4043
+ 'curious',
4044
+ 'empathetic',
4045
+ 'guilty',
4046
+ 'worried',
4047
+ 'disappointed',
4048
+ 'ashamed',
4049
+ 'embarrassed',
4050
+ 'yearning',
4051
+ 'triumphant',
4052
+ 'proud',
4053
+ 'excited',
4054
+ 'scornful',
4055
+ 'disgusted',
4056
+ ];
4057
+ const buildFaceExpressions = () => {
4058
+ const winkRight = {
4059
+ ...symmetricFace({
4060
+ brow: blob({ centre: [6.2, 4.4], width: 5.2, height: 1.9, squareness: 0.5, rotation: -6 }),
4061
+ eye: blob({ centre: [6.4, 12], width: 8.4, height: 8.4 }),
4062
+ pupil: blob({ centre: [7, 11.4], width: 3.2, height: 3.2 }),
4063
+ mouth: band({ from: [9.6, 18.4], to: [14.8, 18], bend: -0.9, thickness: 1.2, endShare: 0.7 }),
4064
+ }),
4065
+ rightBrow: mirrored(blob({ centre: [6.2, 6], width: 5, height: 1.8, squareness: 0.5, rotation: 8 })),
4066
+ rightEye: mirrored(band({ from: [3.2, 12.8], to: [9.6, 12.8], bend: 1.4, thickness: 1.6, endShare: 0.7 })),
4067
+ rightPupil: hidden([17.6, 12]),
4068
+ };
4069
+ return {
4070
+ normal: symmetricFace({
4071
+ brow: blob({ centre: [6.2, 4.6], width: 5.2, height: 1.9, squareness: 0.5, rotation: -6 }),
4072
+ eye: blob({ centre: [6.4, 12], width: 8.4, height: 8.4 }),
4073
+ pupil: blob({ centre: [7, 10.7], width: 3.2, height: 3.2 }),
4074
+ mouth: blob({ centre: [12, 18.8], width: 4.4, height: 1.8, squareness: 0.6 }),
4075
+ }),
4076
+ smiling: symmetricFace({
4077
+ brow: blob({ centre: [6, 5.4], width: 5, height: 4.4, squareness: 0.2, cutBottom: [5.4, 5.4] }),
4078
+ eye: blob({ centre: [6, 13], width: 9.2, height: 7.6 }),
4079
+ pupil: blob({ centre: [6.8, 13.6], width: 3.6, height: 2.4 }),
4080
+ mouth: band({ from: [9, 18.4], to: [15, 18.4], bend: -1, thickness: 1.3, endShare: 0.7 }),
4081
+ }),
4082
+ pleading: symmetricFace({
4083
+ brow: blob({ centre: [6.6, 3.7], width: 4.6, height: 2, squareness: 0.5, rotation: -16 }),
4084
+ eye: blob({ centre: [6.8, 12.4], width: 8.6, height: 9.6, topScale: 0.75, rotation: 12 }),
4085
+ pupil: blob({ centre: [7.6, 10.7], width: 4, height: 4 }),
4086
+ mouth: blob({ centre: [12, 17.6], width: 4.3, height: 4.8, squareness: 0.3, cutTop: [17.6, 17.6] }),
4087
+ }),
4088
+ wonder: symmetricFace({
4089
+ brow: band({ from: [3.4, 4.8], to: [8.6, 3.3], bend: 0.7, thickness: 1.7 }),
4090
+ eye: blob({ centre: [6.4, 12], width: 7.8, height: 9.6 }),
4091
+ pupil: blob({ centre: [6.8, 11.2], width: 3.2, height: 4.2 }),
4092
+ mouth: blob({ centre: [12, 18.6], width: 3.8, height: 1, squareness: 0.6 }),
4093
+ }),
4094
+ focused: symmetricFace({
4095
+ brow: blob({ centre: [6, 4.4], width: 5.6, height: 2.2, squareness: 0.55, rotation: 3 }),
4096
+ eye: blob({ centre: [6.2, 12.8], width: 9, height: 8, cutTop: [10, 11] }),
4097
+ pupil: blob({ centre: [6.8, 13.2], width: 3.8, height: 2.8 }),
4098
+ mouth: blob({ centre: [12, 18.8], width: 4.2, height: 1, squareness: 0.6 }),
4099
+ }),
4100
+ fright: symmetricFace({
4101
+ brow: blob({ centre: [5.7, 3.5], width: 4.8, height: 2.2, squareness: 0.45, rotation: -8 }),
4102
+ eye: blob({ centre: [6.2, 12.4], width: 8.4, height: 9.8, topScale: 0.7, rotation: 8 }),
4103
+ pupil: blob({ centre: [7.2, 12], width: 3.6, height: 4.2 }),
4104
+ mouth: blob({ centre: [12, 18.6], width: 3.8, height: 1.4, squareness: 0.5 }),
4105
+ }),
4106
+ angry: symmetricFace({
4107
+ brow: blob({ centre: [6.4, 5.4], width: 5.2, height: 2, squareness: 0.5, rotation: 12 }),
4108
+ eye: blob({ centre: [6.2, 12.6], width: 8.6, height: 8, cutTop: [9.2, 12] }),
4109
+ pupil: blob({ centre: [7.4, 12.8], width: 3.4, height: 3.4 }),
4110
+ mouth: blob({ centre: [12, 17.8], width: 4.2, height: 4, squareness: 0.3, cutTop: [17.8, 17.8] }),
4111
+ }),
4112
+ asleep: symmetricFace({
4113
+ brow: blob({ centre: [6, 5.4], width: 4, height: 1.7, squareness: 0.4, rotation: 4 }),
4114
+ eye: band({ from: [3, 12.4], to: [9.6, 12.4], bend: -1.2, thickness: 1.5, endShare: 0.6 }),
4115
+ pupil: hidden([6.3, 13.4]),
4116
+ mouth: blob({ centre: [12, 18.8], width: 2.6, height: 1.8 }),
4117
+ }),
4118
+ // more feelings, Claude's drafts (no pictures); the one-sided ones replace pieces of the right half — written in place
4119
+ // (a blob or band is already clockwise from its top) or drawn on the left and mirrored
4120
+ sad: symmetricFace({
4121
+ brow: blob({ centre: [6.4, 5.2], width: 4.8, height: 1.9, squareness: 0.5, rotation: -18 }),
4122
+ eye: blob({ centre: [6.4, 12.6], width: 8.4, height: 8, cutTop: [11.2, 9.6] }),
4123
+ pupil: blob({ centre: [6.8, 13.8], width: 3.2, height: 3.4 }),
4124
+ mouth: band({ from: [9.4, 19.2], to: [14.6, 19.2], bend: 0.9, thickness: 1.2, endShare: 0.7 }),
4125
+ }),
4126
+ surprised: symmetricFace({
4127
+ brow: band({ from: [3.6, 3.6], to: [8.6, 2.8], bend: 0.6, thickness: 1.6 }),
4128
+ eye: blob({ centre: [6.4, 12], width: 8.6, height: 9 }),
4129
+ pupil: blob({ centre: [6.4, 12], width: 2.4, height: 2.4 }),
4130
+ mouth: blob({ centre: [12, 19], width: 2.8, height: 3.4 }),
4131
+ }),
4132
+ laughing: symmetricFace({
4133
+ brow: blob({ centre: [6, 4.6], width: 5, height: 4.4, squareness: 0.2, cutBottom: [4.6, 4.6] }),
4134
+ eye: band({ from: [3.2, 13], to: [9.6, 13], bend: 1.6, thickness: 1.6, endShare: 0.7 }),
4135
+ pupil: hidden([6.4, 12]),
4136
+ mouth: blob({ centre: [12, 17], width: 6, height: 6, squareness: 0.2, cutTop: [17, 17] }),
4137
+ }),
4138
+ tired: symmetricFace({
4139
+ brow: blob({ centre: [6, 6], width: 5, height: 1.7, squareness: 0.5, rotation: -4 }),
4140
+ eye: blob({ centre: [6.4, 12.6], width: 8.4, height: 7.6, cutTop: [11.6, 11.6] }),
4141
+ pupil: blob({ centre: [6.6, 13.6], width: 3.2, height: 3.2 }),
4142
+ mouth: blob({ centre: [12, 19], width: 3.2, height: 0.9, squareness: 0.6 }),
4143
+ }),
4144
+ // wink-left / wink-right = the eye that closes, as seen; one is the other mirrored
4145
+ 'wink-left': mirroredFace(winkRight),
4146
+ 'wink-right': winkRight,
4147
+ suspicious: {
4148
+ ...symmetricFace({
4149
+ brow: band({ from: [3.4, 4], to: [8.8, 3], bend: 0.8, thickness: 1.6 }),
4150
+ eye: blob({ centre: [6.4, 12.6], width: 8.6, height: 7.6, cutTop: [10.8, 10.8] }),
4151
+ pupil: blob({ centre: [4.6, 13.2], width: 3.2, height: 3 }),
4152
+ mouth: blob({ centre: [12.6, 18.8], width: 3.6, height: 1, squareness: 0.6, rotation: -10 }),
4153
+ }),
4154
+ rightBrow: mirrored(blob({ centre: [6.4, 6.4], width: 5.2, height: 1.8, squareness: 0.5, rotation: 10 })),
4155
+ rightEye: mirrored(blob({ centre: [6.4, 12.8], width: 8.6, height: 7.2, cutTop: [11.8, 11.8] })),
4156
+ rightPupil: blob({ centre: [16.2, 13.4], width: 3.2, height: 3 }),
4157
+ },
4158
+ confused: {
4159
+ ...symmetricFace({
4160
+ brow: band({ from: [3.4, 3.8], to: [8.6, 2.6], bend: 0.7, thickness: 1.6 }),
4161
+ eye: blob({ centre: [6.4, 12], width: 8.6, height: 9 }),
4162
+ pupil: blob({ centre: [7.6, 10.6], width: 3, height: 3 }),
4163
+ mouth: blob({ centre: [12.4, 19], width: 3.8, height: 1.1, squareness: 0.5, rotation: 12 }),
4164
+ }),
4165
+ rightBrow: mirrored(blob({ centre: [6.2, 5.8], width: 5, height: 1.8, squareness: 0.5, rotation: 10 })),
4166
+ rightEye: mirrored(blob({ centre: [6.4, 12.6], width: 7.4, height: 7.4 })),
4167
+ rightPupil: blob({ centre: [18.4, 11.4], width: 2.8, height: 2.8 }),
4168
+ },
4169
+ // the user's list of feelings (2026-11-01), Claude's drafts; the ones the set had already are skipped
4170
+ smug: {
4171
+ ...symmetricFace({
4172
+ brow: blob({ centre: [6.2, 5], width: 5, height: 1.8, squareness: 0.5, rotation: -4 }),
4173
+ eye: blob({ centre: [6.4, 12.6], width: 8.4, height: 7.6, cutTop: [11.4, 11.4] }),
4174
+ pupil: blob({ centre: [8, 13.4], width: 3, height: 3 }),
4175
+ mouth: band({ from: [10.2, 18.8], to: [15.2, 17.6], bend: -0.6, thickness: 1.2, endShare: 0.6 }),
4176
+ }),
4177
+ rightBrow: mirrored(band({ from: [3.6, 4.2], to: [8.6, 3.4], bend: 0.6, thickness: 1.6 })),
4178
+ rightPupil: blob({ centre: [19.4, 13.4], width: 3, height: 3 }),
4179
+ },
4180
+ irritated: symmetricFace({
4181
+ brow: blob({ centre: [6.4, 5.8], width: 5.4, height: 1.8, squareness: 0.55, rotation: 6 }),
4182
+ eye: blob({ centre: [6.4, 12.8], width: 8.6, height: 7.6, cutTop: [11.2, 11.8], cutBottom: [15.8, 15.8] }),
4183
+ pupil: blob({ centre: [6.8, 13.6], width: 3.2, height: 3 }),
4184
+ mouth: blob({ centre: [12.4, 18.8], width: 3.6, height: 0.9, squareness: 0.6, rotation: -8 }),
4185
+ }),
4186
+ bored: {
4187
+ ...symmetricFace({
4188
+ brow: blob({ centre: [6, 6.2], width: 5, height: 1.7, squareness: 0.5 }),
4189
+ eye: blob({ centre: [6.4, 12.6], width: 8.4, height: 7.6, cutTop: [11.8, 11.8] }),
4190
+ pupil: blob({ centre: [8, 12.8], width: 3, height: 3 }),
4191
+ mouth: blob({ centre: [13.4, 19], width: 3, height: 0.9, squareness: 0.6 }),
4192
+ }),
4193
+ rightPupil: blob({ centre: [19.4, 12.8], width: 3, height: 3 }),
4194
+ },
4195
+ curious: {
4196
+ ...symmetricFace({
4197
+ brow: band({ from: [3.4, 3.6], to: [8.6, 2.6], bend: 0.7, thickness: 1.6 }),
4198
+ eye: blob({ centre: [6.4, 12], width: 8.6, height: 8.8 }),
4199
+ pupil: blob({ centre: [7.8, 10.8], width: 3.2, height: 3.2 }),
4200
+ mouth: blob({ centre: [12.6, 18.8], width: 2, height: 2.2 }),
4201
+ }),
4202
+ rightBrow: mirrored(blob({ centre: [6.2, 5.2], width: 5, height: 1.8, squareness: 0.5, rotation: 4 })),
4203
+ rightPupil: blob({ centre: [19, 10.8], width: 3.2, height: 3.2 }),
4204
+ },
4205
+ empathetic: symmetricFace({
4206
+ brow: blob({ centre: [6.4, 4.8], width: 5, height: 1.9, squareness: 0.5, rotation: -10 }),
4207
+ eye: blob({ centre: [6.4, 12.4], width: 8.4, height: 8, cutTop: [10, 9.2] }),
4208
+ pupil: blob({ centre: [6.8, 13], width: 3.4, height: 3.4 }),
4209
+ mouth: band({ from: [10, 18.6], to: [14, 18.6], bend: -0.5, thickness: 1.1, endShare: 0.7 }),
4210
+ }),
4211
+ guilty: {
4212
+ ...symmetricFace({
4213
+ brow: blob({ centre: [6.4, 5], width: 4.6, height: 1.8, squareness: 0.5, rotation: -14 }),
4214
+ eye: blob({ centre: [6.4, 12.6], width: 8.2, height: 8, cutTop: [10.6, 10.2] }),
4215
+ pupil: blob({ centre: [5, 14.4], width: 3, height: 3 }),
4216
+ mouth: blob({ centre: [11.4, 19], width: 2.8, height: 0.9, squareness: 0.6, rotation: 6 }),
4217
+ }),
4218
+ rightPupil: blob({ centre: [16.2, 14.4], width: 3, height: 3 }),
4219
+ },
4220
+ worried: symmetricFace({
4221
+ brow: blob({ centre: [6.6, 4.2], width: 4.8, height: 1.9, squareness: 0.5, rotation: -20 }),
4222
+ eye: blob({ centre: [6.4, 12.2], width: 8, height: 8.8, topScale: 0.85, rotation: 6 }),
4223
+ pupil: blob({ centre: [6.8, 12.4], width: 2.6, height: 2.8 }),
4224
+ mouth: band({ from: [10.2, 19], to: [13.8, 19], bend: 0.6, thickness: 1.1, endShare: 0.7 }),
4225
+ }),
4226
+ disappointed: symmetricFace({
4227
+ brow: blob({ centre: [6.2, 5.6], width: 5, height: 1.8, squareness: 0.5, rotation: -8 }),
4228
+ eye: blob({ centre: [6.4, 12.8], width: 8.4, height: 7.6, cutTop: [11.8, 11] }),
4229
+ pupil: blob({ centre: [6.6, 14.4], width: 3, height: 3 }),
4230
+ mouth: band({ from: [9.6, 19.2], to: [14.4, 19.2], bend: 0.4, thickness: 1.1, endShare: 0.8 }),
4231
+ }),
4232
+ ashamed: symmetricFace({
4233
+ brow: blob({ centre: [6.4, 5.6], width: 4.6, height: 1.8, squareness: 0.5, rotation: -16 }),
4234
+ eye: blob({ centre: [6.4, 13], width: 8.2, height: 7.6, cutTop: [12.2, 12.2] }),
4235
+ pupil: blob({ centre: [6.4, 15], width: 3, height: 2.6 }),
4236
+ mouth: blob({ centre: [12, 19.4], width: 2.4, height: 0.9, squareness: 0.6 }),
4237
+ }),
4238
+ embarrassed: {
4239
+ ...symmetricFace({
4240
+ brow: blob({ centre: [6.2, 4.8], width: 4.8, height: 1.8, squareness: 0.5, rotation: -12 }),
4241
+ eye: blob({ centre: [6.4, 12.4], width: 8.2, height: 8.2 }),
4242
+ pupil: blob({ centre: [4.4, 13.6], width: 3, height: 3 }),
4243
+ mouth: band({ from: [9.8, 18.8], to: [14.2, 18.6], bend: -0.5, thickness: 1.1, endShare: 0.7 }),
4244
+ }),
4245
+ rightPupil: blob({ centre: [15.8, 13.6], width: 3, height: 3 }),
4246
+ },
4247
+ yearning: symmetricFace({
4248
+ brow: blob({ centre: [6.2, 4], width: 4.8, height: 1.8, squareness: 0.5, rotation: -10 }),
4249
+ eye: blob({ centre: [6.4, 12.6], width: 8.6, height: 8, cutTop: [11, 11] }),
4250
+ pupil: blob({ centre: [6.8, 13.6], width: 4.2, height: 4.2 }),
4251
+ mouth: blob({ centre: [12, 18.8], width: 2.6, height: 1.6 }),
4252
+ }),
4253
+ triumphant: symmetricFace({
4254
+ brow: blob({ centre: [6.2, 4], width: 5.2, height: 2, squareness: 0.5, rotation: 8 }),
4255
+ eye: blob({ centre: [6.4, 11.8], width: 8.6, height: 8.6, cutBottom: [14, 14] }),
4256
+ pupil: blob({ centre: [6.8, 11.6], width: 3.4, height: 3.4 }),
4257
+ mouth: blob({ centre: [12, 16.8], width: 7, height: 6.4, squareness: 0.3, cutTop: [16.8, 16.8] }),
4258
+ }),
4259
+ proud: symmetricFace({
4260
+ brow: blob({ centre: [6.2, 3.8], width: 5, height: 1.9, squareness: 0.5, rotation: -4 }),
4261
+ eye: blob({ centre: [6.4, 12.4], width: 8.6, height: 8, cutTop: [11, 11] }),
4262
+ pupil: blob({ centre: [6.6, 13.6], width: 3.2, height: 3 }),
4263
+ mouth: band({ from: [8.8, 18], to: [15.2, 18], bend: -1.2, thickness: 1.3, endShare: 0.7 }),
4264
+ }),
4265
+ excited: symmetricFace({
4266
+ brow: band({ from: [3.4, 3.6], to: [8.6, 2.8], bend: 0.8, thickness: 1.6 }),
4267
+ eye: blob({ centre: [6.4, 12], width: 9, height: 9.2 }),
4268
+ pupil: blob({ centre: [7, 11], width: 4, height: 4 }),
4269
+ mouth: blob({ centre: [12, 16.8], width: 6, height: 6, squareness: 0.2, cutTop: [16.8, 16.8] }),
4270
+ }),
4271
+ scornful: {
4272
+ ...symmetricFace({
4273
+ brow: blob({ centre: [6.2, 5.4], width: 5, height: 1.8, squareness: 0.5, rotation: 2 }),
4274
+ eye: blob({ centre: [6.4, 12.6], width: 8.4, height: 7.6, cutTop: [11.4, 11.4] }),
4275
+ pupil: blob({ centre: [5.4, 14], width: 3, height: 3 }),
4276
+ mouth: band({ from: [9.2, 18.8], to: [14.6, 18], bend: -0.2, thickness: 1.1, endShare: 0.7 }),
4277
+ }),
4278
+ rightBrow: mirrored(band({ from: [3.6, 4], to: [8.6, 3.2], bend: 0.6, thickness: 1.6 })),
4279
+ rightPupil: blob({ centre: [16.6, 14], width: 3, height: 3 }),
4280
+ },
4281
+ disgusted: symmetricFace({
4282
+ brow: blob({ centre: [6.6, 6], width: 5, height: 1.8, squareness: 0.5, rotation: 14 }),
4283
+ eye: blob({ centre: [6.4, 12.6], width: 8.4, height: 7.6, cutTop: [11.4, 12.2], cutBottom: [14.8, 14.8] }),
4284
+ pupil: blob({ centre: [6.8, 13.2], width: 3, height: 2.8 }),
4285
+ // an upside-down "D": open, corners pulled down
4286
+ mouth: blob({ centre: [12, 19.6], width: 5, height: 4.8, squareness: 0.2, cutBottom: [19.6, 19.6] }),
4287
+ }),
4288
+ };
4289
+ };
4290
+ let builtExpressions = null;
4291
+ /** Every expression's pieces, built on first use (nothing runs at import). */
4292
+ const faceExpressionPieces = () => (builtExpressions ??= buildFaceExpressions());
4293
+ // ---- morphing and drawing ----
4294
+ const interpolatePoints = (from, to, progress) => from.map(([fromX, fromY], index) => [fromX + (to[index][0] - fromX) * progress, fromY + (to[index][1] - fromY) * progress]);
4295
+ const interpolateFace = (from, to, progress) => Object.fromEntries(PIECE_NAMES.map((name) => [name, interpolatePoints(from[name], to[name], progress)]));
4296
+ const boxCentre = (points) => {
4297
+ const xs = points.map(([x]) => x);
4298
+ const ys = points.map(([, y]) => y);
4299
+ return [(Math.min(...xs) + Math.max(...xs)) / 2, (Math.min(...ys) + Math.max(...ys)) / 2];
4300
+ };
4301
+ /** The face with its eyes partly closed: openness 1 = as drawn, 0 = shut to a flat slit (pupils gone). */
4302
+ const blinked = (face, openness) => {
4303
+ if (openness >= 1)
4304
+ return face;
4305
+ const eyeScale = 0.25 + 0.75 * openness;
4306
+ const squash = (points, middleY) => points.map(([x, y]) => [x, middleY + (y - middleY) * eyeScale]);
4307
+ const shrink = (points, [centreX, centreY]) => points.map(([x, y]) => [centreX + (x - centreX) * openness, centreY + (y - centreY) * openness]);
4308
+ const leftMiddle = boxCentre(face.leftEye)[1];
4309
+ const rightMiddle = boxCentre(face.rightEye)[1];
4310
+ return {
4311
+ ...face,
4312
+ leftEye: squash(face.leftEye, leftMiddle),
4313
+ rightEye: squash(face.rightEye, rightMiddle),
4314
+ leftPupil: squash(shrink(face.leftPupil, boxCentre(face.leftPupil)), leftMiddle),
4315
+ rightPupil: squash(shrink(face.rightPupil, boxCentre(face.rightPupil)), rightMiddle),
4316
+ };
4317
+ };
4318
+ const formatNumber = (value) => String(Math.round(value * 1000) / 1000);
4319
+ /** A smooth closed curve through the points: each handle points along the neighbours' chord and is a third of its own
4320
+ * segment long (no loops where points sit close together, as at a band's round end). Empty for a hidden piece. */
4321
+ const smoothClosedPath = (points) => {
4322
+ const [firstX, firstY] = points[0];
4323
+ if (points.every(([x, y]) => Math.abs(x - firstX) < 1e-6 && Math.abs(y - firstY) < 1e-6))
4324
+ return '';
4325
+ const count = points.length;
4326
+ const directionAt = (index) => {
4327
+ const previous = points[(index - 1 + count) % count];
4328
+ const next = points[(index + 1) % count];
4329
+ const length = Math.hypot(next[0] - previous[0], next[1] - previous[1]) || 1;
4330
+ return [(next[0] - previous[0]) / length, (next[1] - previous[1]) / length];
4331
+ };
4332
+ let path = `M${formatNumber(firstX)} ${formatNumber(firstY)}`;
4333
+ for (let index = 0; index < count; index++) {
4334
+ const start = points[index];
4335
+ const end = points[(index + 1) % count];
4336
+ const handleLength = Math.hypot(end[0] - start[0], end[1] - start[1]) / 3;
4337
+ const startDirection = directionAt(index);
4338
+ const endDirection = directionAt(index + 1);
4339
+ const control1 = [start[0] + startDirection[0] * handleLength, start[1] + startDirection[1] * handleLength];
4340
+ const control2 = [end[0] - endDirection[0] * handleLength, end[1] - endDirection[1] * handleLength];
4341
+ path += `C${[...control1, ...control2, ...end].map(formatNumber).join(' ')}`;
4342
+ }
4343
+ return `${path}Z`;
4344
+ };
4345
+ const facePaths = (face) => ({
4346
+ features: [face.leftBrow, face.rightBrow, face.leftEye, face.rightEye, face.mouth].map(smoothClosedPath).join(''),
4347
+ eyes: [face.leftEye, face.rightEye].map(smoothClosedPath).join(''),
4348
+ pupils: [face.leftPupil, face.rightPupil].map(smoothClosedPath).join(''),
4349
+ });
4350
+ return { OUTLINE_POINTS, PIECE_NAMES, faceExpressionNames, faceExpressionPieces, interpolateFace, blinked, smoothClosedPath, facePaths };
4351
+ })();
4352
+
4353
+ // face.ts
4354
+ const { face } = (() => {
4355
+ // Layer 4: a face that morphs between feelings. 7 soft filled pieces (sets/faces.ts) — brows, eyes and mouth in the
4356
+ // CSS color, the pupils in their own color and clipped to the eyes. Blinking runs on top of any expression, by itself
4357
+ // every few seconds (option) or on blink().
4358
+ const BLINK_CLOSING = 90;
4359
+ const BLINK_SHUT = 50;
4360
+ const BLINK_OPENING = 150;
4361
+ const AUTO_BLINK_MIN_PAUSE = 2500;
4362
+ const AUTO_BLINK_EXTRA_PAUSE = 3500;
4363
+ const DEFAULT_FACE_DURATION = DEFAULT_DURATION + 100;
4364
+ /** Expressions whose eyes are already shut: blinking does nothing. */
4365
+ const EYES_SHUT = ['asleep', 'laughing'];
4366
+ let faceCount = 0;
4367
+ const checkedExpression = (state) => {
4368
+ if (!faceExpressionNames.includes(state))
4369
+ throw new Error(`morphico: face() has no expression "${state}"`);
4370
+ return state;
4371
+ };
4372
+ const resolveEasing = (easing) => (typeof easing === 'function' ? easing : easings[easing ?? 'easeInOutCubic']);
4373
+ /** Adds a morphing face (an <svg>) to `element`. */
4374
+ const face = (element, options = {}) => {
4375
+ const { state: firstState = 'normal', pupilColor = 'white', blink: blinksByItself = true, size = '100%', ...defaultMorphOptions } = options;
4376
+ const expressions = faceExpressionPieces();
4377
+ let state = checkedExpression(firstState);
4378
+ let shownPieces = expressions[state];
4379
+ let openness = 1;
4380
+ const clipId = `morphico-face-${++faceCount}`;
4381
+ const svg = createSvgElement('svg', { viewBox: '0 0 24 24', fill: 'currentColor', role: 'img' });
4382
+ svg.style.width = size;
4383
+ svg.style.height = size;
4384
+ svg.style.overflow = 'visible';
4385
+ const clipPath = svg.appendChild(createSvgElement('clipPath', { id: clipId }));
4386
+ const eyesPath = clipPath.appendChild(createSvgElement('path'));
4387
+ const featuresPath = svg.appendChild(createSvgElement('path'));
4388
+ const pupilsPath = svg.appendChild(createSvgElement('path', { fill: pupilColor, 'clip-path': `url(#${clipId})` }));
4389
+ const render = () => {
4390
+ const paths = facePaths(blinked(shownPieces, openness));
4391
+ featuresPath.setAttribute('d', paths.features);
4392
+ eyesPath.setAttribute('d', paths.eyes);
4393
+ pupilsPath.setAttribute('d', paths.pupils);
4394
+ };
4395
+ const updateLabel = () => svg.setAttribute('aria-label', state);
4396
+ render();
4397
+ updateLabel();
4398
+ element.appendChild(svg);
4399
+ // ---- morphing ----
4400
+ let morph = null;
4401
+ const endMorph = (interrupted) => {
4402
+ if (!morph)
4403
+ return;
4404
+ const { stopAnimation, resolve, state: morphState } = morph;
4405
+ morph = null;
4406
+ stopAnimation();
4407
+ resolve({ state: morphState, interrupted });
4408
+ };
4409
+ const morphTo = (nextState, morphOptions) => {
4410
+ endMorph(true);
4411
+ state = nextState;
4412
+ updateLabel();
4413
+ const fromPieces = shownPieces;
4414
+ const toPieces = expressions[nextState];
4415
+ const duration = morphOptions.duration ?? DEFAULT_FACE_DURATION;
4416
+ if (prefersReducedMotion() || duration <= 0) {
4417
+ shownPieces = toPieces;
4418
+ render();
4419
+ return Promise.resolve({ state: nextState, interrupted: false });
4420
+ }
4421
+ const easing = resolveEasing(morphOptions.easing);
4422
+ let startTime = null;
4423
+ return new Promise((resolve) => {
4424
+ const stopAnimation = animate((frameTime) => {
4425
+ startTime ??= frameTime;
4426
+ const progress = Math.min((frameTime - startTime) / duration, 1);
4427
+ shownPieces = interpolateFace(fromPieces, toPieces, easing(progress));
4428
+ render();
4429
+ if (progress < 1)
4430
+ return true;
4431
+ endMorph(false);
4432
+ return false;
4433
+ });
4434
+ morph = { stopAnimation, resolve, state: nextState };
4435
+ });
4436
+ };
4437
+ // ---- blinking ----
4438
+ let stopBlink = null;
4439
+ let finishBlink = null;
4440
+ const endBlink = () => {
4441
+ stopBlink?.();
4442
+ stopBlink = null;
4443
+ if (openness < 1) {
4444
+ openness = 1;
4445
+ render();
4446
+ }
4447
+ finishBlink?.();
4448
+ finishBlink = null;
4449
+ };
4450
+ const blink = () => {
4451
+ if (EYES_SHUT.includes(state) || prefersReducedMotion())
4452
+ return Promise.resolve();
4453
+ endBlink();
4454
+ let startTime = null;
4455
+ return new Promise((resolve) => {
4456
+ finishBlink = resolve;
4457
+ stopBlink = animate((frameTime) => {
4458
+ startTime ??= frameTime;
4459
+ const elapsed = frameTime - startTime;
4460
+ if (elapsed < BLINK_CLOSING)
4461
+ openness = 1 - elapsed / BLINK_CLOSING;
4462
+ else if (elapsed < BLINK_CLOSING + BLINK_SHUT)
4463
+ openness = 0;
4464
+ else
4465
+ openness = Math.min((elapsed - BLINK_CLOSING - BLINK_SHUT) / BLINK_OPENING, 1);
4466
+ render();
4467
+ if (openness < 1 || elapsed < BLINK_CLOSING)
4468
+ return true;
4469
+ stopBlink = null;
4470
+ finishBlink = null;
4471
+ resolve();
4472
+ return false;
4473
+ });
4474
+ });
4475
+ };
4476
+ let autoBlinkTimer;
4477
+ const scheduleAutoBlink = () => {
4478
+ autoBlinkTimer = setTimeout(() => {
4479
+ if (typeof document === 'undefined' || !document.hidden)
4480
+ void blink();
4481
+ scheduleAutoBlink();
4482
+ }, AUTO_BLINK_MIN_PAUSE + Math.random() * AUTO_BLINK_EXTRA_PAUSE);
4483
+ };
4484
+ if (blinksByItself && !prefersReducedMotion())
4485
+ scheduleAutoBlink();
4486
+ return {
4487
+ get state() { return state; },
4488
+ to(nextState, { onEnd, ...morphOptions } = {}) {
4489
+ const result = morphTo(checkedExpression(nextState), { ...defaultMorphOptions, ...morphOptions });
4490
+ if (onEnd)
4491
+ result.then(onEnd);
4492
+ return result;
4493
+ },
4494
+ blink,
4495
+ destroy() {
4496
+ clearTimeout(autoBlinkTimer);
4497
+ endMorph(true);
4498
+ endBlink();
4499
+ svg.remove();
4500
+ },
4501
+ };
4502
+ };
4503
+ return { face };
4504
+ })();
4505
+
3867
4506
  // icon.ts
3868
4507
  const { icon } = (() => {
3869
4508
  // Layer 1: one icon that morphs between the states of a set, e.g. a single digit.
@@ -3998,6 +4637,6 @@ return { text };
3998
4637
  // index.ts
3999
4638
  // morphico public API.
4000
4639
  /** Package version, e.g. "0.1.0" (tools/build.ts replaces 'dev'). */
4001
- const version = '0.3.0';
4640
+ const version = '0.4.0';
4002
4641
 
4003
- export { clip, easings, icon, iconSets, text, version };
4642
+ export { clip, easings, face, icon, iconSets, faceExpressionNames, text, version };