@istmx/skills 1.1.0 → 2.0.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.
Files changed (153) hide show
  1. package/CODE_OF_CONDUCT.md +47 -0
  2. package/LICENSE +30 -0
  3. package/README.md +48 -22
  4. package/bin/cli.js +130 -119
  5. package/istm-animate/SKILL.md +28 -58
  6. package/istm-animate/core/motion-principles/SKILL.md +284 -0
  7. package/istm-animate/core/motion-principles/references/designers.md +127 -0
  8. package/istm-animate/core/motion-principles/references/easing-guide.md +231 -0
  9. package/istm-animate/core/motion-principles/references/enter-exit-recipes.md +466 -0
  10. package/istm-animate/frameworks/canvas-generative/SKILL.md +325 -0
  11. package/istm-animate/frameworks/canvas-generative/references/algorithms.md +925 -0
  12. package/istm-animate/frameworks/css-native/SKILL.md +319 -0
  13. package/istm-animate/frameworks/css-native/references/modern-css.md +465 -0
  14. package/istm-animate/frameworks/framer-motion/SKILL.md +187 -0
  15. package/istm-animate/frameworks/framer-motion/references/api.md +312 -0
  16. package/istm-animate/frameworks/gsap/SKILL.md +188 -0
  17. package/istm-animate/frameworks/gsap/references/core.md +218 -0
  18. package/istm-animate/frameworks/gsap/references/plugins.md +371 -0
  19. package/istm-animate/frameworks/gsap/references/scrolltrigger.md +325 -0
  20. package/istm-animate/frameworks/gsap/references/timeline.md +218 -0
  21. package/istm-animate/frameworks/threejs-r3f/SKILL.md +243 -0
  22. package/istm-animate/frameworks/threejs-r3f/references/scene-setup.md +392 -0
  23. package/istm-animate/frameworks/threejs-r3f/references/shaders.md +446 -0
  24. package/istm-animate/mobile/android/compose-graphics/SKILL.md +503 -0
  25. package/istm-animate/mobile/android/compose-graphics/references/agsl-recipes.md +356 -0
  26. package/istm-animate/mobile/android/compose-graphics/references/canvas-generative.md +282 -0
  27. package/istm-animate/mobile/android/compose-graphics/references/m3-expressive-deep.md +303 -0
  28. package/istm-animate/mobile/android/compose-motion/SKILL.md +359 -0
  29. package/istm-animate/mobile/android/compose-motion/references/gestures-compose.md +273 -0
  30. package/istm-animate/mobile/android/compose-motion/references/recomposition-and-anim.md +196 -0
  31. package/istm-animate/mobile/android/compose-motion/references/shared-transitions.md +281 -0
  32. package/istm-animate/mobile/android/compose-multiplatform/SKILL.md +311 -0
  33. package/istm-animate/mobile/android/compose-multiplatform/references/cmp-interop.md +288 -0
  34. package/istm-animate/mobile/android/compose-multiplatform/references/cmp-platform-quirks.md +156 -0
  35. package/istm-animate/mobile/apple/swiftui-graphics/SKILL.md +408 -0
  36. package/istm-animate/mobile/apple/swiftui-graphics/references/canvas-swiftui.md +239 -0
  37. package/istm-animate/mobile/apple/swiftui-graphics/references/liquid-glass-deep.md +252 -0
  38. package/istm-animate/mobile/apple/swiftui-graphics/references/metal-recipes.md +382 -0
  39. package/istm-animate/mobile/apple/swiftui-motion/SKILL.md +379 -0
  40. package/istm-animate/mobile/apple/swiftui-motion/references/gestures-swiftui.md +336 -0
  41. package/istm-animate/mobile/apple/swiftui-motion/references/phase-keyframe-deep.md +231 -0
  42. package/istm-animate/mobile/apple/swiftui-motion/references/springs-cheatsheet.md +129 -0
  43. package/istm-animate/references/animate.md +199 -0
  44. package/istm-animate/references/audit.md +115 -0
  45. package/istm-animate/references/design-engineering.md +674 -0
  46. package/istm-animate/references/improve.md +101 -0
  47. package/istm-animate/references/opportunities.md +132 -0
  48. package/istm-animate/references/plan-template.md +73 -0
  49. package/istm-animate/references/recipes.md +324 -0
  50. package/istm-animate/references/review.md +112 -0
  51. package/istm-animate/references/standards.md +187 -0
  52. package/istm-animate/references/vocabulary.md +173 -0
  53. package/istm-architecture/SKILL.md +10 -1
  54. package/istm-awwward-designer/SKILL.md +28 -20
  55. package/istm-design/SKILL.md +31 -58
  56. package/istm-design/core/design-audit/SKILL.md +248 -0
  57. package/istm-design/core/desktop-principles/SKILL.md +363 -0
  58. package/istm-design/core/desktop-principles/references/keyboard-patterns.md +137 -0
  59. package/istm-design/core/desktop-principles/references/multi-window.md +105 -0
  60. package/istm-design/core/mobile-principles/SKILL.md +289 -0
  61. package/istm-design/core/mobile-principles/references/accessibility-mobile.md +175 -0
  62. package/istm-design/core/mobile-principles/references/gestures-deep.md +220 -0
  63. package/istm-design/core/ui-ux-pro-max/SKILL.md +392 -0
  64. package/istm-design/core/ui-ux-pro-max/UPSTREAM.md +23 -0
  65. package/istm-design/core/ui-ux-pro-max/data/app-interface.csv +31 -0
  66. package/istm-design/core/ui-ux-pro-max/data/charts.csv +26 -0
  67. package/istm-design/core/ui-ux-pro-max/data/colors.csv +193 -0
  68. package/istm-design/core/ui-ux-pro-max/data/google-fonts.csv +1924 -0
  69. package/istm-design/core/ui-ux-pro-max/data/icons.csv +106 -0
  70. package/istm-design/core/ui-ux-pro-max/data/landing.csv +35 -0
  71. package/istm-design/core/ui-ux-pro-max/data/motion.csv +17 -0
  72. package/istm-design/core/ui-ux-pro-max/data/products.csv +193 -0
  73. package/istm-design/core/ui-ux-pro-max/data/react-performance.csv +45 -0
  74. package/istm-design/core/ui-ux-pro-max/data/stacks/angular.csv +51 -0
  75. package/istm-design/core/ui-ux-pro-max/data/stacks/astro.csv +54 -0
  76. package/istm-design/core/ui-ux-pro-max/data/stacks/avalonia.csv +57 -0
  77. package/istm-design/core/ui-ux-pro-max/data/stacks/flutter.csv +53 -0
  78. package/istm-design/core/ui-ux-pro-max/data/stacks/html-tailwind.csv +56 -0
  79. package/istm-design/core/ui-ux-pro-max/data/stacks/javafx.csv +76 -0
  80. package/istm-design/core/ui-ux-pro-max/data/stacks/jetpack-compose.csv +53 -0
  81. package/istm-design/core/ui-ux-pro-max/data/stacks/laravel.csv +51 -0
  82. package/istm-design/core/ui-ux-pro-max/data/stacks/nextjs.csv +53 -0
  83. package/istm-design/core/ui-ux-pro-max/data/stacks/nuxt-ui.csv +71 -0
  84. package/istm-design/core/ui-ux-pro-max/data/stacks/nuxtjs.csv +59 -0
  85. package/istm-design/core/ui-ux-pro-max/data/stacks/react-native.csv +52 -0
  86. package/istm-design/core/ui-ux-pro-max/data/stacks/react.csv +54 -0
  87. package/istm-design/core/ui-ux-pro-max/data/stacks/shadcn.csv +61 -0
  88. package/istm-design/core/ui-ux-pro-max/data/stacks/svelte.csv +54 -0
  89. package/istm-design/core/ui-ux-pro-max/data/stacks/swiftui.csv +51 -0
  90. package/istm-design/core/ui-ux-pro-max/data/stacks/threejs.csv +54 -0
  91. package/istm-design/core/ui-ux-pro-max/data/stacks/uno.csv +60 -0
  92. package/istm-design/core/ui-ux-pro-max/data/stacks/uwp.csv +56 -0
  93. package/istm-design/core/ui-ux-pro-max/data/stacks/vue.csv +50 -0
  94. package/istm-design/core/ui-ux-pro-max/data/stacks/winui.csv +60 -0
  95. package/istm-design/core/ui-ux-pro-max/data/stacks/wpf.csv +57 -0
  96. package/istm-design/core/ui-ux-pro-max/data/styles.csv +85 -0
  97. package/istm-design/core/ui-ux-pro-max/data/typography.csv +75 -0
  98. package/istm-design/core/ui-ux-pro-max/data/ui-reasoning.csv +162 -0
  99. package/istm-design/core/ui-ux-pro-max/data/ux-guidelines.csv +100 -0
  100. package/istm-design/core/ui-ux-pro-max/references/pro-rules.md +109 -0
  101. package/istm-design/core/ui-ux-pro-max/references/quick-reference.md +240 -0
  102. package/istm-design/core/ui-ux-pro-max/scripts/core.py +464 -0
  103. package/istm-design/core/ui-ux-pro-max/scripts/design_system.py +1371 -0
  104. package/istm-design/core/ui-ux-pro-max/scripts/search.py +162 -0
  105. package/istm-design/core/ui-ux-pro-max/scripts/tests/test_core.py +134 -0
  106. package/istm-design/core/ui-ux-pro-max/scripts/validate_data.py +114 -0
  107. package/istm-design/references/apple-design.md +282 -0
  108. package/istm-design/references/generation-guide.md +209 -0
  109. package/istm-design/references/pick-ui-library.md +77 -0
  110. package/istm-design/references/picker.md +197 -0
  111. package/istm-design/references/prototype.md +90 -0
  112. package/istm-design/references/schema.md +366 -0
  113. package/istm-workflow/istm-audit/SKILL.md +7 -7
  114. package/istm-workflow/istm-audit/agent-prompt.md +20 -20
  115. package/istm-workflow/istm-audit/modes/area.md +1 -1
  116. package/istm-workflow/istm-audit/modes/greenfield.md +4 -4
  117. package/istm-workflow/istm-audit/modes/tool-skills.md +1 -1
  118. package/istm-workflow/istm-check/SKILL.md +10 -10
  119. package/istm-workflow/istm-check/modes/review.md +9 -9
  120. package/istm-workflow/istm-check/modes/verify.md +19 -19
  121. package/istm-workflow/istm-check/review-agent-prompt.md +2 -2
  122. package/istm-workflow/istm-check/review-guide.md +4 -4
  123. package/istm-workflow/istm-craft/SKILL.md +20 -11
  124. package/istm-workflow/istm-craft/agent-modes/architecture.md +1 -1
  125. package/istm-workflow/istm-craft/agent-prompt.md +8 -8
  126. package/istm-workflow/istm-craft/internal/after-subagent.md +11 -11
  127. package/istm-workflow/istm-craft/internal/design-conversation.md +9 -9
  128. package/istm-workflow/istm-craft/internal/tool-discovery.md +2 -2
  129. package/istm-workflow/istm-craft/spec-template.md +18 -18
  130. package/istm-workflow/istm-debug/SKILL.md +6 -6
  131. package/istm-workflow/istm-develop/SKILL.md +31 -16
  132. package/istm-workflow/istm-develop/checklist.md +1 -1
  133. package/istm-workflow/istm-develop/flow/build.md +21 -21
  134. package/istm-workflow/istm-develop/flow/git.md +4 -4
  135. package/istm-workflow/istm-develop/logical-guide.md +5 -5
  136. package/istm-workflow/istm-develop/ui/implementation.md +2 -2
  137. package/istm-workflow/istm-develop/ui-guide.md +3 -3
  138. package/istm-workflow/istm-document/SKILL.md +6 -6
  139. package/istm-workflow/istm-document/templates/postmortem.md +1 -1
  140. package/istm-workflow/istm-scope/SKILL.md +20 -20
  141. package/istm-workflow/istm-scope/modes/add.md +1 -1
  142. package/istm-workflow/istm-scope/modes/plan-brownfield.md +2 -2
  143. package/istm-workflow/istm-scope/modes/plan-greenfield.md +4 -4
  144. package/istm-workflow/istm-scope/modes/plan-monorepo.md +3 -3
  145. package/istm-workflow/istm-scope/modes/plan.md +16 -16
  146. package/istm-workflow/istm-scope/modes/replan.md +2 -2
  147. package/istm-workflow/istm-scope/scope-template.md +29 -29
  148. package/istm-workflow/istm-sync/SKILL.md +37 -25
  149. package/istm-workflow/istm-sync/agent-prompt.md +37 -19
  150. package/istm-workflow/istm-test/SKILL.md +12 -12
  151. package/istm-workflow/istm-test/modes/setup.md +6 -6
  152. package/istm-workflow/istm-test/writing-guide.md +1 -1
  153. package/package.json +1 -1
