@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,477 @@
1
+ import {
2
+ Circle,
3
+ Easing,
4
+ FPSCounter,
5
+ Game,
6
+ GameObject,
7
+ Painter,
8
+ TextShape,
9
+ TileLayout,
10
+ Rectangle,
11
+ Tweenetik,
12
+ ToggleButton,
13
+ Tooltip,
14
+ Position,
15
+ } from "/gcanvas.es.min.js";
16
+
17
+ // Formulas for each easing function
18
+ const EASING_FORMULAS = {
19
+ linear: "f(t) = t",
20
+ smoothstep: "f(t) = t² × (3 - 2t)",
21
+ smootherstep: "f(t) = t³ × (t × (6t - 15) + 10)",
22
+ easeInQuad: "f(t) = t²",
23
+ easeOutQuad: "f(t) = t × (2 - t)",
24
+ easeInOutQuad: "f(t) = t < 0.5 ? 2t² : -1 + (4 - 2t) × t",
25
+ easeInCubic: "f(t) = t³",
26
+ easeOutCubic: "f(t) = (t - 1)³ + 1",
27
+ easeInOutCubic: "f(t) = t < 0.5 ? 4t³ : (t-1)(2t-2)² + 1",
28
+ easeInQuart: "f(t) = t⁴",
29
+ easeOutQuart: "f(t) = 1 - (t - 1)⁴",
30
+ easeInOutQuart: "f(t) = t < 0.5 ? 8t⁴ : 1 - 8(t-1)⁴",
31
+ easeInSine: "f(t) = 1 - cos(t × π/2)",
32
+ easeOutSine: "f(t) = sin(t × π/2)",
33
+ easeInOutSine: "f(t) = -(cos(πt) - 1) / 2",
34
+ easeInExpo: "f(t) = 2^(10(t - 1))",
35
+ easeOutExpo: "f(t) = 1 - 2^(-10t)",
36
+ easeInOutExpo: "f(t) = ½×2^(20t-10) or 1 - ½×2^(-20t+10)",
37
+ easeInCirc: "f(t) = 1 - √(1 - t²)",
38
+ easeOutCirc: "f(t) = √(1 - (t-1)²)",
39
+ easeInOutCirc: "t<½ ? ½(1-√(1-4t²)) : ½(√(-(2t-3)(2t-1))+1)",
40
+ easeInElastic: "f(t) = -2^(10(t-1)) × sin(2π(t-1-s)/p)",
41
+ easeOutElastic: "f(t) = 2^(-10t) × sin(2π(t-s)/p) + 1",
42
+ easeInOutElastic: "t<½ ? easeIn : easeOut (mirrored)",
43
+ easeInBack: "f(t) = t²((s+1)t - s) where s≈1.7",
44
+ easeOutBack: "f(t) = (t-1)²((s+1)(t-1) + s) + 1",
45
+ easeInOutBack: "t<½ ? easeIn(2t)/2 : easeOut(2t-1)/2 + ½",
46
+ easeInBounce: "f(t) = 1 - easeOutBounce(1-t)",
47
+ easeOutBounce: "4 parabolas: 7.5625(t-c)² + k",
48
+ easeInOutBounce: "t<½ ? easeIn(2t)/2 : easeOut(2t-1)/2 + ½",
49
+ };
50
+
51
+ /**
52
+ * EasingBox: A tile showing an easing curve graph with animated circle
53
+ */
54
+ class EasingBox extends GameObject {
55
+ constructor(game, label, easingFn, formula, tooltip, options = {}) {
56
+ super(game, options);
57
+ this.label = label;
58
+ this.easingFn = easingFn;
59
+ this.formula = formula;
60
+ this.tooltip = tooltip;
61
+ this.animTime = 0;
62
+ this.duration = 1;
63
+ this.pauseDuration = 0.3;
64
+ this.isPlaying = false;
65
+ this.inPausePhase = true; // Start with ball hidden
66
+ this.interactive = true;
67
+
68
+ // Background box
69
+ this.bg = new Rectangle({
70
+ width: 100,
71
+ height: 100,
72
+ color: "rgba(0, 0, 0, 0.1)",
73
+ stroke: "rgba(255, 255, 255, 0.3)",
74
+ lineWidth: 1,
75
+ });
76
+
77
+ // Label
78
+ this.labelText = new TextShape(this.label, {
79
+ x: 0,
80
+ y: 44,
81
+ font: "11px monospace",
82
+ color: "white",
83
+ });
84
+
85
+ // Glow circle
86
+ this.glowCircle = new Circle(8, {
87
+ color: "rgba(0, 255, 0, 0.3)",
88
+ });
89
+
90
+ // Animated circle
91
+ this.animCircle = new Circle(4, {
92
+ color: "#0f0",
93
+ });
94
+
95
+ // Cache scale values
96
+ this.graphScale = 1;
97
+ this.graphOffset = 0;
98
+ this.scaleCalculated = false;
99
+
100
+ // Flag for "play all" mode (controlled by toggle button)
101
+ this.playAll = false;
102
+
103
+ // Setup hover/touch events to play animation
104
+ this.on("mouseover", (e) => {
105
+ this.isHovered = true;
106
+ if (!this.playAll) {
107
+ this.isPlaying = true;
108
+ this.animTime = 0; // restart from beginning
109
+ }
110
+ // Scale up effect like shapes.js
111
+ Tweenetik.to(
112
+ this,
113
+ { scaleX: 1.15, scaleY: 1.15 },
114
+ 0.6,
115
+ Easing.easeOutElastic
116
+ );
117
+ // Show tooltip with formula
118
+ if (this.tooltip && this.formula) {
119
+ this.tooltip.show(this.formula, e.x, e.y);
120
+ }
121
+ // Change cursor to pointer
122
+ this.game.canvas.style.cursor = "pointer";
123
+ });
124
+ this.on("mouseout", () => {
125
+ this.isHovered = false;
126
+ if (!this.playAll) {
127
+ this.isPlaying = false;
128
+ }
129
+ // Scale back down
130
+ Tweenetik.to(
131
+ this,
132
+ { scaleX: 1, scaleY: 1 },
133
+ 0.6,
134
+ Easing.easeOutElastic
135
+ );
136
+ // Hide tooltip
137
+ if (this.tooltip) {
138
+ this.tooltip.hide();
139
+ }
140
+ // Reset cursor
141
+ this.game.canvas.style.cursor = "default";
142
+ });
143
+ // For touch devices - play on touch, stop when touch ends
144
+ this.on("inputdown", () => {
145
+ if (!this.playAll) {
146
+ this.isPlaying = true;
147
+ this.animTime = 0;
148
+ }
149
+ });
150
+ }
151
+
152
+ /**
153
+ * Set play all mode - when true, animation plays continuously
154
+ */
155
+ setPlayAll(playAll) {
156
+ this.playAll = playAll;
157
+ if (playAll) {
158
+ this.isPlaying = true;
159
+ this.animTime = 0;
160
+ } else {
161
+ // Only keep playing if currently hovered
162
+ this.isPlaying = this.isHovered || false;
163
+ }
164
+ }
165
+
166
+ update(dt) {
167
+ super.update(dt);
168
+
169
+ // Calculate graph dimensions
170
+ const size = 100;
171
+ const padding = 10;
172
+ const labelSpace = 14;
173
+ const graphSize = size - padding * 2 - labelSpace;
174
+ const startX = -size / 2 + padding;
175
+ const startY = size / 2 - padding - labelSpace;
176
+
177
+ // Only animate when playing
178
+ if (this.isPlaying) {
179
+ this.animTime += dt;
180
+
181
+ const totalCycle = this.duration + this.pauseDuration;
182
+ if (this.animTime > totalCycle) {
183
+ this.animTime = 0;
184
+ }
185
+
186
+ // Check if we're in the pause phase (animation finished, waiting to restart)
187
+ this.inPausePhase = this.animTime > this.duration;
188
+
189
+ if (!this.inPausePhase) {
190
+ const t = this.animTime / this.duration;
191
+ const easedT = this.easingFn(t);
192
+ const scaledEasedT = easedT * this.graphScale + this.graphOffset;
193
+
194
+ const circleX = startX + t * graphSize;
195
+ const circleY = startY - scaledEasedT * graphSize;
196
+
197
+ // Update circle positions
198
+ this.glowCircle.x = circleX;
199
+ this.glowCircle.y = circleY;
200
+ this.animCircle.x = circleX;
201
+ this.animCircle.y = circleY;
202
+ }
203
+ } else {
204
+ this.inPausePhase = true; // Hide when not playing
205
+ }
206
+ }
207
+
208
+ draw() {
209
+ super.draw();
210
+
211
+ // Calculate scale values once on first draw
212
+ if (!this.scaleCalculated) {
213
+ let minVal = 0;
214
+ let maxVal = 1;
215
+ for (let i = 0; i <= 100; i++) {
216
+ const t = i / 100;
217
+ const val = this.easingFn(t);
218
+ if (val < minVal) minVal = val;
219
+ if (val > maxVal) maxVal = val;
220
+ }
221
+ const range = maxVal - minVal;
222
+ this.graphScale = range > 1 ? 1 / range : 1;
223
+ this.graphOffset = minVal < 0 ? -minVal * this.graphScale : 0;
224
+ this.scaleCalculated = true;
225
+ }
226
+
227
+ // 1. Draw background first
228
+ this.bg.render();
229
+
230
+ const size = 100;
231
+ const padding = 10;
232
+ const labelSpace = 14;
233
+ const graphSize = size - padding * 2 - labelSpace;
234
+ const startX = -size / 2 + padding;
235
+ const startY = size / 2 - padding - labelSpace;
236
+
237
+ // 2. Draw axes and curve using Painter.useCtx for automatic cleanup
238
+ Painter.useCtx((ctx) => {
239
+ // Draw axes
240
+ ctx.strokeStyle = "rgba(255, 255, 255, 0.2)";
241
+ ctx.lineWidth = 1;
242
+ ctx.moveTo(startX, startY - graphSize);
243
+ ctx.lineTo(startX, startY);
244
+ ctx.lineTo(startX + graphSize, startY);
245
+ ctx.stroke();
246
+
247
+ // Draw the easing curve
248
+ ctx.beginPath();
249
+ ctx.strokeStyle = "white";
250
+ ctx.lineWidth = 2;
251
+ ctx.lineCap = "round";
252
+ ctx.lineJoin = "round";
253
+
254
+ for (let i = 0; i <= graphSize; i++) {
255
+ const t = i / graphSize;
256
+ const easedT = this.easingFn(t);
257
+ const scaledEasedT = easedT * this.graphScale + this.graphOffset;
258
+ const x = startX + i;
259
+ const y = startY - scaledEasedT * graphSize;
260
+
261
+ if (i === 0) {
262
+ ctx.moveTo(x, y);
263
+ } else {
264
+ ctx.lineTo(x, y);
265
+ }
266
+ }
267
+ ctx.stroke();
268
+ });
269
+
270
+ // 3. Draw label
271
+ this.labelText.render();
272
+
273
+ // 4. Draw circles on top (hidden during pause phase)
274
+ if (!this.inPausePhase) {
275
+ this.glowCircle.render();
276
+ this.animCircle.render();
277
+ }
278
+ }
279
+ }
280
+
281
+ /**
282
+ * EasingDemo: A TileLayout grid displaying all easing function curves
283
+ */
284
+ class EasingDemo extends TileLayout {
285
+ constructor(game, tooltip, options = {}) {
286
+ super(game, options);
287
+ this.tooltip = tooltip;
288
+ this.cellSize = 120;
289
+ this.maxColumns = 6;
290
+
291
+ // All easing functions organized by type (In, Out, InOut for each)
292
+ this.easingDefinitions = [
293
+ // Basics
294
+ { label: "linear", fn: Easing.linear },
295
+ { label: "smoothstep", fn: Easing.smoothstep },
296
+ { label: "smootherstep", fn: Easing.smootherstep },
297
+
298
+ // Quadratic
299
+ { label: "easeInQuad", fn: Easing.easeInQuad },
300
+ { label: "easeOutQuad", fn: Easing.easeOutQuad },
301
+ { label: "easeInOutQuad", fn: Easing.easeInOutQuad },
302
+
303
+ // Cubic
304
+ { label: "easeInCubic", fn: Easing.easeInCubic },
305
+ { label: "easeOutCubic", fn: Easing.easeOutCubic },
306
+ { label: "easeInOutCubic", fn: Easing.easeInOutCubic },
307
+
308
+ // Quartic
309
+ { label: "easeInQuart", fn: Easing.easeInQuart },
310
+ { label: "easeOutQuart", fn: Easing.easeOutQuart },
311
+ { label: "easeInOutQuart", fn: Easing.easeInOutQuart },
312
+
313
+ // Sine
314
+ { label: "easeInSine", fn: Easing.easeInSine },
315
+ { label: "easeOutSine", fn: Easing.easeOutSine },
316
+ { label: "easeInOutSine", fn: Easing.easeInOutSine },
317
+
318
+ // Exponential
319
+ { label: "easeInExpo", fn: Easing.easeInExpo },
320
+ { label: "easeOutExpo", fn: Easing.easeOutExpo },
321
+ { label: "easeInOutExpo", fn: Easing.easeInOutExpo },
322
+
323
+ // Circular
324
+ { label: "easeInCirc", fn: Easing.easeInCirc },
325
+ { label: "easeOutCirc", fn: Easing.easeOutCirc },
326
+ { label: "easeInOutCirc", fn: Easing.easeInOutCirc },
327
+
328
+ // Elastic
329
+ { label: "easeInElastic", fn: (t) => Easing.easeInElastic(t) },
330
+ { label: "easeOutElastic", fn: (t) => Easing.easeOutElastic(t) },
331
+ { label: "easeInOutElastic", fn: (t) => Easing.easeInOutElastic(t) },
332
+
333
+ // Back
334
+ { label: "easeInBack", fn: (t) => Easing.easeInBack(t) },
335
+ { label: "easeOutBack", fn: (t) => Easing.easeOutBack(t) },
336
+ { label: "easeInOutBack", fn: (t) => Easing.easeInOutBack(t) },
337
+
338
+ // Bounce
339
+ { label: "easeInBounce", fn: Easing.easeInBounce },
340
+ { label: "easeOutBounce", fn: Easing.easeOutBounce },
341
+ { label: "easeInOutBounce", fn: Easing.easeInOutBounce },
342
+ ];
343
+ }
344
+
345
+ init() {
346
+ this.boxes = this.easingDefinitions.map((def) => {
347
+ const formula = EASING_FORMULAS[def.label] || "";
348
+ const box = new EasingBox(
349
+ this.game,
350
+ def.label,
351
+ def.fn,
352
+ formula,
353
+ this.tooltip,
354
+ { width: 100, height: 100 }
355
+ );
356
+ box.transform.size(100, 100);
357
+ this.add(box);
358
+ return box;
359
+ });
360
+ }
361
+
362
+ onResize() {
363
+ const margin = 40;
364
+ const availableWidth = this.game.width - margin;
365
+ const columns = Math.min(
366
+ this.maxColumns,
367
+ Math.max(1, Math.floor(availableWidth / this.cellSize))
368
+ );
369
+
370
+ if (this.columns !== columns) {
371
+ this.columns = columns;
372
+ this.markBoundsDirty();
373
+ }
374
+
375
+ // Center the layout
376
+ this.transform.position(
377
+ Math.round(this.game.width / 2),
378
+ Math.round(this.game.height / 2)
379
+ );
380
+ }
381
+ }
382
+
383
+ /**
384
+ * MyGame: Main game class
385
+ */
386
+ export class MyGame extends Game {
387
+ constructor(canvas) {
388
+ super(canvas);
389
+ this.backgroundColor = "black";
390
+ this.enableFluidSize();
391
+ }
392
+
393
+ init() {
394
+ super.init();
395
+
396
+ // Create tooltip for showing formulas
397
+ this.tooltip = new Tooltip(this, {
398
+ font: "14px monospace",
399
+ textColor: "#0f0",
400
+ bgColor: "rgba(0, 0, 0, 0.9)",
401
+ borderColor: "rgba(0, 255, 0, 0.5)",
402
+ padding: 12,
403
+ });
404
+
405
+ // Toggle button to play all animations
406
+ this.toggleBtn = new ToggleButton(this, {
407
+ text: "Play All",
408
+ anchor: Position.BOTTOM_LEFT,
409
+ width: 100,
410
+ height: 32,
411
+ colorDefaultBg: "rgba(0, 0, 0, 0.9)",
412
+ colorDefaultStroke: "rgba(0, 255, 0, 0.5)",
413
+ colorDefaultText: "#0f0",
414
+ colorActiveBg: "rgba(0, 255, 0, 1)",
415
+ colorActiveStroke: "orange",
416
+ colorActiveText: "#FFFFFF",
417
+ colorHoverBg: "rgba(0, 255, 0, 0.5)",
418
+ colorHoverStroke: "rgba(0, 255, 0, 0.5)",
419
+ colorHoverText: "#0f0",
420
+ colorPressedBg: "rgba(0, 255, 0, 0.5)",
421
+ colorPressedStroke: "rgba(0, 255, 0, 0.5)",
422
+ colorPressedText: "#0f0",
423
+ startToggled: false,
424
+ onToggle: (isOn) => {
425
+ this.setPlayAll(isOn);
426
+ },
427
+ });
428
+ this.pipeline.add(this.toggleBtn);
429
+
430
+ // Calculate initial columns based on screen width
431
+ const cellSize = 120;
432
+ const maxColumns = 6;
433
+ const margin = 40;
434
+ const initialColumns = Math.min(
435
+ maxColumns,
436
+ Math.max(1, Math.floor((this.width - margin) / cellSize))
437
+ );
438
+
439
+ this.easingDemo = new EasingDemo(this, this.tooltip, {
440
+ debug: false,
441
+ anchor: "center",
442
+ spacing: 20,
443
+ columns: initialColumns,
444
+ padding: 30,
445
+ align: "center",
446
+ });
447
+ this.pipeline.add(this.easingDemo);
448
+
449
+ this.pipeline.add(
450
+ new FPSCounter(this, {
451
+ anchor: "bottom-right",
452
+ })
453
+ );
454
+
455
+ // Add tooltip last so it renders on top
456
+ this.pipeline.add(this.tooltip);
457
+
458
+ this.easingDemo.onResize();
459
+ }
460
+
461
+ /**
462
+ * Toggle all animations on/off
463
+ */
464
+ setPlayAll(playAll) {
465
+ if (this.easingDemo && this.easingDemo.boxes) {
466
+ this.easingDemo.boxes.forEach((box) => {
467
+ box.setPlayAll(playAll);
468
+ });
469
+ }
470
+ }
471
+
472
+ onResize() {
473
+ if (this.easingDemo) {
474
+ this.easingDemo.onResize();
475
+ }
476
+ }
477
+ }
@@ -0,0 +1,183 @@
1
+ /**
2
+ * Fluent API Demo Initializations
3
+ *
4
+ * Each function creates a demo on a specific canvas element,
5
+ * showcasing different aspects of the Fluent API.
6
+ */
7
+
8
+ import { gcanvas, sketch } from "/gcanvas.es.min.js";
9
+
10
+ // ─────────────────────────────────────────────────────────
11
+ // Demo 1: Hello World
12
+ // ─────────────────────────────────────────────────────────
13
+
14
+ function initHelloDemo() {
15
+ const canvas = document.getElementById('hello-canvas');
16
+ if (!canvas) return;
17
+ gcanvas({ canvas, bg: '#111' })
18
+ .scene('game')
19
+ .go({ x: 200, y: 125 })
20
+ .circle({ radius: 40, fill: '#00ff88' })
21
+ .start();
22
+ }
23
+
24
+ // ─────────────────────────────────────────────────────────
25
+ // Demo 2: Builder Chain
26
+ // ─────────────────────────────────────────────────────────
27
+
28
+ function initChainDemo() {
29
+ const canvas = document.getElementById('chain-canvas');
30
+ if (!canvas) return;
31
+
32
+ gcanvas({ canvas, bg: '#111' })
33
+ .scene('game')
34
+ .go({ x: 80, y: 125 })
35
+ .circle({ radius: 30, fill: '#ff6b6b' })
36
+ .end()
37
+ .go({ x: 200, y: 125 })
38
+ .star({ points: 5, radius: 35, fill: '#ffd93d' })
39
+ .end()
40
+ .go({ x: 320, y: 125 })
41
+ .rect({ width: 50, height: 50, fill: '#6bcb77' })
42
+ .start();
43
+ }
44
+
45
+ // ─────────────────────────────────────────────────────────
46
+ // Demo 3: Shape Variety
47
+ // ─────────────────────────────────────────────────────────
48
+
49
+ function initShapesDemo() {
50
+ const canvas = document.getElementById('shapes-canvas');
51
+ if (!canvas) return;
52
+
53
+ gcanvas({ canvas, bg: '#111' })
54
+ .scene('shapes')
55
+ .go({ x: 40, y: 125 }).triangle({ size: 30, fill: '#ff6b6b' }).end()
56
+ .go({ x: 105, y: 125 }).hexagon({ radius: 18, fill: '#c44dff' }).end()
57
+ .go({ x: 170, y: 125 }).diamond({ width: 28, height: 36, fill: '#4ecdc4' }).end()
58
+ .go({ x: 235, y: 110 }).heart({ size: 26, fill: '#ff6b9d' }).end()
59
+ .go({ x: 300, y: 125 }).cross({ size: 26, thickness: 6, fill: '#ffd93d' }).end()
60
+ .go({ x: 365, y: 125 }).ring({ innerRadius: 10, outerRadius: 18, fill: '#a8e6cf' }).end()
61
+ .go({ x: 430, y: 125 }).star({ points: 6, radius: 18, fill: '#ff8c42' }).end()
62
+ .go({ x: 485, y: 125 }).cloud({ size: 25, fill: '#dfe6e9' })
63
+ .start();
64
+ }
65
+
66
+ // ─────────────────────────────────────────────────────────
67
+ // Demo 4: Motion Presets
68
+ // ─────────────────────────────────────────────────────────
69
+
70
+ function initMotionDemo() {
71
+ const canvas = document.getElementById('motion-canvas');
72
+ if (!canvas) return;
73
+
74
+ gcanvas({ canvas, bg: '#111' })
75
+ .scene('motion')
76
+ // Pulsing circle
77
+ .go({ x: 100, y: 125 })
78
+ .circle({ radius: 30, fill: '#ff6b6b' })
79
+ .pulse({ min: 0.7, max: 1.3, duration: 1 })
80
+ .end()
81
+ // Oscillating star
82
+ .go({ x: 220, y: 125 })
83
+ .star({ points: 5, radius: 25, fill: '#ffd93d' })
84
+ .oscillate({ prop: 'y', min: -30, max: 30, duration: 2 })
85
+ .end()
86
+ // Orbiting shape
87
+ .go({ x: 300, y: 125 })
88
+ .hexagon({ radius: 20, fill: '#4ecdc4' })
89
+ .orbit({ radiusX: 40, radiusY: 40, duration: 3 })
90
+ .start();
91
+ }
92
+
93
+ // ─────────────────────────────────────────────────────────
94
+ // Demo 5: Sketch Mode (using gcanvas to target existing canvas)
95
+ // Note: sketch() auto-creates a canvas, so we recreate the pattern with gcanvas
96
+ // ─────────────────────────────────────────────────────────
97
+
98
+ function initSketchDemo() {
99
+ const canvas = document.getElementById('sketch-canvas');
100
+ if (!canvas) return;
101
+
102
+ const game = gcanvas({ canvas, bg: '#111' });
103
+ const scene = game.scene('default');
104
+
105
+ // Create radial pattern
106
+ const count = 12;
107
+ const radius = 80;
108
+ const cx = 200;
109
+ const cy = 125;
110
+
111
+ for (let i = 0; i < count; i++) {
112
+ const angle = (i / count) * Math.PI * 2;
113
+ const x = cx + Math.cos(angle) * radius;
114
+ const y = cy + Math.sin(angle) * radius;
115
+
116
+ scene.go({ x, y, name: `shape_${i}` })
117
+ .circle({ radius: 15, fill: `hsl(${i * 30}, 70%, 60%)` });
118
+ }
119
+
120
+ game.on('update', (dt, ctx) => {
121
+ for (let i = 0; i < count; i++) {
122
+ const shape = ctx.refs[`shape_${i}`];
123
+ if (shape) {
124
+ shape.rotation += dt * (i % 2 === 0 ? 1 : -1);
125
+ }
126
+ }
127
+ });
128
+
129
+ game.start();
130
+ }
131
+
132
+ // ─────────────────────────────────────────────────────────
133
+ // Demo 6: Events & Interactivity
134
+ // ─────────────────────────────────────────────────────────
135
+
136
+ function initEventsDemo() {
137
+ const canvas = document.getElementById('events-canvas');
138
+ if (!canvas) return;
139
+
140
+ const game = gcanvas({ canvas, bg: '#111' })
141
+ .state({ clicks: 0 });
142
+
143
+ game.scene('interactive')
144
+ .go({ x: 200, y: 100, name: 'clickTarget' })
145
+ .circle({ radius: 40, fill: '#ff6b6b' })
146
+ .on('click', (ctx) => {
147
+ // Access shared state via game.refs
148
+ const state = game.getState('clicks') || 0;
149
+ game.setState('clicks', state + 1);
150
+
151
+ // Update label text
152
+ const label = game.refs.label;
153
+ if (label && label._fluentShape) {
154
+ label._fluentShape.text = `Clicks: ${state + 1}`;
155
+ }
156
+
157
+ // Visual feedback - brief pulse
158
+ ctx.go.scaleX = 1.2;
159
+ ctx.go.scaleY = 1.2;
160
+ setTimeout(() => {
161
+ ctx.go.scaleX = 1;
162
+ ctx.go.scaleY = 1;
163
+ }, 100);
164
+ })
165
+ .end()
166
+ .go({ x: 200, y: 200, name: 'label' })
167
+ .text('Click the circle!', { fill: '#aaa', font: '14px monospace' });
168
+
169
+ game.start();
170
+ }
171
+
172
+ // ─────────────────────────────────────────────────────────
173
+ // Initialize all demos on page load
174
+ // ─────────────────────────────────────────────────────────
175
+
176
+ window.addEventListener('load', () => {
177
+ initHelloDemo();
178
+ initChainDemo();
179
+ initShapesDemo();
180
+ initMotionDemo();
181
+ initSketchDemo();
182
+ initEventsDemo();
183
+ });