@guinetik/gcanvas 1.0.4 → 1.0.5

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 (193) hide show
  1. package/dist/CNAME +1 -0
  2. package/dist/animations.html +31 -0
  3. package/dist/basic.html +38 -0
  4. package/dist/baskara.html +31 -0
  5. package/dist/bezier.html +35 -0
  6. package/dist/beziersignature.html +29 -0
  7. package/dist/blackhole.html +28 -0
  8. package/dist/blob.html +35 -0
  9. package/dist/coordinates.html +698 -0
  10. package/dist/cube3d.html +23 -0
  11. package/dist/demos.css +303 -0
  12. package/dist/dino.html +42 -0
  13. package/dist/easing.html +28 -0
  14. package/dist/events.html +195 -0
  15. package/dist/fluent.html +647 -0
  16. package/dist/fluid-simple.html +22 -0
  17. package/dist/fluid.html +37 -0
  18. package/dist/fractals.html +36 -0
  19. package/dist/gameobjects.html +626 -0
  20. package/dist/gcanvas.es.js +517 -0
  21. package/dist/gcanvas.es.min.js +1 -1
  22. package/dist/gcanvas.umd.js +1 -1
  23. package/dist/gcanvas.umd.min.js +1 -1
  24. package/dist/genart.html +26 -0
  25. package/dist/gendream.html +26 -0
  26. package/dist/group.html +36 -0
  27. package/dist/home.html +587 -0
  28. package/dist/hyperbolic001.html +23 -0
  29. package/dist/hyperbolic002.html +23 -0
  30. package/dist/hyperbolic003.html +23 -0
  31. package/dist/hyperbolic004.html +23 -0
  32. package/dist/hyperbolic005.html +22 -0
  33. package/dist/index.html +398 -0
  34. package/dist/isometric.html +34 -0
  35. package/dist/js/animations.js +452 -0
  36. package/dist/js/basic.js +204 -0
  37. package/dist/js/baskara.js +751 -0
  38. package/dist/js/bezier.js +692 -0
  39. package/dist/js/beziersignature.js +241 -0
  40. package/dist/js/blackhole/accretiondisk.obj.js +379 -0
  41. package/dist/js/blackhole/blackhole.obj.js +318 -0
  42. package/dist/js/blackhole/index.js +409 -0
  43. package/dist/js/blackhole/particle.js +56 -0
  44. package/dist/js/blackhole/starfield.obj.js +218 -0
  45. package/dist/js/blob.js +2276 -0
  46. package/dist/js/coordinates.js +840 -0
  47. package/dist/js/cube3d.js +789 -0
  48. package/dist/js/dino.js +1420 -0
  49. package/dist/js/easing.js +477 -0
  50. package/dist/js/fluent.js +183 -0
  51. package/dist/js/fluid-simple.js +253 -0
  52. package/dist/js/fluid.js +527 -0
  53. package/dist/js/fractals.js +932 -0
  54. package/dist/js/fractalworker.js +93 -0
  55. package/dist/js/gameobjects.js +176 -0
  56. package/dist/js/genart.js +268 -0
  57. package/dist/js/gendream.js +209 -0
  58. package/dist/js/group.js +140 -0
  59. package/dist/js/hyperbolic001.js +310 -0
  60. package/dist/js/hyperbolic002.js +388 -0
  61. package/dist/js/hyperbolic003.js +319 -0
  62. package/dist/js/hyperbolic004.js +345 -0
  63. package/dist/js/hyperbolic005.js +340 -0
  64. package/dist/js/info-toggle.js +25 -0
  65. package/dist/js/isometric.js +863 -0
  66. package/dist/js/kerr.js +1547 -0
  67. package/dist/js/lavalamp.js +590 -0
  68. package/dist/js/layout.js +354 -0
  69. package/dist/js/mondrian.js +285 -0
  70. package/dist/js/opacity.js +275 -0
  71. package/dist/js/painter.js +484 -0
  72. package/dist/js/particles-showcase.js +514 -0
  73. package/dist/js/particles.js +299 -0
  74. package/dist/js/patterns.js +397 -0
  75. package/dist/js/penrose/artifact.js +69 -0
  76. package/dist/js/penrose/blackhole.js +121 -0
  77. package/dist/js/penrose/constants.js +73 -0
  78. package/dist/js/penrose/game.js +943 -0
  79. package/dist/js/penrose/lore.js +278 -0
  80. package/dist/js/penrose/penrosescene.js +892 -0
  81. package/dist/js/penrose/ship.js +216 -0
  82. package/dist/js/penrose/sounds.js +211 -0
  83. package/dist/js/penrose/voidparticle.js +55 -0
  84. package/dist/js/penrose/voidscene.js +258 -0
  85. package/dist/js/penrose/voidship.js +144 -0
  86. package/dist/js/penrose/wormhole.js +46 -0
  87. package/dist/js/pipeline.js +555 -0
  88. package/dist/js/plane3d.js +256 -0
  89. package/dist/js/platformer.js +1579 -0
  90. package/dist/js/scene.js +304 -0
  91. package/dist/js/scenes.js +320 -0
  92. package/dist/js/schrodinger.js +410 -0
  93. package/dist/js/schwarzschild.js +1015 -0
  94. package/dist/js/shapes.js +628 -0
  95. package/dist/js/space/alien.js +171 -0
  96. package/dist/js/space/boom.js +98 -0
  97. package/dist/js/space/boss.js +353 -0
  98. package/dist/js/space/buff.js +73 -0
  99. package/dist/js/space/bullet.js +102 -0
  100. package/dist/js/space/constants.js +85 -0
  101. package/dist/js/space/game.js +1884 -0
  102. package/dist/js/space/hud.js +112 -0
  103. package/dist/js/space/laserbeam.js +179 -0
  104. package/dist/js/space/lightning.js +277 -0
  105. package/dist/js/space/minion.js +192 -0
  106. package/dist/js/space/missile.js +212 -0
  107. package/dist/js/space/player.js +430 -0
  108. package/dist/js/space/powerup.js +90 -0
  109. package/dist/js/space/starfield.js +58 -0
  110. package/dist/js/space/starpower.js +90 -0
  111. package/dist/js/spacetime.js +559 -0
  112. package/dist/js/sphere3d.js +229 -0
  113. package/dist/js/sprite.js +473 -0
  114. package/dist/js/starfaux/config.js +118 -0
  115. package/dist/js/starfaux/enemy.js +353 -0
  116. package/dist/js/starfaux/hud.js +78 -0
  117. package/dist/js/starfaux/index.js +482 -0
  118. package/dist/js/starfaux/laser.js +182 -0
  119. package/dist/js/starfaux/player.js +468 -0
  120. package/dist/js/starfaux/terrain.js +560 -0
  121. package/dist/js/study001.js +275 -0
  122. package/dist/js/study002.js +366 -0
  123. package/dist/js/study003.js +331 -0
  124. package/dist/js/study004.js +389 -0
  125. package/dist/js/study005.js +209 -0
  126. package/dist/js/study006.js +194 -0
  127. package/dist/js/study007.js +192 -0
  128. package/dist/js/study008.js +413 -0
  129. package/dist/js/svgtween.js +204 -0
  130. package/dist/js/tde/accretiondisk.js +471 -0
  131. package/dist/js/tde/blackhole.js +219 -0
  132. package/dist/js/tde/blackholescene.js +209 -0
  133. package/dist/js/tde/config.js +59 -0
  134. package/dist/js/tde/index.js +820 -0
  135. package/dist/js/tde/jets.js +290 -0
  136. package/dist/js/tde/lensedstarfield.js +154 -0
  137. package/dist/js/tde/tdestar.js +297 -0
  138. package/dist/js/tde/tidalstream.js +372 -0
  139. package/dist/js/tde_old/blackhole.obj.js +354 -0
  140. package/dist/js/tde_old/debris.obj.js +791 -0
  141. package/dist/js/tde_old/flare.obj.js +239 -0
  142. package/dist/js/tde_old/index.js +448 -0
  143. package/dist/js/tde_old/star.obj.js +812 -0
  144. package/dist/js/tetris/config.js +157 -0
  145. package/dist/js/tetris/grid.js +286 -0
  146. package/dist/js/tetris/index.js +1195 -0
  147. package/dist/js/tetris/renderer.js +634 -0
  148. package/dist/js/tetris/tetrominos.js +280 -0
  149. package/dist/js/tiles.js +312 -0
  150. package/dist/js/tweendemo.js +79 -0
  151. package/dist/js/visibility.js +102 -0
  152. package/dist/kerr.html +28 -0
  153. package/dist/lavalamp.html +27 -0
  154. package/dist/layouts.html +37 -0
  155. package/dist/logo.svg +4 -0
  156. package/dist/loop.html +84 -0
  157. package/dist/mondrian.html +32 -0
  158. package/dist/og_image.png +0 -0
  159. package/dist/opacity.html +36 -0
  160. package/dist/painter.html +39 -0
  161. package/dist/particles-showcase.html +28 -0
  162. package/dist/particles.html +24 -0
  163. package/dist/patterns.html +33 -0
  164. package/dist/penrose-game.html +31 -0
  165. package/dist/pipeline.html +737 -0
  166. package/dist/plane3d.html +24 -0
  167. package/dist/platformer.html +43 -0
  168. package/dist/scene.html +33 -0
  169. package/dist/scenes.html +96 -0
  170. package/dist/schrodinger.html +27 -0
  171. package/dist/schwarzschild.html +27 -0
  172. package/dist/shapes.html +16 -0
  173. package/dist/space.html +85 -0
  174. package/dist/spacetime.html +27 -0
  175. package/dist/sphere3d.html +24 -0
  176. package/dist/sprite.html +18 -0
  177. package/dist/starfaux.html +22 -0
  178. package/dist/study001.html +23 -0
  179. package/dist/study002.html +23 -0
  180. package/dist/study003.html +23 -0
  181. package/dist/study004.html +23 -0
  182. package/dist/study005.html +22 -0
  183. package/dist/study006.html +24 -0
  184. package/dist/study007.html +24 -0
  185. package/dist/study008.html +22 -0
  186. package/dist/svgtween.html +29 -0
  187. package/dist/tde.html +28 -0
  188. package/dist/tetris3d.html +25 -0
  189. package/dist/tiles.html +28 -0
  190. package/dist/transforms.html +400 -0
  191. package/dist/tween.html +45 -0
  192. package/dist/visibility.html +33 -0
  193. package/package.json +1 -1
