@elucim/dsl 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.
package/README.md ADDED
@@ -0,0 +1,208 @@
1
+ # @elucim/dsl
2
+
3
+ > JSON-based DSL for creating Elucim diagrams — designed for AI agents.
4
+
5
+ ## Overview
6
+
7
+ `@elucim/dsl` lets you describe animated diagrams as JSON documents. An AI agent (or any code) produces a JSON object conforming to the schema, and the `<DslRenderer>` component renders it as a fully interactive Elucim visualization.
8
+
9
+ ## Quick Start
10
+
11
+ ```tsx
12
+ import { DslRenderer } from '@elucim/dsl';
13
+ import type { ElucimDocument } from '@elucim/dsl';
14
+
15
+ const myDiagram: ElucimDocument = {
16
+ version: '1.0',
17
+ root: {
18
+ type: 'player',
19
+ width: 800,
20
+ height: 600,
21
+ fps: 30,
22
+ durationInFrames: 90,
23
+ background: '#0d0d1a',
24
+ children: [
25
+ {
26
+ type: 'circle',
27
+ cx: 400,
28
+ cy: 300,
29
+ r: 100,
30
+ stroke: '#3b82f6',
31
+ strokeWidth: 3,
32
+ draw: 60,
33
+ },
34
+ ],
35
+ },
36
+ };
37
+
38
+ function App() {
39
+ return <DslRenderer dsl={myDiagram} />;
40
+ }
41
+ ```
42
+
43
+ ## API
44
+
45
+ ### `<DslRenderer dsl={doc} />`
46
+
47
+ Validates the DSL document and renders it as React components. If validation fails, displays error messages.
48
+
49
+ **Props:**
50
+ - `dsl: ElucimDocument` — The DSL document to render
51
+ - `className?: string` — CSS class for the wrapper div
52
+ - `style?: CSSProperties` — Inline styles for the wrapper div
53
+
54
+ ### `validate(doc: unknown): ValidationResult`
55
+
56
+ Validates a DSL document without rendering it.
57
+
58
+ ```ts
59
+ import { validate } from '@elucim/dsl';
60
+
61
+ const result = validate(myDoc);
62
+ if (!result.valid) {
63
+ console.log(result.errors);
64
+ // [{ path: 'root.children[0].cx', message: 'Required numeric field "cx"...', severity: 'error' }]
65
+ }
66
+ ```
67
+
68
+ ### `compileExpression(expr: string)`
69
+
70
+ Compile a math expression string into a callable function.
71
+
72
+ ```ts
73
+ import { compileExpression } from '@elucim/dsl';
74
+
75
+ const fn = compileExpression('sin(x) * 2');
76
+ fn({ x: Math.PI / 2 }); // → 2
77
+ ```
78
+
79
+ ### `compileVectorExpression(expr: string)`
80
+
81
+ Compile a vector field expression returning `[number, number]`.
82
+
83
+ ```ts
84
+ import { compileVectorExpression } from '@elucim/dsl';
85
+
86
+ const fn = compileVectorExpression('[-y, x]');
87
+ fn({ x: 1, y: 2 }); // → [-2, 1]
88
+ ```
89
+
90
+ ## Document Schema
91
+
92
+ Every document has this structure:
93
+
94
+ ```json
95
+ {
96
+ "version": "1.0",
97
+ "root": { "type": "scene|player|presentation", ... }
98
+ }
99
+ ```
100
+
101
+ ### Root Types
102
+
103
+ | Type | Description |
104
+ |------|-------------|
105
+ | `scene` | Raw SVG scene (needs external frame control) |
106
+ | `player` | Interactive player with controls, scrub bar, play/pause |
107
+ | `presentation` | Slide-based presentation with transitions |
108
+
109
+ ### Element Types
110
+
111
+ #### Primitives
112
+ | Type | Required Props | Description |
113
+ |------|---------------|-------------|
114
+ | `circle` | `cx`, `cy`, `r` | SVG circle |
115
+ | `line` | `x1`, `y1`, `x2`, `y2` | SVG line |
116
+ | `arrow` | `x1`, `y1`, `x2`, `y2` | Line with arrowhead |
117
+ | `rect` | `x`, `y`, `width`, `height` | Rectangle |
118
+ | `polygon` | `points` (array of [x,y]) | Polygon/polyline |
119
+ | `text` | `x`, `y`, `content` | Text element |
120
+
121
+ #### Math Visualizations
122
+ | Type | Required Props | Description |
123
+ |------|---------------|-------------|
124
+ | `axes` | — | Coordinate axes with grid/ticks |
125
+ | `functionPlot` | `fn` (expression) | Plot a function like `"sin(x)"` |
126
+ | `vector` | `to` ([x,y]) | Mathematical vector with label |
127
+ | `vectorField` | `fn` (vector expr) | Grid of arrows like `"[-y, x]"` |
128
+ | `matrix` | `values` (2D array) | Matrix with brackets |
129
+ | `graph` | `nodes`, `edges` | Graph theory visualization |
130
+ | `latex` | `expression`, `x`, `y` | LaTeX rendered via KaTeX |
131
+
132
+ #### Animation Wrappers
133
+ | Type | Key Props | Description |
134
+ |------|----------|-------------|
135
+ | `fadeIn` | `duration`, `easing` | Fade children in |
136
+ | `fadeOut` | `duration`, `easing` | Fade children out |
137
+ | `draw` | `duration`, `easing` | Progressive stroke drawing |
138
+ | `write` | `duration`, `easing` | Stroke draw + fill fade |
139
+ | `transform` | `translate`, `scale`, `rotate`, `opacity` | Animate position/scale/rotation |
140
+ | `morph` | `fromColor`, `toColor`, `fromScale`, `toScale` | Color/scale morphing |
141
+ | `stagger` | `staggerDelay` | Sequential delayed children |
142
+ | `parallel` | — | Children animate simultaneously |
143
+
144
+ #### Structural
145
+ | Type | Key Props | Description |
146
+ |------|----------|-------------|
147
+ | `sequence` | `from`, `durationInFrames` | Time-offset wrapper |
148
+ | `group` | `children` | Logical grouping |
149
+
150
+ ### Inline Animation Props
151
+
152
+ All primitives support these optional animation props directly:
153
+ - `fadeIn?: number` — Fade in over N frames
154
+ - `fadeOut?: number` — Fade out over N frames
155
+ - `draw?: number` — Progressive stroke draw over N frames
156
+ - `easing?: string | { type, ... }` — Easing function
157
+
158
+ ### Easing
159
+
160
+ **Named easings:** `linear`, `easeInQuad`, `easeOutQuad`, `easeInOutQuad`, `easeInCubic`, `easeOutCubic`, `easeInOutCubic`, `easeInQuart`, `easeOutQuart`, `easeInOutQuart`, `easeInSine`, `easeOutSine`, `easeInOutSine`, `easeInExpo`, `easeOutExpo`, `easeInOutExpo`, `easeInBack`, `easeOutBack`, `easeOutElastic`, `easeOutBounce`
161
+
162
+ **Spring:** `{ "type": "spring", "stiffness": 100, "damping": 10, "mass": 1 }`
163
+
164
+ **Cubic Bezier:** `{ "type": "cubicBezier", "x1": 0.25, "y1": 0.1, "x2": 0.25, "y2": 1 }`
165
+
166
+ ### Math Expressions
167
+
168
+ Used in `functionPlot.fn` and `vectorField.fn`. Safe evaluation — no `eval()`.
169
+
170
+ **Operators:** `+`, `-`, `*`, `/`, `^` (power), unary `-`
171
+
172
+ **Functions:** `sin`, `cos`, `tan`, `asin`, `acos`, `atan`, `atan2`, `sqrt`, `abs`, `log`, `ln`, `exp`, `floor`, `ceil`, `round`, `min`, `max`, `sign`, `pow`
173
+
174
+ **Constants:** `PI`, `E`, `TAU`
175
+
176
+ **Variables:** `x` (FunctionPlot), `x` and `y` (VectorField)
177
+
178
+ **Examples:**
179
+ - `"sin(x)"` — Sine wave
180
+ - `"x^2 - 1"` — Parabola
181
+ - `"exp(-(x^2) / 2)"` — Gaussian
182
+ - `"[-y, x]"` — Rotation vector field
183
+ - `"[sin(y), cos(x)]"` — Wave vector field
184
+
185
+ ## Examples
186
+
187
+ See the [`examples/`](./examples/) directory:
188
+ - `hello-circle.json` — Minimal animated circle
189
+ - `math-demo.json` — Axes, function plot, vector, LaTeX
190
+ - `animated-scene.json` — FadeIn, Draw, Transform, Stagger, Morph
191
+ - `presentation.json` — Multi-slide presentation
192
+ - `full-showcase.json` — Every feature in one document
193
+
194
+ ## For AI Agents
195
+
196
+ When instructing an LLM to create Elucim diagrams:
197
+
198
+ 1. **Ask it to produce JSON** matching the `ElucimDocument` schema
199
+ 2. **Set `version: "1.0"`** as the first field
200
+ 3. **Choose a root type**: `player` for interactive, `scene` for embedded, `presentation` for slides
201
+ 4. **Use `sequence` nodes** to control timing (offsets in frames)
202
+ 5. **Wrap elements in animation nodes** (`fadeIn`, `draw`, `stagger`) for entrance effects
203
+ 6. **Use math expression strings** for function plots and vector fields
204
+
205
+ Example prompt:
206
+ > "Create an Elucim DSL JSON document that shows a coordinate system with sin(x) and cos(x) plotted,
207
+ > with the sin curve drawing first, then the cos curve drawing 30 frames later,
208
+ > and a LaTeX label fading in at the end."