@flare-engine/render-web 0.4.11 → 0.4.13

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.
package/README.md CHANGED
@@ -75,10 +75,33 @@ renderer.beginFrame(width, height, 0, 1);
75
75
  renderer.drawAtlas(atlasImage, commands);
76
76
  const picture = renderer.endFrame();
77
77
  // endFrame() returns the backend-neutral PictureLike; cast to CanvasKit's Picture to present.
78
- if (picture) canvas.drawPicture(picture as Parameters<typeof canvas.drawPicture>[0]);
78
+ // The caller owns the picture: delete it once drawn, or each frame leaks a WASM-side object.
79
+ if (picture) {
80
+ const ckPicture = picture as Parameters<typeof canvas.drawPicture>[0];
81
+ canvas.drawPicture(ckPicture);
82
+ ckPicture.delete();
83
+ }
79
84
  web.flush();
80
85
  ```
81
86
 
87
+ ### `createWebShapeSkia` — `web-shape-skia.ts`
88
+
89
+ Adapts a `CanvasKit` instance into the `Skia` handle `@flare-engine/shapes` draws through, so `bakeShapeAtlas` can rasterize a shape atlas on web. Its `Surface.Make(width, height)` is CanvasKit's offscreen CPU surface (`kit.MakeSurface`), so the bake needs no WebGL context.
90
+
91
+ ```ts
92
+ import type { CanvasKit } from 'canvaskit-wasm';
93
+ import { createWebShapeSkia } from '@flare-engine/render-web';
94
+ import { bakeShapeAtlas, type ShapeSpec } from '@flare-engine/shapes';
95
+
96
+ declare const kit: CanvasKit;
97
+ declare const specs: ShapeSpec[];
98
+
99
+ const atlas = bakeShapeAtlas(createWebShapeSkia(kit), specs, window.devicePixelRatio);
100
+ // atlas.image / atlas.sheet draw through SpriteBatch like any other atlas.
101
+ ```
102
+
103
+ CanvasKit objects are not garbage-collected. The handle keeps track of the paints and paths it creates and frees them when a surface is disposed, which `bakeShapeAtlas` does after taking its snapshot; the atlas image is yours to `delete()` when you drop the atlas. The handle is meant for baking: calling `drawShape` with it every frame, with no surface to dispose, accumulates those objects.
104
+
82
105
  ### `createPostFxSurface` — `postfx-surface.ts`
83
106
 
84
107
  Creates a `PostFxSurface` that owns an offscreen scene canvas, a ping-pong intermediate, and an onscreen WebGL surface. At `composite()` time it runs the caller-supplied `EffectChain` through compiled SkSL shaders and blits the result to screen.
@@ -141,7 +164,7 @@ if (surface.isLost()) showStaticFallback();
141
164
 
142
165
  - `@flare-engine/postfx` is backend-neutral; `render-web` provides the CanvasKit shim (`SkiaPostFxModuleLike`) that routes `RuntimeEffect.Make` to CanvasKit.
143
166
  - The ping-pong loop is allocation-free in the hot path: the children array and rect scratch buffer are pre-allocated at surface creation.
144
- - `buildPass` is called every frame (no memoization) because `EffectChain` has no mutation signal; a future `buildPassInto` zero-alloc form in `@flare-engine/postfx` is the correct fix.
167
+ - `buildPass` is called every frame (no memoization) because `EffectChain` has no mutation signal. The runtime returns the same pass array and rewrites its uniform buffers in place, so that call allocates nothing while the set of enabled effects is unchanged.
145
168
 
146
169
  See [API spec](https://github.com/grzott/flare-engine/blob/main/docs/design/api-specs.md) and [architecture](https://github.com/grzott/flare-engine/blob/main/docs/design/architecture.md) for the full rendering pipeline.
147
170
 
package/dist/index.d.mts CHANGED
@@ -81,6 +81,75 @@ export declare function createPostFxSurface(kit: CanvasKit, canvas: HTMLCanvasEl
81
81
  * `createPostFxSurface` for that step.
82
82
  */
83
83
  export declare function createWebFrameRenderer(kit: CanvasKit): FrameRenderer;
84
+ /** Paint handle of a `WebShapeSkia`. `setStyle` takes 0 (fill) or 1 (stroke). */
85
+ interface WebShapePaint {
86
+ setColor(color: Float32Array): void;
87
+ setStyle(style: number): void;
88
+ setStrokeWidth(width: number): void;
89
+ }
90
+ /** Mutable path handle of a `WebShapeSkia`. `addArc` takes degrees. */
91
+ interface WebShapePath {
92
+ moveTo(x: number, y: number): void;
93
+ lineTo(x: number, y: number): void;
94
+ close(): void;
95
+ addArc(oval: Float32Array, startDeg: number, sweepDeg: number): void;
96
+ }
97
+ /** Canvas of a `WebShapeSurface`. `rotate` takes degrees. */
98
+ interface WebShapeCanvas {
99
+ save(): number;
100
+ restore(): void;
101
+ translate(dx: number, dy: number): void;
102
+ scale(sx: number, sy: number): void;
103
+ rotate(degrees: number, rx: number, ry: number): void;
104
+ clear(color: Float32Array): void;
105
+ drawCircle(cx: number, cy: number, r: number, paint: WebShapePaint): void;
106
+ drawRect(rect: Float32Array, paint: WebShapePaint): void;
107
+ drawPath(path: WebShapePath, paint: WebShapePaint): void;
108
+ }
109
+ /** A CPU-raster CanvasKit surface made by `WebShapeSkia.Surface.Make`. */
110
+ interface WebShapeSurface {
111
+ getCanvas(): WebShapeCanvas;
112
+ /** Flush and snapshot. The image owns its pixels and outlives `dispose()`. */
113
+ makeImageSnapshot(): Parameters<FrameRenderer['drawAtlas']>[0];
114
+ /**
115
+ * Free the surface, and every paint and path the handle has created so far.
116
+ * Neither the surface nor those may be used afterwards.
117
+ */
118
+ dispose(): void;
119
+ }
120
+ /**
121
+ * A CanvasKit-backed stand-in for the react-native-skia `Skia` handle, limited
122
+ * to what `@flare-engine/shapes` reads: `drawShape` and `bakeShapeAtlas`.
123
+ */
124
+ interface WebShapeSkia {
125
+ Surface: {
126
+ Make(width: number, height: number): WebShapeSurface | null;
127
+ };
128
+ Paint(): WebShapePaint;
129
+ Path: {
130
+ Make(): WebShapePath;
131
+ /** `null` when CanvasKit cannot parse `d`. */
132
+ MakeFromSVGString(d: string): WebShapePath | null;
133
+ };
134
+ /** An LTRB `Float32Array`, as CanvasKit takes rects. */
135
+ XYWHRect(x: number, y: number, w: number, h: number): Float32Array;
136
+ }
137
+ /**
138
+ * Create a `WebShapeSkia` over CanvasKit, so `bakeShapeAtlas` from
139
+ * `@flare-engine/shapes` can rasterize on web:
140
+ * `bakeShapeAtlas(createWebShapeSkia(kit), specs, scale)`.
141
+ *
142
+ * `Surface.Make` is CanvasKit's offscreen CPU surface (`MakeSurface`), so the
143
+ * bake needs no WebGL context. Paints are anti-aliased, as react-native-skia's
144
+ * are by default.
145
+ *
146
+ * CanvasKit objects are not garbage-collected. The handle remembers every
147
+ * paint and path it creates and frees them when a surface is disposed, which
148
+ * `bakeShapeAtlas` does after its snapshot. It is meant for baking: a
149
+ * handle used to draw shapes every frame with no surface to dispose keeps
150
+ * all of those objects alive.
151
+ */
152
+ export declare function createWebShapeSkia(kit: CanvasKit): WebShapeSkia;
84
153
  /**
85
154
  * Create a `FrameRendererModule` backed by CanvasKit.
86
155
  *
@@ -89,6 +158,12 @@ export declare function createWebFrameRenderer(kit: CanvasKit): FrameRenderer;
89
158
  *
90
159
  * CK differences handled here:
91
160
  * - `drawAtlas` takes flat Float32Arrays — the wrapper flattens object pools.
161
+ * Per-sprite colours arrive as `[r, g, b, a]` floats and leave as one packed
162
+ * ARGB `Uint32Array`, with `BlendMode.Modulate`; an uncoloured run still
163
+ * passes `null` for both.
164
+ * - `PictureRecorder().beginRecording()` returns one surface object per recorder,
165
+ * re-pointed at each new recording. Use it only between `beginRecording` and the
166
+ * matching `finishRecordingAsPicture`; do not keep it across recordings.
92
167
  * - `rotate` is forwarded verbatim (no radians→degrees conversion) to match
93
168
  * the native bridge; the dormant radians/degrees mismatch is tracked separately.
94
169
  * - `ColorFilter.MakeBlend(color, mode)` takes an EmbindEnumEntity — the
@@ -123,5 +198,5 @@ interface WebSurface {
123
198
  * Callers should fall back to a CPU surface in that case.
124
199
  */
125
200
  export declare function createWebSurface(kit: CanvasKit, canvas: HTMLCanvasElement): WebSurface | null;
126
- export type { LoadCanvasKitOptions, PostFxSurface, WebSurface };
201
+ export type { LoadCanvasKitOptions, PostFxSurface, WebShapeCanvas, WebShapePaint, WebShapePath, WebShapeSkia, WebShapeSurface, WebSurface };
127
202
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":["CKCanvas","CKCanvas"],"sources":["../src/loader.ts","../src/postfx-surface.ts","../src/web-frame-renderer.ts","../src/web-skia-module.ts","../src/web-surface.ts"],"mappings":";;;;UAQiB;;EAEf;;EAEA;;;;;;;;;;;;wBAkBc,cACd,iBACA,cAAc,2BACd,UAAS,uBACR,QAAQ;;;;;;;;;;;;;UCPM;;WAEN;;WAEA;;EAET,eAAeA;;EAEf,kBAAkBA;;EAElB,MAAM;;EAEN,UAAU,0BAA0B;;;;;;EAMpC,UAAU;;EAEV;;EAEA,OAAO,eAAe;;EAEtB;EACA;;;;;;;;;;;;wBAac,oBACd,KAAK,WACL,QAAQ,mBACR,OAAO,cACN;;;;;;;;;;;;wBCtDa,uBAAuB,KAAK,YAAY;;;;;;;;;;;;;;;;;;wBCoMxC,oBAAoB,KAAK,YAAY;;;;;;;;UCzMpC;;WAEN,QAAQ;;WAER,UAAUC;;EAEnB;;EAEA;;;;;;;;wBAoBc,iBAAiB,KAAK,WAAW,QAAQ,oBAAoB"}
1
+ {"version":3,"file":"index.d.mts","names":["CKCanvas","CKCanvas"],"sources":["../src/loader.ts","../src/postfx-surface.ts","../src/web-frame-renderer.ts","../src/web-shape-skia.ts","../src/web-skia-module.ts","../src/web-surface.ts"],"mappings":";;;;UAQiB;;EAEf;;EAEA;;;;;;;;;;;;wBAkBc,cACd,iBACA,cAAc,2BACd,UAAS,uBACR,QAAQ;;;;;;;;;;;;;UCNM;;WAEN;;WAEA;;EAET,eAAeA;;EAEf,kBAAkBA;;EAElB,MAAM;;EAEN,UAAU,0BAA0B;;;;;;EAMpC,UAAU;;EAEV;;EAEA,OAAO,eAAe;;EAEtB;EACA;;;;;;;;;;;;wBAac,oBACd,KAAK,WACL,QAAQ,mBACR,OAAO,cACN;;;;;;;;;;;;wBCvDa,uBAAuB,KAAK,YAAY;;UCLvC;EACf,SAAS,OAAO;EAChB,SAAS;EACT,eAAe;;;UAIA;EACf,OAAO,WAAW;EAClB,OAAO,WAAW;EAClB;EACA,OAAO,MAAM,cAAc,kBAAkB;;;UAI9B;EACf;EACA;EACA,UAAU,YAAY;EACtB,MAAM,YAAY;EAClB,OAAO,iBAAiB,YAAY;EACpC,MAAM,OAAO;EACb,WAAW,YAAY,YAAY,WAAW,OAAO;EACrD,SAAS,MAAM,cAAc,OAAO;EACpC,SAAS,MAAM,cAAc,OAAO;;;UAIrB;EACf,aAAa;;EAEb,qBAAqB,WAAW;;;;;EAKhC;;;;;;UAOe;EACf;IAAW,KAAK,eAAe,iBAAiB;;EAChD,SAAS;EACT;IACE,QAAQ;;IAER,kBAAkB,YAAY;;;EAGhC,SAAS,WAAW,WAAW,WAAW,YAAY;;;;;;;;;;;;;;;;;wBAoCxC,mBAAmB,KAAK,YAAY;;;;;;;;;;;;;;;;;;;;;;;;wBCuLpC,oBAAoB,KAAK,YAAY;;;;;;;;UC9QpC;;WAEN,QAAQ;;WAER,UAAUC;;EAEnB;;EAEA;;;;;;;;wBAYc,iBAAiB,KAAK,WAAW,QAAQ,oBAAoB"}
package/dist/index.d.ts CHANGED
@@ -81,6 +81,75 @@ export declare function createPostFxSurface(kit: CanvasKit, canvas: HTMLCanvasEl
81
81
  * `createPostFxSurface` for that step.
82
82
  */
83
83
  export declare function createWebFrameRenderer(kit: CanvasKit): FrameRenderer;
84
+ /** Paint handle of a `WebShapeSkia`. `setStyle` takes 0 (fill) or 1 (stroke). */
85
+ interface WebShapePaint {
86
+ setColor(color: Float32Array): void;
87
+ setStyle(style: number): void;
88
+ setStrokeWidth(width: number): void;
89
+ }
90
+ /** Mutable path handle of a `WebShapeSkia`. `addArc` takes degrees. */
91
+ interface WebShapePath {
92
+ moveTo(x: number, y: number): void;
93
+ lineTo(x: number, y: number): void;
94
+ close(): void;
95
+ addArc(oval: Float32Array, startDeg: number, sweepDeg: number): void;
96
+ }
97
+ /** Canvas of a `WebShapeSurface`. `rotate` takes degrees. */
98
+ interface WebShapeCanvas {
99
+ save(): number;
100
+ restore(): void;
101
+ translate(dx: number, dy: number): void;
102
+ scale(sx: number, sy: number): void;
103
+ rotate(degrees: number, rx: number, ry: number): void;
104
+ clear(color: Float32Array): void;
105
+ drawCircle(cx: number, cy: number, r: number, paint: WebShapePaint): void;
106
+ drawRect(rect: Float32Array, paint: WebShapePaint): void;
107
+ drawPath(path: WebShapePath, paint: WebShapePaint): void;
108
+ }
109
+ /** A CPU-raster CanvasKit surface made by `WebShapeSkia.Surface.Make`. */
110
+ interface WebShapeSurface {
111
+ getCanvas(): WebShapeCanvas;
112
+ /** Flush and snapshot. The image owns its pixels and outlives `dispose()`. */
113
+ makeImageSnapshot(): Parameters<FrameRenderer['drawAtlas']>[0];
114
+ /**
115
+ * Free the surface, and every paint and path the handle has created so far.
116
+ * Neither the surface nor those may be used afterwards.
117
+ */
118
+ dispose(): void;
119
+ }
120
+ /**
121
+ * A CanvasKit-backed stand-in for the react-native-skia `Skia` handle, limited
122
+ * to what `@flare-engine/shapes` reads: `drawShape` and `bakeShapeAtlas`.
123
+ */
124
+ interface WebShapeSkia {
125
+ Surface: {
126
+ Make(width: number, height: number): WebShapeSurface | null;
127
+ };
128
+ Paint(): WebShapePaint;
129
+ Path: {
130
+ Make(): WebShapePath;
131
+ /** `null` when CanvasKit cannot parse `d`. */
132
+ MakeFromSVGString(d: string): WebShapePath | null;
133
+ };
134
+ /** An LTRB `Float32Array`, as CanvasKit takes rects. */
135
+ XYWHRect(x: number, y: number, w: number, h: number): Float32Array;
136
+ }
137
+ /**
138
+ * Create a `WebShapeSkia` over CanvasKit, so `bakeShapeAtlas` from
139
+ * `@flare-engine/shapes` can rasterize on web:
140
+ * `bakeShapeAtlas(createWebShapeSkia(kit), specs, scale)`.
141
+ *
142
+ * `Surface.Make` is CanvasKit's offscreen CPU surface (`MakeSurface`), so the
143
+ * bake needs no WebGL context. Paints are anti-aliased, as react-native-skia's
144
+ * are by default.
145
+ *
146
+ * CanvasKit objects are not garbage-collected. The handle remembers every
147
+ * paint and path it creates and frees them when a surface is disposed, which
148
+ * `bakeShapeAtlas` does after its snapshot. It is meant for baking: a
149
+ * handle used to draw shapes every frame with no surface to dispose keeps
150
+ * all of those objects alive.
151
+ */
152
+ export declare function createWebShapeSkia(kit: CanvasKit): WebShapeSkia;
84
153
  /**
85
154
  * Create a `FrameRendererModule` backed by CanvasKit.
86
155
  *
@@ -89,6 +158,12 @@ export declare function createWebFrameRenderer(kit: CanvasKit): FrameRenderer;
89
158
  *
90
159
  * CK differences handled here:
91
160
  * - `drawAtlas` takes flat Float32Arrays — the wrapper flattens object pools.
161
+ * Per-sprite colours arrive as `[r, g, b, a]` floats and leave as one packed
162
+ * ARGB `Uint32Array`, with `BlendMode.Modulate`; an uncoloured run still
163
+ * passes `null` for both.
164
+ * - `PictureRecorder().beginRecording()` returns one surface object per recorder,
165
+ * re-pointed at each new recording. Use it only between `beginRecording` and the
166
+ * matching `finishRecordingAsPicture`; do not keep it across recordings.
92
167
  * - `rotate` is forwarded verbatim (no radians→degrees conversion) to match
93
168
  * the native bridge; the dormant radians/degrees mismatch is tracked separately.
94
169
  * - `ColorFilter.MakeBlend(color, mode)` takes an EmbindEnumEntity — the
@@ -123,5 +198,5 @@ interface WebSurface {
123
198
  * Callers should fall back to a CPU surface in that case.
124
199
  */
125
200
  export declare function createWebSurface(kit: CanvasKit, canvas: HTMLCanvasElement): WebSurface | null;
126
- export type { LoadCanvasKitOptions, PostFxSurface, WebSurface };
201
+ export type { LoadCanvasKitOptions, PostFxSurface, WebShapeCanvas, WebShapePaint, WebShapePath, WebShapeSkia, WebShapeSurface, WebSurface };
127
202
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":["CKCanvas","CKCanvas"],"sources":["../src/loader.ts","../src/postfx-surface.ts","../src/web-frame-renderer.ts","../src/web-skia-module.ts","../src/web-surface.ts"],"mappings":";;;;UAQiB;;EAEf;;EAEA;;;;;;;;;;;;wBAkBc,cACd,iBACA,cAAc,2BACd,UAAS,uBACR,QAAQ;;;;;;;;;;;;;UCPM;;WAEN;;WAEA;;EAET,eAAeA;;EAEf,kBAAkBA;;EAElB,MAAM;;EAEN,UAAU,0BAA0B;;;;;;EAMpC,UAAU;;EAEV;;EAEA,OAAO,eAAe;;EAEtB;EACA;;;;;;;;;;;;wBAac,oBACd,KAAK,WACL,QAAQ,mBACR,OAAO,cACN;;;;;;;;;;;;wBCtDa,uBAAuB,KAAK,YAAY;;;;;;;;;;;;;;;;;;wBCoMxC,oBAAoB,KAAK,YAAY;;;;;;;;UCzMpC;;WAEN,QAAQ;;WAER,UAAUC;;EAEnB;;EAEA;;;;;;;;wBAoBc,iBAAiB,KAAK,WAAW,QAAQ,oBAAoB"}
1
+ {"version":3,"file":"index.d.ts","names":["CKCanvas","CKCanvas"],"sources":["../src/loader.ts","../src/postfx-surface.ts","../src/web-frame-renderer.ts","../src/web-shape-skia.ts","../src/web-skia-module.ts","../src/web-surface.ts"],"mappings":";;;;UAQiB;;EAEf;;EAEA;;;;;;;;;;;;wBAkBc,cACd,iBACA,cAAc,2BACd,UAAS,uBACR,QAAQ;;;;;;;;;;;;;UCNM;;WAEN;;WAEA;;EAET,eAAeA;;EAEf,kBAAkBA;;EAElB,MAAM;;EAEN,UAAU,0BAA0B;;;;;;EAMpC,UAAU;;EAEV;;EAEA,OAAO,eAAe;;EAEtB;EACA;;;;;;;;;;;;wBAac,oBACd,KAAK,WACL,QAAQ,mBACR,OAAO,cACN;;;;;;;;;;;;wBCvDa,uBAAuB,KAAK,YAAY;;UCLvC;EACf,SAAS,OAAO;EAChB,SAAS;EACT,eAAe;;;UAIA;EACf,OAAO,WAAW;EAClB,OAAO,WAAW;EAClB;EACA,OAAO,MAAM,cAAc,kBAAkB;;;UAI9B;EACf;EACA;EACA,UAAU,YAAY;EACtB,MAAM,YAAY;EAClB,OAAO,iBAAiB,YAAY;EACpC,MAAM,OAAO;EACb,WAAW,YAAY,YAAY,WAAW,OAAO;EACrD,SAAS,MAAM,cAAc,OAAO;EACpC,SAAS,MAAM,cAAc,OAAO;;;UAIrB;EACf,aAAa;;EAEb,qBAAqB,WAAW;;;;;EAKhC;;;;;;UAOe;EACf;IAAW,KAAK,eAAe,iBAAiB;;EAChD,SAAS;EACT;IACE,QAAQ;;IAER,kBAAkB,YAAY;;;EAGhC,SAAS,WAAW,WAAW,WAAW,YAAY;;;;;;;;;;;;;;;;;wBAoCxC,mBAAmB,KAAK,YAAY;;;;;;;;;;;;;;;;;;;;;;;;wBCuLpC,oBAAoB,KAAK,YAAY;;;;;;;;UC9QpC;;WAEN,QAAQ;;WAER,UAAUC;;EAEnB;;EAEA;;;;;;;;wBAYc,iBAAiB,KAAK,WAAW,QAAQ,oBAAoB"}
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  let _flare_engine_postfx = require("@flare-engine/postfx");
3
3
  let _flare_engine_render = require("@flare-engine/render");
4
+ let _flare_engine_math = require("@flare-engine/math");
4
5
  // src/loader.ts
5
6
  const DEFAULT_STALL_MS = 15e3;
6
7
  const DEFAULT_DEADLINE_MS = 45e3;
@@ -105,6 +106,29 @@ async function fetchWithProgress(url, onProgress, options) {
105
106
  if (stall) clearTimeout(stall);
106
107
  }
107
108
  }
109
+ // src/runtime-effect.ts
110
+ /**
111
+ * Wrap a CanvasKit `RuntimeEffect` for both consumers in this package: the frame renderer module
112
+ * (`makeShader` plus uniform reflection) and the postfx surface (`makeShaderWithChildren`).
113
+ * Internal: not exported from the package index.
114
+ */
115
+ function wrapRuntimeEffect(eff) {
116
+ return {
117
+ makeShader: (uniforms) => eff.makeShader(uniforms),
118
+ makeShaderWithChildren: (uniforms, children) => eff.makeShaderWithChildren(uniforms, children ?? []),
119
+ getUniformCount: () => eff.getUniformCount(),
120
+ getUniformFloatCount: () => eff.getUniformFloatCount(),
121
+ getUniformName: (i) => eff.getUniformName(i),
122
+ getUniform: (i) => {
123
+ const u = eff.getUniform(i);
124
+ return {
125
+ columns: u.columns,
126
+ rows: u.rows,
127
+ slot: u.slot
128
+ };
129
+ }
130
+ };
131
+ }
108
132
  // src/postfx-surface.ts
109
133
  /**
110
134
  * Create a `PostFxSurface` backed by a WebGL CanvasKit context.
@@ -201,7 +225,7 @@ function createPostFxSurface(kit, canvas, chain) {
201
225
  const runtime = (0, _flare_engine_postfx.createPostFxRuntime)({ RuntimeEffect: { Make(sksl) {
202
226
  const eff = kit.RuntimeEffect.Make(sksl);
203
227
  if (!eff) return null;
204
- return { makeShaderWithChildren: (uniforms, children) => eff.makeShaderWithChildren(uniforms, children ?? []) };
228
+ return wrapRuntimeEffect(eff);
205
229
  } } });
206
230
  function blit(img) {
207
231
  if (lost || !onscreen) return;
@@ -323,6 +347,10 @@ const BLEND_PLUS = 12;
323
347
  function makeAtlasBuffers() {
324
348
  let srcs = /* @__PURE__ */ new Float32Array(128);
325
349
  let xforms = /* @__PURE__ */ new Float32Array(128);
350
+ let colors = /* @__PURE__ */ new Uint32Array(32);
351
+ const srcViews = /* @__PURE__ */ new Map();
352
+ const xformViews = /* @__PURE__ */ new Map();
353
+ const colorViews = /* @__PURE__ */ new Map();
326
354
  return {
327
355
  get srcs() {
328
356
  return srcs;
@@ -330,20 +358,52 @@ function makeAtlasBuffers() {
330
358
  get xforms() {
331
359
  return xforms;
332
360
  },
361
+ get colors() {
362
+ return colors;
363
+ },
333
364
  ensureCapacity(n) {
334
365
  if (n * 4 > srcs.length) {
335
366
  let cap = srcs.length;
336
367
  while (cap < n * 4) cap *= 2;
337
368
  srcs = new Float32Array(cap);
338
369
  xforms = new Float32Array(cap);
370
+ colors = new Uint32Array(cap / 4);
371
+ srcViews.clear();
372
+ xformViews.clear();
373
+ colorViews.clear();
374
+ }
375
+ },
376
+ colorView(n) {
377
+ let view = colorViews.get(n);
378
+ if (view === void 0) {
379
+ view = colors.subarray(0, n);
380
+ colorViews.set(n, view);
339
381
  }
382
+ return view;
383
+ },
384
+ srcView(n) {
385
+ let view = srcViews.get(n);
386
+ if (view === void 0) {
387
+ view = srcs.subarray(0, n * 4);
388
+ srcViews.set(n, view);
389
+ }
390
+ return view;
391
+ },
392
+ xformView(n) {
393
+ let view = xformViews.get(n);
394
+ if (view === void 0) {
395
+ view = xforms.subarray(0, n * 4);
396
+ xformViews.set(n, view);
397
+ }
398
+ return view;
340
399
  }
341
400
  };
342
401
  }
343
402
  function rawPaint(paint) {
344
403
  return paint._ckPaint;
345
404
  }
346
- function wrapCKCanvas(ck, atlasBuffers, kit, ckSampling) {
405
+ function wrapCKCanvas(initial, atlasBuffers, kit, ckSampling) {
406
+ let ck = initial;
347
407
  const imgSrcScratch = /* @__PURE__ */ new Float32Array(4);
348
408
  const imgDstScratch = /* @__PURE__ */ new Float32Array(4);
349
409
  function fillImageScratch(src, dst) {
@@ -357,68 +417,67 @@ function wrapCKCanvas(ck, atlasBuffers, kit, ckSampling) {
357
417
  imgDstScratch[3] = dst.y + dst.height;
358
418
  }
359
419
  return {
360
- save: () => ck.save(),
361
- restore: () => ck.restore(),
362
- translate: (x, y) => ck.translate(x, y),
363
- scale: (sx, sy) => ck.scale(sx, sy),
364
- rotate: (degrees, cx, cy) => ck.rotate(degrees, cx, cy),
365
- clear: (color) => ck.clear(color),
366
- drawAtlas(image, srcRects, dstXforms, paint, _blendMode, _colors, sampling) {
367
- const n = srcRects.length;
368
- if (n === 0) return;
369
- atlasBuffers.ensureCapacity(n);
370
- for (let i = 0; i < n; i++) {
371
- const r = srcRects[i];
372
- const o = i * 4;
373
- atlasBuffers.srcs[o] = r.x;
374
- atlasBuffers.srcs[o + 1] = r.y;
375
- atlasBuffers.srcs[o + 2] = r.x + r.width;
376
- atlasBuffers.srcs[o + 3] = r.y + r.height;
377
- const xf = dstXforms[i];
378
- atlasBuffers.xforms[o] = xf.scos;
379
- atlasBuffers.xforms[o + 1] = xf.ssin;
380
- atlasBuffers.xforms[o + 2] = xf.tx;
381
- atlasBuffers.xforms[o + 3] = xf.ty;
420
+ surface: {
421
+ save: () => ck.save(),
422
+ restore: () => ck.restore(),
423
+ translate: (x, y) => ck.translate(x, y),
424
+ scale: (sx, sy) => ck.scale(sx, sy),
425
+ rotate: (degrees, cx, cy) => ck.rotate(degrees, cx, cy),
426
+ clear: (color) => ck.clear(color),
427
+ drawAtlas(image, srcRects, dstXforms, paint, _blendMode, colors, sampling) {
428
+ const n = srcRects.length;
429
+ if (n === 0) return;
430
+ atlasBuffers.ensureCapacity(n);
431
+ for (let i = 0; i < n; i++) {
432
+ const r = srcRects[i];
433
+ const o = i * 4;
434
+ atlasBuffers.srcs[o] = r.x;
435
+ atlasBuffers.srcs[o + 1] = r.y;
436
+ atlasBuffers.srcs[o + 2] = r.x + r.width;
437
+ atlasBuffers.srcs[o + 3] = r.y + r.height;
438
+ const xf = dstXforms[i];
439
+ atlasBuffers.xforms[o] = xf.scos;
440
+ atlasBuffers.xforms[o + 1] = xf.ssin;
441
+ atlasBuffers.xforms[o + 2] = xf.tx;
442
+ atlasBuffers.xforms[o + 3] = xf.ty;
443
+ }
444
+ const img = image;
445
+ const srcs = atlasBuffers.srcView(n);
446
+ const xforms = atlasBuffers.xformView(n);
447
+ let blendMode = null;
448
+ let ckColors = null;
449
+ if (colors !== void 0) {
450
+ for (let i = 0; i < n; i++) {
451
+ const c = colors[i];
452
+ atlasBuffers.colors[i] = (Math.round(c[3] * 255) << 24 | Math.round(c[0] * 255) << 16 | Math.round(c[1] * 255) << 8 | Math.round(c[2] * 255)) >>> 0;
453
+ }
454
+ blendMode = kit.BlendMode.Modulate;
455
+ ckColors = atlasBuffers.colorView(n);
456
+ }
457
+ if (sampling === void 0) ck.drawAtlas(img, srcs, xforms, rawPaint(paint), blendMode, ckColors);
458
+ else ck.drawAtlas(img, srcs, xforms, rawPaint(paint), blendMode, ckColors, sampling.filter === 0 ? ckSampling.nearest : ckSampling.linear);
459
+ },
460
+ drawImageRect(image, src, dst, paint) {
461
+ fillImageScratch(src, dst);
462
+ ck.drawImageRect(image, imgSrcScratch, imgDstScratch, rawPaint(paint), false);
463
+ },
464
+ drawImageRectOptions(image, src, dst, filterMode, mipmapMode, paint) {
465
+ fillImageScratch(src, dst);
466
+ const fm = filterMode === 0 ? kit.FilterMode.Nearest : kit.FilterMode.Linear;
467
+ const mm = mipmapMode === 0 ? kit.MipmapMode.None : mipmapMode === 1 ? kit.MipmapMode.Nearest : kit.MipmapMode.Linear;
468
+ ck.drawImageRectOptions(image, imgSrcScratch, imgDstScratch, fm, mm, paint == null ? null : rawPaint(paint));
469
+ },
470
+ drawRect(rect, paint) {
471
+ const r = rect;
472
+ imgSrcScratch[0] = r.x;
473
+ imgSrcScratch[1] = r.y;
474
+ imgSrcScratch[2] = r.x + r.width;
475
+ imgSrcScratch[3] = r.y + r.height;
476
+ ck.drawRect(imgSrcScratch, rawPaint(paint));
382
477
  }
383
- const img = image;
384
- const srcs = atlasBuffers.srcs.subarray(0, n * 4);
385
- const xforms = atlasBuffers.xforms.subarray(0, n * 4);
386
- if (sampling === void 0) ck.drawAtlas(img, srcs, xforms, rawPaint(paint), null, null);
387
- else ck.drawAtlas(img, srcs, xforms, rawPaint(paint), null, null, sampling.filter === 0 ? ckSampling.nearest : ckSampling.linear);
388
- },
389
- drawImageRect(image, src, dst, paint) {
390
- fillImageScratch(src, dst);
391
- ck.drawImageRect(image, imgSrcScratch, imgDstScratch, rawPaint(paint), false);
392
- },
393
- drawImageRectOptions(image, src, dst, filterMode, mipmapMode, paint) {
394
- fillImageScratch(src, dst);
395
- const fm = filterMode === 0 ? kit.FilterMode.Nearest : kit.FilterMode.Linear;
396
- const mm = mipmapMode === 0 ? kit.MipmapMode.None : mipmapMode === 1 ? kit.MipmapMode.Nearest : kit.MipmapMode.Linear;
397
- ck.drawImageRectOptions(image, imgSrcScratch, imgDstScratch, fm, mm, paint == null ? null : rawPaint(paint));
398
478
  },
399
- drawRect(rect, paint) {
400
- const r = rect;
401
- imgSrcScratch[0] = r.x;
402
- imgSrcScratch[1] = r.y;
403
- imgSrcScratch[2] = r.x + r.width;
404
- imgSrcScratch[3] = r.y + r.height;
405
- ck.drawRect(imgSrcScratch, rawPaint(paint));
406
- }
407
- };
408
- }
409
- function wrapRuntimeEffect(eff) {
410
- return {
411
- makeShader: (uniforms) => eff.makeShader(uniforms),
412
- getUniformCount: () => eff.getUniformCount(),
413
- getUniformFloatCount: () => eff.getUniformFloatCount(),
414
- getUniformName: (i) => eff.getUniformName(i),
415
- getUniform: (i) => {
416
- const u = eff.getUniform(i);
417
- return {
418
- columns: u.columns,
419
- rows: u.rows,
420
- slot: u.slot
421
- };
479
+ bind(next) {
480
+ ck = next;
422
481
  }
423
482
  };
424
483
  }
@@ -430,6 +489,12 @@ function wrapRuntimeEffect(eff) {
430
489
  *
431
490
  * CK differences handled here:
432
491
  * - `drawAtlas` takes flat Float32Arrays — the wrapper flattens object pools.
492
+ * Per-sprite colours arrive as `[r, g, b, a]` floats and leave as one packed
493
+ * ARGB `Uint32Array`, with `BlendMode.Modulate`; an uncoloured run still
494
+ * passes `null` for both.
495
+ * - `PictureRecorder().beginRecording()` returns one surface object per recorder,
496
+ * re-pointed at each new recording. Use it only between `beginRecording` and the
497
+ * matching `finishRecordingAsPicture`; do not keep it across recordings.
433
498
  * - `rotate` is forwarded verbatim (no radians→degrees conversion) to match
434
499
  * the native bridge; the dormant radians/degrees mismatch is tracked separately.
435
500
  * - `ColorFilter.MakeBlend(color, mode)` takes an EmbindEnumEntity — the
@@ -499,10 +564,14 @@ function createWebSkiaModule(kit) {
499
564
  },
500
565
  PictureRecorder() {
501
566
  const rec = new kit.PictureRecorder();
567
+ let wrapper = null;
502
568
  return {
503
569
  beginRecording(bounds) {
504
570
  const r = bounds;
505
- return wrapCKCanvas(rec.beginRecording(kit.XYWHRect(r.x, r.y, r.width, r.height)), atlasBuffers, kit, ckSampling);
571
+ const ck = rec.beginRecording(kit.XYWHRect(r.x, r.y, r.width, r.height));
572
+ if (wrapper === null) wrapper = wrapCKCanvas(ck, atlasBuffers, kit, ckSampling);
573
+ else wrapper.bind(ck);
574
+ return wrapper.surface;
506
575
  },
507
576
  finishRecordingAsPicture() {
508
577
  return rec.finishRecordingAsPicture();
@@ -543,15 +612,112 @@ function createWebSkiaModule(kit) {
543
612
  function createWebFrameRenderer(kit) {
544
613
  return (0, _flare_engine_render.createFrameRenderer)(createWebSkiaModule(kit));
545
614
  }
615
+ // src/web-shape-skia.ts
616
+ /**
617
+ * Create a `WebShapeSkia` over CanvasKit, so `bakeShapeAtlas` from
618
+ * `@flare-engine/shapes` can rasterize on web:
619
+ * `bakeShapeAtlas(createWebShapeSkia(kit), specs, scale)`.
620
+ *
621
+ * `Surface.Make` is CanvasKit's offscreen CPU surface (`MakeSurface`), so the
622
+ * bake needs no WebGL context. Paints are anti-aliased, as react-native-skia's
623
+ * are by default.
624
+ *
625
+ * CanvasKit objects are not garbage-collected. The handle remembers every
626
+ * paint and path it creates and frees them when a surface is disposed, which
627
+ * `bakeShapeAtlas` does after its snapshot. It is meant for baking: a
628
+ * handle used to draw shapes every frame with no surface to dispose keeps
629
+ * all of those objects alive.
630
+ */
631
+ function createWebShapeSkia(kit) {
632
+ const live = [];
633
+ const hasBuilder = typeof kit.PathBuilder === "function";
634
+ function wrapPath(sink, draw) {
635
+ live.push(sink);
636
+ return {
637
+ _draw: draw,
638
+ moveTo: (x, y) => {
639
+ sink.moveTo(x, y);
640
+ },
641
+ lineTo: (x, y) => {
642
+ sink.lineTo(x, y);
643
+ },
644
+ close: () => {
645
+ sink.close();
646
+ },
647
+ addArc: (oval, startDeg, sweepDeg) => {
648
+ sink.addArc(oval, startDeg, sweepDeg);
649
+ }
650
+ };
651
+ }
652
+ function wrapBuilder(builder) {
653
+ return wrapPath(builder, (canvas, paint) => {
654
+ const path = builder.snapshot();
655
+ canvas.drawPath(path, paint);
656
+ path.delete();
657
+ });
658
+ }
659
+ function wrapMutablePath(path) {
660
+ return wrapPath(path, (canvas, paint) => canvas.drawPath(path, paint));
661
+ }
662
+ const rawPaint = (paint) => paint._ckPaint;
663
+ return {
664
+ Surface: { Make(width, height) {
665
+ const surface = kit.MakeSurface(width, height);
666
+ if (!surface) return null;
667
+ const ck = surface.getCanvas();
668
+ const canvas = {
669
+ save: () => ck.save(),
670
+ restore: () => ck.restore(),
671
+ translate: (dx, dy) => ck.translate(dx, dy),
672
+ scale: (sx, sy) => ck.scale(sx, sy),
673
+ rotate: (degrees, rx, ry) => ck.rotate(degrees, rx, ry),
674
+ clear: (color) => ck.clear(color),
675
+ drawCircle: (cx, cy, r, paint) => ck.drawCircle(cx, cy, r, rawPaint(paint)),
676
+ drawRect: (rect, paint) => ck.drawRect(rect, rawPaint(paint)),
677
+ drawPath: (path, paint) => path._draw(ck, rawPaint(paint))
678
+ };
679
+ return {
680
+ getCanvas: () => canvas,
681
+ makeImageSnapshot() {
682
+ surface.flush();
683
+ return surface.makeImageSnapshot();
684
+ },
685
+ dispose() {
686
+ surface.delete();
687
+ for (const object of live) object.delete();
688
+ live.length = 0;
689
+ }
690
+ };
691
+ } },
692
+ Paint() {
693
+ const p = new kit.Paint();
694
+ p.setAntiAlias(true);
695
+ live.push(p);
696
+ return {
697
+ _ckPaint: p,
698
+ setColor: (color) => p.setColor(color),
699
+ setStyle: (style) => p.setStyle(style === 1 ? kit.PaintStyle.Stroke : kit.PaintStyle.Fill),
700
+ setStrokeWidth: (width) => p.setStrokeWidth(width)
701
+ };
702
+ },
703
+ Path: {
704
+ Make() {
705
+ return hasBuilder ? wrapBuilder(new kit.PathBuilder()) : wrapMutablePath(new kit.Path());
706
+ },
707
+ MakeFromSVGString(d) {
708
+ const parsed = kit.Path.MakeFromSVGString(d);
709
+ if (parsed === null) return null;
710
+ if (!hasBuilder) return wrapMutablePath(parsed);
711
+ const builder = new kit.PathBuilder(parsed);
712
+ parsed.delete();
713
+ return wrapBuilder(builder);
714
+ }
715
+ },
716
+ XYWHRect: (x, y, w, h) => kit.XYWHRect(x, y, w, h)
717
+ };
718
+ }
546
719
  // src/web-surface.ts
547
720
  const clearScratch = /* @__PURE__ */ new Float32Array(4);
548
- /** Unpack a 0xAARRGGBB integer into a Float32Array [r, g, b, a] in [0, 1]. */
549
- function unpackArgb(out, argb) {
550
- out[0] = (argb >>> 16 & 255) / 255;
551
- out[1] = (argb >>> 8 & 255) / 255;
552
- out[2] = (argb & 255) / 255;
553
- out[3] = (argb >>> 24 & 255) / 255;
554
- }
555
721
  /**
556
722
  * Create a `WebSurface` for the given `<canvas>` element.
557
723
  *
@@ -570,7 +736,7 @@ function createWebSurface(kit, canvas) {
570
736
  scale: (sx, sy) => ck.scale(sx, sy),
571
737
  rotate: (degrees, cx, cy) => ck.rotate(degrees, cx, cy),
572
738
  clear: (color) => {
573
- unpackArgb(clearScratch, color);
739
+ (0, _flare_engine_math.argbToFloat4Into)(clearScratch, color);
574
740
  ck.clear(clearScratch);
575
741
  }
576
742
  },
@@ -586,6 +752,7 @@ function createWebSurface(kit, canvas) {
586
752
  }
587
753
  exports.createPostFxSurface = createPostFxSurface;
588
754
  exports.createWebFrameRenderer = createWebFrameRenderer;
755
+ exports.createWebShapeSkia = createWebShapeSkia;
589
756
  exports.createWebSkiaModule = createWebSkiaModule;
590
757
  exports.createWebSurface = createWebSurface;
591
758
  exports.loadCanvasKit = loadCanvasKit;