@@ -0,0 +1,932 @@
1
+ /**
2
+ * GCanvas Fractal Demo
3
+ *
4
+ * This demo showcases the creation of various fractals using the GCanvas engine.
5
+ * It implements multiple fractal algorithms and provides interactive controls.
6
+ */
7
+ import {
8
+ Button,
9
+ Easing,
10
+ FPSCounter,
11
+ Fractals,
12
+ Game,
13
+ HorizontalLayout,
14
+ ImageGo,
15
+ Painter,
16
+ Position,
17
+ Scene,
18
+ TaskManager,
19
+ Text,
20
+ } from "/gcanvas.es.min.js";
21
+
22
+ export class FractalDemo extends Game {
23
+ constructor(canvas) {
24
+ super(canvas);
25
+ this.enableFluidSize();
26
+ // Settings for fractal rendering
27
+ this.settings = {
28
+ type: Fractals.types.MANDELBROT,
29
+ iterations: 32,
30
+ colorScheme: Fractals.colors.FUTURISTIC,
31
+ hueShift: 0,
32
+ zoom: 1,
33
+ offsetX: 0,
34
+ offsetY: 0,
35
+ animating: false,
36
+ typeIndex: 0,
37
+ };
38
+ // Target values for smooth interpolation
39
+ this.target = {
40
+ zoom: 1,
41
+ offsetX: 0,
42
+ offsetY: 0
43
+ };
44
+ // Pan velocity for momentum
45
+ this.panVelocity = { x: 0, y: 0 };
46
+ this.panDamping = 0.92;
47
+ // Easing speed (higher = faster convergence)
48
+ this.easeSpeed = 8;
49
+ this.baseIterations = 32;
50
+ // Render throttling
51
+ this.lastRenderTime = 0;
52
+ this.minRenderInterval = 50; // ms between renders during interaction
53
+ this.isInteracting = false;
54
+ }
55
+
56
+ init() {
57
+ super.init();
58
+
59
+ // Create scenes
60
+ this.mainScene = new Scene(this, {
61
+ anchor: Position.CENTER,
62
+ debug: true
63
+ });
64
+ this.mainScene.width = this.width;
65
+ this.mainScene.height = this.height;
66
+ this.ui = new Scene(this, { debug: true, anchor: Position.CENTER });
67
+ this.pipeline.add(this.mainScene);
68
+ this.pipeline.add(this.ui);
69
+
70
+ // Add fractal renderer - create it at full size initially
71
+ this.fractal = new FractalRenderer(
72
+ this,
73
+ this.mainScene.width,
74
+ this.mainScene.height,
75
+ {
76
+ debug: true,
77
+ debugColor: "white",
78
+ width: this.mainScene.width,
79
+ height: this.mainScene.height,
80
+ }
81
+ );
82
+ this.mainScene.add(this.fractal);
83
+ // Add UI controls
84
+ this.setupUI();
85
+ // Add FPS counter
86
+ this.ui.add(
87
+ new FPSCounter(this, {
88
+ anchor: "bottom-right",
89
+ color: "#0f0",
90
+ })
91
+ );
92
+ // Set up interactive controls
93
+ this.setupInteraction();
94
+ this.onResize();
95
+ // Initial render
96
+ this.updateFractalSettings();
97
+ }
98
+
99
+ onResize() {
100
+ if (this.mainScene) {
101
+ // Full width/height for mobile-friendly display
102
+ const newWidth = this.width;
103
+ const newHeight = this.height;
104
+
105
+ if (
106
+ this.mainScene.width !== newWidth ||
107
+ this.mainScene.height !== newHeight
108
+ ) {
109
+ this.mainScene.width = newWidth;
110
+ this.mainScene.height = newHeight;
111
+ this.ui.width = newWidth;
112
+ this.ui.height = newHeight;
113
+ // Resize fractal renderer with a small delay to prevent multiple resizes
114
+ if (!this.resizeTimeout) {
115
+ this.resizeTimeout = setTimeout(() => {
116
+ // Use actual dimensions, capped for performance on large screens
117
+ const renderWidth = Math.min(newWidth, 1200);
118
+ const renderHeight = Math.min(newHeight, 900);
119
+ this.fractal.resize(renderWidth, renderHeight);
120
+ this.resizeTimeout = null;
121
+ }, 100);
122
+ }
123
+ }
124
+ }
125
+ }
126
+
127
+ update(dt) {
128
+ super.update(dt);
129
+
130
+ let needsRender = false;
131
+ const wasAnimating = this.wasAnimating || false;
132
+
133
+ // Apply pan momentum when not dragging
134
+ if (!this.settings.isDragging && (Math.abs(this.panVelocity.x) > 0.0001 || Math.abs(this.panVelocity.y) > 0.0001)) {
135
+ const scaleFactor = 0.005 / this.settings.zoom;
136
+ this.target.offsetX -= this.panVelocity.x * scaleFactor;
137
+ this.target.offsetY -= this.panVelocity.y * scaleFactor;
138
+ this.panVelocity.x *= this.panDamping;
139
+ this.panVelocity.y *= this.panDamping;
140
+ needsRender = true;
141
+ }
142
+
143
+ // Smooth interpolation toward target values using Easing.lerp
144
+ const lerpFactor = 1 - Math.exp(-this.easeSpeed * dt);
145
+ const zoomDiff = Math.abs(this.settings.zoom - this.target.zoom);
146
+ const offsetXDiff = Math.abs(this.settings.offsetX - this.target.offsetX);
147
+ const offsetYDiff = Math.abs(this.settings.offsetY - this.target.offsetY);
148
+
149
+ if (zoomDiff > 0.001 || offsetXDiff > 0.00001 || offsetYDiff > 0.00001) {
150
+ this.settings.zoom = Easing.lerp(this.settings.zoom, this.target.zoom, lerpFactor);
151
+ this.settings.offsetX = Easing.lerp(this.settings.offsetX, this.target.offsetX, lerpFactor);
152
+ this.settings.offsetY = Easing.lerp(this.settings.offsetY, this.target.offsetY, lerpFactor);
153
+ needsRender = true;
154
+ }
155
+
156
+ // Handle animation if enabled
157
+ if (this.settings.animating) {
158
+ this.settings.hueShift = (this.settings.hueShift + dt * 30) % 360;
159
+ this.target.zoom = this.target.zoom * (1 + dt * 0.08);
160
+ needsRender = true;
161
+ }
162
+
163
+ this.wasAnimating = needsRender;
164
+
165
+ // Throttle renders during animation, but always render when settled
166
+ if (needsRender) {
167
+ const now = performance.now();
168
+ if (now - this.lastRenderTime > this.minRenderInterval) {
169
+ this.lastRenderTime = now;
170
+ this.updateFractalSettings(true); // Preview mode
171
+ }
172
+ } else if (wasAnimating) {
173
+ // Animation just settled - final full-quality render
174
+ this.updateFractalSettings(false);
175
+ }
176
+ }
177
+
178
+ updateFractalSettings(preview = false) {
179
+ // Use lower iterations for preview during interaction
180
+ const iterations = preview
181
+ ? Math.max(16, Math.floor(this.baseIterations * 0.4))
182
+ : this.baseIterations;
183
+
184
+ // Update renderer settings - it will render asynchronously
185
+ this.fractal.updateSettings({
186
+ type: this.settings.type,
187
+ iterations: iterations,
188
+ colorScheme: this.settings.colorScheme,
189
+ hueShift: this.settings.hueShift,
190
+ zoom: this.settings.zoom,
191
+ offsetX: this.settings.offsetX,
192
+ offsetY: this.settings.offsetY,
193
+ });
194
+ }
195
+
196
+ setupUI() {
197
+ // Responsive sizing based on screen width
198
+ const isMobile = this.width < 600;
199
+ const btnHeight = isMobile ? 28 : 30;
200
+ const btnSpacing = isMobile ? 4 : 10;
201
+ const fontSize = isMobile ? "11px" : "13px";
202
+
203
+ // Create UI layout
204
+ const controlsLayout = new HorizontalLayout(this, {
205
+ anchor: Position.BOTTOM_LEFT,
206
+ spacing: btnSpacing,
207
+ padding: isMobile ? 4 : 10,
208
+ height: btnHeight + 10,
209
+ width: this.width,
210
+ debug: false,
211
+ });
212
+
213
+ // Fractal type selector
214
+ const typeBtn = new Button(this, {
215
+ text: isMobile ? this.settings.type.toUpperCase() : `Type: ${this.settings.type.toUpperCase()}`,
216
+ width: isMobile ? 90 : 180,
217
+ height: btnHeight,
218
+ fontSize,
219
+ onClick: () => this.cycleFractalType(),
220
+ });
221
+
222
+ // Iteration controller
223
+ const iterBtn = new Button(this, {
224
+ text: isMobile ? `${this.settings.iterations}` : `Iter: ${this.settings.iterations}`,
225
+ width: isMobile ? 45 : 100,
226
+ height: btnHeight,
227
+ fontSize,
228
+ onClick: () => this.cycleIterations(),
229
+ });
230
+
231
+ // Color mode selector
232
+ const colorBtn = new Button(this, {
233
+ text: isMobile ? "Color" : `Color: ${this.settings.colorScheme}`,
234
+ width: isMobile ? 50 : 150,
235
+ height: btnHeight,
236
+ fontSize,
237
+ onClick: () => this.cycleColorScheme(),
238
+ });
239
+
240
+ // Animate button
241
+ const animateBtn = new Button(this, {
242
+ text: isMobile ? "Anim" : "Animate",
243
+ width: isMobile ? 45 : 80,
244
+ height: btnHeight,
245
+ fontSize,
246
+ onClick: () => {
247
+ this.settings.animating = !this.settings.animating;
248
+ animateBtn.text = this.settings.animating ? "Stop" : (isMobile ? "Anim" : "Animate");
249
+ },
250
+ });
251
+
252
+ // Zoom in button
253
+ const zoomInBtn = new Button(this, {
254
+ text: "+",
255
+ width: isMobile ? 32 : 50,
256
+ height: btnHeight,
257
+ fontSize,
258
+ onClick: () => this.zoomIn(),
259
+ });
260
+
261
+ // Zoom out button
262
+ const zoomOutBtn = new Button(this, {
263
+ text: "-",
264
+ width: isMobile ? 32 : 50,
265
+ height: btnHeight,
266
+ fontSize,
267
+ onClick: () => this.zoomOut(),
268
+ });
269
+
270
+ // Add all buttons to layout
271
+ controlsLayout.add(typeBtn);
272
+ controlsLayout.add(iterBtn);
273
+ controlsLayout.add(colorBtn);
274
+ controlsLayout.add(animateBtn);
275
+ controlsLayout.add(zoomInBtn);
276
+ controlsLayout.add(zoomOutBtn);
277
+
278
+ // Store references to UI elements
279
+ this.controls = {
280
+ typeBtn,
281
+ iterBtn,
282
+ colorBtn,
283
+ animateBtn,
284
+ layout: controlsLayout,
285
+ isMobile,
286
+ };
287
+
288
+ this.controls.typeBtn.type = 0;
289
+
290
+ // Add layout to UI
291
+ this.ui.add(controlsLayout);
292
+
293
+ // Add title (hide on mobile)
294
+ if (!isMobile) {
295
+ const title = new Text(this, "GCanvas Fractal Explorer", {
296
+ font: "bold 24px Arial",
297
+ color: "#fff",
298
+ align: "center",
299
+ baseline: "middle",
300
+ debugColor: "white",
301
+ debug: true,
302
+ width: 10,
303
+ height: 10,
304
+ anchor: Position.BOTTOM_RIGHT,
305
+ });
306
+ this.ui.add(title);
307
+ }
308
+ }
309
+
310
+ resetView() {
311
+ // Set targets - smooth interpolation happens in update()
312
+ this.target.zoom = 1;
313
+ this.target.offsetX = 0;
314
+ this.target.offsetY = 0;
315
+ this.panVelocity.x = 0;
316
+ this.panVelocity.y = 0;
317
+ }
318
+
319
+ setupInteraction() {
320
+ // Throttle function to limit calls
321
+ const throttle = (func, limit) => {
322
+ let inThrottle;
323
+ return function () {
324
+ const args = arguments;
325
+ const context = this;
326
+ if (!inThrottle) {
327
+ func.apply(context, args);
328
+ inThrottle = true;
329
+ setTimeout(() => (inThrottle = false), limit);
330
+ }
331
+ };
332
+ };
333
+
334
+ // Mouse down for panning
335
+ this.canvas.addEventListener("mousedown", (e) => {
336
+ this.settings.isDragging = true;
337
+ this.settings.lastX = e.offsetX;
338
+ this.settings.lastY = e.offsetY;
339
+ this.panVelocity.x = 0;
340
+ this.panVelocity.y = 0;
341
+ });
342
+
343
+ // Mouse move for panning - visual transform only (no re-render)
344
+ this.canvas.addEventListener(
345
+ "mousemove",
346
+ throttle((e) => {
347
+ if (this.settings.isDragging) {
348
+ const dx = e.offsetX - this.settings.lastX;
349
+ const dy = e.offsetY - this.settings.lastY;
350
+
351
+ // Track velocity for momentum
352
+ this.panVelocity.x = dx * 0.8 + this.panVelocity.x * 0.2;
353
+ this.panVelocity.y = dy * 0.8 + this.panVelocity.y * 0.2;
354
+
355
+ // Update offset values
356
+ const scaleFactor = 0.005 / this.settings.zoom;
357
+ const deltaX = dx * scaleFactor;
358
+ const deltaY = dy * scaleFactor;
359
+ this.settings.offsetX -= deltaX;
360
+ this.settings.offsetY -= deltaY;
361
+ this.target.offsetX -= deltaX;
362
+ this.target.offsetY -= deltaY;
363
+
364
+ this.settings.lastX = e.offsetX;
365
+ this.settings.lastY = e.offsetY;
366
+
367
+ // Throttled preview render during drag
368
+ const now = performance.now();
369
+ if (now - this.lastRenderTime > this.minRenderInterval) {
370
+ this.lastRenderTime = now;
371
+ this.updateFractalSettings(true);
372
+ }
373
+ }
374
+ }, 16)
375
+ );
376
+
377
+ // Mouse up to end panning - full quality render
378
+ this.canvas.addEventListener("mouseup", () => {
379
+ this.settings.isDragging = false;
380
+ // If no significant momentum, re-render immediately at full quality
381
+ if (Math.abs(this.panVelocity.x) < 0.5 && Math.abs(this.panVelocity.y) < 0.5) {
382
+ this.panVelocity.x = 0;
383
+ this.panVelocity.y = 0;
384
+ this.updateFractalSettings(false);
385
+ }
386
+ });
387
+
388
+ // Mouse wheel for zooming - target-based smooth easing
389
+ this.canvas.addEventListener(
390
+ "wheel",
391
+ (e) => {
392
+ e.preventDefault();
393
+
394
+ // Calculate zoom factor
395
+ const zoomFactor = e.deltaY < 0 ? 1.25 : 0.8;
396
+
397
+ // Get mouse position in canvas
398
+ const mouseX = e.offsetX;
399
+ const mouseY = e.offsetY;
400
+
401
+ // Calculate normalized position (0-1)
402
+ const normalizedX = mouseX / this.width;
403
+ const normalizedY = mouseY / this.height;
404
+
405
+ // Map to complex plane coordinates using TARGET values for accumulation
406
+ const planeWidth = 3.5 / this.target.zoom;
407
+ const planeHeight = 3.0 / this.target.zoom;
408
+ const planeX = -2.5 / this.target.zoom + this.target.offsetX;
409
+ const planeY = -1.5 / this.target.zoom + this.target.offsetY;
410
+
411
+ // Find the point we're zooming in on
412
+ const pointX = planeX + normalizedX * planeWidth;
413
+ const pointY = planeY + normalizedY * planeHeight;
414
+
415
+ // Calculate new target zoom and offset
416
+ const newZoom = this.target.zoom * zoomFactor;
417
+ const newPlaneWidth = 3.5 / newZoom;
418
+ const newPlaneHeight = 3.0 / newZoom;
419
+ const newPlaneX = pointX - normalizedX * newPlaneWidth;
420
+ const newPlaneY = pointY - normalizedY * newPlaneHeight;
421
+
422
+ // Update target values - smooth interpolation happens in update()
423
+ this.target.zoom = newZoom;
424
+ this.target.offsetX = newPlaneX + 2.5 / newZoom;
425
+ this.target.offsetY = newPlaneY + 1.5 / newZoom;
426
+ },
427
+ { passive: false }
428
+ );
429
+
430
+ // Touch support for mobile
431
+ let lastTouchX = 0;
432
+ let lastTouchY = 0;
433
+ let lastPinchDist = 0;
434
+
435
+ this.canvas.addEventListener("touchstart", (e) => {
436
+ if (e.touches.length === 1) {
437
+ lastTouchX = e.touches[0].clientX;
438
+ lastTouchY = e.touches[0].clientY;
439
+ this.settings.isDragging = true;
440
+ this.panVelocity.x = 0;
441
+ this.panVelocity.y = 0;
442
+ } else if (e.touches.length === 2) {
443
+ const dx = e.touches[0].clientX - e.touches[1].clientX;
444
+ const dy = e.touches[0].clientY - e.touches[1].clientY;
445
+ lastPinchDist = Math.sqrt(dx * dx + dy * dy);
446
+ this.settings.isDragging = false;
447
+ }
448
+ e.preventDefault();
449
+ }, { passive: false });
450
+
451
+ this.canvas.addEventListener("touchmove", throttle((e) => {
452
+ if (e.touches.length === 1 && this.settings.isDragging) {
453
+ const dx = e.touches[0].clientX - lastTouchX;
454
+ const dy = e.touches[0].clientY - lastTouchY;
455
+
456
+ this.panVelocity.x = dx * 0.8 + this.panVelocity.x * 0.2;
457
+ this.panVelocity.y = dy * 0.8 + this.panVelocity.y * 0.2;
458
+
459
+ const scaleFactor = 0.005 / this.settings.zoom;
460
+ const deltaX = dx * scaleFactor;
461
+ const deltaY = dy * scaleFactor;
462
+ this.settings.offsetX -= deltaX;
463
+ this.settings.offsetY -= deltaY;
464
+ this.target.offsetX -= deltaX;
465
+ this.target.offsetY -= deltaY;
466
+
467
+ lastTouchX = e.touches[0].clientX;
468
+ lastTouchY = e.touches[0].clientY;
469
+
470
+ // Throttled preview render during drag
471
+ const now = performance.now();
472
+ if (now - this.lastRenderTime > this.minRenderInterval) {
473
+ this.lastRenderTime = now;
474
+ this.updateFractalSettings(true);
475
+ }
476
+ } else if (e.touches.length === 2) {
477
+ const dx = e.touches[0].clientX - e.touches[1].clientX;
478
+ const dy = e.touches[0].clientY - e.touches[1].clientY;
479
+ const pinchDist = Math.sqrt(dx * dx + dy * dy);
480
+
481
+ if (lastPinchDist > 0) {
482
+ const zoomFactor = pinchDist / lastPinchDist;
483
+ // Direct zoom for responsive feel during pinch
484
+ this.settings.zoom *= zoomFactor;
485
+ this.target.zoom *= zoomFactor;
486
+ // Throttled preview render during pinch
487
+ const now = performance.now();
488
+ if (now - this.lastRenderTime > this.minRenderInterval) {
489
+ this.lastRenderTime = now;
490
+ this.updateFractalSettings(true);
491
+ }
492
+ }
493
+
494
+ lastPinchDist = pinchDist;
495
+ }
496
+ e.preventDefault();
497
+ }, 16), { passive: false });
498
+
499
+ this.canvas.addEventListener("touchend", () => {
500
+ this.settings.isDragging = false;
501
+ lastPinchDist = 0;
502
+ // If no significant momentum, re-render immediately at full quality
503
+ if (Math.abs(this.panVelocity.x) < 0.5 && Math.abs(this.panVelocity.y) < 0.5) {
504
+ this.panVelocity.x = 0;
505
+ this.panVelocity.y = 0;
506
+ this.updateFractalSettings(false);
507
+ }
508
+ });
509
+ }
510
+
511
+ cycleFractalType() {
512
+ const types = Object.values(Fractals.types);
513
+ this.settings.typeIndex = (this.settings.typeIndex + 1) % types.length;
514
+ this.settings.type = types[this.settings.typeIndex];
515
+ this.controls.typeBtn.text = this.controls.isMobile
516
+ ? this.settings.type.toUpperCase()
517
+ : `Type: ${this.settings.type.toUpperCase()}`;
518
+ // Tweaking default params for selected type
519
+ if (this.settings.type === Fractals.types.KOCH) {
520
+ this.settings.iterations = 3;
521
+ this.settings.colorScheme = Fractals.colors.TOPOGRAPHIC;
522
+ } else if (this.settings.type === Fractals.types.BARNSEY_FERN) {
523
+ this.settings.colorScheme = Fractals.colors.ELECTRIC;
524
+ } else if (this.settings.type === Fractals.types.LYAPUNOV) {
525
+ this.settings.colorScheme = Fractals.colors.RAINBOW;
526
+ this.settings.iterations = 8;
527
+ } else if (
528
+ this.settings.type === Fractals.types.MANDELBROT ||
529
+ this.settings.type == Fractals.types.JULIA ||
530
+ this.settings.type == Fractals.types.TRICORN ||
531
+ this.settings.type == Fractals.types.PHOENIX
532
+ ) {
533
+ this.settings.iterations = 32;
534
+ this.settings.colorScheme = Fractals.colors.FUTURISTIC;
535
+ } else if (this.settings.type === Fractals.types.NEWTON) {
536
+ this.settings.iterations = 32;
537
+ this.settings.colorScheme = Fractals.colors.ELECTRIC;
538
+ } else if (
539
+ this.settings.type === Fractals.types.SIERPINSKI ||
540
+ this.settings.type === Fractals.types.SCARPET
541
+ ) {
542
+ this.settings.colorScheme = Fractals.colors.ELECTRIC;
543
+ this.settings.iterations = 4;
544
+ }
545
+ this.baseIterations = this.settings.iterations;
546
+ this.controls.iterBtn.text = this.controls.isMobile
547
+ ? `${this.settings.iterations}`
548
+ : `Iter: ${this.settings.iterations}`;
549
+ this.controls.colorBtn.text = this.controls.isMobile
550
+ ? "Color"
551
+ : `Color: ${this.settings.colorScheme}`;
552
+ this.resetView();
553
+ }
554
+
555
+ cycleIterations() {
556
+ let iterationPresets;
557
+ if (
558
+ this.settings.type === Fractals.types.SIERPINSKI ||
559
+ this.settings.type === Fractals.types.SCARPET
560
+ ) {
561
+ iterationPresets = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25];
562
+ } else if (
563
+ this.settings.type === Fractals.types.MANDELBROT ||
564
+ this.settings.type === Fractals.types.TRICORN ||
565
+ this.settings.type === Fractals.types.PHOENIX ||
566
+ this.settings.type === Fractals.types.JULIA
567
+ ) {
568
+ iterationPresets = [3, 4, 5, 8, 32, 50, 100, 200, 500, 1000];
569
+ } else if (this.settings.type === Fractals.types.KOCH) {
570
+ iterationPresets = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
571
+ } else {
572
+ iterationPresets = [3, 4, 5, 8, 32, 50, 100];
573
+ }
574
+
575
+ const currentIndex = iterationPresets.indexOf(this.settings.iterations);
576
+ const nextIndex = (currentIndex + 1) % iterationPresets.length;
577
+
578
+ this.settings.iterations = iterationPresets[nextIndex];
579
+ this.baseIterations = this.settings.iterations;
580
+ this.controls.iterBtn.text = this.controls.isMobile
581
+ ? `${this.settings.iterations}`
582
+ : `Iter: ${this.settings.iterations}`;
583
+
584
+ // Update with new iterations
585
+ this.updateFractalSettings();
586
+ }
587
+
588
+ cycleColorScheme() {
589
+ let schemes = Object.values(Fractals.colors);
590
+ if (this.settings.type === Fractals.types.LYAPUNOV) {
591
+ schemes = [
592
+ Fractals.colors.RAINBOW,
593
+ Fractals.colors.ELECTRIC,
594
+ Fractals.colors.HISTORIC,
595
+ Fractals.colors.GRAYSCALE,
596
+ Fractals.colors.BINARY,
597
+ ];
598
+ } else if (
599
+ this.settings.type === Fractals.types.SIERPINSKI ||
600
+ this.settings.type === Fractals.types.SCARPET
601
+ ) {
602
+ schemes = [
603
+ Fractals.colors.ELECTRIC,
604
+ Fractals.colors.TOPOGRAPHIC,
605
+ Fractals.colors.OCEAN,
606
+ Fractals.colors.GRAYSCALE,
607
+ Fractals.colors.BINARY,
608
+ ];
609
+ } else if (this.settings.type === Fractals.types.BARNSEY_FERN) {
610
+ schemes = [
611
+ Fractals.colors.ELECTRIC,
612
+ Fractals.colors.RAINBOW,
613
+ Fractals.colors.GRAYSCALE,
614
+ Fractals.colors.BINARY,
615
+ ];
616
+ } else if (this.settings.type === Fractals.types.NEWTON) {
617
+ schemes = [
618
+ Fractals.colors.ELECTRIC,
619
+ Fractals.colors.OCEAN,
620
+ Fractals.colors.BINARY,
621
+ Fractals.colors.FUTURISTIC,
622
+ Fractals.colors.HISTORIC,
623
+ Fractals.colors.RAINBOW,
624
+ Fractals.colors.TOPOGRAPHIC,
625
+ ];
626
+ }
627
+ const currentIndex = schemes.indexOf(this.settings.colorScheme);
628
+ const nextIndex = (currentIndex + 1) % schemes.length;
629
+ this.settings.colorScheme = schemes[nextIndex];
630
+ this.controls.colorBtn.text = this.controls.isMobile
631
+ ? "Color"
632
+ : `Color: ${this.settings.colorScheme}`;
633
+
634
+ // Update with new color scheme
635
+ this.updateFractalSettings();
636
+ }
637
+
638
+ zoomIn() {
639
+ this.target.zoom = this.target.zoom * 1.5;
640
+ }
641
+
642
+ zoomOut() {
643
+ this.target.zoom = this.target.zoom / 1.5;
644
+ }
645
+
646
+ render() {
647
+ super.render();
648
+ //if(this.fractal.active) this.fractal.active = false;
649
+ }
650
+ }
651
+ //
652
+ class FractalRenderer extends ImageGo {
653
+ /**
654
+ * Create a new fractal renderer with double buffering
655
+ *
656
+ * @param {Game} game - Game instance
657
+ * @param {number} width - Canvas width
658
+ * @param {number} height - Canvas height
659
+ * @param {Object} options - Additional options
660
+ */
661
+ constructor(game, width, height, options = {}) {
662
+ // Create a blank ImageData for initial display
663
+ const initialData = Painter.img.createImageData(width, height);
664
+
665
+ // Initialize with the blank data
666
+ super(game, initialData, options);
667
+
668
+ this.width = width;
669
+ this.height = height;
670
+
671
+ // Store the current fractal data
672
+ this.fractalData = null;
673
+
674
+ // Flag to indicate rendering is in progress
675
+ this.isRendering = false;
676
+
677
+ // Default settings
678
+ this.settings = {
679
+ type: Fractals.types.MANDELBROT,
680
+ iterations: 50,
681
+ colorScheme: "rainbow",
682
+ hueShift: 0,
683
+ zoom: 1,
684
+ offsetX: 0,
685
+ offsetY: 0,
686
+ };
687
+
688
+ // Settings that were used for the current display
689
+ this.currentSettings = { ...this.settings };
690
+
691
+ // Create a second ImageData as a working buffer
692
+ this.workingBuffer = Painter.img.createImageData(width, height);
693
+
694
+ // Request initial render
695
+ this.pendingRender = true;
696
+ // Initialize task manager
697
+ this.taskManager = new TaskManager("./js/fractalworker.js");
698
+ }
699
+
700
+ /**
701
+ * Update renderer size
702
+ *
703
+ * @param {number} width - New width
704
+ * @param {number} height - New height
705
+ */
706
+ resize(width, height) {
707
+ this.width = width;
708
+ this.height = height;
709
+
710
+ // Create new working buffer with new dimensions
711
+ this.workingBuffer = Painter.img.createImageData(width, height);
712
+
713
+ // Reset main buffer
714
+ this.reset();
715
+
716
+ // Request a re-render with new dimensions
717
+ this.pendingRender = true;
718
+ }
719
+
720
+ /**
721
+ * Update fractal settings and queue a re-render
722
+ *
723
+ * @param {Object} settings - New settings
724
+ */
725
+ updateSettings(settings) {
726
+ const oldSettings = { ...this.settings };
727
+ this.settings = { ...this.settings, ...settings };
728
+
729
+ // Only request a render if settings actually changed
730
+ if (JSON.stringify(oldSettings) !== JSON.stringify(this.settings)) {
731
+ this.pendingRender = true;
732
+ }
733
+ }
734
+
735
+ /**
736
+ * Check if settings have changed from what's currently displayed
737
+ */
738
+ settingsChanged() {
739
+ return (
740
+ JSON.stringify(this.settings) !== JSON.stringify(this.currentSettings)
741
+ );
742
+ }
743
+
744
+ async renderFractal() {
745
+ this.isRendering = true;
746
+ const renderSettings = { ...this.settings };
747
+ try {
748
+ // Step 1: Generate raw fractal data
749
+ const fractalData = this.generateFractalData(renderSettings);
750
+ //console.log("fractalData", fractalData);
751
+ const result = await this.taskManager.runTask("generateFractal", {
752
+ width: this.width,
753
+ height: this.height,
754
+ type: renderSettings.type,
755
+ iterations: renderSettings.iterations,
756
+ zoom: renderSettings.zoom,
757
+ offsetX: renderSettings.offsetX,
758
+ offsetY: renderSettings.offsetY,
759
+ fractalFunction: fractalData.fractalFunction,
760
+ args: fractalData.args,
761
+ colorFunction: Fractals.applyColorScheme.toString(),
762
+ colorArgs: [
763
+ renderSettings.colorScheme,
764
+ renderSettings.iterations,
765
+ renderSettings.hueShift,
766
+ ],
767
+ });
768
+ //console.log("worker result", result);
769
+ // Apply the result to our image
770
+ this.shape.bitmap = result.image;
771
+ // Update current settings
772
+ this.currentSettings = { ...renderSettings };
773
+ } catch (error) {
774
+ console.error("Error rendering fractal:", error);
775
+ } finally {
776
+ this.isRendering = false;
777
+ }
778
+ }
779
+
780
+ update(dt) {
781
+ if (this.pendingRender && !this.isRendering && this.active) {
782
+ this.pendingRender = false;
783
+ this.renderFractal();
784
+ }
785
+ super.update(dt);
786
+ }
787
+
788
+ generateFractalData(settings) {
789
+ const { type, iterations, zoom, offsetX, offsetY } = settings;
790
+
791
+ switch (type) {
792
+ case "pythagorasTree":
793
+ return {
794
+ fractalFunction: Fractals.pythagorasTree.toString(),
795
+ args: [
796
+ iterations,
797
+ -1.5 / zoom + offsetX,
798
+ 1 / zoom + offsetX,
799
+ -1.5 / zoom + offsetY,
800
+ 1.5 / zoom + offsetY,
801
+ ],
802
+ };
803
+ case "mandelbrot":
804
+ return {
805
+ fractalFunction: Fractals.mandelbrot.toString(),
806
+ args: [
807
+ iterations,
808
+ -2.5 / zoom + offsetX,
809
+ 1 / zoom + offsetX,
810
+ -1.5 / zoom + offsetY,
811
+ 1.5 / zoom + offsetY,
812
+ ],
813
+ };
814
+
815
+ case "julia":
816
+ return {
817
+ fractalFunction: Fractals.julia.toString(),
818
+ args: [
819
+ iterations,
820
+ -0.7, // cReal
821
+ 0.27, // cImag
822
+ zoom,
823
+ offsetX,
824
+ offsetY,
825
+ ],
826
+ };
827
+
828
+ case "newton":
829
+ return {
830
+ fractalFunction: Fractals.newton.toString(),
831
+ args: [
832
+ iterations,
833
+ 0.000001,
834
+ (-1 / zoom) * 2 + offsetX,
835
+ (1 / zoom) * 2 + offsetX,
836
+ (-1 / zoom) * 2 + offsetY,
837
+ (1 / zoom) * 2 + offsetY,
838
+ ],
839
+ };
840
+
841
+ case "sierpinski":
842
+ return {
843
+ fractalFunction: Fractals.sierpinski.toString(),
844
+ args: [
845
+ Math.min(50, iterations),
846
+ (-16 / zoom) * 2 + offsetX * 10,
847
+ (16 / zoom) * 2 + offsetX * 10,
848
+ (-16 / zoom) * 2 + offsetY * 10,
849
+ (16 / zoom) * 2 + offsetY * 10,
850
+ ],
851
+ };
852
+
853
+ case "sierpinskiCarpet":
854
+ const f = 3 / zoom;
855
+ return {
856
+ fractalFunction: Fractals.sierpinskiCarpet.toString(),
857
+ args: [
858
+ Math.min(50, iterations),
859
+ -f * 2 + offsetX + 2,
860
+ f * 2 + offsetX + 2,
861
+ -f * 2 + offsetY + 2,
862
+ f * 2 + offsetY + 2,
863
+ ],
864
+ };
865
+
866
+ case "barnsleyFern":
867
+ return {
868
+ fractalFunction: Fractals.barnsleyFern.toString(),
869
+ args: [iterations * 1000],
870
+ };
871
+
872
+ case "lyapunov":
873
+ return {
874
+ fractalFunction: Fractals.lyapunov.toString(),
875
+ args: [
876
+ iterations,
877
+ "ABAB",
878
+ (-1 / zoom) * 2 + offsetX,
879
+ (1 / zoom) * 2 + offsetX,
880
+ (-1 / zoom) * 2 + offsetY,
881
+ (1 / zoom) * 2 + offsetY,
882
+ 1,
883
+ iterations,
884
+ ],
885
+ };
886
+
887
+ case "tricorn":
888
+ return {
889
+ fractalFunction: Fractals.tricorn.toString(),
890
+ args: [
891
+ iterations,
892
+ -2.5 / zoom + offsetX,
893
+ 1.5 / zoom + offsetX,
894
+ -1.5 / zoom + offsetY,
895
+ 1.5 / zoom + offsetY,
896
+ ],
897
+ };
898
+
899
+ case "phoenix":
900
+ return {
901
+ fractalFunction: Fractals.phoenix.toString(),
902
+ args: [
903
+ iterations,
904
+ 0.8,
905
+ 0.3,
906
+ -2 / zoom + offsetX - 1,
907
+ 2 / zoom + offsetX,
908
+ -2 / zoom + offsetY,
909
+ 2 / zoom + offsetY,
910
+ ],
911
+ };
912
+
913
+ case "koch":
914
+ return {
915
+ fractalFunction: Fractals.koch.toString(),
916
+ args: [
917
+ iterations,
918
+ (-1 / zoom) * 2 + offsetX,
919
+ (1 / zoom) * 2 + offsetX,
920
+ (-1 / zoom) * 2 + offsetY + 1,
921
+ (1 / zoom) * 2 + offsetY + 1,
922
+ ],
923
+ };
924
+
925
+ default:
926
+ return {
927
+ fractalFunction: Fractals.mandelbrot.toString(),
928
+ args: [iterations],
929
+ };
930
+ }
931
+ }
932
+ }