melonjs 20.1.1 → 20.3.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 (165) hide show
  1. package/README.md +88 -10
  2. package/build/application/settings.d.ts +4 -2
  3. package/build/application/settings.d.ts.map +1 -1
  4. package/build/audio/audio.d.ts +2 -2
  5. package/build/audio/audio.d.ts.map +1 -1
  6. package/build/audio/backend/buffer.d.ts +26 -0
  7. package/build/audio/backend/buffer.d.ts.map +1 -0
  8. package/build/audio/backend/cache.d.ts +2 -0
  9. package/build/audio/backend/cache.d.ts.map +1 -0
  10. package/build/audio/backend/core.d.ts +24 -0
  11. package/build/audio/backend/core.d.ts.map +1 -0
  12. package/build/audio/backend/engine.d.ts +54 -0
  13. package/build/audio/backend/engine.d.ts.map +1 -0
  14. package/build/audio/backend/sound.d.ts +79 -0
  15. package/build/audio/backend/sound.d.ts.map +1 -0
  16. package/build/audio/backend/spatial.d.ts +191 -0
  17. package/build/audio/backend/spatial.d.ts.map +1 -0
  18. package/build/audio/backend/types.d.ts +97 -0
  19. package/build/audio/backend/types.d.ts.map +1 -0
  20. package/build/audio/backend/voice.d.ts +33 -0
  21. package/build/audio/backend/voice.d.ts.map +1 -0
  22. package/build/audio/playback.d.ts +49 -4
  23. package/build/audio/playback.d.ts.map +1 -1
  24. package/build/audio/{backend.d.ts → state.d.ts} +13 -11
  25. package/build/audio/state.d.ts.map +1 -0
  26. package/build/audio/types.d.ts +93 -1
  27. package/build/audio/types.d.ts.map +1 -1
  28. package/build/camera/camera3d.d.ts +5 -5
  29. package/build/const.d.ts +2 -2
  30. package/build/index.d.ts.map +1 -1
  31. package/build/index.js +10048 -10341
  32. package/build/index.js.map +4 -4
  33. package/build/input/input.d.ts +19 -0
  34. package/build/input/input.d.ts.map +1 -1
  35. package/build/input/pointerevent.d.ts.map +1 -1
  36. package/build/lang/deprecated.d.ts +4 -2
  37. package/build/lang/deprecated.d.ts.map +1 -1
  38. package/build/level/gltf/GLTFScene.d.ts +12 -0
  39. package/build/level/gltf/GLTFScene.d.ts.map +1 -1
  40. package/build/level/tiled/TMXObjectFactory.d.ts.map +1 -1
  41. package/build/loader/loader.d.ts +4 -40
  42. package/build/loader/loader.d.ts.map +1 -1
  43. package/build/loader/parsers/audio.d.ts +31 -0
  44. package/build/loader/parsers/audio.d.ts.map +1 -0
  45. package/build/loader/parsers/shader.d.ts.map +1 -1
  46. package/build/particles/emitter.d.ts +81 -3
  47. package/build/particles/emitter.d.ts.map +1 -1
  48. package/build/particles/particle.d.ts +62 -1
  49. package/build/particles/particle.d.ts.map +1 -1
  50. package/build/particles/settings.d.ts +50 -7
  51. package/build/particles/settings.d.ts.map +1 -1
  52. package/build/physics/adapter.d.ts +3 -3
  53. package/build/physics/builtin/body.d.ts +23 -24
  54. package/build/physics/builtin/body.d.ts.map +1 -1
  55. package/build/physics/builtin/detector.d.ts +1 -1
  56. package/build/plugin/plugin.d.ts +9 -1
  57. package/build/plugin/plugin.d.ts.map +1 -1
  58. package/build/polyfill/index.d.ts +1 -4
  59. package/build/polyfill/index.d.ts.map +1 -1
  60. package/build/renderable/container.d.ts +10 -0
  61. package/build/renderable/container.d.ts.map +1 -1
  62. package/build/renderable/entity/entity.d.ts +10 -0
  63. package/build/renderable/entity/entity.d.ts.map +1 -1
  64. package/build/renderable/mesh.d.ts +16 -0
  65. package/build/renderable/mesh.d.ts.map +1 -1
  66. package/build/renderable/renderable.d.ts +122 -32
  67. package/build/renderable/renderable.d.ts.map +1 -1
  68. package/build/renderable/sprite.d.ts +7 -7
  69. package/build/renderable/sprite.d.ts.map +1 -1
  70. package/build/state/stage.d.ts +4 -2
  71. package/build/state/stage.d.ts.map +1 -1
  72. package/build/system/event.d.ts +1 -1
  73. package/build/system/platform.d.ts +21 -1
  74. package/build/system/platform.d.ts.map +1 -1
  75. package/build/utils/fetchdata.d.ts.map +1 -0
  76. package/build/video/blendmodes.d.ts +107 -0
  77. package/build/video/blendmodes.d.ts.map +1 -0
  78. package/build/video/canvas/canvas_renderer.d.ts +31 -24
  79. package/build/video/canvas/canvas_renderer.d.ts.map +1 -1
  80. package/build/video/effects/blendEffect.d.ts +57 -0
  81. package/build/video/effects/blendEffect.d.ts.map +1 -0
  82. package/build/video/effects/blur.d.ts +4 -4
  83. package/build/video/effects/blur.d.ts.map +1 -1
  84. package/build/video/effects/chromaticAberration.d.ts +5 -5
  85. package/build/video/effects/chromaticAberration.d.ts.map +1 -1
  86. package/build/video/effects/colorMatrix.d.ts +5 -5
  87. package/build/video/effects/colorMatrix.d.ts.map +1 -1
  88. package/build/video/effects/desaturate.d.ts +5 -5
  89. package/build/video/effects/desaturate.d.ts.map +1 -1
  90. package/build/video/effects/dissolve.d.ts +4 -4
  91. package/build/video/effects/dissolve.d.ts.map +1 -1
  92. package/build/video/effects/dropShadow.d.ts +5 -5
  93. package/build/video/effects/dropShadow.d.ts.map +1 -1
  94. package/build/video/effects/flash.d.ts +4 -4
  95. package/build/video/effects/flash.d.ts.map +1 -1
  96. package/build/video/effects/glow.d.ts +5 -5
  97. package/build/video/effects/glow.d.ts.map +1 -1
  98. package/build/video/effects/hologram.d.ts +4 -4
  99. package/build/video/effects/hologram.d.ts.map +1 -1
  100. package/build/video/effects/invert.d.ts +5 -5
  101. package/build/video/effects/invert.d.ts.map +1 -1
  102. package/build/video/effects/outline.d.ts +6 -6
  103. package/build/video/effects/outline.d.ts.map +1 -1
  104. package/build/video/effects/pixelate.d.ts +4 -4
  105. package/build/video/effects/pixelate.d.ts.map +1 -1
  106. package/build/video/effects/radialGradient.d.ts +2 -2
  107. package/build/video/effects/scanline.d.ts +6 -6
  108. package/build/video/effects/scanline.d.ts.map +1 -1
  109. package/build/video/effects/sepia.d.ts +5 -5
  110. package/build/video/effects/sepia.d.ts.map +1 -1
  111. package/build/video/effects/shadereffect.d.ts +12 -10
  112. package/build/video/effects/shadereffect.d.ts.map +1 -1
  113. package/build/video/effects/shine.d.ts +3 -3
  114. package/build/video/effects/shine.d.ts.map +1 -1
  115. package/build/video/effects/tintPulse.d.ts +4 -4
  116. package/build/video/effects/tintPulse.d.ts.map +1 -1
  117. package/build/video/effects/vignette.d.ts +6 -6
  118. package/build/video/effects/vignette.d.ts.map +1 -1
  119. package/build/video/effects/wave.d.ts +4 -4
  120. package/build/video/effects/wave.d.ts.map +1 -1
  121. package/build/video/renderer.d.ts +3 -4
  122. package/build/video/renderer.d.ts.map +1 -1
  123. package/build/video/rendertarget/canvasrendertarget.d.ts +1 -0
  124. package/build/video/rendertarget/canvasrendertarget.d.ts.map +1 -1
  125. package/build/video/texture/atlas.d.ts +1 -1
  126. package/build/video/webgl/batchers/batcher.d.ts.map +1 -1
  127. package/build/video/webgl/glshader.d.ts +2 -2
  128. package/build/video/webgl/glshader.d.ts.map +1 -1
  129. package/build/video/webgl/utils/program.d.ts.map +1 -1
  130. package/build/video/webgl/webgl_renderer.d.ts +117 -29
  131. package/build/video/webgl/webgl_renderer.d.ts.map +1 -1
  132. package/build/video/webgpu/batchers/quad_batcher.d.ts +7 -0
  133. package/build/video/webgpu/batchers/quad_batcher.d.ts.map +1 -1
  134. package/build/video/webgpu/pipeline/cache.d.ts +0 -9
  135. package/build/video/webgpu/pipeline/cache.d.ts.map +1 -1
  136. package/build/video/webgpu/webgpu_renderer.d.ts +101 -6
  137. package/build/video/webgpu/webgpu_renderer.d.ts.map +1 -1
  138. package/package.json +6 -10
  139. package/skills/AGENTS.md +67 -0
  140. package/skills/melonjs/SKILL.md +156 -0
  141. package/skills/melonjs-20-migration/SKILL.md +160 -0
  142. package/skills/melonjs-3d/SKILL.md +226 -0
  143. package/skills/melonjs-3d-assets/SKILL.md +239 -0
  144. package/skills/melonjs-audio/SKILL.md +215 -0
  145. package/skills/melonjs-camera-and-drawing/SKILL.md +171 -0
  146. package/skills/melonjs-deployment/SKILL.md +113 -0
  147. package/skills/melonjs-effects-and-shaders/SKILL.md +177 -0
  148. package/skills/melonjs-events/SKILL.md +159 -0
  149. package/skills/melonjs-getting-started/SKILL.md +183 -0
  150. package/skills/melonjs-input/SKILL.md +166 -0
  151. package/skills/melonjs-lighting/SKILL.md +139 -0
  152. package/skills/melonjs-loading-assets/SKILL.md +157 -0
  153. package/skills/melonjs-particles-and-trails/SKILL.md +136 -0
  154. package/skills/melonjs-performance/SKILL.md +178 -0
  155. package/skills/melonjs-physics/SKILL.md +321 -0
  156. package/skills/melonjs-plugins/SKILL.md +191 -0
  157. package/skills/melonjs-renderables/SKILL.md +214 -0
  158. package/skills/melonjs-renderer-backends/SKILL.md +173 -0
  159. package/skills/melonjs-scenes-and-state/SKILL.md +180 -0
  160. package/skills/melonjs-sprites-and-animation/SKILL.md +173 -0
  161. package/skills/melonjs-tilemaps/SKILL.md +193 -0
  162. package/skills/melonjs-ui-and-text/SKILL.md +175 -0
  163. package/build/audio/backend.d.ts.map +0 -1
  164. package/build/loader/parsers/fetchdata.d.ts.map +0 -1
  165. /package/build/{loader/parsers → utils}/fetchdata.d.ts +0 -0
