@clypra-studio/engine 0.1.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/dist/TransitionDefinition-CC6oSTYB.d.cts +158 -0
  2. package/dist/TransitionDefinition-CC6oSTYB.d.ts +158 -0
  3. package/dist/chunk-4Y7ZSGHJ.js +2990 -0
  4. package/dist/chunk-4Y7ZSGHJ.js.map +1 -0
  5. package/dist/chunk-AUMNXFYQ.js +667 -0
  6. package/dist/chunk-AUMNXFYQ.js.map +1 -0
  7. package/dist/{chunk-CJRH4PAV.js → chunk-LGW5SSLZ.js} +292 -61
  8. package/dist/chunk-LGW5SSLZ.js.map +1 -0
  9. package/dist/{chunk-EVQAI3P3.js → chunk-S24UVNPZ.js} +5 -1
  10. package/dist/{chunk-EVQAI3P3.js.map → chunk-S24UVNPZ.js.map} +1 -1
  11. package/dist/{index-BWo6qV_z.d.cts → index-BP1N8Yg7.d.cts} +2 -2
  12. package/dist/{index-BWo6qV_z.d.ts → index-BP1N8Yg7.d.ts} +2 -2
  13. package/dist/index.cjs +3882 -1366
  14. package/dist/index.cjs.map +1 -1
  15. package/dist/index.d.cts +428 -3798
  16. package/dist/index.d.ts +428 -3798
  17. package/dist/index.js +1373 -1498
  18. package/dist/index.js.map +1 -1
  19. package/dist/textEffects/index.cjs.map +1 -1
  20. package/dist/textEffects/index.js +1 -1
  21. package/dist/transitions/index.cjs +2207 -3
  22. package/dist/transitions/index.cjs.map +1 -1
  23. package/dist/transitions/index.d.cts +54 -5
  24. package/dist/transitions/index.d.ts +54 -5
  25. package/dist/transitions/index.js +53 -3
  26. package/dist/v2/backends/index.cjs +246 -33
  27. package/dist/v2/backends/index.cjs.map +1 -1
  28. package/dist/v2/backends/index.d.cts +1 -1
  29. package/dist/v2/backends/index.d.ts +1 -1
  30. package/dist/v2/backends/index.js +1 -1
  31. package/dist/videoEffects/index.cjs +287 -62
  32. package/dist/videoEffects/index.cjs.map +1 -1
  33. package/dist/videoEffects/index.d.cts +31 -134
  34. package/dist/videoEffects/index.d.ts +31 -134
  35. package/dist/videoEffects/index.js +1 -1
  36. package/package.json +2 -2
  37. package/dist/chunk-5JOAEDIQ.js +0 -446
  38. package/dist/chunk-5JOAEDIQ.js.map +0 -1
  39. package/dist/chunk-CJRH4PAV.js.map +0 -1
  40. package/dist/chunk-QMRMNH52.js +0 -811
  41. package/dist/chunk-QMRMNH52.js.map +0 -1
@@ -1,8 +1,6 @@
1
- var __create = Object.create;
2
1
  var __defProp = Object.defineProperty;
3
2
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
3
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
4
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
5
  var __export = (target, all) => {
8
6
  for (var name in all)
@@ -16,14 +14,6 @@ var __copyProps = (to, from, except, desc) => {
16
14
  }
17
15
  return to;
18
16
  };
19
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
- // If the importer is in node compatibility mode or this is not an ESM
21
- // file that has been converted to a CommonJS file using a Babel-
22
- // compatible transform (i.e. "__esModule" has not been set), then set
23
- // "default" to the CommonJS "module.exports" for node compatibility.
24
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
- mod
26
- ));
27
17
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
18
 
29
19
  // src/videoEffects/index.ts
