@guinetik/gcanvas 1.0.4 → 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 (261) hide show
  1. package/dist/CNAME +1 -0
  2. package/dist/aizawa.html +27 -0
  3. package/dist/animations.html +31 -0
  4. package/dist/basic.html +38 -0
  5. package/dist/baskara.html +31 -0
  6. package/dist/bezier.html +35 -0
  7. package/dist/beziersignature.html +29 -0
  8. package/dist/blackhole.html +28 -0
  9. package/dist/blob.html +35 -0
  10. package/dist/clifford.html +25 -0
  11. package/dist/cmb.html +24 -0
  12. package/dist/coordinates.html +698 -0
  13. package/dist/cube3d.html +23 -0
  14. package/dist/dadras.html +26 -0
  15. package/dist/dejong.html +25 -0
  16. package/dist/demos.css +303 -0
  17. package/dist/dino.html +42 -0
  18. package/dist/easing.html +28 -0
  19. package/dist/events.html +195 -0
  20. package/dist/fluent.html +647 -0
  21. package/dist/fluid-simple.html +22 -0
  22. package/dist/fluid.html +37 -0
  23. package/dist/fractals.html +36 -0
  24. package/dist/gameobjects.html +626 -0
  25. package/dist/gcanvas.es.js +14368 -9093
  26. package/dist/gcanvas.es.min.js +1 -1
  27. package/dist/gcanvas.umd.js +1 -1
  28. package/dist/gcanvas.umd.min.js +1 -1
  29. package/dist/genart.html +26 -0
  30. package/dist/gendream.html +26 -0
  31. package/dist/group.html +36 -0
  32. package/dist/halvorsen.html +27 -0
  33. package/dist/home.html +587 -0
  34. package/dist/hyperbolic001.html +23 -0
  35. package/dist/hyperbolic002.html +23 -0
  36. package/dist/hyperbolic003.html +23 -0
  37. package/dist/hyperbolic004.html +23 -0
  38. package/dist/hyperbolic005.html +22 -0
  39. package/dist/index.html +446 -0
  40. package/dist/isometric.html +34 -0
  41. package/dist/js/aizawa.js +425 -0
  42. package/dist/js/animations.js +452 -0
  43. package/dist/js/basic.js +204 -0
  44. package/dist/js/baskara.js +751 -0
  45. package/dist/js/bezier.js +692 -0
  46. package/dist/js/beziersignature.js +241 -0
  47. package/dist/js/blackhole/accretiondisk.obj.js +379 -0
  48. package/dist/js/blackhole/blackhole.obj.js +318 -0
  49. package/dist/js/blackhole/index.js +409 -0
  50. package/dist/js/blackhole/particle.js +56 -0
  51. package/dist/js/blackhole/starfield.obj.js +218 -0
  52. package/dist/js/blob.js +2276 -0
  53. package/dist/js/clifford.js +236 -0
  54. package/dist/js/cmb.js +594 -0
  55. package/dist/js/coordinates.js +840 -0
  56. package/dist/js/cube3d.js +789 -0
  57. package/dist/js/dadras.js +405 -0
  58. package/dist/js/dejong.js +257 -0
  59. package/dist/js/dino.js +1420 -0
  60. package/dist/js/easing.js +477 -0
  61. package/dist/js/fluent.js +183 -0
  62. package/dist/js/fluid-simple.js +253 -0
  63. package/dist/js/fluid.js +527 -0
  64. package/dist/js/fractals.js +932 -0
  65. package/dist/js/fractalworker.js +93 -0
  66. package/dist/js/gameobjects.js +176 -0
  67. package/dist/js/genart.js +268 -0
  68. package/dist/js/gendream.js +209 -0
  69. package/dist/js/group.js +140 -0
  70. package/dist/js/halvorsen.js +405 -0
  71. package/dist/js/hyperbolic001.js +310 -0
  72. package/dist/js/hyperbolic002.js +388 -0
  73. package/dist/js/hyperbolic003.js +319 -0
  74. package/dist/js/hyperbolic004.js +345 -0
  75. package/dist/js/hyperbolic005.js +340 -0
  76. package/dist/js/info-toggle.js +25 -0
  77. package/dist/js/isometric.js +851 -0
  78. package/dist/js/kerr.js +1547 -0
  79. package/dist/js/lavalamp.js +590 -0
  80. package/dist/js/layout.js +354 -0
  81. package/dist/js/lorenz.js +425 -0
  82. package/dist/js/mondrian.js +285 -0
  83. package/dist/js/opacity.js +275 -0
  84. package/dist/js/painter.js +484 -0
  85. package/dist/js/particles-showcase.js +514 -0
  86. package/dist/js/particles.js +299 -0
  87. package/dist/js/patterns.js +397 -0
  88. package/dist/js/penrose/artifact.js +69 -0
  89. package/dist/js/penrose/blackhole.js +121 -0
  90. package/dist/js/penrose/constants.js +73 -0
  91. package/dist/js/penrose/game.js +943 -0
  92. package/dist/js/penrose/lore.js +278 -0
  93. package/dist/js/penrose/penrosescene.js +892 -0
  94. package/dist/js/penrose/ship.js +216 -0
  95. package/dist/js/penrose/sounds.js +211 -0
  96. package/dist/js/penrose/voidparticle.js +55 -0
  97. package/dist/js/penrose/voidscene.js +258 -0
  98. package/dist/js/penrose/voidship.js +144 -0
  99. package/dist/js/penrose/wormhole.js +46 -0
  100. package/dist/js/pipeline.js +555 -0
  101. package/dist/js/plane3d.js +256 -0
  102. package/dist/js/platformer.js +1579 -0
  103. package/dist/js/rossler.js +480 -0
  104. package/dist/js/scene.js +304 -0
  105. package/dist/js/scenes.js +320 -0
  106. package/dist/js/schrodinger.js +706 -0
  107. package/dist/js/schwarzschild.js +1015 -0
  108. package/dist/js/shapes.js +628 -0
  109. package/dist/js/space/alien.js +171 -0
  110. package/dist/js/space/boom.js +98 -0
  111. package/dist/js/space/boss.js +353 -0
  112. package/dist/js/space/buff.js +73 -0
  113. package/dist/js/space/bullet.js +102 -0
  114. package/dist/js/space/constants.js +85 -0
  115. package/dist/js/space/game.js +1884 -0
  116. package/dist/js/space/hud.js +112 -0
  117. package/dist/js/space/laserbeam.js +179 -0
  118. package/dist/js/space/lightning.js +277 -0
  119. package/dist/js/space/minion.js +192 -0
  120. package/dist/js/space/missile.js +212 -0
  121. package/dist/js/space/player.js +430 -0
  122. package/dist/js/space/powerup.js +90 -0
  123. package/dist/js/space/starfield.js +58 -0
  124. package/dist/js/space/starpower.js +90 -0
  125. package/dist/js/spacetime.js +559 -0
  126. package/dist/js/sphere3d.js +229 -0
  127. package/dist/js/sprite.js +473 -0
  128. package/dist/js/starfaux/config.js +118 -0
  129. package/dist/js/starfaux/enemy.js +353 -0
  130. package/dist/js/starfaux/hud.js +78 -0
  131. package/dist/js/starfaux/index.js +482 -0
  132. package/dist/js/starfaux/laser.js +182 -0
  133. package/dist/js/starfaux/player.js +468 -0
  134. package/dist/js/starfaux/terrain.js +560 -0
  135. package/dist/js/study001.js +275 -0
  136. package/dist/js/study002.js +366 -0
  137. package/dist/js/study003.js +331 -0
  138. package/dist/js/study004.js +389 -0
  139. package/dist/js/study005.js +209 -0
  140. package/dist/js/study006.js +194 -0
  141. package/dist/js/study007.js +192 -0
  142. package/dist/js/study008.js +413 -0
  143. package/dist/js/svgtween.js +204 -0
  144. package/dist/js/tde/accretiondisk.js +471 -0
  145. package/dist/js/tde/blackhole.js +219 -0
  146. package/dist/js/tde/blackholescene.js +209 -0
  147. package/dist/js/tde/config.js +59 -0
  148. package/dist/js/tde/index.js +820 -0
  149. package/dist/js/tde/jets.js +290 -0
  150. package/dist/js/tde/lensedstarfield.js +154 -0
  151. package/dist/js/tde/tdestar.js +297 -0
  152. package/dist/js/tde/tidalstream.js +372 -0
  153. package/dist/js/tde_old/blackhole.obj.js +354 -0
  154. package/dist/js/tde_old/debris.obj.js +791 -0
  155. package/dist/js/tde_old/flare.obj.js +239 -0
  156. package/dist/js/tde_old/index.js +448 -0
  157. package/dist/js/tde_old/star.obj.js +812 -0
  158. package/dist/js/tetris/config.js +157 -0
  159. package/dist/js/tetris/grid.js +286 -0
  160. package/dist/js/tetris/index.js +1195 -0
  161. package/dist/js/tetris/renderer.js +634 -0
  162. package/dist/js/tetris/tetrominos.js +280 -0
  163. package/dist/js/thomas.js +394 -0
  164. package/dist/js/tiles.js +312 -0
  165. package/dist/js/tweendemo.js +79 -0
  166. package/dist/js/visibility.js +102 -0
  167. package/dist/kerr.html +28 -0
  168. package/dist/lavalamp.html +27 -0
  169. package/dist/layouts.html +37 -0
  170. package/dist/logo.svg +4 -0
  171. package/dist/loop.html +84 -0
  172. package/dist/lorenz.html +27 -0
  173. package/dist/mondrian.html +32 -0
  174. package/dist/og_image.png +0 -0
  175. package/dist/opacity.html +36 -0
  176. package/dist/painter.html +39 -0
  177. package/dist/particles-showcase.html +28 -0
  178. package/dist/particles.html +24 -0
  179. package/dist/patterns.html +33 -0
  180. package/dist/penrose-game.html +31 -0
  181. package/dist/pipeline.html +737 -0
  182. package/dist/plane3d.html +24 -0
  183. package/dist/platformer.html +43 -0
  184. package/dist/rossler.html +27 -0
  185. package/dist/scene-interactivity-test.html +220 -0
  186. package/dist/scene.html +33 -0
  187. package/dist/scenes.html +96 -0
  188. package/dist/schrodinger.html +27 -0
  189. package/dist/schwarzschild.html +27 -0
  190. package/dist/shapes.html +16 -0
  191. package/dist/space.html +85 -0
  192. package/dist/spacetime.html +27 -0
  193. package/dist/sphere3d.html +24 -0
  194. package/dist/sprite.html +18 -0
  195. package/dist/starfaux.html +22 -0
  196. package/dist/study001.html +23 -0
  197. package/dist/study002.html +23 -0
  198. package/dist/study003.html +23 -0
  199. package/dist/study004.html +23 -0
  200. package/dist/study005.html +22 -0
  201. package/dist/study006.html +24 -0
  202. package/dist/study007.html +24 -0
  203. package/dist/study008.html +22 -0
  204. package/dist/svgtween.html +29 -0
  205. package/dist/tde.html +28 -0
  206. package/dist/tetris3d.html +25 -0
  207. package/dist/thomas.html +27 -0
  208. package/dist/tiles.html +28 -0
  209. package/dist/transforms.html +400 -0
  210. package/dist/tween.html +45 -0
  211. package/dist/visibility.html +33 -0
  212. package/package.json +1 -1
  213. package/readme.md +30 -22
  214. package/src/game/objects/go.js +7 -0
  215. package/src/game/objects/index.js +2 -0
  216. package/src/game/objects/isometric-scene.js +53 -3
  217. package/src/game/objects/layoutscene.js +57 -0
  218. package/src/game/objects/mask.js +241 -0
  219. package/src/game/objects/scene.js +19 -0
  220. package/src/game/objects/wrapper.js +14 -2
  221. package/src/game/pipeline.js +17 -0
  222. package/src/game/ui/button.js +101 -16
  223. package/src/game/ui/theme.js +0 -6
  224. package/src/game/ui/togglebutton.js +25 -14
  225. package/src/game/ui/tooltip.js +12 -4
  226. package/src/index.js +3 -0
  227. package/src/io/gesture.js +409 -0
  228. package/src/io/index.js +4 -1
  229. package/src/io/keys.js +9 -1
  230. package/src/io/screen.js +476 -0
  231. package/src/math/attractors.js +664 -0
  232. package/src/math/heat.js +106 -0
  233. package/src/math/index.js +1 -0
  234. package/src/mixins/draggable.js +15 -19
  235. package/src/painter/painter.shapes.js +11 -5
  236. package/src/particle/particle-system.js +165 -1
  237. package/src/physics/index.js +26 -0
  238. package/src/physics/physics-updaters.js +333 -0
  239. package/src/physics/physics.js +375 -0
  240. package/src/shapes/image.js +5 -5
  241. package/src/shapes/index.js +2 -0
  242. package/src/shapes/parallelogram.js +147 -0
  243. package/src/shapes/righttriangle.js +115 -0
  244. package/src/shapes/svg.js +281 -100
  245. package/src/shapes/text.js +22 -6
  246. package/src/shapes/transformable.js +5 -0
  247. package/src/sound/effects.js +807 -0
  248. package/src/sound/index.js +13 -0
  249. package/src/webgl/index.js +7 -0
  250. package/src/webgl/shaders/clifford-point-shaders.js +131 -0
  251. package/src/webgl/shaders/dejong-point-shaders.js +131 -0
  252. package/src/webgl/shaders/point-sprite-shaders.js +152 -0
  253. package/src/webgl/webgl-clifford-renderer.js +477 -0
  254. package/src/webgl/webgl-dejong-renderer.js +472 -0
  255. package/src/webgl/webgl-line-renderer.js +391 -0
  256. package/src/webgl/webgl-particle-renderer.js +410 -0
  257. package/types/index.d.ts +30 -2
  258. package/types/io.d.ts +217 -0
  259. package/types/physics.d.ts +299 -0
  260. package/types/shapes.d.ts +8 -0
  261. package/types/webgl.d.ts +188 -109
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Worker for the fractal demo.
3
+ */
4
+
5
+ /**
6
+ * Handle messages from the main thread.
7
+ * @param {MessageEvent} e - The message event.
8
+ */
9
+ self.onmessage = function (e) {
10
+ if (e.data.taskName === "generateFractal") {
11
+ //console.time("generateFractal");
12
+ const { width, height } = e.data.params;
13
+ //console.log("fractalworker", e.data.params);
14
+ // Generate the raw fractal data
15
+ const rawData = generateFractalData(width, height, e.data.params.fractalFunction, e.data.params.args);
16
+ //console.log("rawData", e.data.params.args);
17
+ // Create and fill the image data
18
+ const imageData = new ImageData(width, height);
19
+ // Apply color scheme
20
+ const coloredData = applyColorScheme(rawData, imageData, e.data.params.colorFunction, e.data.params.colorArgs);
21
+ //console.log("coloredData", coloredData);
22
+ // Send back the completed image data using the format TaskManager expects
23
+ self.postMessage(
24
+ {
25
+ taskId: e.data.taskId, // Include the taskId
26
+ status: 'complete', // Use 'complete' status
27
+ result: { // Put the data inside 'result'
28
+ image: coloredData,
29
+ settings: e.data.params
30
+ }
31
+ },
32
+ [coloredData.data.buffer]
33
+ );
34
+ //console.timeEnd("generateFractal");
35
+ }
36
+ };
37
+
38
+ /**
39
+ * Generate the raw fractal data.
40
+ * @param {number} width - The width of the fractal.
41
+ * @param {number} height - The height of the fractal.
42
+ * @param {string} fractalFunction - The fractal function.
43
+ * @param {Array} fractalArgs - The arguments for the fractal function.
44
+ * @returns {Array} The raw fractal data.
45
+ */
46
+ function generateFractalData(width, height, fractalFunction, fractalArgs) {
47
+ try {
48
+ // For class methods, we need to convert to a standalone function
49
+ // This extracts just the function part without the name
50
+ const functionStr = fractalFunction.replace(/^[^(]+/, "function");
51
+ // Create the function from the string
52
+ const f = new Function("return (" + functionStr + ")")();
53
+ // Execute the function with our parameters
54
+ return f(width, height, ...fractalArgs);
55
+ } catch (error) {
56
+ console.error("Error evaluating fractal function:", error);
57
+ console.log("Function string:", fractalFunction);
58
+ throw error;
59
+ }
60
+ }
61
+
62
+ function applyColorScheme(rawData, imageData, colorFunction, colorArgs) {
63
+ try {
64
+ const functionStr = colorFunction.replace(/^[^(]+/, "function");
65
+
66
+ // Create the function from the string
67
+ const f = new Function("return (" + functionStr + ")")();
68
+ // Execute the function with our parameters
69
+ return f(rawData, imageData, ...colorArgs, hslToRgb);
70
+ } catch (error) {
71
+ console.error("Error evaluating color function:", error);
72
+ console.log("Function string:", colorFunction);
73
+ throw error;
74
+ }
75
+ }
76
+
77
+ // Copied from Painter.colors for convenience.
78
+ function hslToRgb(h, s, l) {
79
+ const c = (1 - Math.abs(2 * l - 1)) * s;
80
+ const hPrime = h / 60;
81
+ const x = c * (1 - Math.abs((hPrime % 2) - 1));
82
+ let [r, g, b] = [0, 0, 0];
83
+
84
+ if (hPrime >= 0 && hPrime < 1) [r, g, b] = [c, x, 0];
85
+ else if (hPrime >= 1 && hPrime < 2) [r, g, b] = [x, c, 0];
86
+ else if (hPrime >= 2 && hPrime < 3) [r, g, b] = [0, c, x];
87
+ else if (hPrime >= 3 && hPrime < 4) [r, g, b] = [0, x, c];
88
+ else if (hPrime >= 4 && hPrime < 5) [r, g, b] = [x, 0, c];
89
+ else if (hPrime >= 5 && hPrime < 6) [r, g, b] = [c, 0, x];
90
+
91
+ const m = l - c / 2;
92
+ return [(r + m) * 255, (g + m) * 255, (b + m) * 255];
93
+ }
@@ -0,0 +1,176 @@
1
+ /**
2
+ * Gameobjects.html - Live demos for Shapes vs GameObjects documentation
3
+ */
4
+ import {
5
+ Circle,
6
+ Game,
7
+ Group,
8
+ Painter,
9
+ Rectangle,
10
+ Scene,
11
+ ShapeGOFactory,
12
+ Sprite,
13
+ } from "/gcanvas.es.min.js";
14
+
15
+ // ==================== Demo 1: Shapes Only ====================
16
+ // Even "shapes only" needs Painter initialized, so we use a minimal Game
17
+ class ShapesDemo extends Game {
18
+ constructor(canvas) {
19
+ super(canvas);
20
+ this.backgroundColor = "#050505";
21
+ }
22
+
23
+ init() {
24
+ super.init();
25
+ // Shapes are created and rendered in render() below
26
+ }
27
+
28
+ render() {
29
+ super.render(); // Clears canvas
30
+
31
+ const centerY = this.canvas.height / 2;
32
+
33
+ // Create shapes - spread across canvas
34
+ const circle = new Circle(35, { x: this.canvas.width * 0.2, y: centerY, color: "#0f0" });
35
+ const rect = new Rectangle({ x: this.canvas.width * 0.5, y: centerY, width: 80, height: 50, color: "#0ff" });
36
+
37
+ // Group with rotation
38
+ const group = new Group({ x: this.canvas.width * 0.8, y: centerY, rotation: Math.PI / 4 });
39
+ group.add(new Circle(20, { color: "#f0f" }));
40
+ group.add(new Rectangle({ y: 30, width: 40, height: 20, color: "#ff0" }));
41
+
42
+ // Render directly - calling render() on shapes, not using pipeline
43
+ circle.render();
44
+ rect.render();
45
+ group.render();
46
+ }
47
+ }
48
+
49
+ function initShapesDemo() {
50
+ const canvas = document.getElementById("shapes-canvas");
51
+ if (!canvas) return;
52
+
53
+ const rect = canvas.getBoundingClientRect();
54
+ canvas.width = rect.width;
55
+ canvas.height = rect.height;
56
+
57
+ const demo = new ShapesDemo(canvas);
58
+ demo.start();
59
+ }
60
+
61
+ // ==================== Demo 2: GameObjects with Pipeline ====================
62
+ class GameObjectsDemo extends Game {
63
+ constructor(canvas) {
64
+ super(canvas);
65
+ this.backgroundColor = "#050505";
66
+ }
67
+
68
+ init() {
69
+ super.init();
70
+
71
+ const centerX = this.canvas.width / 2;
72
+ const centerY = this.canvas.height / 2;
73
+
74
+ // Create a scene (GameObject container)
75
+ this.scene = new Scene(this, { x: centerX, y: centerY });
76
+
77
+ // Create a sprite with animation (GameObject)
78
+ this.player = new Sprite(this, { frameRate: 8, loop: true, autoPlay: true });
79
+
80
+ // Add frames - pulsing circle animation
81
+ const colors = ["#0f0", "#0ff", "#0f0", "#ff0", "#0f0"];
82
+ const sizes = [20, 25, 30, 25, 20];
83
+ colors.forEach((color, i) => {
84
+ this.player.addFrame(new Circle(sizes[i], { color, stroke: "#fff", lineWidth: 2 }));
85
+ });
86
+
87
+ // Add to scene
88
+ this.scene.add(this.player);
89
+
90
+ // Add scene to pipeline - now it's managed
91
+ this.pipeline.add(this.scene);
92
+ }
93
+ }
94
+
95
+ function initGameObjectsDemo() {
96
+ const canvas = document.getElementById("gameobjects-canvas");
97
+ if (!canvas) return;
98
+
99
+ // Set actual canvas size
100
+ const rect = canvas.getBoundingClientRect();
101
+ canvas.width = rect.width;
102
+ canvas.height = rect.height;
103
+
104
+ const demo = new GameObjectsDemo(canvas);
105
+ demo.start();
106
+ }
107
+
108
+ // ==================== Demo 3: Bridging Shape to GameObject ====================
109
+ class BridgingDemo extends Game {
110
+ constructor(canvas) {
111
+ super(canvas);
112
+ this.backgroundColor = "#050505";
113
+ }
114
+
115
+ init() {
116
+ super.init();
117
+
118
+ const centerX = this.canvas.width / 2;
119
+ const centerY = this.canvas.height / 2;
120
+
121
+ // Create a Group of shapes (like a simple avatar)
122
+ const avatar = new Group();
123
+ avatar.add(new Circle(25, { y: -30, color: "#0f0", stroke: "#0a0", lineWidth: 2 })); // head
124
+ avatar.add(new Rectangle({ y: 20, width: 40, height: 50, color: "#0f0", stroke: "#0a0", lineWidth: 2 })); // body
125
+
126
+ // Wrap it as a GameObject so it can join the pipeline
127
+ const avatarGO = ShapeGOFactory.create(this, avatar, {
128
+ x: centerX,
129
+ y: centerY,
130
+ });
131
+
132
+ // Now it's a proper GameObject - add to pipeline
133
+ this.pipeline.add(avatarGO);
134
+
135
+ // Add a second avatar to show multiple instances
136
+ const avatar2 = new Group();
137
+ avatar2.add(new Circle(20, { y: -25, color: "#0ff", stroke: "#0aa", lineWidth: 2 }));
138
+ avatar2.add(new Rectangle({ y: 15, width: 30, height: 40, color: "#0ff", stroke: "#0aa", lineWidth: 2 }));
139
+
140
+ const avatar2GO = ShapeGOFactory.create(this, avatar2, {
141
+ x: centerX + 100,
142
+ y: centerY,
143
+ });
144
+ this.pipeline.add(avatar2GO);
145
+
146
+ // Third one with rotation
147
+ const avatar3 = new Group({ rotation: 0.2 });
148
+ avatar3.add(new Circle(18, { y: -22, color: "#f0f", stroke: "#a0a", lineWidth: 2 }));
149
+ avatar3.add(new Rectangle({ y: 12, width: 25, height: 35, color: "#f0f", stroke: "#a0a", lineWidth: 2 }));
150
+
151
+ const avatar3GO = ShapeGOFactory.create(this, avatar3, {
152
+ x: centerX - 100,
153
+ y: centerY,
154
+ });
155
+ this.pipeline.add(avatar3GO);
156
+ }
157
+ }
158
+
159
+ function initBridgingDemo() {
160
+ const canvas = document.getElementById("bridging-canvas");
161
+ if (!canvas) return;
162
+
163
+ const rect = canvas.getBoundingClientRect();
164
+ canvas.width = rect.width;
165
+ canvas.height = rect.height;
166
+
167
+ const demo = new BridgingDemo(canvas);
168
+ demo.start();
169
+ }
170
+
171
+ // ==================== Initialize ====================
172
+ window.addEventListener("load", () => {
173
+ initShapesDemo();
174
+ initGameObjectsDemo();
175
+ initBridgingDemo();
176
+ });
@@ -0,0 +1,268 @@
1
+ /**
2
+ * Hypnotic Mandala - Generative Art Demo
3
+ *
4
+ * A trippy, psychedelic mandala using the gcanvas fluent API.
5
+ * Features:
6
+ * - Concentric rings of shapes rotating at different speeds
7
+ * - HSL color cycling based on time and position
8
+ * - Pulsing and breathing effects
9
+ * - Responsive scaling for mobile
10
+ * - 3D rotation with inertia (drag to rotate!)
11
+ */
12
+
13
+ import { gcanvas, Camera3D } from "/gcanvas.es.min.js";
14
+
15
+ // Configuration - base values at 800px reference size
16
+ const CONFIG = {
17
+ referenceSize: 800, // Design reference size
18
+ minScale: 0.4, // Minimum scale on small screens
19
+ rings: 6,
20
+ shapesPerRing: [8, 12, 16, 20, 24, 32],
21
+ baseRadius: 50,
22
+ ringSpacing: 45,
23
+ rotationSpeeds: [0.8, -0.6, 0.5, -0.4, 0.3, -0.2],
24
+ pulseAmplitudes: [0.12, 0.1, 0.08, 0.06, 0.05, 0.04],
25
+ colorOffsets: [0, 60, 120, 180, 240, 300],
26
+ };
27
+
28
+ // Calculate scale factor based on screen size
29
+ function getScaleFactor(width, height) {
30
+ const minDimension = Math.min(width, height);
31
+ const scale = minDimension / CONFIG.referenceSize;
32
+ return Math.max(CONFIG.minScale, Math.min(1.2, scale));
33
+ }
34
+
35
+ // Shape factory functions
36
+ const shapeTypes = ['circle', 'star', 'triangle', 'hexagon', 'diamond', 'ring'];
37
+
38
+ function getShapeConfig(ringIndex, shapeIndex) {
39
+ const type = shapeTypes[ringIndex % shapeTypes.length];
40
+ const baseSize = 12 - ringIndex;
41
+
42
+ switch (type) {
43
+ case 'circle':
44
+ return { method: 'circle', opts: { radius: baseSize } };
45
+ case 'star':
46
+ return { method: 'star', opts: { radius: baseSize, points: 5 + (ringIndex % 3), inset: 0.5 } };
47
+ case 'triangle':
48
+ return { method: 'triangle', opts: { size: baseSize * 1.8 } };
49
+ case 'hexagon':
50
+ return { method: 'hexagon', opts: { radius: baseSize } };
51
+ case 'diamond':
52
+ return { method: 'diamond', opts: { width: baseSize, height: baseSize * 1.5 } };
53
+ case 'ring':
54
+ return { method: 'ring', opts: { innerRadius: baseSize * 0.5, outerRadius: baseSize } };
55
+ default:
56
+ return { method: 'circle', opts: { radius: baseSize } };
57
+ }
58
+ }
59
+
60
+ // Initialize the mandala
61
+ window.addEventListener('load', () => {
62
+ const canvas = document.getElementById('game');
63
+ if (!canvas) return;
64
+
65
+ const game = gcanvas({ canvas, bg: '#0a0a0f', fluid: true });
66
+ const scene = game.scene('mandala');
67
+
68
+ // Create 3D camera with inertia for interactive rotation
69
+ const camera = new Camera3D({
70
+ rotationX: 0, // Start flat
71
+ rotationY: 0,
72
+ perspective: 600,
73
+ sensitivity: 0.008,
74
+ inertia: true,
75
+ friction: 0.96, // Higher = more drift before stopping
76
+ velocityScale: 2.5, // More momentum on fast flicks
77
+ autoRotate: false, // No auto-rotation - stays where user leaves it
78
+ clampX: false, // Free rotation in all directions
79
+ });
80
+
81
+ // Enable mouse/touch drag to rotate
82
+ camera.enableMouseControl(canvas);
83
+
84
+ // Store shape references for animation
85
+ const allShapes = [];
86
+
87
+ // Create center piece - a pulsing core (position will be updated dynamically)
88
+ scene.go({ x: 0, y: 0, name: 'core' })
89
+ .star({ radius: 25, points: 8, inset: 0.6, fill: '#fff' });
90
+ allShapes.push({ name: 'core', ring: -1, index: 0 });
91
+
92
+ // Create concentric rings
93
+ for (let ring = 0; ring < CONFIG.rings; ring++) {
94
+ const shapesInRing = CONFIG.shapesPerRing[ring];
95
+ const ringRadius = CONFIG.baseRadius + ring * CONFIG.ringSpacing;
96
+
97
+ for (let i = 0; i < shapesInRing; i++) {
98
+ const angle = (i / shapesInRing) * Math.PI * 2;
99
+ const name = `r${ring}_s${i}`;
100
+
101
+ const shapeConfig = getShapeConfig(ring, i);
102
+ const goBuilder = scene.go({ x: 0, y: 0, name });
103
+
104
+ // Apply the shape with initial color
105
+ const hue = (ring * 60 + i * (360 / shapesInRing)) % 360;
106
+ goBuilder[shapeConfig.method]({
107
+ ...shapeConfig.opts,
108
+ fill: `hsl(${hue}, 80%, 60%)`
109
+ });
110
+
111
+ allShapes.push({
112
+ name,
113
+ ring,
114
+ index: i,
115
+ baseAngle: angle,
116
+ ringRadius,
117
+ shapeConfig
118
+ });
119
+ }
120
+ }
121
+
122
+ // Create outer particle ring
123
+ const particleCount = 48;
124
+ const particleRadius = CONFIG.baseRadius + CONFIG.rings * CONFIG.ringSpacing + 40;
125
+
126
+ for (let i = 0; i < particleCount; i++) {
127
+ const angle = (i / particleCount) * Math.PI * 2;
128
+ const name = `particle_${i}`;
129
+
130
+ scene.go({ x: 0, y: 0, name })
131
+ .circle({ radius: 3, fill: '#fff' });
132
+
133
+ allShapes.push({
134
+ name,
135
+ ring: CONFIG.rings,
136
+ index: i,
137
+ baseAngle: angle,
138
+ ringRadius: particleRadius,
139
+ isParticle: true
140
+ });
141
+ }
142
+
143
+ // Animation update loop
144
+ let time = 0;
145
+
146
+ // Get reference to underlying game for live dimensions
147
+ const gameInstance = game.game;
148
+
149
+ // Override clear for trail effect (like particles.js)
150
+ gameInstance.clear = function() {
151
+ this.ctx.fillStyle = 'rgba(10, 10, 15, 0.15)';
152
+ this.ctx.fillRect(0, 0, this.width, this.height);
153
+ };
154
+
155
+ game.on('update', (dt, ctx) => {
156
+ time += dt;
157
+
158
+ // Update camera (handles inertia and auto-rotation)
159
+ camera.update(dt);
160
+
161
+ // Get dynamic center and scale based on LIVE canvas size
162
+ const cx = gameInstance.width / 2;
163
+ const cy = gameInstance.height / 2;
164
+ const scaleFactor = getScaleFactor(gameInstance.width, gameInstance.height);
165
+
166
+ // Animate core - project through 3D camera
167
+ const core = ctx.refs.core;
168
+ if (core) {
169
+ // Core at the tip of the cone (closest), with breathing pulse
170
+ const coreZ = Math.sin(time * 2) * 40 - 150; // Oscillates at the front tip
171
+ const projected = camera.project(0, 0, coreZ);
172
+ core.x = cx + projected.x;
173
+ core.y = cy + projected.y;
174
+ const coreScale = (1 + Math.sin(time * 4) * 0.25) * scaleFactor * projected.scale;
175
+ core.scaleX = coreScale;
176
+ core.scaleY = coreScale;
177
+ core.rotation = time * 1.5;
178
+
179
+ // Update core color
180
+ if (core._fluentShape) {
181
+ const coreHue = (time * 100) % 360;
182
+ core._fluentShape.color = `hsl(${coreHue}, 100%, 70%)`;
183
+ }
184
+ }
185
+
186
+ // Animate each ring with 3D projection
187
+ for (const shape of allShapes) {
188
+ if (shape.ring < 0) continue; // Skip core
189
+
190
+ const go = ctx.refs[shape.name];
191
+ if (!go) continue;
192
+
193
+ if (shape.isParticle) {
194
+ // Particle animation - orbit and twinkle
195
+ const particleSpeed = 0.15;
196
+ const newAngle = shape.baseAngle + time * particleSpeed;
197
+ const wobble = Math.sin(time * 2 + shape.index * 0.3) * 8 * scaleFactor;
198
+ const scaledRadius = shape.ringRadius * scaleFactor;
199
+
200
+ // Calculate 2D position on the mandala plane
201
+ const localX = Math.cos(newAngle) * (scaledRadius + wobble);
202
+ const localY = Math.sin(newAngle) * (scaledRadius + wobble);
203
+
204
+ // Project through 3D camera (particles at base of cone, furthest back)
205
+ const projected = camera.project(localX, localY, 300);
206
+ go.x = cx + projected.x;
207
+ go.y = cy + projected.y;
208
+
209
+ // Scale particle size with perspective
210
+ go.scaleX = scaleFactor * projected.scale;
211
+ go.scaleY = scaleFactor * projected.scale;
212
+
213
+ // Twinkle effect
214
+ const alpha = 0.4 + Math.abs(Math.sin(time * 8 + shape.index * 0.3)) * 0.6;
215
+ if (go._fluentShape) {
216
+ go._fluentShape.opacity = alpha;
217
+ }
218
+ } else {
219
+ // Ring shape animation
220
+ const ringIndex = shape.ring;
221
+ const rotationSpeed = CONFIG.rotationSpeeds[ringIndex];
222
+ const pulseAmp = CONFIG.pulseAmplitudes[ringIndex];
223
+
224
+ // Rotate around center - smooth continuous rotation
225
+ const newAngle = shape.baseAngle + time * rotationSpeed;
226
+
227
+ // Gentle breathing effect on radius (scaled)
228
+ const breathPhase = time * 0.8 + ringIndex * 0.5;
229
+ const scaledRadius = shape.ringRadius * scaleFactor;
230
+ const breathRadius = scaledRadius + Math.sin(breathPhase) * 8 * scaleFactor;
231
+
232
+ // Calculate 2D position on the mandala plane
233
+ const localX = Math.cos(newAngle) * breathRadius;
234
+ const localY = Math.sin(newAngle) * breathRadius;
235
+
236
+ // Project through 3D camera - CONE shape: inner rings close, outer rings far
237
+ const ringDepth = ringIndex * 60 - 50; // Ring 0 at -50, Ring 5 at 250
238
+ const projected = camera.project(localX, localY, ringDepth);
239
+ go.x = cx + projected.x;
240
+ go.y = cy + projected.y;
241
+
242
+ // Smooth pulse scale with perspective
243
+ const pulsePhase = time * 2 + ringIndex * 0.3;
244
+ const scale = (1 + Math.sin(pulsePhase) * pulseAmp) * scaleFactor * projected.scale;
245
+ go.scaleX = scale;
246
+ go.scaleY = scale;
247
+
248
+ // Rotate individual shapes - smooth spin
249
+ go.rotation = time * (ringIndex % 2 === 0 ? 2 : -2);
250
+
251
+ // Color cycling - psychedelic HSL animation
252
+ if (go._fluentShape) {
253
+ const baseHue = CONFIG.colorOffsets[ringIndex];
254
+ const hueShift = time * 60; // Faster color rotation
255
+ const positionHue = shape.index * (360 / CONFIG.shapesPerRing[ringIndex]);
256
+
257
+ const finalHue = (baseHue + hueShift + positionHue) % 360;
258
+ const saturation = 85;
259
+ const lightness = 55 + Math.sin(time * 3 + ringIndex) * 10;
260
+
261
+ go._fluentShape.color = `hsl(${finalHue}, ${saturation}%, ${lightness}%)`;
262
+ }
263
+ }
264
+ }
265
+ });
266
+
267
+ game.start();
268
+ });