shape-morph 0.1.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.
@@ -0,0 +1,219 @@
1
+ declare const distanceEpsilon = 0.0001;
2
+ declare const angleEpsilon = 0.000001;
3
+ declare const relaxedDistanceEpsilon = 0.005;
4
+ declare const floatPi: number;
5
+ declare const twoPi: number;
6
+ interface Point {
7
+ x: number;
8
+ y: number;
9
+ }
10
+
11
+ declare class Cubic {
12
+ readonly points: Float64Array;
13
+ constructor(points: Float64Array);
14
+ constructor(ax0: number, ay0: number, cx0: number, cy0: number, cx1: number, cy1: number, ax1: number, ay1: number);
15
+ get anchor0X(): number;
16
+ get anchor0Y(): number;
17
+ get control0X(): number;
18
+ get control0Y(): number;
19
+ get control1X(): number;
20
+ get control1Y(): number;
21
+ get anchor1X(): number;
22
+ get anchor1Y(): number;
23
+ pointOnCurve(t: number): Point;
24
+ zeroLength(): boolean;
25
+ convexTo(next: Cubic): boolean;
26
+ split(t: number): [Cubic, Cubic];
27
+ reverse(): Cubic;
28
+ transformed(f: (x: number, y: number) => Point): Cubic;
29
+ calculateBounds(approximate?: boolean): [number, number, number, number];
30
+ static straightLine(x0: number, y0: number, x1: number, y1: number): Cubic;
31
+ static circularArc(centerX: number, centerY: number, x0: number, y0: number, x1: number, y1: number): Cubic;
32
+ static empty(x0: number, y0: number): Cubic;
33
+ }
34
+ type Feature = {
35
+ type: "edge";
36
+ cubics: Cubic[];
37
+ } | {
38
+ type: "corner";
39
+ cubics: Cubic[];
40
+ convex: boolean;
41
+ };
42
+
43
+ interface CornerRounding {
44
+ radius: number;
45
+ smoothing: number;
46
+ }
47
+ declare const unrounded: CornerRounding;
48
+ declare function cornerRounding(radius: number, smoothing?: number): CornerRounding;
49
+ declare class RoundedPolygon {
50
+ readonly features: Feature[];
51
+ readonly center: Point;
52
+ readonly cubics: Cubic[];
53
+ constructor(features: Feature[], center: Point);
54
+ get centerX(): number;
55
+ get centerY(): number;
56
+ transformed(f: (x: number, y: number) => Point): RoundedPolygon;
57
+ normalized(): RoundedPolygon;
58
+ calculateBounds(approximate?: boolean): [number, number, number, number];
59
+ calculateMaxBounds(): [number, number, number, number];
60
+ }
61
+ declare function createPolygonFromVertices(vertices: number[], rounding?: CornerRounding, perVertexRounding?: CornerRounding[] | null, centerX?: number, centerY?: number): RoundedPolygon;
62
+ declare function createPolygon(numVertices: number, radius?: number, centerX?: number, centerY?: number, rounding?: CornerRounding, perVertexRounding?: CornerRounding[] | null): RoundedPolygon;
63
+ declare function createCircle(numVertices?: number, radius?: number, centerX?: number, centerY?: number): RoundedPolygon;
64
+ declare function createRectangle(width?: number, height?: number, rounding?: CornerRounding, perVertexRounding?: CornerRounding[] | null, centerX?: number, centerY?: number): RoundedPolygon;
65
+ declare function createStar(numVerticesPerRadius: number, radius?: number, innerRadius?: number, rounding?: CornerRounding, innerRounding?: CornerRounding | null, perVertexRounding?: CornerRounding[] | null, centerX?: number, centerY?: number): RoundedPolygon;
66
+
67
+ declare function circle(): RoundedPolygon;
68
+ declare function square(): RoundedPolygon;
69
+ declare function slanted(): RoundedPolygon;
70
+ declare function arch(): RoundedPolygon;
71
+ declare function fan(): RoundedPolygon;
72
+ declare function arrow(): RoundedPolygon;
73
+ declare function semiCircle(): RoundedPolygon;
74
+ declare function oval(): RoundedPolygon;
75
+ declare function pill(): RoundedPolygon;
76
+ declare function triangle(): RoundedPolygon;
77
+ declare function diamond(): RoundedPolygon;
78
+ declare function clamShell(): RoundedPolygon;
79
+ declare function pentagon(): RoundedPolygon;
80
+ declare function gem(): RoundedPolygon;
81
+ declare function sunny(): RoundedPolygon;
82
+ declare function verySunny(): RoundedPolygon;
83
+ declare function cookie4(): RoundedPolygon;
84
+ declare function cookie6(): RoundedPolygon;
85
+ declare function cookie7(): RoundedPolygon;
86
+ declare function cookie9(): RoundedPolygon;
87
+ declare function cookie12(): RoundedPolygon;
88
+ declare function ghostish(): RoundedPolygon;
89
+ declare function clover4(): RoundedPolygon;
90
+ declare function clover8(): RoundedPolygon;
91
+ declare function burst(): RoundedPolygon;
92
+ declare function softBurst(): RoundedPolygon;
93
+ declare function boom(): RoundedPolygon;
94
+ declare function softBoom(): RoundedPolygon;
95
+ declare function flower(): RoundedPolygon;
96
+ declare function puffy(): RoundedPolygon;
97
+ declare function puffyDiamond(): RoundedPolygon;
98
+ declare function pixelCircle(): RoundedPolygon;
99
+ declare function pixelTriangle(): RoundedPolygon;
100
+ declare function bun(): RoundedPolygon;
101
+ declare function heart(): RoundedPolygon;
102
+ declare const shapeFactories: {
103
+ Circle: typeof circle;
104
+ Square: typeof square;
105
+ Slanted: typeof slanted;
106
+ Arch: typeof arch;
107
+ Fan: typeof fan;
108
+ Arrow: typeof arrow;
109
+ SemiCircle: typeof semiCircle;
110
+ Oval: typeof oval;
111
+ Pill: typeof pill;
112
+ Triangle: typeof triangle;
113
+ Diamond: typeof diamond;
114
+ ClamShell: typeof clamShell;
115
+ Pentagon: typeof pentagon;
116
+ Gem: typeof gem;
117
+ Sunny: typeof sunny;
118
+ VerySunny: typeof verySunny;
119
+ Cookie4Sided: typeof cookie4;
120
+ Cookie6Sided: typeof cookie6;
121
+ Cookie7Sided: typeof cookie7;
122
+ Cookie9Sided: typeof cookie9;
123
+ Cookie12Sided: typeof cookie12;
124
+ Ghostish: typeof ghostish;
125
+ Clover4Leaf: typeof clover4;
126
+ Clover8Leaf: typeof clover8;
127
+ Burst: typeof burst;
128
+ SoftBurst: typeof softBurst;
129
+ Boom: typeof boom;
130
+ SoftBoom: typeof softBoom;
131
+ Flower: typeof flower;
132
+ Puffy: typeof puffy;
133
+ PuffyDiamond: typeof puffyDiamond;
134
+ PixelCircle: typeof pixelCircle;
135
+ PixelTriangle: typeof pixelTriangle;
136
+ Bun: typeof bun;
137
+ Heart: typeof heart;
138
+ };
139
+ type ShapeName = keyof typeof shapeFactories;
140
+ declare const shapeNames: ShapeName[];
141
+ declare function getShape(name: ShapeName): RoundedPolygon;
142
+
143
+ declare class Morph {
144
+ private readonly morphMatch;
145
+ constructor(start: RoundedPolygon, end: RoundedPolygon);
146
+ asCubics(progress: number): Cubic[];
147
+ }
148
+
149
+ /**
150
+ * Convert cubics to a CSS `clip-path: path("...")` value.
151
+ *
152
+ * Uses SVG path syntax inside clip-path. Note that CSS cannot
153
+ * transition between different `path()` values — use `toClipPathPolygon`
154
+ * for animatable shapes.
155
+ */
156
+ declare function toClipPathPath(cubics: Cubic[], size?: number): string;
157
+ /**
158
+ * Sample points along cubic bezier curves to produce a
159
+ * CSS `clip-path: polygon(...)` value using percentage coordinates.
160
+ *
161
+ * This is the key function for CSS transitions with Tailwind.
162
+ * CSS can transition between `polygon()` values that have the same
163
+ * number of vertices. By using a fixed `samplesPerCubic`, all shapes
164
+ * from the same `Morph` produce polygons with identical vertex counts,
165
+ * enabling pure-CSS morphing:
166
+ *
167
+ * ```
168
+ * const morph = new Morph(getShape('Circle'), getShape('Heart'));
169
+ * const start = toClipPathPolygon(morph.asCubics(0));
170
+ * const end = toClipPathPolygon(morph.asCubics(1));
171
+ * // Both have the same vertex count → CSS can transition between them
172
+ * ```
173
+ *
174
+ * @param cubics - Array of Cubic bezier curves (normalized 0–1)
175
+ * @param samplesPerCubic - Points to sample per cubic segment (default 4)
176
+ * @returns CSS `polygon(...)` string with percentage coordinates
177
+ */
178
+ declare function toClipPathPolygon(cubics: Cubic[], samplesPerCubic?: number): string;
179
+ /**
180
+ * Pre-compute a pair of `polygon()` strings from two shapes that are
181
+ * guaranteed to have the same vertex count. This enables pure-CSS
182
+ * transitions between the two shapes.
183
+ *
184
+ * The `Morph` class internally aligns both shapes so they have the same
185
+ * number of cubic segments. Evaluating at progress 0 and 1 gives two
186
+ * cubic arrays with identical segment counts, which produce polygon
187
+ * strings with identical vertex counts when sampled.
188
+ *
189
+ * ```tsx
190
+ * const [from, to] = toMorphPair(getShape('Circle'), getShape('Heart'));
191
+ * // Use as: style={{ clipPath: from }}
192
+ * // Hover: style={{ clipPath: to }}
193
+ * // With: transition: clip-path 500ms ease
194
+ * ```
195
+ *
196
+ * @param start - The starting shape
197
+ * @param end - The ending shape
198
+ * @param samplesPerCubic - Points to sample per cubic segment (default 4)
199
+ * @returns A tuple of [startPolygon, endPolygon] CSS `polygon()` strings
200
+ */
201
+ declare function toMorphPair(start: RoundedPolygon, end: RoundedPolygon, samplesPerCubic?: number): [string, string];
202
+
203
+ /**
204
+ * Convert an array of Cubics to an SVG path `d` attribute string.
205
+ *
206
+ * Each Cubic stores four points: anchor0 (start), control0, control1,
207
+ * anchor1 (end). These map to the SVG cubic bezier command:
208
+ * C control0X,control0Y control1X,control1Y anchor1X,anchor1Y
209
+ *
210
+ * Shapes from `getShape()` are normalized to 0–1 coordinates.
211
+ * The `size` parameter scales them to pixel/viewBox space.
212
+ */
213
+ declare function toPathD(cubics: Cubic[], size?: number): string;
214
+ /**
215
+ * Convert a RoundedPolygon to an SVG path `d` attribute string.
216
+ */
217
+ declare function toSvgPath(polygon: RoundedPolygon, size?: number): string;
218
+
219
+ export { type CornerRounding, Cubic, type Feature, Morph, type Point, RoundedPolygon, type ShapeName, angleEpsilon, cornerRounding, createCircle, createPolygon, createPolygonFromVertices, createRectangle, createStar, distanceEpsilon, floatPi, getShape, relaxedDistanceEpsilon, shapeNames, toClipPathPath, toClipPathPolygon, toMorphPair, toPathD, toSvgPath, twoPi, unrounded };