@@ -2246,9 +2236,20 @@ var import_pixi = require("pixi.js");
2246
2236
  var PixiRenderer = class {
2247
2237
  app = null;
2248
2238
  videoSprite = null;
2239
+ transitionSprite = null;
2240
+ _activeTransition = null;
2241
+ fromRenderTexture = null;
2242
+ toRenderTexture = null;
2243
+ blitSprite = null;
2244
+ _transitionFromTex = null;
2245
+ _transitionToTex = null;
2249
2246
  overlayContainer = null;
2247
+ baseMediaContainer = null;
2250
2248
  mounted = /* @__PURE__ */ new Map();
2251
2249
  initialized = false;
2250
+ initializing = false;
2251
+ _activeSource = null;
2252
+ _fitMode = "fit";
2252
2253
  /**
2253
2254
  * Initialize the PixiJS Application.
2254
2255
  * Call once, reuse across effect changes.
@@ -2258,47 +2259,77 @@ var PixiRenderer = class {
2258
2259
  * @param height Canvas height in pixels
2259
2260
  */
2260
2261
  async init(canvas, width, height) {
2261
- if (this.initialized) return;
2262
- this.app = new import_pixi.Application();
2263
- await this.app.init({
2264
- canvas,
2265
- width,
2266
- height,
2267
- backgroundAlpha: 0,
2268
- // transparent — composited over video below
2269
- antialias: true,
2270
- preference: "webgl",
2271
- // WebGL for production stability; swap to 'webgpu' later
2272
- resolution: typeof window !== "undefined" ? window.devicePixelRatio || 1 : 1,
2273
- autoDensity: true,
2274
- preserveDrawingBuffer: true
2262
+ if (this.initialized || this.initializing) return;
2263
+ this.initializing = true;
2264
+ canvas.addEventListener("webglcontextlost", (event) => {
2265
+ event.preventDefault();
2266
+ console.warn("[PixiRenderer] WebGL context lost \u2014 attempting recovery");
2275
2267
  });
2268
+ canvas.addEventListener("webglcontextrestored", () => {
2269
+ console.log("[PixiRenderer] WebGL context restored");
2270
+ });
2271
+ const app = new import_pixi.Application();
2272
+ try {
2273
+ await app.init({
2274
+ canvas,
2275
+ width,
2276
+ height,
2277
+ backgroundAlpha: 0,
2278
+ // transparent — composited over video below
2279
+ antialias: true,
2280
+ preference: "webgl",
2281
+ // WebGL for production stability; swap to 'webgpu' later
2282
+ resolution: typeof window !== "undefined" ? window.devicePixelRatio || 1 : 1,
2283
+ autoDensity: true,
2284
+ preserveDrawingBuffer: true
2285
+ });
2286
+ } catch (e) {
2287
+ this.initializing = false;
2288
+ throw e;
2289
+ }
2290
+ if (!this.initializing) {
2291
+ app.destroy(true);
2292
+ return;
2293
+ }
2294
+ this.app = app;
2276
2295
  this.videoSprite = new import_pixi.Sprite();
2277
2296
  this.videoSprite.width = width;
2278
2297
  this.videoSprite.height = height;
2298
+ this.transitionSprite = new import_pixi.Sprite();
2299
+ this.transitionSprite.width = width;
2300
+ this.transitionSprite.height = height;
2301
+ this.transitionSprite.visible = false;
2279
2302
  this.overlayContainer = new import_pixi.Container();
2303
+ this.baseMediaContainer = new import_pixi.Container();
2304
+ this.baseMediaContainer.sortableChildren = true;
2305
+ this.app.stage.addChildAt(this.baseMediaContainer, 0);
2280
2306
  this.app.stage.addChild(this.videoSprite);
2307
+ this.app.stage.addChild(this.transitionSprite);
2281
2308
  this.app.stage.addChild(this.overlayContainer);
2282
2309
  this.initialized = true;
2310
+ this.initializing = false;
2283
2311
  }
2284
- /**
2285
- * Point the video sprite at a live HTMLVideoElement.
2286
- * PixiJS polls the video element each frame automatically.
2287
- */
2288
2312
  setVideoSource(video) {
2313
+ if (this._activeSource === video) {
2314
+ this.resizeSprites();
2315
+ return;
2316
+ }
2317
+ this._activeSource = video;
2289
2318
  if (!this.videoSprite || !this.app) return;
2290
- import("pixi.js").then(({ VideoSource, Texture }) => {
2291
- const source = new VideoSource({ resource: video, autoPlay: false });
2292
- const texture = new Texture({ source });
2293
- this.videoSprite.texture = texture;
2294
- });
2319
+ const texture = import_pixi.Texture.from(video);
2320
+ this.videoSprite.texture = texture;
2321
+ this.resizeSprites();
2295
2322
  }
2296
2323
  setImageSource(image) {
2324
+ if (this._activeSource === image) {
2325
+ this.resizeSprites();
2326
+ return;
2327
+ }
2328
+ this._activeSource = image;
2297
2329
  if (!this.videoSprite || !this.app) return;
2298
- import("pixi.js").then(({ Texture }) => {
2299
- const texture = Texture.from(image);
2300
- this.videoSprite.texture = texture;
2301
- });
2330
+ const texture = import_pixi.Texture.from(image);
2331
+ this.videoSprite.texture = texture;
2332
+ this.resizeSprites();
2302
2333
  }
2303
2334
  // -------------------------------------------------------------------------
2304
2335
  // Effect lifecycle
@@ -2329,11 +2360,7 @@ var PixiRenderer = class {
2329
2360
  Object.assign(m.params, params);
2330
2361
  Object.assign(m.ctx.params, params);
2331
2362
  if (m.filters && m.definition.filterSpec?.updateUniforms) {
2332
- m.definition.filterSpec.updateUniforms(
2333
- m.filters.length === 1 ? m.filters[0] : m.filters,
2334
- m.params,
2335
- 0
2336
- );
2363
+ m.definition.filterSpec.updateUniforms(m.filters.length === 1 ? m.filters[0] : m.filters, m.params, 0);
2337
2364
  }
2338
2365
  if (m.filters) filterChain.push(...m.filters);
2339
2366
  continue;
@@ -2358,11 +2385,7 @@ var PixiRenderer = class {
2358
2385
  tickerFn = (ticker) => {
2359
2386
  elapsed.value += ticker.deltaMS;
2360
2387
  if (filters) {
2361
- spec.updateUniforms(
2362
- filters.length === 1 ? filters[0] : filters,
2363
- params,
2364
- elapsed.value
2365
- );
2388
+ spec.updateUniforms(filters.length === 1 ? filters[0] : filters, params, elapsed.value);
2366
2389
  }
2367
2390
  };
2368
2391
  this.app.ticker.add(tickerFn);
@@ -2384,11 +2407,7 @@ var PixiRenderer = class {
2384
2407
  Object.assign(m.params, params);
2385
2408
  Object.assign(m.ctx.params, params);
2386
2409
  if (m.filters && m.definition.filterSpec?.updateUniforms) {
2387
- m.definition.filterSpec.updateUniforms(
2388
- m.filters.length === 1 ? m.filters[0] : m.filters,
2389
- m.params,
2390
- 0
2391
- );
2410
+ m.definition.filterSpec.updateUniforms(m.filters.length === 1 ? m.filters[0] : m.filters, m.params, 0);
2392
2411
  }
2393
2412
  }
2394
2413
  /**
@@ -2408,11 +2427,7 @@ var PixiRenderer = class {
2408
2427
  m.params[key] = value;
2409
2428
  m.ctx.params[key] = value;
2410
2429
  if (m.filters && m.definition.filterSpec?.updateUniforms) {
2411
- m.definition.filterSpec.updateUniforms(
2412
- m.filters.length === 1 ? m.filters[0] : m.filters,
2413
- m.params,
2414
- 0
2415
- );
2430
+ m.definition.filterSpec.updateUniforms(m.filters.length === 1 ? m.filters[0] : m.filters, m.params, 0);
2416
2431
  }
2417
2432
  m.definition.onParamChange?.(m.ctx, key, value);
2418
2433
  }
@@ -2433,9 +2448,78 @@ var PixiRenderer = class {
2433
2448
  resize(width, height) {
2434
2449
  if (!this.app) return;
2435
2450
  this.app.renderer.resize(width, height);
2436
- if (this.videoSprite) {
2437
- this.videoSprite.width = width;
2438
- this.videoSprite.height = height;
2451
+ this.resizeSprites();
2452
+ }
2453
+ setFitMode(mode) {
2454
+ if (this._fitMode === mode) return;
2455
+ this._fitMode = mode;
2456
+ this.resizeSprites();
2457
+ }
2458
+ resizeSprites() {
2459
+ if (this.videoSprite && this.videoSprite.texture) {
2460
+ this._resizeSprite(this.videoSprite, this.videoSprite.texture, this._fitMode);
2461
+ }
2462
+ if (this.transitionSprite) {
2463
+ if (this._activeTransition) {
2464
+ this.transitionSprite.width = this.app?.screen.width ?? 1280;
2465
+ this.transitionSprite.height = this.app?.screen.height ?? 720;
2466
+ this.transitionSprite.position.set(0, 0);
2467
+ } else if (this.transitionSprite.texture) {
2468
+ this._resizeSprite(this.transitionSprite, this.transitionSprite.texture, this._fitMode);
2469
+ }
2470
+ }
2471
+ }
2472
+ _getElementDimensions(texture) {
2473
+ const sourceEl = this._activeSource;
2474
+ if (sourceEl) {
2475
+ const tagName = sourceEl.tagName;
2476
+ if ((tagName === "VIDEO" || sourceEl.videoWidth !== void 0) && sourceEl.videoWidth > 0) {
2477
+ return { width: sourceEl.videoWidth, height: sourceEl.videoHeight };
2478
+ }
2479
+ if ((tagName === "IMG" || sourceEl.naturalWidth !== void 0) && sourceEl.naturalWidth > 0) {
2480
+ return { width: sourceEl.naturalWidth, height: sourceEl.naturalHeight };
2481
+ }
2482
+ if ((tagName === "CANVAS" || sourceEl.getContext !== void 0) && sourceEl.width > 0) {
2483
+ return { width: sourceEl.width, height: sourceEl.height };
2484
+ }
2485
+ }
2486
+ const resource = texture.source?.resource || texture.source?.source;
2487
+ if (resource) {
2488
+ const tagName = resource.tagName;
2489
+ if ((tagName === "VIDEO" || resource.videoWidth !== void 0) && resource.videoWidth > 0) {
2490
+ return { width: resource.videoWidth, height: resource.videoHeight };
2491
+ }
2492
+ if ((tagName === "IMG" || resource.naturalWidth !== void 0) && resource.naturalWidth > 0) {
2493
+ return { width: resource.naturalWidth, height: resource.naturalHeight };
2494
+ }
2495
+ if ((tagName === "CANVAS" || resource.getContext !== void 0) && resource.width > 0) {
2496
+ return { width: resource.width, height: resource.height };
2497
+ }
2498
+ }
2499
+ if (texture.width > 0) {
2500
+ return { width: texture.width, height: texture.height };
2501
+ }
2502
+ return { width: 1280, height: 720 };
2503
+ }
2504
+ _resizeSprite(sprite, texture, fitMode) {
2505
+ if (!this.app) return;
2506
+ const canvasWidth = this.app.screen.width;
2507
+ const canvasHeight = this.app.screen.height;
2508
+ const { width: texWidth, height: texHeight } = this._getElementDimensions(texture);
2509
+ if (fitMode === "stretch") {
2510
+ sprite.width = canvasWidth;
2511
+ sprite.height = canvasHeight;
2512
+ sprite.position.set(0, 0);
2513
+ } else {
2514
+ const scaleX = canvasWidth / texWidth;
2515
+ const scaleY = canvasHeight / texHeight;
2516
+ const scale = fitMode === "fit" ? Math.min(scaleX, scaleY) : Math.max(scaleX, scaleY);
2517
+ sprite.width = texWidth * scale;
2518
+ sprite.height = texHeight * scale;
2519
+ sprite.position.set(
2520
+ (canvasWidth - sprite.width) / 2,
2521
+ (canvasHeight - sprite.height) / 2
2522
+ );
2439
2523
  }
2440
2524
  }
2441
2525
  /**
@@ -2443,15 +2527,40 @@ var PixiRenderer = class {
2443
2527
  * Call when the editor session ends or the Studio workspace unmounts.
2444
2528
  */
2445
2529
  destroy() {
2530
+ this.initializing = false;
2446
2531
  for (const mounted of this.mounted.values()) {
2447
2532
  this._unmount(mounted);
2448
2533
  }
2449
2534
  this.mounted.clear();
2450
- this.app?.destroy(false, { children: true, texture: true });
2451
- this.app = null;
2535
+ if (this.app) {
2536
+ try {
2537
+ const gl = this.app.renderer.gl;
2538
+ this.app.destroy(true, { children: true, texture: true });
2539
+ gl?.getExtension("WEBGL_lose_context")?.loseContext();
2540
+ } catch (e) {
2541
+ console.warn("Error destroying Pixi application:", e);
2542
+ }
2543
+ this.app = null;
2544
+ }
2452
2545
  this.videoSprite = null;
2546
+ this.transitionSprite = null;
2453
2547
  this.overlayContainer = null;
2548
+ this.baseMediaContainer = null;
2454
2549
  this.initialized = false;
2550
+ this._activeTransition = null;
2551
+ this._activeSource = null;
2552
+ if (this.fromRenderTexture) {
2553
+ this.fromRenderTexture.destroy(true);
2554
+ this.fromRenderTexture = null;
2555
+ }
2556
+ if (this.toRenderTexture) {
2557
+ this.toRenderTexture.destroy(true);
2558
+ this.toRenderTexture = null;
2559
+ }
2560
+ if (this.blitSprite) {
2561
+ this.blitSprite.destroy();
2562
+ this.blitSprite = null;
2563
+ }
2455
2564
  }
2456
2565
  // -------------------------------------------------------------------------
2457
2566
  // Private helpers
@@ -2476,9 +2585,125 @@ var PixiRenderer = class {
2476
2585
  getVideoSprite() {
2477
2586
  return this.videoSprite;
2478
2587
  }
2588
+ getTransitionSprite() {
2589
+ return this.transitionSprite;
2590
+ }
2591
+ getBaseMediaContainer() {
2592
+ return this.baseMediaContainer;
2593
+ }
2594
+ getOverlayContainer() {
2595
+ return this.overlayContainer;
2596
+ }
2479
2597
  get isReady() {
2480
2598
  return this.initialized;
2481
2599
  }
2600
+ _ensureRenderTextures(width, height) {
2601
+ if (!this.fromRenderTexture || this.fromRenderTexture.width !== width || this.fromRenderTexture.height !== height) {
2602
+ if (this.fromRenderTexture) this.fromRenderTexture.destroy(true);
2603
+ this.fromRenderTexture = import_pixi.RenderTexture.create({ width, height });
2604
+ }
2605
+ if (!this.toRenderTexture || this.toRenderTexture.width !== width || this.toRenderTexture.height !== height) {
2606
+ if (this.toRenderTexture) this.toRenderTexture.destroy(true);
2607
+ this.toRenderTexture = import_pixi.RenderTexture.create({ width, height });
2608
+ }
2609
+ if (!this.blitSprite) {
2610
+ this.blitSprite = new import_pixi.Sprite();
2611
+ }
2612
+ }
2613
+ _blitTransitionFrames() {
2614
+ if (!this.app || !this._transitionFromTex || !this._transitionToTex || !this.fromRenderTexture || !this.toRenderTexture || !this.blitSprite) {
2615
+ return;
2616
+ }
2617
+ const currentFitMode = this._fitMode;
2618
+ this.blitSprite.texture = this._transitionFromTex;
2619
+ this._resizeSprite(this.blitSprite, this._transitionFromTex, currentFitMode);
2620
+ this.app.renderer.render({
2621
+ container: this.blitSprite,
2622
+ target: this.fromRenderTexture,
2623
+ clear: true
2624
+ });
2625
+ this.blitSprite.texture = this._transitionToTex;
2626
+ this._resizeSprite(this.blitSprite, this._transitionToTex, currentFitMode);
2627
+ this.app.renderer.render({
2628
+ container: this.blitSprite,
2629
+ target: this.toRenderTexture,
2630
+ clear: true
2631
+ });
2632
+ this.blitSprite.texture = import_pixi.Texture.EMPTY;
2633
+ }
2634
+ mountTransition(definition, fromTexture, toTexture, params) {
2635
+ if (!this.transitionSprite || !this.videoSprite || !this.app) return;
2636
+ this._activeSource = null;
2637
+ if (this._activeTransition?.filter) {
2638
+ this._activeTransition.filter.destroy();
2639
+ }
2640
+ this._transitionFromTex = fromTexture;
2641
+ this._transitionToTex = toTexture;
2642
+ const canvasWidth = this.app.screen.width;
2643
+ const canvasHeight = this.app.screen.height;
2644
+ this._ensureRenderTextures(canvasWidth, canvasHeight);
2645
+ this._blitTransitionFrames();
2646
+ const fromRT = this.fromRenderTexture;
2647
+ const toRT = this.toRenderTexture;
2648
+ const originalFilterFrom = import_pixi.Filter.from;
2649
+ import_pixi.Filter.from = function(options) {
2650
+ if (options && options.resources) {
2651
+ if (!options.resources.uFrom) {
2652
+ options.resources.uFrom = fromRT.source;
2653
+ }
2654
+ if (!options.resources.uTo) {
2655
+ options.resources.uTo = toRT.source;
2656
+ }
2657
+ }
2658
+ return originalFilterFrom.call(this, options);
2659
+ };
2660
+ let filter;
2661
+ try {
2662
+ filter = definition.create(params);
2663
+ } finally {
2664
+ import_pixi.Filter.from = originalFilterFrom;
2665
+ }
2666
+ filter.resources.uFrom = fromRT.source;
2667
+ filter.resources.uTo = toRT.source;
2668
+ this.transitionSprite.texture = fromRT;
2669
+ this.transitionSprite.filters = [filter];
2670
+ this.transitionSprite.visible = true;
2671
+ this.videoSprite.visible = false;
2672
+ if (this.baseMediaContainer) {
2673
+ this.baseMediaContainer.visible = false;
2674
+ }
2675
+ this._activeTransition = { definition, filter, params };
2676
+ this.resizeSprites();
2677
+ }
2678
+ getActiveTransitionId() {
2679
+ return this._activeTransition?.definition.id || null;
2680
+ }
2681
+ updateTransitionProgress(id, progress, params) {
2682
+ if (!this._activeTransition) return;
2683
+ if (params) {
2684
+ this._activeTransition.params = params;
2685
+ }
2686
+ this._blitTransitionFrames();
2687
+ this._activeTransition.definition.updateProgress(this._activeTransition.filter, progress, this._activeTransition.params);
2688
+ }
2689
+ unmountTransition() {
2690
+ if (this.transitionSprite) {
2691
+ this.transitionSprite.visible = false;
2692
+ this.transitionSprite.filters = null;
2693
+ }
2694
+ if (this.videoSprite) {
2695
+ this.videoSprite.visible = true;
2696
+ }
2697
+ if (this.baseMediaContainer) {
2698
+ this.baseMediaContainer.visible = true;
2699
+ }
2700
+ if (this._activeTransition?.filter) {
2701
+ this._activeTransition.filter.destroy();
2702
+ }
2703
+ this._activeTransition = null;
2704
+ this._transitionFromTex = null;
2705
+ this._transitionToTex = null;
2706
+ }
2482
2707
  };
2483
2708
 
2484
2709
  // src/videoEffects/example.ts