canvasengine 2.0.0-beta.3 → 2.0.0-beta.4

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/dist/index.d.ts CHANGED
@@ -813,15 +813,53 @@ declare function NineSliceSprite(props: NineSliceSpriteProps): Element<Component
813
813
 
814
814
  interface Listen<T = any> {
815
815
  config: T | undefined;
816
- seed: number;
816
+ seed: {
817
+ config: T | undefined;
818
+ value: number;
819
+ resolve: (value: any) => void;
820
+ };
817
821
  }
818
822
  interface Trigger<T = any> {
819
- start: () => void;
823
+ start: () => Promise<void>;
820
824
  listen: () => Listen<T> | undefined;
821
825
  }
826
+ /**
827
+ * Checks if the given argument is a Trigger object
828
+ * @param arg - The value to check
829
+ * @returns True if the argument is a Trigger object
830
+ */
822
831
  declare function isTrigger(arg: any): arg is Trigger<any>;
823
- declare function trigger<T = any>(config?: T): Trigger<T>;
824
- declare function on(triggerSignal: any, callback: (config: any) => void): void;
832
+ /**
833
+ * Creates a new trigger that can be used to pass data between components
834
+ * @param globalConfig - Optional configuration data to be passed when the trigger is activated
835
+ * @returns A Trigger object with start and listen methods
836
+ * @example
837
+ * ```ts
838
+ * const myTrigger = trigger()
839
+ *
840
+ * on(myTrigger, (data) => {
841
+ * console.log('Triggered with data:', data)
842
+ * })
843
+ *
844
+ * myTrigger.start({ message: 'Hello' })
845
+ * ```
846
+ */
847
+ declare function trigger<T = any>(globalConfig?: T): Trigger<T>;
848
+ /**
849
+ * Subscribes to a trigger and executes a callback when the trigger is activated
850
+ * @param triggerSignal - The trigger to subscribe to
851
+ * @param callback - Function to execute when the trigger is activated
852
+ * @throws Error if triggerSignal is not a valid trigger
853
+ * @example
854
+ * ```ts
855
+ * const click = trigger()
856
+ *
857
+ * on(click, () => {
858
+ * console.log('Click triggered')
859
+ * })
860
+ * ```
861
+ */
862
+ declare function on(triggerSignal: any, callback: (config: any) => void | Promise<void>): void;
825
863
 
