@genart-dev/core 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,283 @@
1
+ import { ParamDef, ColorDef, RendererType, SketchState, CanvasSpec, SketchDefinition } from '@genart-dev/format';
2
+ export * from '@genart-dev/format';
3
+
4
+ /** A design knowledge skill definition. */
5
+ interface SkillDefinition {
6
+ /** Unique skill identifier (kebab-case). */
7
+ readonly id: string;
8
+ /** Human-readable skill name. */
9
+ readonly name: string;
10
+ /** Skill category. */
11
+ readonly category: "composition" | "color";
12
+ /** Complexity level. */
13
+ readonly complexity: "beginner" | "intermediate" | "advanced";
14
+ /** Brief description. */
15
+ readonly description: string;
16
+ /** Core design theory (markdown). */
17
+ readonly theory: string;
18
+ /** Key principles. */
19
+ readonly principles: readonly string[];
20
+ /** Academic references. */
21
+ readonly references: readonly SkillReference[];
22
+ /** Suggested parameters for sketches using this skill. */
23
+ readonly suggestedParameters?: readonly ParamDef[];
24
+ /** Suggested color definitions. */
25
+ readonly suggestedColors?: readonly ColorDef[];
26
+ /** Renderer-specific example algorithms. */
27
+ readonly examples?: Readonly<Partial<Record<RendererType, string>>>;
28
+ }
29
+ /** An academic reference for a skill. */
30
+ interface SkillReference {
31
+ /** Book or paper title. */
32
+ readonly title: string;
33
+ /** Author name. */
34
+ readonly author: string;
35
+ /** Year of publication. */
36
+ readonly year?: number;
37
+ }
38
+ /** Result of validating an algorithm string. */
39
+ interface ValidationResult {
40
+ /** Whether the algorithm is valid. */
41
+ readonly valid: boolean;
42
+ /** Validation error messages, if any. */
43
+ readonly errors: readonly string[];
44
+ }
45
+ /** Opaque compiled algorithm handle. */
46
+ type CompiledAlgorithm = unknown;
47
+ /** A runtime dependency required by a renderer. */
48
+ interface RuntimeDependency {
49
+ /** Package or CDN name. */
50
+ readonly name: string;
51
+ /** Version constraint. */
52
+ readonly version: string;
53
+ /** CDN URL for standalone HTML export. */
54
+ readonly cdnUrl: string;
55
+ }
56
+ /** Options for frame capture. */
57
+ interface CaptureOptions {
58
+ /** Output format. */
59
+ readonly format?: "png" | "jpeg" | "webp";
60
+ /** Quality (0-1) for lossy formats. */
61
+ readonly quality?: number;
62
+ /** Scale multiplier. */
63
+ readonly scale?: number;
64
+ }
65
+ /**
66
+ * Renderer adapter — pluggable rendering engine interface.
67
+ * Each renderer type (p5, Three.js, GLSL, Canvas 2D, SVG) implements this.
68
+ */
69
+ interface RendererAdapter {
70
+ /** The renderer type this adapter handles. */
71
+ readonly type: RendererType;
72
+ /** Human-readable renderer name. */
73
+ readonly displayName: string;
74
+ /** Language used for the algorithm field. */
75
+ readonly algorithmLanguage: "javascript" | "glsl" | "typescript";
76
+ /** Validate algorithm source without executing it. */
77
+ validate(algorithm: string): ValidationResult;
78
+ /** Compile an algorithm string into a runnable form. */
79
+ compile(algorithm: string): Promise<CompiledAlgorithm>;
80
+ /** Create a live sketch instance from compiled algorithm + state. */
81
+ createInstance(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec): SketchInstance;
82
+ /** Render a single frame offscreen (for capture/export). */
83
+ renderOffscreen(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec, options?: CaptureOptions): Promise<Uint8Array | Blob>;
84
+ /** Generate a standalone HTML page embedding the sketch. */
85
+ generateStandaloneHTML(sketch: SketchDefinition): string;
86
+ /** Return a starter algorithm template for this renderer. */
87
+ getAlgorithmTemplate(): string;
88
+ /** List runtime dependencies needed for standalone export. */
89
+ getRuntimeDependencies(): RuntimeDependency[];
90
+ }
91
+ /**
92
+ * A live sketch instance mounted in the DOM.
93
+ * Created by RendererAdapter.createInstance().
94
+ */
95
+ interface SketchInstance {
96
+ /** Mount the sketch into a DOM container. */
97
+ mount(container: HTMLElement): void;
98
+ /** Unmount and remove from DOM. */
99
+ unmount(): void;
100
+ /** Update runtime state (params, colors, seed). */
101
+ updateState(state: SketchState): void;
102
+ /** Trigger a redraw with current state. */
103
+ redraw(): void;
104
+ /** Pause animation/rendering. */
105
+ pause(): void;
106
+ /** Resume animation/rendering. */
107
+ resume(): void;
108
+ /** Whether the sketch is currently animating. */
109
+ readonly isAnimating: boolean;
110
+ /** Capture the current frame as a data URL. */
111
+ captureFrame(options?: CaptureOptions): Promise<string>;
112
+ /** Capture raw ImageData from the current frame. */
113
+ captureImageData(): Promise<ImageData>;
114
+ /** Dispose all resources (WebGL contexts, event listeners, etc.). */
115
+ dispose(): void;
116
+ }
117
+
118
+ /**
119
+ * P5 Renderer Adapter — full implementation.
120
+ *
121
+ * Validates algorithms for the `sketch(p, state)` instance-mode signature,
122
+ * compiles them into executable factories, and creates live sketch instances.
123
+ */
124
+ declare class P5RendererAdapter implements RendererAdapter {
125
+ readonly type: RendererType;
126
+ readonly displayName = "p5.js";
127
+ readonly algorithmLanguage: "javascript";
128
+ validate(algorithm: string): ValidationResult;
129
+ compile(algorithm: string): Promise<CompiledAlgorithm>;
130
+ createInstance(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec): SketchInstance;
131
+ renderOffscreen(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec, options?: CaptureOptions): Promise<Uint8Array | Blob>;
132
+ generateStandaloneHTML(sketch: SketchDefinition): string;
133
+ getAlgorithmTemplate(): string;
134
+ getRuntimeDependencies(): RuntimeDependency[];
135
+ }
136
+
137
+ /**
138
+ * Canvas 2D Renderer Adapter — full implementation.
139
+ *
140
+ * Validates algorithms for the `sketch(ctx, state)` function signature,
141
+ * compiles them into executable factories, and creates live sketch instances.
142
+ */
143
+ declare class Canvas2DRendererAdapter implements RendererAdapter {
144
+ readonly type: RendererType;
145
+ readonly displayName = "Canvas 2D";
146
+ readonly algorithmLanguage: "javascript";
147
+ validate(algorithm: string): ValidationResult;
148
+ compile(algorithm: string): Promise<CompiledAlgorithm>;
149
+ createInstance(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec): SketchInstance;
150
+ renderOffscreen(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec, options?: CaptureOptions): Promise<Uint8Array | Blob>;
151
+ generateStandaloneHTML(sketch: SketchDefinition): string;
152
+ getAlgorithmTemplate(): string;
153
+ getRuntimeDependencies(): RuntimeDependency[];
154
+ }
155
+
156
+ /**
157
+ * Three.js Renderer Adapter — full implementation.
158
+ *
159
+ * Validates algorithms for the `sketch(THREE, state, container)` signature,
160
+ * compiles them into executable factories, and creates live sketch instances.
161
+ */
162
+ declare class ThreeRendererAdapter implements RendererAdapter {
163
+ readonly type: RendererType;
164
+ readonly displayName = "Three.js";
165
+ readonly algorithmLanguage: "javascript";
166
+ validate(algorithm: string): ValidationResult;
167
+ compile(algorithm: string): Promise<CompiledAlgorithm>;
168
+ createInstance(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec): SketchInstance;
169
+ renderOffscreen(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec, options?: CaptureOptions): Promise<Uint8Array | Blob>;
170
+ generateStandaloneHTML(sketch: SketchDefinition): string;
171
+ getAlgorithmTemplate(): string;
172
+ getRuntimeDependencies(): RuntimeDependency[];
173
+ }
174
+
175
+ /**
176
+ * Parse a hex color string to RGB floats in [0, 1] range.
177
+ * Accepts "#rrggbb" or "rrggbb" format.
178
+ */
179
+ declare function hexToVec3(hex: string): [number, number, number];
180
+ /**
181
+ * GLSL Renderer Adapter — full implementation.
182
+ *
183
+ * Validates GLSL fragment shaders, compiles them with a fullscreen quad
184
+ * vertex shader, and creates live sketch instances using WebGL2.
185
+ */
186
+ declare class GLSLRendererAdapter implements RendererAdapter {
187
+ readonly type: RendererType;
188
+ readonly displayName = "GLSL Shader";
189
+ readonly algorithmLanguage: "glsl";
190
+ validate(algorithm: string): ValidationResult;
191
+ compile(algorithm: string): Promise<CompiledAlgorithm>;
192
+ createInstance(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec): SketchInstance;
193
+ renderOffscreen(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec, options?: CaptureOptions): Promise<Uint8Array | Blob>;
194
+ generateStandaloneHTML(sketch: SketchDefinition): string;
195
+ getAlgorithmTemplate(): string;
196
+ getRuntimeDependencies(): RuntimeDependency[];
197
+ }
198
+
199
+ /**
200
+ * SVG Renderer Adapter — full implementation.
201
+ *
202
+ * Validates algorithms for the `sketch(state)` function signature,
203
+ * compiles them into executable factories, and creates live sketch instances.
204
+ * SVG sketches are static — there is no animation loop.
205
+ */
206
+ declare class SVGRendererAdapter implements RendererAdapter {
207
+ readonly type: RendererType;
208
+ readonly displayName = "SVG";
209
+ readonly algorithmLanguage: "javascript";
210
+ validate(algorithm: string): ValidationResult;
211
+ compile(algorithm: string): Promise<CompiledAlgorithm>;
212
+ createInstance(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec): SketchInstance;
213
+ renderOffscreen(compiled: CompiledAlgorithm, state: SketchState, canvas: CanvasSpec, _options?: CaptureOptions): Promise<Uint8Array | Blob>;
214
+ generateStandaloneHTML(sketch: SketchDefinition): string;
215
+ getAlgorithmTemplate(): string;
216
+ getRuntimeDependencies(): RuntimeDependency[];
217
+ }
218
+
219
+ /**
220
+ * Registry for renderer adapters. Manages registration and lookup
221
+ * of RendererAdapter implementations by renderer type.
222
+ */
223
+ declare class RendererRegistry {
224
+ private readonly adapters;
225
+ private defaultType;
226
+ /**
227
+ * Register a renderer adapter. Replaces any existing adapter for the same type.
228
+ */
229
+ register(adapter: RendererAdapter): void;
230
+ /**
231
+ * Resolve a renderer adapter by type.
232
+ * If type is undefined, returns the default adapter (p5 — v1.0 compat).
233
+ *
234
+ * @throws Error if the type is not registered.
235
+ */
236
+ resolve(type?: RendererType): RendererAdapter;
237
+ /**
238
+ * List all registered renderer types.
239
+ */
240
+ list(): RendererType[];
241
+ /**
242
+ * Get the default renderer adapter (p5).
243
+ */
244
+ getDefault(): RendererAdapter;
245
+ /**
246
+ * Check if a renderer type is registered.
247
+ */
248
+ has(type: RendererType): boolean;
249
+ }
250
+ /**
251
+ * Create a RendererRegistry pre-loaded with all 5 renderer adapters.
252
+ * This is the standard way to get a registry instance.
253
+ */
254
+ declare function createDefaultRegistry(): RendererRegistry;
255
+
256
+ /**
257
+ * Registry for design knowledge skills.
258
+ * Mirrors the RendererRegistry pattern.
259
+ */
260
+ declare class SkillRegistry {
261
+ private readonly skills;
262
+ /** Register a skill definition. */
263
+ register(skill: SkillDefinition): void;
264
+ /** Resolve a skill by ID. Throws if not found. */
265
+ resolve(id: string): SkillDefinition;
266
+ /** Get a skill by ID, or undefined if not found. */
267
+ get(id: string): SkillDefinition | undefined;
268
+ /** List all skills, optionally filtered by category. */
269
+ list(category?: string): SkillDefinition[];
270
+ /** Check if a skill ID is registered. */
271
+ has(id: string): boolean;
272
+ /** Return unique categories across all registered skills. */
273
+ categories(): string[];
274
+ }
275
+ /**
276
+ * Create a skill registry pre-loaded with all built-in skills.
277
+ */
278
+ declare function createDefaultSkillRegistry(): SkillRegistry;
279
+
280
+ declare const COMPOSITION_SKILLS: readonly SkillDefinition[];
281
+ declare const COLOR_SKILLS: readonly SkillDefinition[];
282
+
283
+ export { COLOR_SKILLS, COMPOSITION_SKILLS, Canvas2DRendererAdapter, type CaptureOptions, type CompiledAlgorithm, GLSLRendererAdapter, P5RendererAdapter, type RendererAdapter, RendererRegistry, type RuntimeDependency, SVGRendererAdapter, type SketchInstance, type SkillDefinition, type SkillReference, SkillRegistry, ThreeRendererAdapter, type ValidationResult, createDefaultRegistry, createDefaultSkillRegistry, hexToVec3 };