lecodes-cli 0.18.2 → 0.19.2

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 (224) hide show
  1. package/dist/index.js +1866 -301
  2. package/package.json +14 -12
  3. package/runtime/scene-harness.json +1 -1
  4. package/runtime/sdk-types.json +1 -1
  5. package/src/api.ts +302 -0
  6. package/src/browserAuth.ts +87 -0
  7. package/src/cmgenTool.ts +104 -0
  8. package/src/commands/app.ts +891 -0
  9. package/src/commands/appAndroid.ts +603 -0
  10. package/src/commands/appDesktop.ts +326 -0
  11. package/src/commands/appDesktopMac.ts +470 -0
  12. package/src/commands/appIcon.ts +187 -0
  13. package/src/commands/appShared.ts +448 -0
  14. package/src/commands/appTemplates.ts +883 -0
  15. package/src/commands/appTemplatesAndroid.ts +599 -0
  16. package/src/commands/appTemplatesGradlew.ts +9 -0
  17. package/src/commands/assets.ts +28 -0
  18. package/src/commands/clone.ts +60 -0
  19. package/src/commands/compile.ts +253 -0
  20. package/src/commands/create.ts +59 -0
  21. package/src/commands/design.ts +863 -0
  22. package/src/commands/designTemplates.ts +11 -0
  23. package/src/commands/desktop.ts +122 -0
  24. package/src/commands/dev.ts +214 -0
  25. package/src/commands/diff.ts +59 -0
  26. package/src/commands/init.ts +191 -0
  27. package/src/commands/install.ts +198 -0
  28. package/src/commands/lightmap.ts +290 -0
  29. package/src/commands/link.ts +147 -0
  30. package/src/commands/login.ts +24 -0
  31. package/src/commands/navmesh.ts +225 -0
  32. package/src/commands/pn.ts +305 -0
  33. package/src/commands/projectTemplates.ts +129 -0
  34. package/src/commands/pull.ts +109 -0
  35. package/src/commands/push.ts +173 -0
  36. package/src/commands/render.ts +414 -0
  37. package/src/commands/scene.ts +148 -0
  38. package/src/commands/shaders.ts +157 -0
  39. package/src/commands/status.ts +29 -0
  40. package/src/commands/test.ts +370 -0
  41. package/src/commands/thumbs.ts +178 -0
  42. package/src/commands/types.ts +87 -0
  43. package/src/commands/update.ts +190 -0
  44. package/src/compile/assetIcons.ts +214 -0
  45. package/src/compile/collect.ts +46 -0
  46. package/src/compile/collectLocal.ts +37 -0
  47. package/src/compile/designCompile.ts +104 -0
  48. package/src/compile/fonts.ts +156 -0
  49. package/src/compile/headlessBundle.ts +129 -0
  50. package/src/compile/nativeStack.ts +21 -0
  51. package/src/compile/projectCompile.ts +109 -0
  52. package/src/compile/sceneCompile.ts +113 -0
  53. package/src/compile/screenEntry.ts +127 -0
  54. package/src/compile/shaders.ts +245 -0
  55. package/src/config.ts +42 -0
  56. package/src/designMeta.ts +35 -0
  57. package/src/desktopRenderer.ts +532 -0
  58. package/src/desktopScript.ts +276 -0
  59. package/src/dev/clientTemplates.ts +160 -0
  60. package/src/dev/devServer.ts +292 -0
  61. package/src/dev/wsServer.ts +144 -0
  62. package/src/distRoot.ts +20 -0
  63. package/src/ignore.ts +163 -0
  64. package/src/index.ts +491 -0
  65. package/src/lecodes-3d-editor.d.ts +41 -0
  66. package/src/lecodes-assets.d.ts +7 -0
  67. package/src/lecodes-design.d.ts +191 -0
  68. package/src/lecodes-renderer.d.ts +131 -0
  69. package/src/localFiles.ts +144 -0
  70. package/src/manifest.ts +46 -0
  71. package/src/matcTool.ts +137 -0
  72. package/src/peers.ts +40 -0
  73. package/src/project.ts +61 -0
  74. package/src/projectEnv.ts +94 -0
  75. package/src/qrcode-terminal.d.ts +9 -0
  76. package/src/releases.ts +125 -0
  77. package/src/serverDiff.ts +78 -0
  78. package/src/textDiff.ts +103 -0
  79. package/src/types.ts +0 -0
  80. package/src/util.ts +146 -0
  81. package/runtime/sdk/animate/animate.ts +0 -238
  82. package/runtime/sdk/animate/bezier.ts +0 -138
  83. package/runtime/sdk/animate/easings.ts +0 -126
  84. package/runtime/sdk/canvas/Canvas.ts +0 -305
  85. package/runtime/sdk/core/Aspect.ts +0 -512
  86. package/runtime/sdk/core/InspectorUI.ts +0 -212
  87. package/runtime/sdk/core/color.ts +0 -66
  88. package/runtime/sdk/core/compWrite.ts +0 -42
  89. package/runtime/sdk/core/events.ts +0 -38
  90. package/runtime/sdk/core/fields.ts +0 -120
  91. package/runtime/sdk/core/registry.ts +0 -23
  92. package/runtime/sdk/core/signals.ts +0 -277
  93. package/runtime/sdk/core/time.ts +0 -81
  94. package/runtime/sdk/g2/Camera2D.ts +0 -40
  95. package/runtime/sdk/g2/CharacterController2D.ts +0 -276
  96. package/runtime/sdk/g2/Node2D.ts +0 -267
  97. package/runtime/sdk/g2/OneWay2D.ts +0 -66
  98. package/runtime/sdk/g2/Physics2D.ts +0 -346
  99. package/runtime/sdk/g2/Scene2D.ts +0 -209
  100. package/runtime/sdk/g2/Shape2D.ts +0 -259
  101. package/runtime/sdk/g2/Sprite.ts +0 -89
  102. package/runtime/sdk/g2/SpriteAnimation.ts +0 -171
  103. package/runtime/sdk/g2/SpriteSheet.ts +0 -166
  104. package/runtime/sdk/g2/Texture2D.ts +0 -47
  105. package/runtime/sdk/g2/Tilemap.ts +0 -41
  106. package/runtime/sdk/g2/Tileset.ts +0 -71
  107. package/runtime/sdk/g2/Trigger2D.ts +0 -77
  108. package/runtime/sdk/g2/autotile.ts +0 -433
  109. package/runtime/sdk/g2/cells.ts +0 -91
  110. package/runtime/sdk/g2/defineScene2d.ts +0 -381
  111. package/runtime/sdk/g2/groups2d.ts +0 -106
  112. package/runtime/sdk/g2/loop.ts +0 -50
  113. package/runtime/sdk/g2/scenarios2d.ts +0 -69
  114. package/runtime/sdk/g2/touch.ts +0 -83
  115. package/runtime/sdk/gl/Camera.ts +0 -160
  116. package/runtime/sdk/gl/CameraPlace.ts +0 -52
  117. package/runtime/sdk/gl/CharacterController.ts +0 -238
  118. package/runtime/sdk/gl/Gearbox.ts +0 -212
  119. package/runtime/sdk/gl/Geometry.ts +0 -279
  120. package/runtime/sdk/gl/IK.ts +0 -193
  121. package/runtime/sdk/gl/InstancedMesh.ts +0 -132
  122. package/runtime/sdk/gl/Light.ts +0 -99
  123. package/runtime/sdk/gl/Lightmap.ts +0 -179
  124. package/runtime/sdk/gl/Material.ts +0 -245
  125. package/runtime/sdk/gl/Mesh.ts +0 -83
  126. package/runtime/sdk/gl/Model.ts +0 -64
  127. package/runtime/sdk/gl/Node.ts +0 -350
  128. package/runtime/sdk/gl/Noise.ts +0 -30
  129. package/runtime/sdk/gl/Particles.ts +0 -676
  130. package/runtime/sdk/gl/Physics.ts +0 -222
  131. package/runtime/sdk/gl/Plane.ts +0 -53
  132. package/runtime/sdk/gl/Ray.ts +0 -16
  133. package/runtime/sdk/gl/Scene.ts +0 -479
  134. package/runtime/sdk/gl/Shape.ts +0 -377
  135. package/runtime/sdk/gl/Texture.ts +0 -46
  136. package/runtime/sdk/gl/Trigger.ts +0 -45
  137. package/runtime/sdk/gl/Vehicle.ts +0 -473
  138. package/runtime/sdk/gl/Wheel.ts +0 -240
  139. package/runtime/sdk/gl/animation/AnimationClip.ts +0 -204
  140. package/runtime/sdk/gl/animation/Animator.ts +0 -87
  141. package/runtime/sdk/gl/animation/Layer.ts +0 -29
  142. package/runtime/sdk/gl/animation/Loop.ts +0 -25
  143. package/runtime/sdk/gl/animation/Playback.ts +0 -43
  144. package/runtime/sdk/gl/animation/core.ts +0 -294
  145. package/runtime/sdk/gl/controls.ts +0 -95
  146. package/runtime/sdk/gl/physicsEvents.ts +0 -20
  147. package/runtime/sdk/gl/scenarios.ts +0 -291
  148. package/runtime/sdk/gl/state.ts +0 -6
  149. package/runtime/sdk/gl/touch.ts +0 -68
  150. package/runtime/sdk/inject.ts +0 -186
  151. package/runtime/sdk/math/Mathf.ts +0 -118
  152. package/runtime/sdk/math/mat4.ts +0 -278
  153. package/runtime/sdk/math/quat.ts +0 -232
  154. package/runtime/sdk/math/vec.ts +0 -255
  155. package/runtime/sdk/plugins/camera.ts +0 -81
  156. package/runtime/sdk/plugins/geolocation.ts +0 -123
  157. package/runtime/sdk/plugins/oauth.ts +0 -61
  158. package/runtime/sdk/plugins/permission.ts +0 -7
  159. package/runtime/sdk/plugins/push.ts +0 -132
  160. package/runtime/sdk/plugins/qr.ts +0 -73
  161. package/runtime/sdk/plugins/service.ts +0 -47
  162. package/runtime/sdk/runtime/app.ts +0 -101
  163. package/runtime/sdk/runtime/appEvents.ts +0 -54
  164. package/runtime/sdk/runtime/channel.ts +0 -61
  165. package/runtime/sdk/runtime/clipboard.ts +0 -20
  166. package/runtime/sdk/runtime/datetime.ts +0 -329
  167. package/runtime/sdk/runtime/device.ts +0 -293
  168. package/runtime/sdk/runtime/fetch.ts +0 -77
  169. package/runtime/sdk/runtime/files.ts +0 -23
  170. package/runtime/sdk/runtime/input.ts +0 -175
  171. package/runtime/sdk/runtime/media.ts +0 -111
  172. package/runtime/sdk/runtime/misc.ts +0 -16
  173. package/runtime/sdk/runtime/net.ts +0 -36
  174. package/runtime/sdk/runtime/rpc.ts +0 -218
  175. package/runtime/sdk/runtime/service.ts +0 -83
  176. package/runtime/sdk/runtime/share.ts +0 -9
  177. package/runtime/sdk/runtime/storage.ts +0 -13
  178. package/runtime/sdk/runtime/touch.ts +0 -76
  179. package/runtime/sdk/scene/defineScene.ts +0 -1227
  180. package/runtime/sdk/scene/editorPlugins.ts +0 -92
  181. package/runtime/sdk/scene/gizmos.ts +0 -148
  182. package/runtime/sdk/scene/grammar.ts +0 -120
  183. package/runtime/sdk/scene/material.ts +0 -188
  184. package/runtime/sdk/server/auth/appConfig.ts +0 -12
  185. package/runtime/sdk/server/auth/global.ts +0 -80
  186. package/runtime/sdk/server/auth/host.ts +0 -318
  187. package/runtime/sdk/server/auth/models.ts +0 -83
  188. package/runtime/sdk/server/auth/types.ts +0 -50
  189. package/runtime/sdk/server/channel.ts +0 -56
  190. package/runtime/sdk/server/context.ts +0 -36
  191. package/runtime/sdk/server/db/defineDb.ts +0 -237
  192. package/runtime/sdk/server/db/fields.ts +0 -132
  193. package/runtime/sdk/server/db/httpTransport.ts +0 -93
  194. package/runtime/sdk/server/db/index.ts +0 -7
  195. package/runtime/sdk/server/db/marci/query.ts +0 -412
  196. package/runtime/sdk/server/db/types.ts +0 -202
  197. package/runtime/sdk/server/errors.ts +0 -12
  198. package/runtime/sdk/server/host.ts +0 -74
  199. package/runtime/sdk/server/inject.ts +0 -13
  200. package/runtime/sdk/server/runtime.ts +0 -133
  201. package/runtime/sdk/server/validate.ts +0 -87
  202. package/runtime/sdk/ui/NativeView.ts +0 -142
  203. package/runtime/sdk/ui/UI.ts +0 -39
  204. package/runtime/sdk/ui/UIBottomSheet.ts +0 -139
  205. package/runtime/sdk/ui/UIButton.ts +0 -101
  206. package/runtime/sdk/ui/UIContainer.ts +0 -60
  207. package/runtime/sdk/ui/UIImage.ts +0 -83
  208. package/runtime/sdk/ui/UIInput.ts +0 -185
  209. package/runtime/sdk/ui/UIModal.ts +0 -139
  210. package/runtime/sdk/ui/UINode.ts +0 -826
  211. package/runtime/sdk/ui/UIPager.ts +0 -362
  212. package/runtime/sdk/ui/UIPopover.ts +0 -100
  213. package/runtime/sdk/ui/UIScreen.ts +0 -123
  214. package/runtime/sdk/ui/UIScrollable.ts +0 -87
  215. package/runtime/sdk/ui/UISpacer.ts +0 -14
  216. package/runtime/sdk/ui/UITabs.ts +0 -236
  217. package/runtime/sdk/ui/UIText.ts +0 -51
  218. package/runtime/sdk/ui/UIVideo.ts +0 -88
  219. package/runtime/sdk/ui/UIVirtualizedList.ts +0 -241
  220. package/runtime/sdk/ui/UIWidget.ts +0 -127
  221. package/runtime/sdk/ui/fonts.ts +0 -13
  222. package/runtime/sdk/ui/presentable.ts +0 -117
  223. package/runtime/sdk/ui/router.ts +0 -132
  224. package/runtime/sdk/ui/theme.ts +0 -84