826
864
  /**
827
865
  * Bootstraps a canvas element and renders it to the DOM.
@@ -891,6 +929,14 @@ declare const Easing: {
891
929
  bounceOut: (p: number) => number;
892
930
  };
893
931
 
932
+ /**
933
+ * Creates a radial gradient texture that can be used in PixiJS.
934
+ * @example
935
+ * const gradient = new RadialGradient(size, size, 0, size, size, 0);
936
+ * gradient.addColorStop(0, "rgba(255, 255, 0, 1)");
937
+ * gradient.addColorStop(0.5, "rgba(255, 255, 0, 0.3)");
938
+ * gradient.addColorStop(0.8, "rgba(255, 255, 0, 0)");
939
+ */
894
940
  declare class RadialGradient {
895
941
  private x0;
896
942
  private y0;
@@ -905,8 +951,29 @@ declare class RadialGradient {
905
951
  private texture;
906
952
  transform: Matrix;
907
953
  size: number;
954
+ /**
955
+ * Creates a new RadialGradient instance
956
+ * @param x0 - The x-coordinate of the starting circle
957
+ * @param y0 - The y-coordinate of the starting circle
958
+ * @param x1 - The x-coordinate of the ending circle
959
+ * @param y1 - The y-coordinate of the ending circle
960
+ * @param x2 - The x-coordinate for gradient transformation
961
+ * @param y2 - The y-coordinate for gradient transformation
962
+ * @param focalPoint - The focal point of the gradient (0-1), defaults to 0
963
+ */
908
964
  constructor(x0: number, y0: number, x1: number, y1: number, x2: number, y2: number, focalPoint?: number);
965
+ /**
966
+ * Adds a color stop to the gradient
967
+ * @param offset - The position of the color stop (0-1)
968
+ * @param color - The color value (any valid CSS color string)
969
+ */
909
970
  addColorStop(offset: number, color: string): void;
971
+ /**
972
+ * Renders the gradient and returns the texture with its transformation matrix
973
+ * @param options - Render options
974
+ * @param options.translate - Optional translation coordinates
975
+ * @returns Object containing the texture and transformation matrix
976
+ */
910
977
  render({ translate }?: {
911
978
  translate?: {
912
979
  x: number;
@@ -918,4 +985,101 @@ declare class RadialGradient {
918
985
  };
919
986
  }
920
987
 
921
- export { type AnimatedSignal, type AnimatedState, type ArrayChange, Canvas, Circle, type ComponentFunction, type ComponentInstance, Container, DisplayObject, EVENTS, Easing, type Element, Ellipse, Graphics, NineSliceSprite, ParticlesEmitter, type Props, RadialGradient, Rect, Scene, Sprite, Text, TilingSprite, Triangle, Viewport, animatedSignal, bootstrapCanvas, cond, createComponent, currentSubscriptionsTracker, h, isAnimatedSignal, isElement, isPrimitive, isTrigger, loop, mount, mountTracker, on, registerComponent, Svg as svg, tick, trigger, useDefineProps, useProps };
988
+ /**
989
+ * Checks if code is running in a browser environment
990
+ * @returns {boolean} True if running in browser, false otherwise
991
+ */
992
+ declare function isBrowser(): boolean;
993
+ /**
994
+ * Returns current high-resolution timestamp
995
+ * @returns {number} Current time in milliseconds
996
+ */
997
+ declare function preciseNow(): number;
998
+ /**
999
+ * Converts frames per second to milliseconds
1000
+ * @param {number} fps - Frames per second
1001
+ * @returns {number} Milliseconds per frame
1002
+ */
1003
+ declare function fps2ms(fps: number): number;
1004
+ /**
1005
+ * Checks if a value is a Promise
1006
+ * @param {any} value - Value to check
1007
+ * @returns {boolean} True if value is a Promise, false otherwise
1008
+ */
1009
+ declare function isPromise(value: any): boolean;
1010
+ declare function arrayEquals(a: any[], b: any[]): boolean;
1011
+ /**
1012
+ * Checks if a value is a function
1013
+ * @param {unknown} val - Value to check
1014
+ * @returns {boolean} True if value is a function, false otherwise
1015
+ */
1016
+ declare function isFunction(val: unknown): boolean;
1017
+ /**
1018
+ * Checks if a value is a plain object
1019
+ * @param {unknown} val - Value to check
1020
+ * @returns {boolean} True if value is an object (not null and not array), false otherwise
1021
+ */
1022
+ declare function isObject(val: unknown): boolean;
1023
+ /**
1024
+ * Sets a value in an object using a dot notation path
1025
+ * @param {Record<string, any>} obj - Target object
1026
+ * @param {string | string[]} path - Path to set value at (e.g., 'a.b.c' or ['a', 'b', 'c'])
1027
+ * @param {any} value - Value to set
1028
+ * @param {boolean} onlyPlainObject - If true, only creates plain objects in path
1029
+ * @returns {Record<string, any>} Modified object
1030
+ */
1031
+ declare function set(obj: Record<string, any>, path: string | string[], value: any, onlyPlainObject?: boolean): Record<string, any>;
1032
+ /**
1033
+ * Gets a value from an object using a dot notation path
1034
+ * @param {Record<string, any>} obj - Source object
1035
+ * @param {string} path - Path to get value from (e.g., 'a.b.c')
1036
+ * @returns {any} Value at path or undefined if not found
1037
+ */
1038
+ declare function get(obj: Record<string, any>, path: string): any;
1039
+ /**
1040
+ * Logs a message to console
1041
+ * @param {any} text - Message to log
1042
+ */
1043
+ declare function log(text: any): void;
1044
+ /**
1045
+ * Logs an error message to console
1046
+ * @param {any} text - Error message to log
1047
+ */
1048
+ declare function error(text: any): void;
1049
+ /**
1050
+ * Sets the position of an ObservablePoint using various input formats
1051
+ * @param {ObservablePoint} observablePoint - The point to modify
1052
+ * @param {Object | number | [number, number]} point - New position value
1053
+ */
1054
+ declare function setObservablePoint(observablePoint: ObservablePoint, point: {
1055
+ x: number;
1056
+ y: number;
1057
+ } | number | [number, number]): void;
1058
+ /**
1059
+ * Calculates the Euclidean distance between two points
1060
+ * @param {number} x1 - X coordinate of first point
1061
+ * @param {number} y1 - Y coordinate of first point
1062
+ * @param {number} x2 - X coordinate of second point
1063
+ * @param {number} y2 - Y coordinate of second point
1064
+ * @returns {number} Distance between the points
1065
+ */
1066
+ declare function calculateDistance(x1: number, y1: number, x2: number, y2: number): number;
1067
+
1068
+ declare const utils_arrayEquals: typeof arrayEquals;
1069
+ declare const utils_calculateDistance: typeof calculateDistance;
1070
+ declare const utils_error: typeof error;
1071
+ declare const utils_fps2ms: typeof fps2ms;
1072
+ declare const utils_get: typeof get;
1073
+ declare const utils_isBrowser: typeof isBrowser;
1074
+ declare const utils_isFunction: typeof isFunction;
1075
+ declare const utils_isObject: typeof isObject;
1076
+ declare const utils_isPromise: typeof isPromise;
1077
+ declare const utils_log: typeof log;
1078
+ declare const utils_preciseNow: typeof preciseNow;
1079
+ declare const utils_set: typeof set;
1080
+ declare const utils_setObservablePoint: typeof setObservablePoint;
1081
+ declare namespace utils {
1082
+ export { utils_arrayEquals as arrayEquals, utils_calculateDistance as calculateDistance, utils_error as error, utils_fps2ms as fps2ms, utils_get as get, utils_isBrowser as isBrowser, utils_isFunction as isFunction, utils_isObject as isObject, utils_isPromise as isPromise, utils_log as log, utils_preciseNow as preciseNow, utils_set as set, utils_setObservablePoint as setObservablePoint };
1083
+ }
1084
+
1085
+ export { type AnimatedSignal, type AnimatedState, type ArrayChange, Canvas, Circle, type ComponentFunction, type ComponentInstance, Container, DisplayObject, EVENTS, Easing, type Element, Ellipse, Graphics, NineSliceSprite, ParticlesEmitter, type Props, RadialGradient, Rect, Scene, Sprite, Text, TilingSprite, Triangle, utils as Utils, Viewport, animatedSignal, bootstrapCanvas, cond, createComponent, currentSubscriptionsTracker, h, isAnimatedSignal, isElement, isPrimitive, isTrigger, loop, mount, mountTracker, on, registerComponent, Svg as svg, tick, trigger, useDefineProps, useProps };
package/dist/index.js CHANGED
@@ -1,6 +1,11 @@
1
+ var __defProp = Object.defineProperty;
1
2
  var __typeError = (msg) => {
2
3
  throw TypeError(msg);
3
4
  };
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
4
9
  var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
10
  var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
11
  var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
@@ -24,6 +29,22 @@ function applyDirective(element, directiveName) {
24
29
  }
25
30
 
26
31
  // src/engine/utils.ts
32
+ var utils_exports = {};
33
+ __export(utils_exports, {
34
+ arrayEquals: () => arrayEquals,
35
+ calculateDistance: () => calculateDistance,
36
+ error: () => error,
37
+ fps2ms: () => fps2ms,
38
+ get: () => get,
39
+ isBrowser: () => isBrowser,
40
+ isFunction: () => isFunction,
41
+ isObject: () => isObject,
42
+ isPromise: () => isPromise,
43
+ log: () => log,
44
+ preciseNow: () => preciseNow,
45
+ set: () => set,
46
+ setObservablePoint: () => setObservablePoint
47
+ });
27
48
  function isBrowser() {
28
49
  return typeof window !== "undefined";
29
50
  }
@@ -92,7 +113,7 @@ function set(obj, path, value, onlyPlainObject = false) {
92
113
  for (let i = 0; i < len - 1; i++) {
93
114
  let segment = path[i];
94
115
  let nextSegment = path[i + 1];
95
- let isNextNumeric = !isNaN(nextSegment) && isFinite(nextSegment);
116
+ let isNextNumeric = !isNaN(Number(nextSegment)) && isFinite(Number(nextSegment));
96
117
  if (!current[segment] || typeof current[segment] !== "object") {
97
118
  current[segment] = isNextNumeric && !onlyPlainObject ? [] : {};
98
119
  }
@@ -101,6 +122,20 @@ function set(obj, path, value, onlyPlainObject = false) {
101
122
  current[path[len - 1]] = value;
102
123
  return obj;
103
124
  }
125
+ function get(obj, path) {
126
+ const keys = path.split(".");
127
+ let current = obj;
128
+ for (let key of keys) {
129
+ if (current[key] === void 0) {
130
+ return void 0;
131
+ }
132
+ current = current[key];
133
+ }
134
+ return current;
135
+ }
136
+ function log(text) {
137
+ console.log(text);
138
+ }
104
139
  function error(text) {
105
140
  console.error(text);
106
141
  }
@@ -1189,6 +1224,22 @@ function createComponent(tag, props) {
1189
1224
  const elementsListen = new Subject();
1190
1225
  if (props?.isRoot) {
1191
1226
  const propagateContext = async (element2) => {
1227
+ if (element2.props.attach) {
1228
+ const isReactiveAttach = isSignal2(element2.propObservables?.attach);
1229
+ if (!isReactiveAttach) {
1230
+ element2.props.children.push(element2.props.attach);
1231
+ } else {
1232
+ let lastElement = null;
1233
+ element2.propObservables.attach.observable.subscribe(({ value, type }) => {
1234
+ if (type != "init") {
1235
+ destroyElement(lastElement);
1236
+ }
1237
+ lastElement = value;
1238
+ onMount(element2, value);
1239
+ propagateContext(value);
1240
+ });
1241
+ }
1242
+ }
1192
1243
  if (!element2.props.children) {
1193
1244
  return;
1194
1245
  }
@@ -2178,7 +2229,7 @@ function animatedSignal(initialValue, options = {}) {
2178
2229
  }
2179
2230
 
2180
2231
  // src/components/Sprite.ts
2181
- var log = console.log;
2232
+ var log2 = console.log;
2182
2233
  var CanvasSprite = class extends DisplayObject(PixiSprite) {
2183
2234
  constructor() {
2184
2235
  super(...arguments);
@@ -2205,12 +2256,12 @@ var CanvasSprite = class extends DisplayObject(PixiSprite) {
2205
2256
  const rectX = j * spriteWidth + offsetX;
2206
2257
  const rectY = i * spriteHeight + offsetY;
2207
2258
  if (rectY > height) {
2208
- throw log(
2259
+ throw log2(
2209
2260
  `Warning, there is a problem with the height of the "${this.id}" spritesheet. When cutting into frames, the frame exceeds the height of the image.`
2210
2261
  );
2211
2262
  }
2212
2263
  if (rectX > width) {
2213
- throw log(
2264
+ throw log2(
2214
2265
  `Warning, there is a problem with the width of the "${this.id}" spritesheet. When cutting into frames, the frame exceeds the width of the image.`
2215
2266
  );
2216
2267
  }
@@ -2487,15 +2538,28 @@ import { effect as effect8, signal as signal5 } from "@signe/reactive";
2487
2538
  function isTrigger(arg) {
2488
2539
  return arg?.start && arg?.listen;
2489
2540
  }
2490
- function trigger(config) {
2491
- const _signal = signal5(0);
2541
+ function trigger(globalConfig) {
2542
+ const _signal = signal5({
2543
+ config: globalConfig,
2544
+ value: 0,
2545
+ resolve: (value) => void 0
2546
+ });
2492
2547
  return {
2493
- start: () => {
2494
- _signal.set(Math.random());
2548
+ start: (config) => {
2549
+ return new Promise((resolve) => {
2550
+ _signal.set({
2551
+ config: {
2552
+ ...globalConfig,
2553
+ ...config
2554
+ },
2555
+ resolve,
2556
+ value: Math.random()
2557
+ });
2558
+ });
2495
2559
  },
2496
2560
  listen: () => {
2497
2561
  return {
2498
- config,
2562
+ config: globalConfig,
2499
2563
  seed: _signal()
2500
2564
  };
2501
2565
  }
@@ -2507,7 +2571,12 @@ function on(triggerSignal, callback) {
2507
2571
  }
2508
2572
  effect8(() => {
2509
2573
  const result = triggerSignal.listen();
2510
- if (result?.seed) callback(result.config);
2574
+ if (result?.seed.value) {
2575
+ const ret = callback(result?.seed.config);
2576
+ if (ret && typeof ret.then === "function") {
2577
+ ret.then(result?.seed.resolve);
2578
+ }
2579
+ }
2511
2580
  });
2512
2581
  }
2513
2582
 
@@ -2838,6 +2907,16 @@ var Easing = {
2838
2907
  // src/utils/RadialGradient.ts
2839
2908
  import { Texture as Texture5, ImageSource, DOMAdapter, Matrix } from "pixi.js";
2840
2909
  var RadialGradient = class {
2910
+ /**
2911
+ * Creates a new RadialGradient instance
2912
+ * @param x0 - The x-coordinate of the starting circle
2913
+ * @param y0 - The y-coordinate of the starting circle
2914
+ * @param x1 - The x-coordinate of the ending circle
2915
+ * @param y1 - The y-coordinate of the ending circle
2916
+ * @param x2 - The x-coordinate for gradient transformation
2917
+ * @param y2 - The y-coordinate for gradient transformation
2918
+ * @param focalPoint - The focal point of the gradient (0-1), defaults to 0
2919
+ */
2841
2920
  constructor(x0, y0, x1, y1, x2, y2, focalPoint = 0) {
2842
2921
  this.x0 = x0;
2843
2922
  this.y0 = y0;
@@ -2866,11 +2945,22 @@ var RadialGradient = class {
2866
2945
  );
2867
2946
  }
2868
2947
  }
2948
+ /**
2949
+ * Adds a color stop to the gradient
2950
+ * @param offset - The position of the color stop (0-1)
2951
+ * @param color - The color value (any valid CSS color string)
2952
+ */
2869
2953
  addColorStop(offset, color) {
2870
2954
  if (this.gradient) {
2871
2955
  this.gradient.addColorStop(offset, color);
2872
2956
  }
2873
2957
  }
2958
+ /**
2959
+ * Renders the gradient and returns the texture with its transformation matrix
2960
+ * @param options - Render options
2961
+ * @param options.translate - Optional translation coordinates
2962
+ * @returns Object containing the texture and transformation matrix
2963
+ */
2874
2964
  render({ translate } = {}) {
2875
2965
  const { x0, y0, x1, y1, x2, y2, focalPoint } = this;
2876
2966
  const defaultSize = this.size;
@@ -2925,6 +3015,7 @@ export {
2925
3015
  Text,
2926
3016
  TilingSprite,
2927
3017
  Triangle,
3018
+ utils_exports as Utils,
2928
3019
  Viewport,
2929
3020
  animatedSignal,
2930
3021
  bootstrapCanvas,