@shopify/klint 0.2.0 → 0.3.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,560 @@
1
+ import { K as KlintContext } from '../Klint-CsVzll4n.cjs';
2
+ import 'react';
3
+
4
+ interface FontPoint {
5
+ x: number;
6
+ y: number;
7
+ contour: number;
8
+ }
9
+ interface FontLetter {
10
+ center: {
11
+ x: number;
12
+ y: number;
13
+ };
14
+ letterIndex: number;
15
+ wordIndex: number;
16
+ lineIndex: number;
17
+ width: number;
18
+ height: number;
19
+ char?: string;
20
+ }
21
+ interface FontLetterWithPath extends FontLetter {
22
+ path: Path2D;
23
+ }
24
+ interface FontLetterWithPoints extends FontLetter {
25
+ shape: FontPoint[];
26
+ }
27
+ interface FontTextBlock {
28
+ width: number;
29
+ height: number;
30
+ }
31
+ interface FontPathsResult {
32
+ letters: FontLetterWithPath[];
33
+ block: FontTextBlock;
34
+ }
35
+ interface FontPointsResult {
36
+ letters: FontLetterWithPoints[];
37
+ block: FontTextBlock;
38
+ }
39
+ interface FontTextOptions {
40
+ align?: "left" | "center" | "right";
41
+ baseline?: "top" | "center" | "bottom" | "baseline";
42
+ anchor?: "default" | "center";
43
+ letterSpacing?: number;
44
+ lineSpacing?: number;
45
+ wordSpacing?: number;
46
+ sampling?: number;
47
+ axisValues?: number[];
48
+ }
49
+ interface FontData {
50
+ toPaths(text: string, size?: number, options?: FontTextOptions): FontPathsResult;
51
+ toPoints(text: string, size?: number, options?: FontTextOptions): FontPointsResult;
52
+ layoutText(font: any, text: string, size: number, options?: FontTextOptions): any;
53
+ head?: {
54
+ unitsPerEm: number;
55
+ xMin: number;
56
+ yMin: number;
57
+ xMax: number;
58
+ yMax: number;
59
+ };
60
+ hhea?: {
61
+ ascender: number;
62
+ descender: number;
63
+ lineGap: number;
64
+ };
65
+ name?: {
66
+ fontFamily: string;
67
+ postScriptName: string;
68
+ };
69
+ fvar?: any;
70
+ [key: string]: any;
71
+ }
72
+ declare class FontParser {
73
+ fonts: Map<any, any>;
74
+ constructor();
75
+ load(url: string): Promise<any>;
76
+ loadFromBuffer(buffer: any): any;
77
+ createAPI(font: any): any;
78
+ }
79
+
80
+ /**
81
+ * Triangle structure for Delaunay triangulation
82
+ */
83
+ interface Triangle {
84
+ p1: {
85
+ x: number;
86
+ y: number;
87
+ };
88
+ p2: {
89
+ x: number;
90
+ y: number;
91
+ };
92
+ p3: {
93
+ x: number;
94
+ y: number;
95
+ };
96
+ }
97
+ /**
98
+ * Static Delaunay Plugin
99
+ *
100
+ * Performs Delaunay triangulation on point sets without requiring Klint context.
101
+ * Context is only passed when drawing operations are needed.
102
+ *
103
+ * @example
104
+ * ```tsx
105
+ * import { Delaunay } from '@shopify/klint/plugins';
106
+ *
107
+ * const draw = (K) => {
108
+ * const points = [
109
+ * { x: 100, y: 100 },
110
+ * { x: 200, y: 150 },
111
+ * { x: 150, y: 250 }
112
+ * ];
113
+ *
114
+ * const triangles = Delaunay.triangulate(points);
115
+ * Delaunay.drawTriangles(K, triangles);
116
+ * };
117
+ * ```
118
+ */
119
+ declare class Delaunay {
120
+ /**
121
+ * Perform Delaunay triangulation on a set of points
122
+ */
123
+ static triangulate(points: Array<{
124
+ x: number;
125
+ y: number;
126
+ }>): Triangle[];
127
+ /**
128
+ * Draw triangles to the canvas
129
+ */
130
+ static drawTriangles(ctx: KlintContext, triangles: Triangle[], options?: {
131
+ fill?: boolean;
132
+ stroke?: boolean;
133
+ fillStyle?: string;
134
+ strokeStyle?: string;
135
+ }): void;
136
+ /**
137
+ * Calculate circumcenter of a triangle
138
+ */
139
+ static circumcenter(triangle: Triangle): {
140
+ x: number;
141
+ y: number;
142
+ };
143
+ /**
144
+ * Check if a point is inside a triangle's circumcircle
145
+ */
146
+ static inCircumcircle(point: {
147
+ x: number;
148
+ y: number;
149
+ }, triangle: Triangle): boolean;
150
+ }
151
+
152
+ /**
153
+ * Static CatmullRom Plugin
154
+ *
155
+ * Provides Catmull-Rom spline interpolation without requiring Klint context.
156
+ * Context is only passed when drawing operations are needed.
157
+ *
158
+ * @example
159
+ * ```tsx
160
+ * import { CatmullRom } from '@shopify/klint/plugins';
161
+ *
162
+ * const draw = (K) => {
163
+ * const points = [
164
+ * { x: 100, y: 100 },
165
+ * { x: 200, y: 150 },
166
+ * { x: 300, y: 100 },
167
+ * { x: 400, y: 200 }
168
+ * ];
169
+ *
170
+ * const smooth = CatmullRom.interpolate(points);
171
+ * CatmullRom.draw(K, points);
172
+ * };
173
+ * ```
174
+ */
175
+ declare class CatmullRom {
176
+ /**
177
+ * Interpolate points using Catmull-Rom spline
178
+ */
179
+ static interpolate(points: Array<{
180
+ x: number;
181
+ y: number;
182
+ }>, tension?: number, segments?: number): Array<{
183
+ x: number;
184
+ y: number;
185
+ }>;
186
+ /**
187
+ * Draw interpolated curve to canvas
188
+ */
189
+ static draw(ctx: KlintContext, points: Array<{
190
+ x: number;
191
+ y: number;
192
+ }>, options?: {
193
+ tension?: number;
194
+ segments?: number;
195
+ closed?: boolean;
196
+ strokeStyle?: string;
197
+ lineWidth?: number;
198
+ }): void;
199
+ /**
200
+ * Get path as a Path2D object
201
+ */
202
+ static toPath2D(points: Array<{
203
+ x: number;
204
+ y: number;
205
+ }>, options?: {
206
+ tension?: number;
207
+ segments?: number;
208
+ closed?: boolean;
209
+ }): Path2D;
210
+ }
211
+
212
+ /**
213
+ * Configuration options for Things plugin
214
+ */
215
+ interface ThingsConfig {
216
+ maxThings?: number;
217
+ defaultSize?: number;
218
+ defaultColor?: string;
219
+ }
220
+ /**
221
+ * Individual Thing interface
222
+ */
223
+ interface Thing {
224
+ id: string;
225
+ x: number;
226
+ y: number;
227
+ width: number;
228
+ height: number;
229
+ rotation: number;
230
+ scale: number;
231
+ color: string;
232
+ data?: any;
233
+ }
234
+ /**
235
+ * Static Things Plugin
236
+ *
237
+ * Manages a collection of "things" - generic objects that can be positioned,
238
+ * transformed, and rendered without requiring Klint context initialization.
239
+ * Context is only passed when drawing operations are needed.
240
+ *
241
+ * @example
242
+ * ```tsx
243
+ * import { Things } from '@shopify/klint/plugins';
244
+ *
245
+ * // Create things
246
+ * Things.create({ x: 100, y: 100 });
247
+ * Things.create({ x: 200, y: 200, color: '#ff0066' });
248
+ *
249
+ * // Update and draw
250
+ * Things.animatePhysics(deltaTime);
251
+ * Things.draw(K);
252
+ * ```
253
+ */
254
+ declare class Things {
255
+ private static things;
256
+ private static config;
257
+ private static idCounter;
258
+ /**
259
+ * Configure the plugin
260
+ */
261
+ static configure(config: ThingsConfig): void;
262
+ /**
263
+ * Create a new thing
264
+ */
265
+ static create(options?: Partial<Thing>): Thing;
266
+ /**
267
+ * Get a thing by ID
268
+ */
269
+ static get(id: string): Thing | undefined;
270
+ /**
271
+ * Get all things
272
+ */
273
+ static getAll(): Thing[];
274
+ /**
275
+ * Update a thing's properties
276
+ */
277
+ static update(id: string, updates: Partial<Thing>): void;
278
+ /**
279
+ * Remove a thing
280
+ */
281
+ static remove(id: string): boolean;
282
+ /**
283
+ * Clear all things
284
+ */
285
+ static clear(): void;
286
+ /**
287
+ * Move a thing
288
+ */
289
+ static move(id: string, dx: number, dy: number): void;
290
+ /**
291
+ * Rotate a thing
292
+ */
293
+ static rotate(id: string, angle: number): void;
294
+ /**
295
+ * Scale a thing
296
+ */
297
+ static scale(id: string, factor: number): void;
298
+ /**
299
+ * Find things within a radius
300
+ */
301
+ static findNear(x: number, y: number, radius: number): Thing[];
302
+ /**
303
+ * Find things that overlap with a rectangle
304
+ */
305
+ static findInRect(x: number, y: number, width: number, height: number): Thing[];
306
+ /**
307
+ * Apply a function to all things
308
+ */
309
+ static forEach(fn: (thing: Thing) => void): void;
310
+ /**
311
+ * Map things to a new array
312
+ */
313
+ static map<T>(fn: (thing: Thing) => T): T[];
314
+ /**
315
+ * Filter things
316
+ */
317
+ static filter(fn: (thing: Thing) => boolean): Thing[];
318
+ /**
319
+ * Sort things by a property or function
320
+ */
321
+ static sort(fn: (a: Thing, b: Thing) => number): Thing[];
322
+ /**
323
+ * Draw all things
324
+ */
325
+ static draw(ctx: KlintContext, options?: {
326
+ customDraw?: (ctx: KlintContext, thing: Thing) => void;
327
+ filter?: (thing: Thing) => boolean;
328
+ }): void;
329
+ /**
330
+ * Default drawing method for a thing
331
+ */
332
+ private static drawThing;
333
+ /**
334
+ * Animate things with a simple physics update
335
+ */
336
+ static animatePhysics(deltaTime: number, options?: {
337
+ gravity?: number;
338
+ friction?: number;
339
+ bounds?: {
340
+ x: number;
341
+ y: number;
342
+ width: number;
343
+ height: number;
344
+ };
345
+ }): void;
346
+ /**
347
+ * Get the count of things
348
+ */
349
+ static count(): number;
350
+ /**
351
+ * Check if a thing exists
352
+ */
353
+ static has(id: string): boolean;
354
+ /**
355
+ * Utility: Get distance between two things
356
+ */
357
+ static distance(id1: string, id2: string): number;
358
+ /**
359
+ * Utility: Check collision between two things
360
+ */
361
+ static collides(id1: string, id2: string): boolean;
362
+ }
363
+
364
+ /**
365
+ * Sprite configuration
366
+ */
367
+ interface SpriteConfig {
368
+ name: string;
369
+ url: string;
370
+ spriteWidth: number;
371
+ spriteHeight: number;
372
+ gap?: number;
373
+ }
374
+ /**
375
+ * Spritesheet data
376
+ */
377
+ interface Spritesheet {
378
+ image: HTMLImageElement;
379
+ srcNaturalWidth: number;
380
+ srcNaturalHeight: number;
381
+ numSprites: number;
382
+ spriteWidth: number;
383
+ spriteHeight: number;
384
+ gap: number;
385
+ cols: number;
386
+ rows: number;
387
+ }
388
+ /**
389
+ * Static Sprites Plugin
390
+ *
391
+ * Manages spritesheets and sprite rendering without requiring Klint context initialization.
392
+ * Context is only passed when drawing operations are needed.
393
+ *
394
+ * @example
395
+ * ```tsx
396
+ * import { Sprites } from '@shopify/klint/plugins';
397
+ *
398
+ * const preload = async () => {
399
+ * await Sprites.load({
400
+ * name: 'player',
401
+ * url: '/assets/player-sprites.png',
402
+ * spriteWidth: 32,
403
+ * spriteHeight: 32,
404
+ * gap: 0
405
+ * });
406
+ * };
407
+ *
408
+ * const draw = (K) => {
409
+ * const sheet = Sprites.sheet('player');
410
+ * const frame = Math.floor(K.frame / 10) % sheet.numSprites;
411
+ * Sprites.draw(K, 'player', frame, 100, 100);
412
+ * };
413
+ * ```
414
+ */
415
+ declare class Sprites {
416
+ private static spritesheets;
417
+ private static loadingPromises;
418
+ /**
419
+ * Load one or more spritesheets
420
+ * @param configs - Sprite configuration(s) to load
421
+ * @returns Promise that resolves when all sprites are loaded
422
+ */
423
+ static load(...configs: SpriteConfig[]): Promise<void>;
424
+ /**
425
+ * Load a single spritesheet
426
+ * @param config - Sprite configuration
427
+ * @returns Promise that resolves when sprite is loaded
428
+ */
429
+ private static loadSingle;
430
+ /**
431
+ * Get spritesheet information
432
+ * @param name - Name of the spritesheet
433
+ * @returns Spritesheet data or undefined if not loaded
434
+ */
435
+ static sheet(name: string): Spritesheet | undefined;
436
+ /**
437
+ * Draw a sprite from a spritesheet
438
+ * @param ctx - Klint context
439
+ * @param sheetName - Name of the spritesheet
440
+ * @param sprite - Sprite index to draw
441
+ * @param x - X position to draw at
442
+ * @param y - Y position to draw at
443
+ * @param options - Drawing options
444
+ */
445
+ static draw(ctx: KlintContext, sheetName: string, sprite: number, x: number, y: number, options?: {
446
+ width?: number;
447
+ height?: number;
448
+ rotation?: number;
449
+ scale?: number;
450
+ flipX?: boolean;
451
+ flipY?: boolean;
452
+ alpha?: number;
453
+ }): void;
454
+ /**
455
+ * Draw a sprite at its actual position without centering
456
+ * @param ctx - Klint context
457
+ * @param sheetName - Name of the spritesheet
458
+ * @param sprite - Sprite index to draw
459
+ * @param x - X position to draw at
460
+ * @param y - Y position to draw at
461
+ * @param width - Width to draw (optional)
462
+ * @param height - Height to draw (optional)
463
+ */
464
+ static drawCorner(ctx: KlintContext, sheetName: string, sprite: number, x: number, y: number, width?: number, height?: number): void;
465
+ /**
466
+ * Create an animation from a range of sprites
467
+ * @param sheetName - Name of the spritesheet
468
+ * @param startSprite - Starting sprite index
469
+ * @param endSprite - Ending sprite index
470
+ * @param frameDuration - Duration of each frame in milliseconds
471
+ * @returns Animation object
472
+ */
473
+ static animation(sheetName: string, startSprite: number, endSprite: number, frameDuration?: number): SpriteAnimation;
474
+ /**
475
+ * Check if a spritesheet is loaded
476
+ * @param name - Name of the spritesheet
477
+ * @returns True if loaded, false otherwise
478
+ */
479
+ static hasSheet(name: string): boolean;
480
+ /**
481
+ * Unload a spritesheet
482
+ * @param name - Name of the spritesheet to unload
483
+ */
484
+ static unload(name: string): void;
485
+ /**
486
+ * Unload all spritesheets
487
+ */
488
+ static clear(): void;
489
+ /**
490
+ * Get the number of loaded spritesheets
491
+ * @returns Number of loaded spritesheets
492
+ */
493
+ static count(): number;
494
+ /**
495
+ * Get all loaded spritesheet names
496
+ * @returns Array of spritesheet names
497
+ */
498
+ static getSheetNames(): string[];
499
+ }
500
+ /**
501
+ * Sprite animation helper class
502
+ */
503
+ declare class SpriteAnimation {
504
+ private sheetName;
505
+ private startSprite;
506
+ private endSprite;
507
+ private frameDuration;
508
+ private currentFrame;
509
+ private lastFrameTime;
510
+ private playing;
511
+ private loop;
512
+ constructor(sheetName: string, startSprite: number, endSprite: number, frameDuration: number);
513
+ /**
514
+ * Update animation frame
515
+ * @param deltaTime - Time since last update in milliseconds
516
+ */
517
+ update(deltaTime: number): void;
518
+ /**
519
+ * Draw the current animation frame
520
+ * @param ctx - Klint context
521
+ * @param x - X position
522
+ * @param y - Y position
523
+ * @param options - Drawing options
524
+ */
525
+ draw(ctx: KlintContext, x: number, y: number, options?: Parameters<typeof Sprites.draw>[5]): void;
526
+ /**
527
+ * Play the animation
528
+ */
529
+ play(): void;
530
+ /**
531
+ * Pause the animation
532
+ */
533
+ pause(): void;
534
+ /**
535
+ * Stop the animation and reset to start
536
+ */
537
+ stop(): void;
538
+ /**
539
+ * Set whether the animation should loop
540
+ * @param loop - True to loop, false to play once
541
+ */
542
+ setLoop(loop: boolean): void;
543
+ /**
544
+ * Get the current frame
545
+ * @returns Current frame index
546
+ */
547
+ getCurrentFrame(): number;
548
+ /**
549
+ * Set the current frame
550
+ * @param frame - Frame index to set
551
+ */
552
+ setFrame(frame: number): void;
553
+ /**
554
+ * Check if animation is playing
555
+ * @returns True if playing, false otherwise
556
+ */
557
+ isPlaying(): boolean;
558
+ }
559
+
560
+ export { CatmullRom, Delaunay, type FontData, type FontLetter, type FontLetterWithPath, type FontLetterWithPoints, FontParser, type FontPathsResult, type FontPoint, type FontPointsResult, type FontTextBlock, type FontTextOptions, Sprites, Things };