@@ -1,305 +0,0 @@
1
- // A core 2D drawing surface that bakes to a texture — see docs/canvas-contract.md. It is NOT part of
2
- // creator-2d/creator-gl: the texture it produces is consumed by 2D sprites, 3D materials and UI
3
- // images alike ("a canvas is just a texture"), so world/label text and arbitrary 2D graphics are
4
- // drawn once, per platform, on that platform's native 2D stack (web OffscreenCanvas, iOS CoreGraphics,
5
- // Android Canvas).
6
- //
7
- // The API looks immediate but RECORDS commands into a compact buffer and flushes them to the backend
8
- // ONCE per bake (one bridge crossing, not one per lineTo). Colors and fonts are CSS strings, resolved
9
- // by the platform. Do all drawing AFTER `await registerFont(...)` — an unloaded font mis-measures.
10
- //
11
- // State (fillStyle, font, textAlign, …) is get/set like the browser; drawing calls are methods (they
12
- // return `this`, so drawing still chains). `pixelRatio` is the device-pixel multiplier (browser-style
13
- // `scale(sx, sy)` is a transform).
14
- //
15
- // const c = new Canvas(160, 40, { pixelRatio: 2 })
16
- // c.font = 'bold 24px Inter'
17
- // const w = c.measureText('Alice').width + 24
18
- // c.resize(w, 40)
19
- // c.fillStyle = '#000a'; c.roundRect(0, 0, w, 40, 12).fill()
20
- // c.fillStyle = '#fff'; c.textAlign = 'center'; c.fillText('Alice', w / 2, 27)
21
- // new Sprite({ texture: c }) // Canvas is a valid texture source everywhere
22
-
23
- import { Texture2D } from "../g2/Texture2D"
24
- import { File, type FetchResponse } from "../runtime/fetch"
25
- import type { SvgSourceValue } from "../runtime/misc"
26
-
27
- // Opcodes — MUST stay in sync with docs/canvas-contract.md.
28
- const OP = {
29
- SAVE: 0, RESTORE: 1, TRANSLATE: 2, SCALE: 3, ROTATE: 4, GLOBAL_ALPHA: 5,
30
- FILL_STYLE: 6, STROKE_STYLE: 7, LINE_WIDTH: 8, LINE_JOIN: 9, LINE_CAP: 10,
31
- FONT: 11, TEXT_ALIGN: 12, TEXT_BASELINE: 13,
32
- BEGIN_PATH: 14, CLOSE_PATH: 15, MOVE_TO: 16, LINE_TO: 17, QUADRATIC_TO: 18, BEZIER_TO: 19,
33
- ARC: 20, RECT: 21, ROUND_RECT: 22, FILL: 23, STROKE: 24,
34
- FILL_RECT: 25, STROKE_RECT: 26, CLEAR_RECT: 27, FILL_TEXT: 28, STROKE_TEXT: 29,
35
- DRAW_IMAGE: 30,
36
- } as const
37
-
38
- const ALIGN = { left: 0, center: 1, right: 2, start: 3, end: 4 } as const
39
- const BASELINE = { alphabetic: 0, top: 1, middle: 2, bottom: 3, hanging: 4, ideographic: 5 } as const
40
- const JOIN = { miter: 0, round: 1, bevel: 2 } as const
41
- const CAP = { butt: 0, round: 1, square: 2 } as const
42
-
43
- export type TextAlign = keyof typeof ALIGN
44
- export type TextBaseline = keyof typeof BASELINE
45
- export type LineJoin = keyof typeof JOIN
46
- export type LineCap = keyof typeof CAP
47
- export interface CanvasTextMetrics { width: number, ascent: number, descent: number }
48
-
49
- export class Canvas {
50
- /** Brand so `Sprite`/`UIImage`/`Material` can accept a Canvas anywhere a texture source is expected. */
51
- readonly __canvas = true as const
52
-
53
- width: number
54
- height: number
55
- /** Device-pixel multiplier (draw at 2 for crisp retina text). Applied by the backend. */
56
- pixelRatio: number
57
-
58
- private _cmd: number[] = []
59
- private _refs: string[] = []
60
- // Shadowed drawing state so the get/set accessors read back the current value (like the browser).
61
- private _globalAlpha = 1
62
- private _fillStyle = "#000000"
63
- private _strokeStyle = "#000000"
64
- private _lineWidth = 1
65
- private _lineJoin: LineJoin = "miter"
66
- private _lineCap: LineCap = "butt"
67
- private _font = "10px sans-serif"
68
- private _textAlign: TextAlign = "start"
69
- private _textBaseline: TextBaseline = "alphabetic"
70
- private _surfaceId = 0
71
- private _tex2d?: Texture2D // cached 2D texture, re-uploaded on update()
72
- /** @internal — cached 3D (creator-gl) texture id, set by gl/Texture.fromCanvas; re-uploaded on
73
- * update(). Kept as a bare id (not a Texture import) so a 2D-only canvas never pulls the 3D engine. */
74
- _tex3dId = 0
75
- // Set once this canvas is baked as a UIImage source (see _bakeSurfaceForUI). When true, update()
76
- // also refreshes the on-screen image node(s) via _creatorUI — the UI consumer the 2D/3D texture
77
- // hooks below don't cover. Stays a bare flag so a non-UI canvas never references the UI bridge.
78
- private _uiBound = false
79
-
80
- constructor(width: number, height: number, opts: { pixelRatio?: number } = {}) {
81
- this.width = width
82
- this.height = height
83
- this.pixelRatio = opts.pixelRatio ?? 1
84
- }
85
-
86
- private _ref(s: string): number {
87
- const i = this._refs.indexOf(s)
88
- if (i >= 0) return i
89
- this._refs.push(s)
90
- return this._refs.length - 1
91
- }
92
-
93
- /** Discard the recorded drawing to start a new one (for dynamic canvases). */
94
- reset(): this { this._cmd.length = 0; this._refs.length = 0; return this }
95
- /** Change the surface size; takes effect on the next bake. */
96
- resize(width: number, height: number): this { this.width = width; this.height = height; return this }
97
-
98
- // --- transforms & state stack (actions; chainable) ---
99
- save(): this { this._cmd.push(OP.SAVE); return this }
100
- restore(): this { this._cmd.push(OP.RESTORE); return this }
101
- translate(x: number, y: number): this { this._cmd.push(OP.TRANSLATE, x, y); return this }
102
- scale(sx: number, sy: number): this { this._cmd.push(OP.SCALE, sx, sy); return this }
103
- rotate(rad: number): this { this._cmd.push(OP.ROTATE, rad); return this }
104
-
105
- // --- drawing state (get/set like the browser; colors/fonts are CSS strings resolved by the platform).
106
- // The setter records the state op AND shadows the value so the getter reads it back.
107
- get globalAlpha(): number { return this._globalAlpha }
108
- set globalAlpha(a: number) { this._globalAlpha = a; this._cmd.push(OP.GLOBAL_ALPHA, a) }
109
- get fillStyle(): string { return this._fillStyle }
110
- set fillStyle(color: string) { this._fillStyle = color; this._cmd.push(OP.FILL_STYLE, this._ref(color)) }
111
- get strokeStyle(): string { return this._strokeStyle }
112
- set strokeStyle(color: string) { this._strokeStyle = color; this._cmd.push(OP.STROKE_STYLE, this._ref(color)) }
113
- get lineWidth(): number { return this._lineWidth }
114
- set lineWidth(w: number) { this._lineWidth = w; this._cmd.push(OP.LINE_WIDTH, w) }
115
- get lineJoin(): LineJoin { return this._lineJoin }
116
- set lineJoin(j: LineJoin) { this._lineJoin = j; this._cmd.push(OP.LINE_JOIN, JOIN[j]) }
117
- get lineCap(): LineCap { return this._lineCap }
118
- set lineCap(c: LineCap) { this._lineCap = c; this._cmd.push(OP.LINE_CAP, CAP[c]) }
119
- get font(): string { return this._font }
120
- set font(f: string) { this._font = f; this._cmd.push(OP.FONT, this._ref(f)) }
121
- get textAlign(): TextAlign { return this._textAlign }
122
- set textAlign(a: TextAlign) { this._textAlign = a; this._cmd.push(OP.TEXT_ALIGN, ALIGN[a]) }
123
- get textBaseline(): TextBaseline { return this._textBaseline }
124
- set textBaseline(b: TextBaseline) { this._textBaseline = b; this._cmd.push(OP.TEXT_BASELINE, BASELINE[b]) }
125
-
126
- // --- paths ---
127
- beginPath(): this { this._cmd.push(OP.BEGIN_PATH); return this }
128
- closePath(): this { this._cmd.push(OP.CLOSE_PATH); return this }
129
- moveTo(x: number, y: number): this { this._cmd.push(OP.MOVE_TO, x, y); return this }
130
- lineTo(x: number, y: number): this { this._cmd.push(OP.LINE_TO, x, y); return this }
131
- quadraticCurveTo(cx: number, cy: number, x: number, y: number): this { this._cmd.push(OP.QUADRATIC_TO, cx, cy, x, y); return this }
132
- bezierCurveTo(c1x: number, c1y: number, c2x: number, c2y: number, x: number, y: number): this {
133
- this._cmd.push(OP.BEZIER_TO, c1x, c1y, c2x, c2y, x, y); return this
134
- }
135
- arc(x: number, y: number, r: number, a0: number, a1: number, ccw = false): this {
136
- this._cmd.push(OP.ARC, x, y, r, a0, a1, ccw ? 1 : 0); return this
137
- }
138
- rect(x: number, y: number, w: number, h: number): this { this._cmd.push(OP.RECT, x, y, w, h); return this }
139
- roundRect(x: number, y: number, w: number, h: number, r: number): this { this._cmd.push(OP.ROUND_RECT, x, y, w, h, r); return this }
140
- fill(evenOdd = false): this { this._cmd.push(OP.FILL, evenOdd ? 1 : 0); return this }
141
- stroke(): this { this._cmd.push(OP.STROKE); return this }
142
-
143
- // --- shape/text sugar ---
144
- fillRect(x: number, y: number, w: number, h: number): this { this._cmd.push(OP.FILL_RECT, x, y, w, h); return this }
145
- strokeRect(x: number, y: number, w: number, h: number): this { this._cmd.push(OP.STROKE_RECT, x, y, w, h); return this }
146
- clearRect(x: number, y: number, w: number, h: number): this { this._cmd.push(OP.CLEAR_RECT, x, y, w, h); return this }
147
- fillText(text: string, x: number, y: number, maxWidth = 0): this { this._cmd.push(OP.FILL_TEXT, this._ref(text), x, y, maxWidth); return this }
148
- strokeText(text: string, x: number, y: number, maxWidth = 0): this { this._cmd.push(OP.STROKE_TEXT, this._ref(text), x, y, maxWidth); return this }
149
-
150
- // --- images ---
151
- /** Blit a `Bitmap` (a snapshot from `toBitmap()`) onto this canvas. All coordinates are LOGICAL —
152
- * including the source sub-rect in the 9-arg form (it's scaled by the bitmap's pixelRatio here).
153
- * Records a DRAW_IMAGE op, so the blit runs at bake time: keep the Bitmap alive until the next
154
- * `update()`/`toBitmap()`/`texture()`. Three browser-style forms:
155
- * drawImage(bmp, dx, dy) — whole bitmap at its natural size
156
- * drawImage(bmp, dx, dy, dw, dh) — whole bitmap, scaled into dw×dh
157
- * drawImage(bmp, sx, sy, sw, sh, dx, dy, dw, dh) — a sub-rect of the bitmap, scaled into dw×dh */
158
- drawImage(img: Bitmap, dx: number, dy: number): this
159
- drawImage(img: Bitmap, dx: number, dy: number, dw: number, dh: number): this
160
- drawImage(img: Bitmap, sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number): this
161
- drawImage(img: Bitmap, a: number, b: number, c?: number, d?: number, e?: number, f?: number, g?: number, h?: number): this {
162
- const pr = img.pixelRatio
163
- let sx: number, sy: number, sw: number, sh: number, dx: number, dy: number, dw: number, dh: number
164
- if (e === undefined) { // (dx, dy) or (dx, dy, dw, dh): the whole bitmap
165
- sx = 0; sy = 0; sw = img.pixelWidth; sh = img.pixelHeight
166
- dx = a; dy = b; dw = c ?? img.width; dh = d ?? img.height
167
- } else { // (sx, sy, sw, sh, dx, dy, dw, dh): a sub-rect (logical → device px)
168
- sx = a * pr; sy = b * pr; sw = c! * pr; sh = d! * pr
169
- dx = e; dy = f!; dw = g!; dh = h!
170
- }
171
- this._cmd.push(OP.DRAW_IMAGE, img._surfaceId, sx, sy, sw, sh, dx, dy, dw, dh)
172
- return this
173
- }
174
-
175
- /** Decode an image source into a standalone `Bitmap` you can blit with `drawImage()` — either an
176
- * `SvgSource(...)` (rasterized) or a `FetchResponse` holding already-fetched image bytes (decoded).
177
- * This is async because turning SVG markup / encoded bytes into pixels needs a decode step (on the
178
- * web, `createImageBitmap` / an `<img>` load); `await` it ONCE, then `drawImage(bitmap, …)` is
179
- * synchronous and the Bitmap is reusable. The returned Bitmap owns a native surface at the image's
180
- * natural pixel size (`pixelRatio` 1) — `destroy()` it when done. Typical use:
181
- * const logo = await canvas.loadImage(SvgSource(svg)) // or: await fetch(url) → the response
182
- * canvas.drawImage(logo, 0, 0) // sync, blit as often as you like */
183
- loadImage(source: SvgSourceValue | FetchResponse): Promise<Bitmap> {
184
- // Discriminate without touching FetchResponse internals: only an SvgSource carries `.svg`.
185
- const isSvg = typeof (source as SvgSourceValue).svg === "string"
186
- const svg = isSvg ? (source as SvgSourceValue).svg : ""
187
- const bufferId = isSvg ? -1 : (source as any)._id // <0 = the SVG path; else the host buffer id
188
- return new Promise<Bitmap>((res, rej) => {
189
- _creatorCanvas.loadImage(svg, bufferId, (surfaceId, w, h) => res(new Bitmap(surfaceId, w, h, 1)), rej)
190
- })
191
- }
192
-
193
- /** Measure `text` in the current font on THIS platform. The one synchronous round-trip. */
194
- measureText(text: string): CanvasTextMetrics {
195
- const o = _creatorCanvas.measureText(text, this._font)
196
- return { width: o[0], ascent: o[1], descent: o[2] }
197
- }
198
-
199
- /** @internal — rasterize the recorded commands into the surface; returns the surface id. */
200
- _bakeSurface(): number {
201
- const buf = new Float32Array(this._cmd)
202
- this._surfaceId = _creatorCanvas.rasterize(this._surfaceId, this.width, this.height, this.pixelRatio, buf, buf.length, this._refs)
203
- return this._surfaceId
204
- }
205
-
206
- /** @internal — bake for a `UIImage(canvas)` source: same as `_bakeSurface()` but also flags this
207
- * canvas UI-bound, so a later `update()` re-reads the re-baked pixels into the on-screen image
208
- * node(s) — not just the 2D/3D engine textures. Called by UIImage when a Canvas is assigned as src. */
209
- _bakeSurfaceForUI(): number {
210
- this._uiBound = true
211
- return this._bakeSurface()
212
- }
213
-
214
- /** Bake and return the 2D texture (created once, then cached). */
215
- texture(): Texture2D {
216
- if (!this._tex2d) this._tex2d = Texture2D.fromCanvas(this)
217
- return this._tex2d
218
- }
219
-
220
- /** Re-rasterize and re-upload to every texture this canvas produced (call after redrawing) — the
221
- * cached 2D and/or 3D texture, whichever engines this canvas was baked into. */
222
- update(): this {
223
- const surfaceId = this._bakeSurface()
224
- if (this._tex2d) _creator2d.updateTextureFromCanvas(this._tex2d.id, surfaceId)
225
- if (this._tex3dId) _creator.updateTextureFromCanvas(this._tex3dId, surfaceId)
226
- // UIImage(canvas): repaint any live image node showing this surface with the re-baked pixels —
227
- // draw-only, no relayout (the surface keeps its id and size). Feature-detected so an older host
228
- // without the hook just no-ops (fall back to re-assigning UIImage.src). See docs/canvas-contract.md.
229
- if (this._uiBound) _creatorUI.refreshCanvasSurface?.(surfaceId)
230
- return this
231
- }
232
-
233
- /** Copy the current pixels into a standalone, immutable `Bitmap` — independent of this canvas, so
234
- * later drawing/`reset()` never touches it. Draw it back with `drawImage()`, export it with
235
- * `toDataURL()`, or use it to "flatten" an ever-growing drawing so re-bakes stay O(1):
236
- * const snap = canvas.toBitmap()
237
- * canvas.reset(); canvas.drawImage(snap, 0, 0) // one op replaces the whole history
238
- * // …keep drawing on top; snap.destroy() once it's been drawn in. */
239
- toBitmap(): Bitmap {
240
- return new Bitmap(_creatorCanvas.snapshot(this._bakeSurface()), this.width, this.height, this.pixelRatio)
241
- }
242
-
243
- /** Encode the current pixels to an image `File` — 'image/png' (default) or 'image/jpeg'. The File
244
- * is an opaque host buffer handle (`name`/`size` only): append it to a FormData to upload, load it
245
- * as a texture (`Texture2D.load(file)`), or hand it to a share/save sheet. */
246
- toFile(name = "canvas.png", type = "image/png"): Promise<File> {
247
- const surfaceId = this._bakeSurface()
248
- return new Promise<File>((res, rej) => {
249
- _creatorCanvas.toFile(surfaceId, type, (bufferId, size) => res(new File(bufferId, name, size)), rej)
250
- })
251
- }
252
-
253
- destroy(): void {
254
- if (this._surfaceId) _creatorCanvas.destroySurface(this._surfaceId)
255
- this._tex2d?.destroy()
256
- // The 3D texture (if any) is owned by creator-gl's engine and freed at its teardown — no per-texture
257
- // destroy in the 3D API — so we just drop our cached id here.
258
- this._surfaceId = 0
259
- this._tex2d = undefined
260
- this._tex3dId = 0
261
- }
262
- }
263
-
264
- /** Duck-type guard so consumers can accept a Canvas anywhere a texture source is expected. */
265
- export const isCanvas = (v: unknown): v is Canvas =>
266
- typeof v === "object" && v !== null && (v as { __canvas?: unknown }).__canvas === true
267
-
268
- /** An immutable RGBA8 snapshot of a canvas, produced by `Canvas.toBitmap()`. It owns its own baked
269
- * surface (a copy), so the source canvas can keep drawing or `reset()` without affecting it. Use it
270
- * to stamp/blit back (`canvas.drawImage(bitmap, …)`), to export (`bitmap.toDataURL()`), or as a
271
- * cheap "layer" you composite. Call `destroy()` when done — it holds a native surface. */
272
- export class Bitmap {
273
- readonly __bitmap = true as const
274
- /** @internal — a standalone `_creatorCanvas` surface (RGBA8), independent of any Canvas. */
275
- readonly _surfaceId: number
276
- /** Logical width, matching the Canvas it was captured from (pixelRatio is crispness, not size). */
277
- readonly width: number
278
- readonly height: number
279
- readonly pixelRatio: number
280
-
281
- constructor(surfaceId: number, width: number, height: number, pixelRatio: number) {
282
- this._surfaceId = surfaceId
283
- this.width = width
284
- this.height = height
285
- this.pixelRatio = pixelRatio
286
- }
287
-
288
- /** Device-pixel dimensions of the stored bitmap (`width * pixelRatio`, rounded). */
289
- get pixelWidth(): number { return Math.round(this.width * this.pixelRatio) }
290
- get pixelHeight(): number { return Math.round(this.height * this.pixelRatio) }
291
-
292
- /** Encode to an image `File` — 'image/png' (default) or 'image/jpeg'. See `Canvas.toFile`. */
293
- toFile(name = "canvas.png", type = "image/png"): Promise<File> {
294
- return new Promise<File>((res, rej) => {
295
- _creatorCanvas.toFile(this._surfaceId, type, (bufferId, size) => res(new File(bufferId, name, size)), rej)
296
- })
297
- }
298
-
299
- /** Free the native surface. The bitmap must not be drawn or encoded afterwards. */
300
- destroy(): void { _creatorCanvas.destroySurface(this._surfaceId) }
301
- }
302
-
303
- /** Duck-type guard for a Bitmap (see `isCanvas`). */
304
- export const isBitmap = (v: unknown): v is Bitmap =>
305
- typeof v === "object" && v !== null && (v as { __bitmap?: unknown }).__bitmap === true