@@ -0,0 +1,239 @@
1
+ ---
2
+ name: melonjs-3d-assets
3
+ description: "Use this skill when loading 3D models into melonJS — glTF and GLB scenes, OBJ/MTL models, materials, imported lights, node animation, ground shadows and GPU instancing. Covers level.load options, the rightHanded conversion, and exactly what the loader does and does not support. Triggers on: glTF, gltf, glb, OBJ, MTL, 3D model, getGLTF, getOBJ, getMTL, GLTFModel, GLTFScene, level.load glb, rightHanded, lightIntensityScale, castGroundShadow, shadowGroundY, EXT_mesh_gpu_instancing, KHR_lights_punctual, skinning, Blender export, 3D asset."
4
+ license: MIT
5
+ ---
6
+
7
+ # Loading 3D assets
8
+
9
+ > glTF/GLB is the primary path for 3D content, and it goes through the **level
10
+ > director** rather than the world directly: `level.load("scene")`, the same call
11
+ > a Tiled map uses. OBJ/MTL is the simpler path for a single static model.
12
+
13
+ ## Loading a glTF/GLB scene
14
+
15
+ ```js
16
+ await loader.preload([
17
+ { name: "diorama", type: "glb", src: "data/scenes/diorama.glb" },
18
+ ]);
19
+
20
+ level.load("diorama", { scale: 50 });
21
+ ```
22
+
23
+ `type: "gltf"` (JSON plus external buffers) and `type: "glb"` (single binary)
24
+ are both loader types. A self-contained GLB is the better shipping format — one
25
+ request, nothing to resolve relatively.
26
+
27
+ The scene loads under whatever camera the stage has; view it under a `Camera3d`
28
+ or it renders flat. See `melonjs-3d` for the camera.
29
+
30
+ ### `level.load` options for glTF/GLB
31
+
32
+ | option | default | what it does |
33
+ |---|---|---|
34
+ | `scale` | `1` | pixels per glTF unit, applied to the whole scene. Blender's metre-scale export usually needs 20–100. |
35
+ | `container` | `game.world` | where the nodes are added |
36
+ | `onLoaded` | `app.onLevelLoaded` | called with the **level id**, not the scene |
37
+ | `rightHanded` | `true` | see below |
38
+ | `lights` | `true` | instantiate authored `KHR_lights_punctual` lights as `Light3d` world children |
39
+ | `lightIntensityScale` | — | keep authored intensity ratios instead of normalising every light to 1 |
40
+ | `castGroundShadow` | inherits the app setting (**on**) | blob shadows for this scene's meshes |
41
+ | `shadowGroundY` | each object's own base | world Y of the floor the blobs land on |
42
+
43
+ `onLoaded` receives the level id — it is a "done" signal, not a handle on the
44
+ scene. You need it: with the game loop running, `level.load` stops the loop and
45
+ defers the actual load to the next tick, so it returns *before* anything is in
46
+ the world. To get at what was loaded, load into a container you own, or look the
47
+ nodes up by their authored names:
48
+
49
+ ```js
50
+ level.load("diorama", { scale: 50, onLoaded: () => {
51
+ const sun = app.world.getChildByName("Sun")[0]; // note: returns an array
52
+ sun.intensity = 2;
53
+ }});
54
+ ```
55
+
56
+ ## `rightHanded` — and the default that differs between the two APIs
57
+
58
+ glTF is authored **Y-up, right-handed**; melonJS is **Y-down, +Z forward**.
59
+ There are two ways to bridge that, and they are not equivalent:
60
+
61
+ - a **rotation** — preserves chirality, matches the authoring tool
62
+ - a **mirror** (negate Y only) — flips the scene left/right
63
+
64
+ `level.load` defaults `rightHanded: true`, so a scene loaded through the level
65
+ director comes in correctly. **The `Mesh` constructor defaults it to `false`**,
66
+ so glTF or OBJ geometry you feed to a `Mesh` yourself comes in mirrored unless
67
+ you pass `rightHanded: true`. A model that is subtly wrong — text backwards, a
68
+ character's gear on the wrong side — is this and nothing else.
69
+
70
+ Do not add a flip of your own on top; two corrections cancel back to wrong.
71
+
72
+ ## Materials — what actually arrives
73
+
74
+ The importer covers a deliberate subset:
75
+
76
+ - `pbrMetallicRoughness` **base colour texture and factor**
77
+ - metallic/roughness **factors** converted to the engine's specular model
78
+ - `emissiveFactor`, including `KHR_materials_emissive_strength`
79
+ - `KHR_materials_unlit` — honoured, for baked/stylised workflows
80
+ - `alphaMode: "MASK"` — the cutout threshold is applied
81
+ - `doubleSided`
82
+
83
+ Not imported: **normal maps**, metallic/roughness *textures*, occlusion maps,
84
+ morph targets, `KHR_texture_transform`. Nothing errors — the material simply
85
+ arrives without them, which reads as "flatter than the preview in my authoring
86
+ tool". Bake what you need into the base colour.
87
+
88
+ **Draco compression is the exception that does error.** Its geometry sits in
89
+ accessors with no buffer view, which the parser rejects outright, so the whole
90
+ asset fails to load. Export uncompressed.
91
+
92
+ `textureFilter` on the Application settings (`"nearest"` / `"linear"` /
93
+ `"auto"`) drives sampling; glTF materials that declare their own sampler carry
94
+ it through. Pixel-art models want `"nearest"`.
95
+
96
+ ## Lights
97
+
98
+ Authored `KHR_lights_punctual` lights — sun, point, spot — become `Light3d`
99
+ world children, plus a soft ambient fill, and each keeps its authored name.
100
+ A scene carrying any of them renders through the **lit** path automatically.
101
+
102
+ glTF stores physical units — lux for suns, candela for lamps — and a Blender
103
+ daylight sun is 1000+, which blows out the engine's stylised shading. So every
104
+ light is normalised to intensity 1 by default. To keep the *ratios* the artist
105
+ set, scale instead of normalising:
106
+
107
+ ```js
108
+ // a 1000-lux sun lands at 1, a 500-lux fill lands at 0.5
109
+ level.load("diorama", { scale: 50, lightIntensityScale: 0.001 });
110
+ ```
111
+
112
+ Pass `lights: false` to manage lighting yourself.
113
+
114
+ ## Animation
115
+
116
+ Node-TRS animation **is** supported: translation, rotation and scale channels,
117
+ the rigid hierarchical animation used by blocky/low-poly character packs. An
118
+ animated asset instantiates as a single `GLTFModel` keeping the node hierarchy
119
+ intact, with an API deliberately mirroring `Sprite`. The model carries the asset
120
+ name, which is how you get hold of it after loading:
121
+
122
+ ```js
123
+ const model = app.world.getChildByName("hero")[0]; // the GLTFModel
124
+
125
+ model.getAnimationNames(); // ["idle", "walk", ...]
126
+ model.setCurrentAnimation("walk", { loop: true, speed: 1, onComplete });
127
+ model.setCurrentAnimation("jump", { next: "idle" }); // chain
128
+ model.isCurrentAnimation("walk");
129
+ model.play();
130
+ model.pause();
131
+ ```
132
+
133
+ `speed` is a playback multiplier (1 = authored speed), not a frame delay. Only
134
+ an explicit `loop: false` stops looping. `setCurrentAnimation` **throws** on a
135
+ clip name the asset does not define — check `getAnimationNames()` first.
136
+
137
+ **Vertex skinning is out of scope** — `JOINTS_0` / `WEIGHTS_0` are not read, and
138
+ neither are morph targets. A smoothly-deforming character will not deform. The
139
+ options are: rig it hierarchically instead (separate meshes parented into a
140
+ node tree), use `Sprite3d` billboards, or use the Spine plugin for 2D skeletal
141
+ work.
142
+
143
+ ## Instancing
144
+
145
+ A node using `EXT_mesh_gpu_instancing` loads as an `InstancedMesh` — one draw
146
+ call for every copy, which is the difference between a hundred trees and a
147
+ hundred thousand. Export with instancing enabled where your tool supports it.
148
+ Sparse accessors on the instancing attributes are rejected with a clear error
149
+ rather than silently misplacing every instance.
150
+
151
+ ## Ground shadows
152
+
153
+ ```js
154
+ level.load("diorama", { scale: 50, castGroundShadow: true, shadowGroundY: 0 });
155
+ ```
156
+
157
+ `castGroundShadow` is tri-state: set it and it overrides the application setting
158
+ for this scene in both directions; omit it and the scene inherits — and that
159
+ setting is `true` by default, so shadows are already on. As a scene-wide opt-in
160
+ it skips nodes with no vertical extent — a ground plane is exactly that, and
161
+ shadowing it with itself smears a blob across the whole floor. Blob shadows need
162
+ a GPU backend and a `Camera3d`; nothing is drawn on the 2D-camera or Canvas
163
+ paths.
164
+
165
+ Omit `shadowGroundY` and each blob sits at its own object's base at full
166
+ strength, which is right for props already resting on the ground. Set it when
167
+ things jump or fly, so the shadow stays on the floor and shrinks with height.
168
+
169
+ ## OBJ/MTL
170
+
171
+ A different shape entirely: OBJ produces raw geometry you hand to a `Mesh`,
172
+ not a scene. Preload the two files and name them in the `Mesh` settings — the
173
+ `model` and `material` names are independent, the same name for both is just a
174
+ convention:
175
+
176
+ ```js
177
+ await loader.preload([
178
+ { name: "fox", type: "obj", src: "models/fox.obj" },
179
+ { name: "fox", type: "mtl", src: "models/fox.mtl" },
180
+ ]);
181
+
182
+ const mesh = new Mesh(400, 300, {
183
+ model: "fox",
184
+ material: "fox", // texture resolved from the material's map_Kd
185
+ width: 200,
186
+ height: 200,
187
+ rightHanded: true, // the Mesh default is false — see above
188
+ });
189
+ world.addChild(mesh);
190
+ ```
191
+
192
+ **The MTL loader fetches the textures it references itself** — `map_Kd` and
193
+ `map_d` are resolved relative to the `.mtl` and preloaded for you, so there is
194
+ no image entry in the manifest above. Passing `texture:` explicitly overrides
195
+ that and pins *one* binding over the whole model, which on a multi-material
196
+ model suppresses the per-material texture split; leave it out unless you mean
197
+ that.
198
+
199
+ MTL contributes `Kd` (diffuse tint), `d` / `Tr` (opacity), `Ke` (emissive),
200
+ `Ks` + `Ns` (specular highlight), `map_Kd` (diffuse texture) and `map_d`
201
+ (per-texel opacity). Normal and specular *maps* are not supported.
202
+ `loader.getOBJ(name)` and `loader.getMTL(name)` return the parsed data if you
203
+ want it directly. OBJ has no scene graph, no lights, no animation — use glTF for
204
+ anything beyond a single static model.
205
+
206
+ ## Packaging
207
+
208
+ A self-contained GLB is one file with nothing to resolve — the safest thing to
209
+ ship. External references still work in both formats: `.bin` buffers and image
210
+ `uri`s are resolved **relative to the asset's own URL** (a GLB with an external
211
+ texture loads as-shipped). But an external file that 404s rejects the parse, so
212
+ the whole asset fails to load rather than arriving partially — no untextured
213
+ fallback. Note that `loader.setBaseURL("*", …)` deliberately skips `gltf`,
214
+ `glb`, `obj` and `mtl` for this reason; set those types individually if you
215
+ need a prefix.
216
+
217
+ ## Symptom → cause
218
+
219
+ | symptom | cause |
220
+ |---|---|
221
+ | model mirrored — text backwards, gear on the wrong side | `Mesh` defaults `rightHanded` to `false`; pass `true` |
222
+ | model upside down | a manual axis flip on top of the loader's conversion |
223
+ | scene is enormous or invisibly small | `scale` left at 1 against a metre-scale export |
224
+ | the asset fails to load entirely | an external buffer/image `uri` that 404s, or Draco-compressed geometry |
225
+ | everything is blown out white | authored lux/candela intensities; use `lightIntensityScale` |
226
+ | materials flatter than the authoring-tool preview | no normal or metallic/roughness *textures* are imported |
227
+ | a character does not deform | vertex skinning is out of scope; rig hierarchically or billboard |
228
+ | animation names come back empty | the asset has no node-TRS channels (skin-only rig) |
229
+ | a hundred copies tank the frame rate | exported without `EXT_mesh_gpu_instancing` |
230
+ | a shadow smeared across the whole floor | a ground plane cast its own blob — use the scene-wide opt-in |
231
+ | `onLoaded` gets a string, not the scene | it is called with the level id; load into your own container instead |
232
+ | scene renders flat and unlit | no `Camera3d` — the 2D-camera path is CPU-projected and unlit (with a `Camera3d` on Canvas you get a black canvas instead) |
233
+
234
+ ## Related skills
235
+
236
+ - `melonjs-3d` — conventions, `Camera3d`, meshes, `Light3d`, instancing
237
+ - `melonjs-lighting` — how `Light3d` behaves once imported
238
+ - `melonjs-loading-assets` — the loader, asset types and base URLs
239
+ - `melonjs-plugins` — the Spine plugin, for skeletal characters
@@ -0,0 +1,215 @@
1
+ ---
2
+ name: melonjs-audio
3
+ description: "Use this skill for sound and music in melonJS — loading audio, playing effects, background tracks, audio sprites, volume and muting, spatial audio, and procedural tone/noise generation. Covers the mandatory audio.init format list, the directory-not-file src convention, playTrack versus play, and browser autoplay unlocking. Triggers on: audio, sound, audio.init, audio.play, playTrack, stopTrack, audio.load, sfx, music, volume, mute, sprite audio, stereo, panner, audio.tone, audio.noise, getAudioContext, autoplay."
4
+ license: MIT
5
+ ---
6
+
7
+ # Audio
8
+
9
+ ## Two setup rules
10
+
11
+ ```js
12
+ import { audio, loader } from "melonjs";
13
+
14
+ audio.init("mp3,ogg"); // 1. BEFORE preloading any audio
15
+
16
+ await loader.preload([
17
+ { name: "cling", type: "audio", src: "data/sfx/" }, // 2. a PREFIX, not a file
18
+ ]);
19
+ ```
20
+
21
+ 1. **`audio.init(format)` must run before any audio asset is preloaded**, and
22
+ lists the formats you actually ship. Preloading first throws
23
+ `target audio extension(s) should be set through me.audio.init() before
24
+ calling the preloader.` It returns `false` when no audio backend is
25
+ available, so it is worth checking. The format list defaults to `"mp3"`.
26
+ 2. **`src` is a prefix, not a file.** The loader appends the asset `name` plus
27
+ each configured extension and uses the first that loads — so
28
+ `data/sfx/` + `cling` + `mp3` → `data/sfx/cling.mp3`. Passing a full filename
29
+ gives you a 404. A `data:audio/...` URL is the one exception: it is used
30
+ verbatim, prefix and extension skipped.
31
+
32
+ Order matters — melonJS tries the listed formats left to right, so put the
33
+ preferred one first. Two formats (`"webm,mp3"` or `"mp3,ogg"`) is the usual
34
+ belt-and-braces for codec coverage. Accepted tokens: `mp3`, `mpeg`, `opus`,
35
+ `ogg`, `oga`, `wav`, `aac`, `caf`, `m4a`, `m4b`, `mp4`, `weba`, `webm`, `dolby`,
36
+ `flac`.
37
+
38
+ ## Effects versus music
39
+
40
+ ```js
41
+ audio.play("cling"); // fire and forget
42
+ audio.play("cling", false, null, 0.5); // loop, onend, volume
43
+ const id = audio.play("engine", true); // looping; keep the id to stop it
44
+
45
+ audio.playTrack("theme"); // background music
46
+ audio.stopTrack();
47
+ ```
48
+
49
+ Use **`playTrack`** for music, not `play`. `playTrack(name, volume?)` always
50
+ loops, and it records the name as the current track — which is what
51
+ `app.pauseOnBlur` / `resumeOnFocus` / `stopOnBlur` act on, via
52
+ `state.pause(true)` → `pauseTrack()`. `stopTrack()`, `pauseTrack()`,
53
+ `resumeTrack()` and `getCurrentTrack()` all read that same single slot, so there
54
+ is only ever one current track. Stop it in `onDestroyEvent`.
55
+
56
+ Every per-clip function throws `audio clip <name> does not exist` if the clip was
57
+ never loaded, and `play` additionally throws when a named sprite region is
58
+ missing.
59
+
60
+ ## Audio sprites
61
+
62
+ One file carrying many effects, with named regions in milliseconds:
63
+
64
+ ```js
65
+ await loader.preload([{
66
+ name: "sfx", type: "audio", src: "data/sfx/",
67
+ sprite: { jump: [0, 450], hit: [2000, 250], music: [4000, 12000, true] },
68
+ }]);
69
+
70
+ audio.play("sfx", { sprite: "jump" });
71
+ ```
72
+
73
+ The optional third element marks a region as looping. `play()`'s second argument
74
+ takes either the original `loop` boolean or a `PlayOptions` object
75
+ (`sprite`, `loop`, `onend`, `volume`) — both forms work. Object fields win over
76
+ the positional `onend` / `volume` arguments when both are supplied.
77
+
78
+ ## Clip state
79
+
80
+ ```js
81
+ audio.duration("theme"); // seconds — 0 while still loading
82
+ audio.duration("sfx", id); // duration of the region THAT instance plays
83
+ audio.playing("theme"); // is anything playing?
84
+ audio.playing("theme", id); // is that instance playing?
85
+ audio.state("theme"); // "unloaded" | "loading" | "loaded"
86
+ ```
87
+
88
+ `stop()`, `pause()` and `resume()` take an optional instance `id` too; omit it
89
+ and the whole group is affected. `audio.stop()` with no arguments at all stops
90
+ every sound. `audio.seek(name)` reads the position, `audio.seek(name, s, id?)`
91
+ writes it; `audio.rate` and `audio.fade(name, from, to, ms, id?)` round it out.
92
+
93
+ ## Volume and muting
94
+
95
+ ```js
96
+ audio.setVolume(0.5); // global, 0..1
97
+ audio.getVolume(); // read it back
98
+ audio.mute("cling"); // one clip — mute(name, id?, shouldMute = true)
99
+ audio.unmute("cling");
100
+ audio.muteAll(); // everything (audio.disable() is an alias)
101
+ audio.unmuteAll(); // (audio.enable() is an alias)
102
+ audio.muted(); // global mute state
103
+ ```
104
+
105
+ `audio.unload(name)` / `audio.unloadAll()` free the decoded buffers; `unload`
106
+ also clears the current-track pointer if it named that clip.
107
+
108
+ ## Spatial audio
109
+
110
+ ```js
111
+ audio.position("engine", x, y, z);
112
+ audio.orientation("engine", x, y, z);
113
+ audio.stereo("engine", -1); // -1 left … 1 right
114
+ audio.panner("engine", { coneInnerAngle: 90, /* … */ });
115
+ ```
116
+
117
+ `position(name, x, y = 0, z = -0.5, id?)` and
118
+ `orientation(name, x, y, z, id?)` both default `id` to "the whole group".
119
+ Called with just the clip name, each of the four returns the current value —
120
+ `stereo` gives `0` and `position` gives `[0, 0, 0]` before they have ever been
121
+ set.
122
+
123
+ **Spatial audio is WebAudio-only.** On a clip loaded with `stream: true` (or
124
+ `html5: true`) `stereo` / `position` / `orientation` return early and do
125
+ nothing — which is exactly the case for the long music tracks people reach for
126
+ first.
127
+
128
+ ## Procedural sound
129
+
130
+ For beeps, blips and noise without shipping files. Both take **one options
131
+ object**, and `duration` (in seconds) is required:
132
+
133
+ ```js
134
+ audio.tone({ freq: 440, duration: 0.2, gain: 0.3, pan: -0.5 });
135
+ audio.tone({ freq: [440, 660, 880], duration: 0.4 }); // layered partials
136
+ audio.noise({ duration: 0.5, filter: { type: "lowpass", frequency: 800 } });
137
+ ```
138
+
139
+ `tone` also takes `wave` (default `"sine"`), `attack` (`0.005`) and `pitchSlide`
140
+ (`1`). `noise` takes `type` (`"white"` | `"pink"` | `"brown"`, default
141
+ `"white"`), `attack`, `pan`, `filter` and `filterSweep`. `gain` defaults to `0.1`
142
+ on both.
143
+
144
+ Both are **silent no-ops** when there is no WebAudio context — they bail on
145
+ `getAudioContext() === null` rather than throwing. Neither needs the clip to be
146
+ loaded; neither goes through the file-playback path at all.
147
+
148
+ `audio.getAudioContext()` and `audio.getMasterGain()` are the escape hatches for
149
+ custom Web Audio work; both return `null` when audio is unavailable, so guard.
150
+ Connect a custom graph to the master gain rather than `ctx.destination` if you
151
+ want it to respect `setVolume` / `muteAll`.
152
+
153
+ ## Autoplay: sound is silent until the first gesture
154
+
155
+ Browsers create the audio context in the `suspended` state and keep it there
156
+ until a user interaction. Nothing errors — audio simply does not play, which
157
+ reads as a bug. melonJS installs one-shot `touchstart` / `touchend` / `click` /
158
+ `keydown` unlock listeners on `document`, so the first real gesture anywhere
159
+ unlocks it and fires the clip's `on.unlock` callback. Design for it: start music
160
+ on the first click or key press, not on load. `tone` / `noise` additionally call
161
+ `ctx.resume()` themselves, best-effort, on every invocation.
162
+
163
+ Playback also starts **asynchronously**: `play()` returns an id while the
164
+ instance is still starting, so `playing()` can legitimately be `false` on the
165
+ very next line.
166
+
167
+ ## Load-time options
168
+
169
+ ```js
170
+ { name: "sfx", type: "audio", src: "data/sfx/",
171
+ sprite: { jump: [0, 450] },
172
+ pool: 5, // finished instances kept for reuse — NOT a concurrency cap
173
+ rate: 1.0, // initial playback rate, 0.5..4.0
174
+ mute: false,
175
+ loop: false,
176
+ autoplay: false,
177
+ preload: true, // or "metadata"
178
+ format: "mp3", // explicit hint when the URL has no usable extension
179
+ stream: true, // long music: stream rather than decode into memory
180
+ on: { play() {}, end() {}, stop() {}, unlock() {} },
181
+ }
182
+ ```
183
+
184
+ `pool` (default `5`) is a **reuse cap on finished voices**, not a limit on
185
+ simultaneous playback: when the number of ended voices exceeds it, the surplus
186
+ are disconnected and dropped. Raise it for a clip triggered in rapid bursts, not
187
+ to allow more overlap.
188
+
189
+ Use `stream: true` for long music tracks — it plays through an HTML5 `<audio>`
190
+ element instead of decoding the whole file into memory. (`html5: true` is the
191
+ same switch under its backend name.) `on` accepts the full lifecycle set:
192
+ `play`, `pause`, `stop`, `end`, `fade`, `seek`, `rate`, `volume`, `mute`,
193
+ `unlock`.
194
+
195
+ Re-preloading a manifest that already contains a loaded clip is a no-op —
196
+ `audio.unload(name)` first if you genuinely want to reload it.
197
+
198
+ ## Symptom → cause
199
+
200
+ | symptom | cause |
201
+ |---|---|
202
+ | audio assets 404 | `src` given as a full filename instead of a directory prefix |
203
+ | `target audio extension(s) should be set...` thrown at preload | `audio.init()` not called before preloading |
204
+ | `audio clip X does not exist` | the clip was never preloaded, or the name is mis-cased — every per-clip call throws this |
205
+ | nothing plays until the user clicks | browser autoplay policy — expected, design around it |
206
+ | `playing()` is false right after `play()` | playback starts asynchronously |
207
+ | music keeps playing after leaving a scene | used `play` instead of `playTrack`, or no `stopTrack` in teardown |
208
+ | `stereo` / `position` do nothing | the clip was loaded with `stream: true` — spatial is WebAudio-only |
209
+ | one format works, another does not | that codec is not in the `audio.init()` list |
210
+ | `audio.tone(440, {...})` does nothing / errors | `tone` takes a single options object with a required `duration` |
211
+ | `tone` / `noise` are silent with no error | no WebAudio context — `getAudioContext()` returned `null` |
212
+
213
+ ## Related skills
214
+
215
+ - `melonjs-getting-started` — preloading and asset ordering
@@ -0,0 +1,171 @@
1
+ ---
2
+ name: melonjs-camera-and-drawing
3
+ description: "Use this skill for camera control and immediate-mode drawing in melonJS — following a target, viewport bounds, shake and fade, secondary cameras, and drawing shapes, lines and gradients inside a custom draw(). Covers world versus screen coordinate conversion, clipping and masking, and baking with CanvasRenderTarget. Triggers on: Camera2d, viewport, follow, setBounds, shake, fadeIn, fadeOut, worldToLocal, localToWorld, colorMatrix, renderer.fill, renderer.stroke, Rect, Ellipse, Polygon, Line, Gradient, clipRect, mask, CanvasRenderTarget, NoiseTexture2d."
4
+ license: MIT
5
+ ---
6
+
7
+ # Camera and immediate-mode drawing
8
+
9
+ ## Following a target
10
+
11
+ ```js
12
+ app.viewport.follow(player, app.viewport.AXIS.BOTH, 0.1);
13
+ ```
14
+
15
+ **Pass the renderable, not `renderable.pos`.** `follow()` accepts a `Renderable`,
16
+ a `Vector2d` or a `Vector3d` and **throws** `invalid target for me.Camera2d.follow`
17
+ for anything else. A renderable's `pos` is an `ObservableVector3d`, which is not
18
+ a `Vector3d` subclass, so `follow(player.pos, …)` throws.
19
+
20
+ The third argument is a damping factor in `[0 .. 1]` — lower is smoother and
21
+ laggier, `1` snaps. Omit it for a hard lock (it defaults to `1`). It is
22
+ frame-rate independent since 19.7. `AXIS.HORIZONTAL` / `AXIS.VERTICAL` /
23
+ `AXIS.BOTH` / `AXIS.NONE`.
24
+
25
+ Call it from the followed entity's `onActivateEvent`, using `this.parentApp`.
26
+
27
+ ## Bounds are the thing people forget
28
+
29
+ The camera's bounds default to the **renderer size**, so it cannot scroll until
30
+ you widen them to the world:
31
+
32
+ ```js
33
+ app.viewport.setBounds(0, 0, mapWidth, mapHeight);
34
+ ```
35
+
36
+ Symptom: `follow()` is called, looks right, and the view never moves. Note a
37
+ canvas resize resets the default camera's bounds to `(0, 0, w, h)`, so re-apply
38
+ them from a `CANVAS_ONRESIZE` handler if your game resizes. Loading a Tiled map
39
+ sets them for you from the map size.
40
+
41
+ ## Camera effects
42
+
43
+ ```js
44
+ app.viewport.shake(4, 200); // intensity, ms
45
+ app.viewport.fadeIn("#000000", 500); // fade TO black
46
+ app.viewport.fadeOut("#000000", 500); // start black, fade back to normal
47
+ app.viewport.colorMatrix.contrast(1.1).saturate(1.1);
48
+ app.viewport.addPostEffect(new VignetteEffect(app.renderer));
49
+ ```
50
+
51
+ The fade pair is named from the *colour's* point of view, not the scene's:
52
+ `fadeIn` fades the colour in (screen goes to `color`), `fadeOut` fades it out
53
+ (screen starts at `color` and clears). Both default to a 1000 ms duration and
54
+ take an optional completion callback.
55
+
56
+ `colorMatrix` (`brightness` / `contrast` / `saturate` / `hueRotate` / `sepia` /
57
+ `invertColors`, all chainable) is the cheap way to grade a whole camera: the
58
+ camera appends it as a final post-effect pass by itself whenever the matrix is
59
+ non-identity, and drops it again after the frame. It is **not** shader-free —
60
+ it is a `ColorMatrixEffect`, i.e. a `ShaderEffect`, so like every post effect it
61
+ is inert on the Canvas renderer.
62
+
63
+ ## Coordinate conversion
64
+
65
+ ```js
66
+ app.viewport.worldToLocal(x, y); // world → screen
67
+ app.viewport.localToWorld(x, y); // screen → world
68
+ ```
69
+
70
+ Both return a `Vector2d`; with no third argument that vector comes from the
71
+ shared pool, so in a per-frame path pass your own `v` as the third argument, or
72
+ release it with `getPool("vector2d").release(v)`.
73
+
74
+ Pointer events already carry every frame of reference: `gameWorldX`/`gameWorldY`
75
+ (world), `gameScreenX`/`gameScreenY` (screen), `gameLocalX`/`gameLocalY`
76
+ (relative to the registered region's container) and `gameX`/`gameY`, which is
77
+ whichever of world/screen matches the region — screen for a `floating` region,
78
+ world otherwise. Use the screen pair for anything that *moves* the camera, or
79
+ the drag feeds back on itself.
80
+
81
+ ## Secondary cameras
82
+
83
+ Cameras are first-class — a minimap is a second camera with `autoResize = false`
84
+ and an overridden `postDraw`:
85
+
86
+ ```js
87
+ this.cameras.set("minimap", new MinimapCamera());
88
+ ```
89
+
90
+ ## Immediate-mode drawing
91
+
92
+ Inside a custom `draw(renderer)` you can draw shapes directly. Remember the two
93
+ renderable rules: read `this.pos`, and zero the anchor.
94
+
95
+ ```js
96
+ draw(renderer) {
97
+ renderer.setColor("#ff8800");
98
+ renderer.fillRect(this.pos.x, this.pos.y, this.width, this.height);
99
+
100
+ renderer.setColor("#ffffff");
101
+ renderer.lineWidth = 2; // property, not setLineWidth()
102
+ renderer.stroke(new Line(0, 0, [/* … */]));
103
+ }
104
+ ```
105
+
106
+ Geometry available: `Rect`, `RoundRect`, `Ellipse`, `Polygon`, `Line`, `Point`
107
+ (and a `Bounds`). `renderer.fill(shape)` and `renderer.stroke(shape)` take any
108
+ of them and throw `Invalid geometry for fill/stroke` for anything else.
109
+
110
+ `Gradient` gives linear and radial fills, built with
111
+ `renderer.createLinearGradient(x0, y0, x1, y1)` /
112
+ `createRadialGradient(x0, y0, r0, x1, y1, r1)` and then handed to
113
+ `renderer.setColor(gradient)`. `NoiseTexture2d` generates cellular and simplex
114
+ noise — including seamless tiling and `asNormalMap` — so you rarely need to ship
115
+ noise PNGs.
116
+
117
+ ## Clipping and masking
118
+
119
+ ```js
120
+ renderer.clipRect(x, y, w, h); // rectangular clip
121
+ renderable.mask = new Ellipse(/* … */); // shape mask on an object
122
+ ```
123
+
124
+ Note that a `Container` with `clipping` enabled clips **after** its own
125
+ `translate(pos)`, in container-local coordinates — `clipRect(0, 0, width,
126
+ height)`. It also silently does nothing unless the container is non-root and has
127
+ a finite `width`/`height`: a `Container` built with no explicit size is
128
+ `Infinity × Infinity`, and `clipping = true` on it is a no-op.
129
+
130
+ ## Bake once, draw many
131
+
132
+ When you would otherwise issue hundreds of identical draws every frame, render
133
+ them once into a `CanvasRenderTarget` and draw the result:
134
+
135
+ ```js
136
+ const rt = new CanvasRenderTarget(width, height, { context: "2d" });
137
+ const ctx = rt.context; // a CanvasRenderingContext2D — draw into it once…
138
+ // then draw the backing canvas, not the target object:
139
+ renderer.drawImage(rt.canvas, x, y);
140
+ // or wrap it: new Sprite(0, 0, { image: rt.canvas })
141
+ ```
142
+
143
+ Pass `rt.canvas`, never `rt` — a `CanvasRenderTarget` is a wrapper, not a
144
+ drawable, and the engine's own bakes (the particle emitter's default texture)
145
+ hand `.canvas` on for exactly that reason.
146
+
147
+ This is the sanctioned performance idiom before reaching for a custom shader.
148
+
149
+ `renderer.toFrameTexture()` is the related tool for capturing the *current frame*
150
+ into a texture for a shader to sample.
151
+
152
+ ## Symptom → cause
153
+
154
+ | symptom | cause |
155
+ |---|---|
156
+ | `follow()` set but the view never moves | viewport bounds still at renderer size |
157
+ | `invalid target for me.Camera2d.follow` | passed `player.pos` — pass the renderable, a `Vector2d` or a `Vector3d` |
158
+ | camera drag jumps around | using world (`gameWorldX`) instead of screen (`gameScreenX`) coordinates |
159
+ | `colorMatrix` does nothing | it is a shader effect — inert on the Canvas renderer |
160
+ | custom drawing appears at the origin | `draw()` not reading `this.pos` |
161
+ | custom drawing offset by half its size | centred `anchorPoint` not zeroed |
162
+ | `setLineWidth` is not a function | it is the `lineWidth` property |
163
+ | clipping in the wrong place inside a container | container clips in local coordinates, after its own translate |
164
+ | `container.clipping = true` does nothing | container has no explicit size, so `width`/`height` are `Infinity` |
165
+ | baked target draws nothing / throws | passed the `CanvasRenderTarget`, not its `.canvas` |
166
+ | frame rate drops with many static draws | bake into a `CanvasRenderTarget` instead |
167
+
168
+ ## Related skills
169
+
170
+ - `melonjs-renderables` — the `draw()` contract and anchors
171
+ - `melonjs-3d` — `Camera3d`, which extends `Camera2d` and keeps all of the above