patternl 0.0.0-stage → 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,359 @@
1
+ import * as react from 'react';
2
+
3
+ /**
4
+ * Controls how a numeric property changes with each successive stamp copy.
5
+ * - fixed: same value every copy (use `min`)
6
+ * - sequence: starts at `min`, increments by `amount` each copy, clamps to `max`
7
+ * - grade: evenly distributes across `steps` slots between `min` and `max`, cycling
8
+ * - random: seeded pseudo-random within [min, max]
9
+ */
10
+ type NumericStepMode = {
11
+ mode: "fixed";
12
+ } | {
13
+ mode: "sequence";
14
+ amount: number;
15
+ } | {
16
+ mode: "grade";
17
+ steps: number;
18
+ } | {
19
+ mode: "random";
20
+ seed: number;
21
+ };
22
+ interface NumericRange {
23
+ min: number;
24
+ max: number;
25
+ step: NumericStepMode;
26
+ }
27
+ /**
28
+ * Controls how colors cycle across stamp copies.
29
+ * - sequence: round-robin through the colors list
30
+ * - random: seeded pseudo-random pick from the colors list
31
+ */
32
+ type ColorStepMode = {
33
+ mode: "sequence";
34
+ } | {
35
+ mode: "random";
36
+ seed: number;
37
+ };
38
+ /**
39
+ * How a color is applied to a source.
40
+ * - source-in: fills the whole shape (silhouette)
41
+ * - multiply / overlay: blends with the source's own colors
42
+ * - luminance: light areas take the color and dark areas turn transparent, so a
43
+ * logo's dark details (holes, inner shapes) survive as cutouts
44
+ */
45
+ type TintMode = "multiply" | "source-in" | "overlay" | "luminance";
46
+ interface ColorConfig {
47
+ colors: string[];
48
+ step: ColorStepMode;
49
+ tintMode: TintMode;
50
+ }
51
+ interface SvgSource {
52
+ id: string;
53
+ /**
54
+ * URL of an SVG or a raster image (PNG, JPEG, WebP, GIF). http(s) URLs must be
55
+ * CORS-accessible from the page; data: URLs always work.
56
+ */
57
+ url: string;
58
+ /** Width to draw the source at (in px). Raster images are fit inside, keeping aspect. Default: 200 */
59
+ spriteWidth: number;
60
+ /** Height to draw the source at (in px). Default: 200 */
61
+ spriteHeight: number;
62
+ }
63
+ interface GridArrangement {
64
+ type: "grid";
65
+ repeatX: number;
66
+ repeatY: number;
67
+ gapX: number;
68
+ gapY: number;
69
+ gridRotation: number;
70
+ }
71
+ interface BrickArrangement {
72
+ type: "brick";
73
+ repeatX: number;
74
+ repeatY: number;
75
+ gapX: number;
76
+ gapY: number;
77
+ staggerOffset: number;
78
+ staggerAxis: "row" | "column";
79
+ }
80
+ interface HexArrangement {
81
+ type: "hex";
82
+ countAcross: number;
83
+ spacing: number;
84
+ orientation: "flat" | "pointy";
85
+ }
86
+ interface RadialArrangement {
87
+ type: "radial";
88
+ count: number;
89
+ radius: number;
90
+ startAngle: number;
91
+ arc: number;
92
+ faceOutward: boolean;
93
+ mirrorAlternate: boolean;
94
+ }
95
+ interface ConcentricArrangement {
96
+ type: "concentric";
97
+ rings: number;
98
+ copiesPerRing: number;
99
+ ringScaling: "fixed" | "proportional";
100
+ ringGap: number;
101
+ innerRadius: number;
102
+ faceOutward: boolean;
103
+ scalePerRing: number;
104
+ }
105
+ interface SpiralArrangement {
106
+ type: "spiral";
107
+ count: number;
108
+ innerRadius: number;
109
+ outerRadius: number;
110
+ turns: number;
111
+ faceOutward: boolean;
112
+ }
113
+ interface MirrorArrangement {
114
+ type: "mirror";
115
+ axes: number;
116
+ reflect: boolean;
117
+ radius: number;
118
+ }
119
+ /** A rectangle kept free of copies, in fractions of the canvas (0 to 1). */
120
+ interface ClearZone {
121
+ x: number;
122
+ y: number;
123
+ width: number;
124
+ height: number;
125
+ }
126
+ /**
127
+ * Seeded random placement without overlaps. Each copy gets its own size in px,
128
+ * which replaces the source's drawn size (keep `scale` fixed at 1 for exact px).
129
+ * With sources of different sizes, sizes apply to the largest; smaller sources
130
+ * keep their proportion.
131
+ */
132
+ interface ScatterParams {
133
+ count: number;
134
+ /** Smallest copy, in px across. */
135
+ minSize: number;
136
+ /** Largest copy, in px across. */
137
+ maxSize: number;
138
+ /** 1 = sizes spread evenly; above 1 favors small copies (2 = mostly small, a few large). */
139
+ sizeBias: number;
140
+ /** Minimum space between copies, in px. */
141
+ gap: number;
142
+ /** Blur on every copy, in px. */
143
+ blur: number;
144
+ seed: number;
145
+ }
146
+ interface ScatterArrangement extends ScatterParams {
147
+ type: "scatter";
148
+ clearZones: ClearZone[];
149
+ }
150
+ /**
151
+ * One large focal copy plus smaller satellite copies placed around it. A
152
+ * composition for a single canvas (a page backdrop or splash), not a tile.
153
+ * The focal copy ignores the scale/opacity/rotation steppers and uses its own
154
+ * values; satellites use the steppers like any other copy.
155
+ */
156
+ interface FocalArrangement {
157
+ type: "focal";
158
+ focus: {
159
+ /** Center, in fractions of the canvas (may sit partly off-canvas). */
160
+ x: number;
161
+ y: number;
162
+ /** px across. */
163
+ size: number;
164
+ rotation: number;
165
+ opacity: number;
166
+ blur: number;
167
+ };
168
+ satellites: ScatterParams;
169
+ clearZones: ClearZone[];
170
+ }
171
+ type StampArrangement = GridArrangement | BrickArrangement | HexArrangement | RadialArrangement | ConcentricArrangement | SpiralArrangement | MirrorArrangement | ScatterArrangement | FocalArrangement;
172
+ interface StampConfig {
173
+ canvas: {
174
+ width: number;
175
+ height: number;
176
+ /** CSS color string, or empty string for transparent */
177
+ background: string;
178
+ };
179
+ /** SVG sources to stamp. Multiple sources cycle across copies. */
180
+ sources: SvgSource[];
181
+ /** How copies are distributed across multiple sources */
182
+ sourceDistribution: "sequential" | "random";
183
+ /** Seed for random source distribution */
184
+ sourceSeed: number;
185
+ /** How copies are positioned on the canvas */
186
+ arrangement: StampArrangement;
187
+ /** Scale of each copy (1 = sprite's natural rasterized size) */
188
+ scale: NumericRange;
189
+ /** Opacity of each copy */
190
+ opacity: NumericRange;
191
+ /** Rotation of each copy in degrees */
192
+ rotation: NumericRange;
193
+ /** Per-copy color tinting. null = use original SVG colors */
194
+ color: ColorConfig | null;
195
+ /** Per-copy randomness for organic variation (seeded, stable across renders) */
196
+ jitter: {
197
+ position: number;
198
+ rotation: number;
199
+ scale: number;
200
+ seed: number;
201
+ };
202
+ /** Scale/opacity at edge vs center (1 = uniform) */
203
+ distanceFalloff: {
204
+ scaleEnd: number;
205
+ opacityEnd: number;
206
+ };
207
+ maxCopies: number;
208
+ }
209
+ interface StampCopy {
210
+ index: number;
211
+ x: number;
212
+ y: number;
213
+ rotation: number;
214
+ mirrorX: boolean;
215
+ scaleMultiplier: number;
216
+ opacityMultiplier: number;
217
+ /** Blur in px (0 = none). */
218
+ blur: number;
219
+ /**
220
+ * When true, the copy's own scaleMultiplier/opacityMultiplier/rotation are its
221
+ * final values and the config steppers, jitter and falloff are skipped.
222
+ */
223
+ fixedStyle: boolean;
224
+ }
225
+ type OutputFormat = "png" | "jpg" | "webp" | "svg";
226
+
227
+ declare const DEFAULT_CONFIG: StampConfig;
228
+ declare function defaultArrangement(type: string): StampArrangement;
229
+ declare const PRESETS: Record<string, {
230
+ label: string;
231
+ config: Partial<StampConfig>;
232
+ }>;
233
+
234
+ declare function stepNumeric(range: NumericRange, index: number, total: number): number;
235
+ declare function stepColor(colors: string[], mode: {
236
+ mode: "sequence";
237
+ } | {
238
+ mode: "random";
239
+ seed: number;
240
+ }, index: number): string;
241
+ /** Final scale, opacity and rotation for one copy (steppers unless fixedStyle). */
242
+ declare function resolveCopyStyle(config: StampConfig, copy: StampCopy, total: number): {
243
+ scale: number;
244
+ opacity: number;
245
+ rotation: number;
246
+ };
247
+ /** Whether the arrangement repeats seamlessly as a tile (focal is a one-off composition). */
248
+ declare function isTileable(config: StampConfig): boolean;
249
+ declare function computeLayout(config: StampConfig): StampCopy[];
250
+
251
+ /** Deterministic integer hash — stable across frames and devices. */
252
+ declare function seededHash(index: number, salt: number, seed: number): number;
253
+ /** Hash → float in [0, 1]. */
254
+ declare function seededFloat01(index: number, salt: number, seed: number): number;
255
+ /** Hash → float in [-1, 1]. */
256
+ declare function seededFloat(index: number, salt: number, seed: number): number;
257
+ /** Hash → integer index in [0, count). */
258
+ declare function seededIndex(index: number, count: number, seed: number): number;
259
+
260
+ /**
261
+ * Fetch an SVG URL and rasterize it to an HTMLCanvasElement at the specified size.
262
+ * Results are cached by (url + size) so repeated calls are free.
263
+ *
264
+ * CORS note: the SVG URL must be same-origin or have CORS headers.
265
+ * For cross-origin SVGs, host them on a CORS-enabled CDN or use a data: URL.
266
+ */
267
+ declare function loadSprite(source: SvgSource): Promise<HTMLCanvasElement>;
268
+ /** Load all sources, returning sprites in the same order. */
269
+ declare function loadSprites(sources: SvgSource[]): Promise<HTMLCanvasElement[]>;
270
+ /** Clear the sprite and fetch caches (e.g. when sources change). */
271
+ declare function clearSpriteCache(): void;
272
+ /**
273
+ * Returns a tinted copy of the sprite, cached by (spriteKey + color + mode).
274
+ * Uses canvas compositing to apply the color while preserving alpha shape.
275
+ */
276
+ declare function tintSprite(sprite: HTMLCanvasElement, spriteKey: string, color: string, mode: TintMode): HTMLCanvasElement;
277
+ declare function clearTintCache(): void;
278
+
279
+ /**
280
+ * Source fetching and SVG parsing with no DOM, shared by the browser renderers
281
+ * and the server renderer (Node). Uses only fetch, atob and btoa, which exist in
282
+ * browsers and in Node 18+.
283
+ */
284
+ /** A fetched source: SVG markup, or a raster image as a data: URL. */
285
+ type FetchedSource = {
286
+ kind: "svg";
287
+ text: string;
288
+ } | {
289
+ kind: "raster";
290
+ dataUrl: string;
291
+ };
292
+ /** Fetch a source once per URL. Raster images are inlined as data: URLs. */
293
+ declare function fetchSource(url: string): Promise<FetchedSource>;
294
+ interface ParsedSvg {
295
+ viewBox: string;
296
+ /** Everything inside the root <svg> element. */
297
+ inner: string;
298
+ /** Namespace declarations from the root (e.g. xmlns:xlink) the inner markup may rely on. */
299
+ namespaces: string;
300
+ }
301
+ /**
302
+ * Pull the viewBox and inner markup out of an SVG document with plain string
303
+ * handling, so it runs without a DOM. Attributes on the root element other than
304
+ * size and namespaces are dropped.
305
+ */
306
+ declare function parseSvgMarkup(text: string): ParsedSvg;
307
+
308
+ /**
309
+ * Render the stamp config to an HTMLCanvasElement.
310
+ * Loads SVG sources internally (cached after first load).
311
+ */
312
+ declare function renderToCanvas(config: StampConfig): Promise<HTMLCanvasElement>;
313
+
314
+ declare function clearSvgSourceCache(): void;
315
+ /**
316
+ * Fixed-size SVG — one tile at canvas dimensions. Use as an image or CSS background.
317
+ */
318
+ declare function renderToSvg(config: StampConfig): Promise<string>;
319
+ /**
320
+ * Infinite-tiling SVG — tiles the pattern seamlessly via <pattern>.
321
+ * Stamps that bleed past tile edges are wrapped to the opposite side.
322
+ *
323
+ * vpWidth/vpHeight set the SVG viewport for standalone export.
324
+ * Omit both (default "100%") to get a fluid SVG that fills its DOM container.
325
+ */
326
+ declare function renderTileableSvg(config: StampConfig, vpWidth?: number | "100%", vpHeight?: number | "100%"): Promise<string>;
327
+ declare function renderToSvgBlob(config: StampConfig): Promise<Blob>;
328
+ declare function downloadSvg(config: StampConfig, filename?: string, vpWidth?: number, vpHeight?: number): Promise<void>;
329
+
330
+ /**
331
+ * Render the config and return a data URL.
332
+ * @param quality - JPEG/WebP quality, 0-1. Ignored for PNG.
333
+ */
334
+ declare function renderToDataURL(config: StampConfig, format?: OutputFormat, quality?: number): Promise<string>;
335
+ /**
336
+ * Render the config and return a Blob.
337
+ */
338
+ declare function renderToBlob(config: StampConfig, format?: OutputFormat, quality?: number): Promise<Blob>;
339
+ /**
340
+ * Render the config and trigger a browser download.
341
+ */
342
+ declare function downloadPattern(config: StampConfig, filename?: string, format?: OutputFormat, quality?: number): Promise<void>;
343
+
344
+ interface PatternPreviewProps {
345
+ config: StampConfig;
346
+ className?: string;
347
+ }
348
+ declare function PatternPreview({ config, className }: PatternPreviewProps): react.JSX.Element;
349
+
350
+ interface PatternStudioProps {
351
+ /** Pre-populate the studio with an existing config */
352
+ initialConfig?: Partial<StampConfig>;
353
+ /** Called whenever the config changes */
354
+ onChange?: (config: StampConfig) => void;
355
+ className?: string;
356
+ }
357
+ declare function PatternStudio({ initialConfig, onChange, className }: PatternStudioProps): react.JSX.Element;
358
+
359
+ export { type BrickArrangement, type ClearZone, type ColorConfig, type ColorStepMode, type ConcentricArrangement, DEFAULT_CONFIG, type FetchedSource, type FocalArrangement, type GridArrangement, type HexArrangement, type MirrorArrangement, type NumericRange, type NumericStepMode, type OutputFormat, PRESETS, type ParsedSvg, PatternPreview, type PatternPreviewProps, PatternStudio, type PatternStudioProps, type RadialArrangement, type ScatterArrangement, type ScatterParams, type SpiralArrangement, type StampArrangement, type StampConfig, type StampCopy, type SvgSource, type TintMode, clearSpriteCache, clearSvgSourceCache, clearTintCache, computeLayout, defaultArrangement, downloadPattern, downloadSvg, fetchSource, isTileable, loadSprite, loadSprites, parseSvgMarkup, renderTileableSvg, renderToBlob, renderToCanvas, renderToDataURL, renderToSvg, renderToSvgBlob, resolveCopyStyle, seededFloat, seededFloat01, seededHash, seededIndex, stepColor, stepNumeric, tintSprite };