@@ -0,0 +1,925 @@
1
+ # Algorithms Reference
2
+
3
+ > Complete, commented implementations for generative art.
4
+ > Copy-paste ready. Each algorithm is self-contained.
5
+
6
+ ---
7
+
8
+ ## Simplex Noise 2D/3D
9
+
10
+ Compact implementation based on Stefan Gustavson's work. No external dependencies.
11
+
12
+ ```js
13
+ // ─── Simplex Noise ──────────────────────────────────────────────
14
+ // Attempt to produce smooth, isotropic noise without grid-aligned
15
+ // artifacts. Works in 2D and 3D. Returns values in [-1, 1].
16
+
17
+ const SimplexNoise = (() => {
18
+ // Gradient vectors for 2D (12 directions)
19
+ const grad2 = [
20
+ [1,1],[-1,1],[1,-1],[-1,-1],
21
+ [1,0],[-1,0],[0,1],[0,-1],
22
+ [1,1],[-1,1],[1,-1],[-1,-1]
23
+ ];
24
+
25
+ // Gradient vectors for 3D
26
+ const grad3 = [
27
+ [1,1,0],[-1,1,0],[1,-1,0],[-1,-1,0],
28
+ [1,0,1],[-1,0,1],[1,0,-1],[-1,0,-1],
29
+ [0,1,1],[0,-1,1],[0,1,-1],[0,-1,-1]
30
+ ];
31
+
32
+ // Permutation table (256 entries, doubled to avoid wrapping)
33
+ const perm = new Uint8Array(512);
34
+ const permMod12 = new Uint8Array(512);
35
+
36
+ // Seed the permutation table
37
+ // Pass any number to get reproducible results
38
+ function seed(s) {
39
+ const p = new Uint8Array(256);
40
+ for (let i = 0; i < 256; i++) p[i] = i;
41
+
42
+ // Fisher-Yates shuffle seeded with a simple LCG
43
+ let v = s * 1664525 + 1013904223;
44
+ for (let i = 255; i > 0; i--) {
45
+ v = (v * 1664525 + 1013904223) | 0;
46
+ const j = (v >>> 0) % (i + 1);
47
+ [p[i], p[j]] = [p[j], p[i]];
48
+ }
49
+
50
+ for (let i = 0; i < 512; i++) {
51
+ perm[i] = p[i & 255];
52
+ permMod12[i] = perm[i] % 12;
53
+ }
54
+ }
55
+
56
+ // Skewing factors for 2D simplex
57
+ const F2 = 0.5 * (Math.sqrt(3) - 1); // skew input space to grid
58
+ const G2 = (3 - Math.sqrt(3)) / 6; // unskew back
59
+
60
+ // Skewing factors for 3D
61
+ const F3 = 1 / 3;
62
+ const G3 = 1 / 6;
63
+
64
+ function dot2(g, x, y) { return g[0] * x + g[1] * y; }
65
+ function dot3(g, x, y, z) { return g[0] * x + g[1] * y + g[2] * z; }
66
+
67
+ /**
68
+ * 2D Simplex Noise
69
+ * @param {number} xin - x coordinate
70
+ * @param {number} yin - y coordinate
71
+ * @returns {number} noise value in [-1, 1]
72
+ */
73
+ function noise2D(xin, yin) {
74
+ let n0, n1, n2; // contributions from 3 corners
75
+
76
+ // Skew input to find which simplex cell we're in
77
+ const s = (xin + yin) * F2;
78
+ const i = Math.floor(xin + s);
79
+ const j = Math.floor(yin + s);
80
+
81
+ // Unskew back to get cell origin in (x,y) space
82
+ const t = (i + j) * G2;
83
+ const X0 = i - t;
84
+ const Y0 = j - t;
85
+ const x0 = xin - X0; // distance from cell origin
86
+ const y0 = yin - Y0;
87
+
88
+ // Determine which simplex we're in (upper or lower triangle)
89
+ let i1, j1;
90
+ if (x0 > y0) { i1 = 1; j1 = 0; } // lower triangle
91
+ else { i1 = 0; j1 = 1; } // upper triangle
92
+
93
+ // Offsets for middle and last corner
94
+ const x1 = x0 - i1 + G2;
95
+ const y1 = y0 - j1 + G2;
96
+ const x2 = x0 - 1 + 2 * G2;
97
+ const y2 = y0 - 1 + 2 * G2;
98
+
99
+ // Hash coordinates to gradient indices
100
+ const ii = i & 255;
101
+ const jj = j & 255;
102
+ const gi0 = permMod12[ii + perm[jj]];
103
+ const gi1 = permMod12[ii + i1 + perm[jj + j1]];
104
+ const gi2 = permMod12[ii + 1 + perm[jj + 1]];
105
+
106
+ // Calculate contribution from each corner
107
+ // Each corner has a radial falloff of r^2 = 0.5
108
+ let t0 = 0.5 - x0 * x0 - y0 * y0;
109
+ n0 = t0 < 0 ? 0 : (t0 *= t0, t0 * t0 * dot2(grad2[gi0], x0, y0));
110
+
111
+ let t1 = 0.5 - x1 * x1 - y1 * y1;
112
+ n1 = t1 < 0 ? 0 : (t1 *= t1, t1 * t1 * dot2(grad2[gi1], x1, y1));
113
+
114
+ let t2 = 0.5 - x2 * x2 - y2 * y2;
115
+ n2 = t2 < 0 ? 0 : (t2 *= t2, t2 * t2 * dot2(grad2[gi2], x2, y2));
116
+
117
+ // Scale to [-1, 1]
118
+ return 70 * (n0 + n1 + n2);
119
+ }
120
+
121
+ /**
122
+ * 3D Simplex Noise
123
+ * @param {number} xin - x coordinate
124
+ * @param {number} yin - y coordinate
125
+ * @param {number} zin - z coordinate
126
+ * @returns {number} noise value in [-1, 1]
127
+ */
128
+ function noise3D(xin, yin, zin) {
129
+ let n0, n1, n2, n3;
130
+
131
+ // Skew input space
132
+ const s = (xin + yin + zin) * F3;
133
+ const i = Math.floor(xin + s);
134
+ const j = Math.floor(yin + s);
135
+ const k = Math.floor(zin + s);
136
+
137
+ const t = (i + j + k) * G3;
138
+ const X0 = i - t;
139
+ const Y0 = j - t;
140
+ const Z0 = k - t;
141
+ const x0 = xin - X0;
142
+ const y0 = yin - Y0;
143
+ const z0 = zin - Z0;
144
+
145
+ // Determine which simplex we're in (6 possible tetrahedra)
146
+ let i1, j1, k1, i2, j2, k2;
147
+ if (x0 >= y0) {
148
+ if (y0 >= z0) { i1=1; j1=0; k1=0; i2=1; j2=1; k2=0; }
149
+ else if (x0 >= z0) { i1=1; j1=0; k1=0; i2=1; j2=0; k2=1; }
150
+ else { i1=0; j1=0; k1=1; i2=1; j2=0; k2=1; }
151
+ } else {
152
+ if (y0 < z0) { i1=0; j1=0; k1=1; i2=0; j2=1; k2=1; }
153
+ else if (x0 < z0) { i1=0; j1=1; k1=0; i2=0; j2=1; k2=1; }
154
+ else { i1=0; j1=1; k1=0; i2=1; j2=1; k2=0; }
155
+ }
156
+
157
+ const x1 = x0 - i1 + G3;
158
+ const y1 = y0 - j1 + G3;
159
+ const z1 = z0 - k1 + G3;
160
+ const x2 = x0 - i2 + 2 * G3;
161
+ const y2 = y0 - j2 + 2 * G3;
162
+ const z2 = z0 - k2 + 2 * G3;
163
+ const x3 = x0 - 1 + 3 * G3;
164
+ const y3 = y0 - 1 + 3 * G3;
165
+ const z3 = z0 - 1 + 3 * G3;
166
+
167
+ const ii = i & 255;
168
+ const jj = j & 255;
169
+ const kk = k & 255;
170
+ const gi0 = permMod12[ii + perm[jj + perm[kk]]];
171
+ const gi1 = permMod12[ii + i1 + perm[jj + j1 + perm[kk + k1]]];
172
+ const gi2 = permMod12[ii + i2 + perm[jj + j2 + perm[kk + k2]]];
173
+ const gi3 = permMod12[ii + 1 + perm[jj + 1 + perm[kk + 1]]];
174
+
175
+ let t0 = 0.6 - x0*x0 - y0*y0 - z0*z0;
176
+ n0 = t0 < 0 ? 0 : (t0 *= t0, t0 * t0 * dot3(grad3[gi0], x0, y0, z0));
177
+
178
+ let t1 = 0.6 - x1*x1 - y1*y1 - z1*z1;
179
+ n1 = t1 < 0 ? 0 : (t1 *= t1, t1 * t1 * dot3(grad3[gi1], x1, y1, z1));
180
+
181
+ let t2 = 0.6 - x2*x2 - y2*y2 - z2*z2;
182
+ n2 = t2 < 0 ? 0 : (t2 *= t2, t2 * t2 * dot3(grad3[gi2], x2, y2, z2));
183
+
184
+ let t3 = 0.6 - x3*x3 - y3*y3 - z3*z3;
185
+ n3 = t3 < 0 ? 0 : (t3 *= t3, t3 * t3 * dot3(grad3[gi3], x3, y3, z3));
186
+
187
+ return 32 * (n0 + n1 + n2 + n3);
188
+ }
189
+
190
+ // Auto-seed with a default
191
+ seed(0);
192
+
193
+ return { noise2D, noise3D, seed };
194
+ })();
195
+ ```
196
+
197
+ **Usage:**
198
+ ```js
199
+ SimplexNoise.seed(42); // reproducible
200
+
201
+ const val = SimplexNoise.noise2D(x * 0.01, y * 0.01); // scale coords down!
202
+ // val is in [-1, 1]
203
+ ```
204
+
205
+ ---
206
+
207
+ ## Particle System with Pool
208
+
209
+ Zero-allocation particle system. Pre-allocates everything. Swap-and-shrink for death.
210
+
211
+ ```js
212
+ // ─── Particle Pool System ───────────────────────────────────────
213
+ // Fixed-size pool: no new/delete during runtime.
214
+ // Particles are stored contiguously. Dead particles are swapped
215
+ // to the end, keeping iteration tight.
216
+
217
+ class ParticleSystem {
218
+ constructor(maxParticles = 10000) {
219
+ this.max = maxParticles;
220
+ this.count = 0; // number of alive particles
221
+
222
+ // Struct-of-Arrays layout for cache friendliness
223
+ // Each property is a flat Float32Array
224
+ this.x = new Float32Array(maxParticles);
225
+ this.y = new Float32Array(maxParticles);
226
+ this.vx = new Float32Array(maxParticles);
227
+ this.vy = new Float32Array(maxParticles);
228
+ this.life = new Float32Array(maxParticles); // current age (frames)
229
+ this.maxLife = new Float32Array(maxParticles);
230
+ this.size = new Float32Array(maxParticles);
231
+ this.alpha = new Float32Array(maxParticles);
232
+ }
233
+
234
+ /**
235
+ * Spawn a particle at (x, y) with given velocity and lifespan.
236
+ * Returns false if pool is full.
237
+ */
238
+ spawn(x, y, vx, vy, life, size = 2) {
239
+ if (this.count >= this.max) return false;
240
+
241
+ const i = this.count++;
242
+ this.x[i] = x;
243
+ this.y[i] = y;
244
+ this.vx[i] = vx;
245
+ this.vy[i] = vy;
246
+ this.life[i] = 0;
247
+ this.maxLife[i] = life;
248
+ this.size[i] = size;
249
+ this.alpha[i] = 1;
250
+ return true;
251
+ }
252
+
253
+ /**
254
+ * Spawn N particles in a burst pattern (radial).
255
+ */
256
+ burst(x, y, count, speed = 2, life = 60, size = 2) {
257
+ for (let i = 0; i < count; i++) {
258
+ const angle = (i / count) * Math.PI * 2;
259
+ const v = speed * (0.5 + Math.random() * 0.5); // slight randomness
260
+ this.spawn(
261
+ x, y,
262
+ Math.cos(angle) * v,
263
+ Math.sin(angle) * v,
264
+ life * (0.7 + Math.random() * 0.6),
265
+ size
266
+ );
267
+ }
268
+ }
269
+
270
+ /**
271
+ * Update all alive particles.
272
+ * @param {number} dt - delta time in seconds
273
+ * @param {number} gravity - gravity acceleration (positive = downward)
274
+ * @param {number} friction - velocity multiplier per frame (0.99 = slight drag)
275
+ */
276
+ update(dt = 1/60, gravity = 0, friction = 1) {
277
+ for (let i = this.count - 1; i >= 0; i--) {
278
+ // Apply forces
279
+ this.vy[i] += gravity * dt;
280
+ this.vx[i] *= friction;
281
+ this.vy[i] *= friction;
282
+
283
+ // Integrate position
284
+ this.x[i] += this.vx[i];
285
+ this.y[i] += this.vy[i];
286
+
287
+ // Age
288
+ this.life[i]++;
289
+
290
+ // Fade alpha based on remaining life
291
+ const lifeRatio = this.life[i] / this.maxLife[i];
292
+ this.alpha[i] = 1 - lifeRatio; // linear fade out
293
+
294
+ // Kill if expired: swap with last alive particle
295
+ if (this.life[i] >= this.maxLife[i]) {
296
+ this.count--;
297
+ if (i < this.count) {
298
+ // Swap all properties
299
+ this.x[i] = this.x[this.count];
300
+ this.y[i] = this.y[this.count];
301
+ this.vx[i] = this.vx[this.count];
302
+ this.vy[i] = this.vy[this.count];
303
+ this.life[i] = this.life[this.count];
304
+ this.maxLife[i] = this.maxLife[this.count];
305
+ this.size[i] = this.size[this.count];
306
+ this.alpha[i] = this.alpha[this.count];
307
+ }
308
+ }
309
+ }
310
+ }
311
+
312
+ /**
313
+ * Render all alive particles to a Canvas 2D context.
314
+ * @param {CanvasRenderingContext2D} ctx
315
+ * @param {string} color - base color (rgb format, alpha applied per-particle)
316
+ */
317
+ render(ctx, color = '255, 255, 255') {
318
+ for (let i = 0; i < this.count; i++) {
319
+ ctx.beginPath();
320
+ ctx.arc(this.x[i], this.y[i], this.size[i], 0, Math.PI * 2);
321
+ ctx.fillStyle = `rgba(${color}, ${this.alpha[i].toFixed(3)})`;
322
+ ctx.fill();
323
+ }
324
+ }
325
+ }
326
+ ```
327
+
328
+ **Usage:**
329
+ ```js
330
+ const ps = new ParticleSystem(5000);
331
+
332
+ // Continuous emitter
333
+ function emit() {
334
+ ps.spawn(
335
+ canvas.width / 2, canvas.height / 2,
336
+ (Math.random() - 0.5) * 4,
337
+ (Math.random() - 0.5) * 4,
338
+ 80 + Math.random() * 40
339
+ );
340
+ }
341
+
342
+ function loop() {
343
+ for (let i = 0; i < 5; i++) emit(); // 5 particles per frame
344
+ ps.update(1/60, 0.1, 0.99);
345
+
346
+ ctx.fillStyle = 'rgba(0, 0, 0, 0.05)'; // trail
347
+ ctx.fillRect(0, 0, w, h);
348
+ ps.render(ctx);
349
+
350
+ requestAnimationFrame(loop);
351
+ }
352
+ ```
353
+
354
+ ---
355
+
356
+ ## Complete Flow Field
357
+
358
+ Noise-driven vector field with particle followers. Full implementation with color and trail effects.
359
+
360
+ ```js
361
+ // ─── Flow Field ─────────────────────────────────────────────────
362
+ // A grid of angle vectors derived from noise. Particles follow
363
+ // the field, creating organic line patterns.
364
+
365
+ class FlowField {
366
+ /**
367
+ * @param {number} width - canvas logical width
368
+ * @param {number} height - canvas logical height
369
+ * @param {number} cellSize - grid cell size in pixels
370
+ * @param {object} options
371
+ */
372
+ constructor(width, height, cellSize = 20, options = {}) {
373
+ this.width = width;
374
+ this.height = height;
375
+ this.cellSize = cellSize;
376
+
377
+ this.cols = Math.ceil(width / cellSize);
378
+ this.rows = Math.ceil(height / cellSize);
379
+
380
+ // Flat array of angles (radians)
381
+ this.field = new Float32Array(this.cols * this.rows);
382
+
383
+ // Config
384
+ this.noiseScale = options.noiseScale || 0.003; // how zoomed-in the noise is
385
+ this.noiseZ = 0; // animate noise over time
386
+ this.noiseSpeed = options.noiseSpeed || 0.002; // z-axis speed
387
+
388
+ // Particles
389
+ this.particleCount = options.particles || 1000;
390
+ this.particles = [];
391
+ this.maxSpeed = options.maxSpeed || 2;
392
+ this.force = options.force || 0.3;
393
+
394
+ this._initParticles();
395
+ }
396
+
397
+ _initParticles() {
398
+ for (let i = 0; i < this.particleCount; i++) {
399
+ this.particles.push({
400
+ x: Math.random() * this.width,
401
+ y: Math.random() * this.height,
402
+ vx: 0,
403
+ vy: 0,
404
+ // Previous position for line drawing
405
+ px: 0,
406
+ py: 0,
407
+ age: Math.floor(Math.random() * 200), // stagger lifespans
408
+ maxAge: 200 + Math.floor(Math.random() * 100),
409
+ hue: 0, // set during update
410
+ });
411
+ }
412
+ }
413
+
414
+ /**
415
+ * Rebuild the angle grid from noise.
416
+ * Call once per frame before updating particles.
417
+ */
418
+ updateField() {
419
+ this.noiseZ += this.noiseSpeed;
420
+
421
+ for (let y = 0; y < this.rows; y++) {
422
+ for (let x = 0; x < this.cols; x++) {
423
+ // Noise value in [-1, 1], map to full rotation
424
+ const n = SimplexNoise.noise3D(
425
+ x * this.noiseScale * this.cellSize,
426
+ y * this.noiseScale * this.cellSize,
427
+ this.noiseZ
428
+ );
429
+ this.field[y * this.cols + x] = n * Math.PI * 2;
430
+ }
431
+ }
432
+ }
433
+
434
+ /**
435
+ * Get the field angle at a given pixel position.
436
+ * Returns 0 if out of bounds.
437
+ */
438
+ lookup(x, y) {
439
+ const col = Math.floor(x / this.cellSize);
440
+ const row = Math.floor(y / this.cellSize);
441
+ if (col < 0 || col >= this.cols || row < 0 || row >= this.rows) return 0;
442
+ return this.field[row * this.cols + col];
443
+ }
444
+
445
+ /**
446
+ * Update all particles: follow field, apply velocity, handle wrapping/respawn.
447
+ */
448
+ updateParticles() {
449
+ for (const p of this.particles) {
450
+ // Store previous position (for line drawing)
451
+ p.px = p.x;
452
+ p.py = p.y;
453
+
454
+ // Get field angle at current position
455
+ const angle = this.lookup(p.x, p.y);
456
+
457
+ // Apply force in field direction
458
+ p.vx += Math.cos(angle) * this.force;
459
+ p.vy += Math.sin(angle) * this.force;
460
+
461
+ // Clamp speed
462
+ const speed = Math.sqrt(p.vx * p.vx + p.vy * p.vy);
463
+ if (speed > this.maxSpeed) {
464
+ p.vx = (p.vx / speed) * this.maxSpeed;
465
+ p.vy = (p.vy / speed) * this.maxSpeed;
466
+ }
467
+
468
+ // Move
469
+ p.x += p.vx;
470
+ p.y += p.vy;
471
+
472
+ // Slight friction
473
+ p.vx *= 0.96;
474
+ p.vy *= 0.96;
475
+
476
+ // Color based on angle
477
+ p.hue = ((angle / (Math.PI * 2)) * 360 + 360) % 360;
478
+
479
+ // Age and respawn
480
+ p.age++;
481
+ if (p.age > p.maxAge || p.x < 0 || p.x > this.width || p.y < 0 || p.y > this.height) {
482
+ p.x = Math.random() * this.width;
483
+ p.y = Math.random() * this.height;
484
+ p.px = p.x;
485
+ p.py = p.y;
486
+ p.vx = 0;
487
+ p.vy = 0;
488
+ p.age = 0;
489
+ }
490
+ }
491
+ }
492
+
493
+ /**
494
+ * Render particle trails as lines.
495
+ */
496
+ render(ctx) {
497
+ for (const p of this.particles) {
498
+ // Skip first frame (no previous position)
499
+ if (p.age === 0) continue;
500
+
501
+ const alpha = Math.min(1, (1 - p.age / p.maxAge) * 0.8);
502
+ ctx.beginPath();
503
+ ctx.moveTo(p.px, p.py);
504
+ ctx.lineTo(p.x, p.y);
505
+ ctx.strokeStyle = `hsla(${p.hue}, 70%, 60%, ${alpha})`;
506
+ ctx.lineWidth = 1;
507
+ ctx.stroke();
508
+ }
509
+ }
510
+ }
511
+
512
+ // ─── Usage ──────────────────────────────────────────────────────
513
+ //
514
+ // const canvas = document.getElementById('canvas');
515
+ // const ctx = setupCanvas(canvas, window.innerWidth, window.innerHeight);
516
+ // SimplexNoise.seed(42);
517
+ //
518
+ // const ff = new FlowField(canvas.clientWidth, canvas.clientHeight, 20, {
519
+ // particles: 2000,
520
+ // noiseScale: 0.004,
521
+ // noiseSpeed: 0.001,
522
+ // maxSpeed: 2,
523
+ // force: 0.2,
524
+ // });
525
+ //
526
+ // function loop() {
527
+ // // Trail fade (no clearRect!)
528
+ // ctx.fillStyle = 'rgba(10, 10, 15, 0.03)';
529
+ // ctx.fillRect(0, 0, canvas.clientWidth, canvas.clientHeight);
530
+ //
531
+ // ff.updateField();
532
+ // ff.updateParticles();
533
+ // ff.render(ctx);
534
+ //
535
+ // requestAnimationFrame(loop);
536
+ // }
537
+ // requestAnimationFrame(loop);
538
+ ```
539
+
540
+ ---
541
+
542
+ ## L-System Renderer
543
+
544
+ Complete L-system with string expansion and turtle graphics rendering.
545
+
546
+ ```js
547
+ // ─── L-System ───────────────────────────────────────────────────
548
+ // String-rewriting system + turtle graphics.
549
+ // Produces fractal plants, trees, curves.
550
+
551
+ class LSystem {
552
+ /**
553
+ * @param {string} axiom - starting string
554
+ * @param {Object<string, string>} rules - production rules { 'F': 'F[+F]F[-F]F' }
555
+ * @param {number} angle - turn angle in degrees
556
+ */
557
+ constructor(axiom, rules, angle) {
558
+ this.axiom = axiom;
559
+ this.rules = rules;
560
+ this.angle = (angle * Math.PI) / 180; // convert to radians
561
+ this.commands = axiom;
562
+ }
563
+
564
+ /**
565
+ * Expand the L-system string by N iterations.
566
+ * WARNING: string length grows exponentially. Keep iterations low (4-7).
567
+ */
568
+ generate(iterations) {
569
+ this.commands = this.axiom;
570
+ for (let i = 0; i < iterations; i++) {
571
+ let next = '';
572
+ for (const char of this.commands) {
573
+ next += this.rules[char] || char;
574
+ }
575
+ this.commands = next;
576
+ }
577
+ return this.commands;
578
+ }
579
+
580
+ /**
581
+ * Render the L-system commands to a canvas context.
582
+ * @param {CanvasRenderingContext2D} ctx
583
+ * @param {number} startX - turtle start x
584
+ * @param {number} startY - turtle start y
585
+ * @param {number} startAngle - initial heading in radians (default: up)
586
+ * @param {number} stepLength - length per 'F' step
587
+ * @param {object} style - { strokeStyle, lineWidth, depthFade }
588
+ */
589
+ render(ctx, startX, startY, startAngle = -Math.PI / 2, stepLength = 5, style = {}) {
590
+ const {
591
+ strokeStyle = '#88cc88',
592
+ lineWidth = 1,
593
+ depthFade = true, // fade color with stack depth
594
+ } = style;
595
+
596
+ // Turtle state
597
+ let x = startX;
598
+ let y = startY;
599
+ let angle = startAngle;
600
+ let depth = 0;
601
+ const stack = []; // save/restore stack for [ ]
602
+
603
+ ctx.save();
604
+ ctx.lineCap = 'round';
605
+
606
+ for (const char of this.commands) {
607
+ switch (char) {
608
+ // F: move forward, drawing a line
609
+ case 'F': {
610
+ const nx = x + Math.cos(angle) * stepLength;
611
+ const ny = y + Math.sin(angle) * stepLength;
612
+
613
+ ctx.beginPath();
614
+ ctx.moveTo(x, y);
615
+ ctx.lineTo(nx, ny);
616
+
617
+ if (depthFade) {
618
+ const alpha = Math.max(0.1, 1 - depth * 0.15);
619
+ const width = Math.max(0.5, lineWidth - depth * 0.2);
620
+ ctx.strokeStyle = strokeStyle.includes('rgba')
621
+ ? strokeStyle
622
+ : `rgba(136, 204, 136, ${alpha})`;
623
+ ctx.lineWidth = width;
624
+ } else {
625
+ ctx.strokeStyle = strokeStyle;
626
+ ctx.lineWidth = lineWidth;
627
+ }
628
+
629
+ ctx.stroke();
630
+ x = nx;
631
+ y = ny;
632
+ break;
633
+ }
634
+
635
+ // f: move forward without drawing (pen up)
636
+ case 'f': {
637
+ x += Math.cos(angle) * stepLength;
638
+ y += Math.sin(angle) * stepLength;
639
+ break;
640
+ }
641
+
642
+ // +: turn right (clockwise)
643
+ case '+':
644
+ angle += this.angle;
645
+ break;
646
+
647
+ // -: turn left (counter-clockwise)
648
+ case '-':
649
+ angle -= this.angle;
650
+ break;
651
+
652
+ // [: push state (branch start)
653
+ case '[':
654
+ stack.push({ x, y, angle, depth });
655
+ depth++;
656
+ break;
657
+
658
+ // ]: pop state (branch end)
659
+ case ']': {
660
+ const state = stack.pop();
661
+ if (state) {
662
+ x = state.x;
663
+ y = state.y;
664
+ angle = state.angle;
665
+ depth = state.depth;
666
+ }
667
+ break;
668
+ }
669
+ }
670
+ }
671
+
672
+ ctx.restore();
673
+ }
674
+ }
675
+
676
+ // ─── Preset L-Systems ──────────────────────────────────────────
677
+
678
+ const LSYSTEM_PRESETS = {
679
+ // Fractal plant (bush-like)
680
+ plant: {
681
+ axiom: 'X',
682
+ rules: {
683
+ 'X': 'F+[[X]-X]-F[-FX]+X',
684
+ 'F': 'FF',
685
+ },
686
+ angle: 25,
687
+ iterations: 6,
688
+ stepLength: 3,
689
+ },
690
+
691
+ // Koch snowflake
692
+ koch: {
693
+ axiom: 'F--F--F',
694
+ rules: { 'F': 'F+F--F+F' },
695
+ angle: 60,
696
+ iterations: 4,
697
+ stepLength: 3,
698
+ },
699
+
700
+ // Sierpinski triangle
701
+ sierpinski: {
702
+ axiom: 'F-G-G',
703
+ rules: { 'F': 'F-G+F+G-F', 'G': 'GG' },
704
+ angle: 120,
705
+ iterations: 6,
706
+ stepLength: 4,
707
+ },
708
+
709
+ // Dragon curve
710
+ dragon: {
711
+ axiom: 'FX',
712
+ rules: { 'X': 'X+YF+', 'Y': '-FX-Y' },
713
+ angle: 90,
714
+ iterations: 12,
715
+ stepLength: 5,
716
+ },
717
+
718
+ // Binary tree
719
+ tree: {
720
+ axiom: 'F',
721
+ rules: { 'F': 'FF+[+F-F-F]-[-F+F+F]' },
722
+ angle: 22.5,
723
+ iterations: 4,
724
+ stepLength: 8,
725
+ },
726
+ };
727
+
728
+ // ─── Usage ──────────────────────────────────────────────────────
729
+ //
730
+ // const preset = LSYSTEM_PRESETS.plant;
731
+ // const ls = new LSystem(preset.axiom, preset.rules, preset.angle);
732
+ // ls.generate(preset.iterations);
733
+ //
734
+ // ctx.fillStyle = '#111';
735
+ // ctx.fillRect(0, 0, w, h);
736
+ // ls.render(ctx, w / 2, h, -Math.PI / 2, preset.stepLength, {
737
+ // strokeStyle: '#88cc88',
738
+ // lineWidth: 1.5,
739
+ // depthFade: true,
740
+ // });
741
+ ```
742
+
743
+ ---
744
+
745
+ ## Lorenz Attractor
746
+
747
+ The classic strange attractor. 3D system projected to 2D canvas.
748
+
749
+ ```js
750
+ // ─── Lorenz Attractor ───────────────────────────────────────────
751
+ // A chaotic system of 3 coupled ODEs. The trajectory never repeats
752
+ // and never intersects itself, creating the iconic butterfly shape.
753
+ //
754
+ // dx/dt = sigma * (y - x)
755
+ // dy/dt = x * (rho - z) - y
756
+ // dz/dt = x * y - beta * z
757
+
758
+ class LorenzAttractor {
759
+ /**
760
+ * @param {object} options
761
+ * @param {number} options.sigma - Prandtl number (default 10)
762
+ * @param {number} options.rho - Rayleigh number (default 28)
763
+ * @param {number} options.beta - geometric factor (default 8/3)
764
+ * @param {number} options.dt - integration timestep
765
+ * @param {number} options.trailLength - how many points to remember
766
+ */
767
+ constructor(options = {}) {
768
+ this.sigma = options.sigma || 10;
769
+ this.rho = options.rho || 28;
770
+ this.beta = options.beta || 8 / 3;
771
+ this.dt = options.dt || 0.005;
772
+
773
+ this.trailLength = options.trailLength || 5000;
774
+
775
+ // Current position in 3D space
776
+ this.x = 0.01;
777
+ this.y = 0;
778
+ this.z = 0;
779
+
780
+ // Trail history (ring buffer)
781
+ this.trail = new Float32Array(this.trailLength * 3); // [x,y,z, x,y,z, ...]
782
+ this.trailHead = 0;
783
+ this.trailCount = 0;
784
+ }
785
+
786
+ /**
787
+ * Step the simulation forward by one timestep (Runge-Kutta 4th order).
788
+ * RK4 is crucial for chaotic systems -- Euler integration diverges fast.
789
+ */
790
+ step() {
791
+ const { sigma, rho, beta, dt } = this;
792
+ let { x, y, z } = this;
793
+
794
+ // Derivatives function
795
+ const f = (x, y, z) => ({
796
+ dx: sigma * (y - x),
797
+ dy: x * (rho - z) - y,
798
+ dz: x * y - beta * z,
799
+ });
800
+
801
+ // RK4 integration
802
+ const k1 = f(x, y, z);
803
+ const k2 = f(
804
+ x + k1.dx * dt * 0.5,
805
+ y + k1.dy * dt * 0.5,
806
+ z + k1.dz * dt * 0.5
807
+ );
808
+ const k3 = f(
809
+ x + k2.dx * dt * 0.5,
810
+ y + k2.dy * dt * 0.5,
811
+ z + k2.dz * dt * 0.5
812
+ );
813
+ const k4 = f(
814
+ x + k3.dx * dt,
815
+ y + k3.dy * dt,
816
+ z + k3.dz * dt
817
+ );
818
+
819
+ this.x += (dt / 6) * (k1.dx + 2*k2.dx + 2*k3.dx + k4.dx);
820
+ this.y += (dt / 6) * (k1.dy + 2*k2.dy + 2*k3.dy + k4.dy);
821
+ this.z += (dt / 6) * (k1.dz + 2*k2.dz + 2*k3.dz + k4.dz);
822
+
823
+ // Store in trail ring buffer
824
+ const idx = this.trailHead * 3;
825
+ this.trail[idx] = this.x;
826
+ this.trail[idx + 1] = this.y;
827
+ this.trail[idx + 2] = this.z;
828
+ this.trailHead = (this.trailHead + 1) % this.trailLength;
829
+ if (this.trailCount < this.trailLength) this.trailCount++;
830
+ }
831
+
832
+ /**
833
+ * Step N times (useful for faster evolution per frame).
834
+ */
835
+ stepN(n) {
836
+ for (let i = 0; i < n; i++) this.step();
837
+ }
838
+
839
+ /**
840
+ * Project 3D point to 2D using simple orthographic projection.
841
+ * The Lorenz attractor lives roughly in x:[-20,20], y:[-25,25], z:[0,50].
842
+ * We map x->screen.x and z->screen.y for the classic side view.
843
+ *
844
+ * @param {number} cx - canvas center x
845
+ * @param {number} cy - canvas center y
846
+ * @param {number} scale - zoom factor
847
+ * @returns {{ sx: number, sy: number }} screen coordinates
848
+ */
849
+ project(px, py, pz, cx, cy, scale = 8) {
850
+ return {
851
+ sx: cx + px * scale,
852
+ sy: cy - (pz - 25) * scale, // center z around 25, flip y-axis
853
+ };
854
+ }
855
+
856
+ /**
857
+ * Render the attractor trail to a 2D canvas.
858
+ * Older points are more transparent, creating a fade-in effect.
859
+ */
860
+ render(ctx, cx, cy, scale = 8) {
861
+ if (this.trailCount < 2) return;
862
+
863
+ // Walk the ring buffer from oldest to newest
864
+ const start = this.trailCount < this.trailLength
865
+ ? 0
866
+ : this.trailHead;
867
+
868
+ let prevIdx = start * 3;
869
+ let prev = this.project(
870
+ this.trail[prevIdx], this.trail[prevIdx+1], this.trail[prevIdx+2],
871
+ cx, cy, scale
872
+ );
873
+
874
+ for (let i = 1; i < this.trailCount; i++) {
875
+ const bufIdx = ((start + i) % this.trailLength) * 3;
876
+ const cur = this.project(
877
+ this.trail[bufIdx], this.trail[bufIdx+1], this.trail[bufIdx+2],
878
+ cx, cy, scale
879
+ );
880
+
881
+ // Alpha: oldest = faint, newest = bright
882
+ const t = i / this.trailCount;
883
+ const alpha = t * 0.9 + 0.1;
884
+
885
+ // Color: hue shifts along the trail
886
+ const hue = (t * 240 + 200) % 360; // blue to purple
887
+
888
+ ctx.beginPath();
889
+ ctx.moveTo(prev.sx, prev.sy);
890
+ ctx.lineTo(cur.sx, cur.sy);
891
+ ctx.strokeStyle = `hsla(${hue}, 80%, 60%, ${alpha})`;
892
+ ctx.lineWidth = 0.8;
893
+ ctx.stroke();
894
+
895
+ prev = cur;
896
+ }
897
+ }
898
+ }
899
+
900
+ // ─── Usage ──────────────────────────────────────────────────────
901
+ //
902
+ // const canvas = document.getElementById('canvas');
903
+ // const ctx = setupCanvas(canvas, window.innerWidth, window.innerHeight);
904
+ // const lorenz = new LorenzAttractor({ trailLength: 8000, dt: 0.005 });
905
+ //
906
+ // function loop() {
907
+ // // Dark background with slight trail
908
+ // ctx.fillStyle = 'rgba(5, 5, 15, 0.02)';
909
+ // ctx.fillRect(0, 0, canvas.clientWidth, canvas.clientHeight);
910
+ //
911
+ // // Step multiple times per frame for faster evolution
912
+ // lorenz.stepN(10);
913
+ //
914
+ // // Render centered on canvas
915
+ // lorenz.render(ctx, canvas.clientWidth / 2, canvas.clientHeight / 2, 8);
916
+ //
917
+ // requestAnimationFrame(loop);
918
+ // }
919
+ // requestAnimationFrame(loop);
920
+ //
921
+ // ─── Classic Parameters ─────────────────────────────────────────
922
+ // sigma=10, rho=28, beta=8/3 → classic butterfly
923
+ // sigma=10, rho=99.96, beta=8/3 → tighter knots
924
+ // sigma=14, rho=28, beta=8/3 → wider wings
925
+ ```