canvasengine 1.3.0 → 2.0.0-beta.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 (96) hide show
  1. package/package.json +51 -17
  2. package/src/components/Canvas.ts +134 -0
  3. package/src/components/Container.ts +46 -0
  4. package/src/components/DisplayObject.ts +458 -0
  5. package/src/components/DrawMap/index.ts +65 -0
  6. package/src/components/Graphic.ts +147 -0
  7. package/src/components/NineSliceSprite.ts +46 -0
  8. package/src/components/ParticleEmitter.ts +39 -0
  9. package/src/components/Scene.ts +6 -0
  10. package/src/components/Sprite.ts +493 -0
  11. package/src/components/Text.ts +145 -0
  12. package/src/components/Tilemap/Tile.ts +79 -0
  13. package/src/components/Tilemap/TileGroup.ts +207 -0
  14. package/src/components/Tilemap/TileLayer.ts +163 -0
  15. package/src/components/Tilemap/TileSet.ts +41 -0
  16. package/src/components/Tilemap/index.ts +80 -0
  17. package/src/components/TilingSprite.ts +39 -0
  18. package/src/components/Viewport.ts +159 -0
  19. package/src/components/index.ts +13 -0
  20. package/src/components/types/DisplayObject.ts +69 -0
  21. package/src/components/types/MouseEvent.ts +3 -0
  22. package/src/components/types/Spritesheet.ts +389 -0
  23. package/src/components/types/index.ts +4 -0
  24. package/src/directives/Drag.ts +84 -0
  25. package/src/directives/KeyboardControls.ts +922 -0
  26. package/src/directives/Scheduler.ts +101 -0
  27. package/src/directives/Sound.ts +91 -0
  28. package/src/directives/Transition.ts +45 -0
  29. package/src/directives/ViewportCull.ts +40 -0
  30. package/src/directives/ViewportFollow.ts +26 -0
  31. package/src/directives/index.ts +7 -0
  32. package/src/engine/animation.ts +113 -0
  33. package/src/engine/bootstrap.ts +19 -0
  34. package/src/engine/directive.ts +23 -0
  35. package/src/engine/reactive.ts +379 -0
  36. package/src/engine/signal.ts +138 -0
  37. package/src/engine/trigger.ts +40 -0
  38. package/src/engine/utils.ts +135 -0
  39. package/src/hooks/addContext.ts +6 -0
  40. package/src/hooks/useProps.ts +155 -0
  41. package/src/hooks/useRef.ts +21 -0
  42. package/src/index.ts +13 -0
  43. package/src/utils/Ease.ts +33 -0
  44. package/src/utils/RadialGradient.ts +86 -0
  45. package/.gitattributes +0 -22
  46. package/.npmignore +0 -163
  47. package/canvasengine-1.3.0.all.min.js +0 -21
  48. package/canvasengine.js +0 -5802
  49. package/core/DB.js +0 -24
  50. package/core/ModelServer.js +0 -348
  51. package/core/Users.js +0 -190
  52. package/core/engine-common.js +0 -952
  53. package/doc/cocoonjs.md +0 -36
  54. package/doc/doc-lang.yml +0 -43
  55. package/doc/doc-router.yml +0 -14
  56. package/doc/doc-tuto.yml +0 -9
  57. package/doc/doc.yml +0 -39
  58. package/doc/element.md +0 -37
  59. package/doc/entity.md +0 -90
  60. package/doc/extend.md +0 -47
  61. package/doc/get_started.md +0 -19
  62. package/doc/images/entity.png +0 -0
  63. package/doc/multitouch.md +0 -58
  64. package/doc/nodejs.md +0 -142
  65. package/doc/scene.md +0 -44
  66. package/doc/text.md +0 -156
  67. package/examples/server/client.html +0 -31
  68. package/examples/server/server.js +0 -16
  69. package/examples/tiled_server/client.html +0 -52
  70. package/examples/tiled_server/images/tiles_spritesheet.png +0 -0
  71. package/examples/tiled_server/server/map.json +0 -50
  72. package/examples/tiled_server/server/map.tmx +0 -16
  73. package/examples/tiled_server/server/server.js +0 -16
  74. package/extends/Animation.js +0 -910
  75. package/extends/Effect.js +0 -252
  76. package/extends/Gleed2d.js +0 -252
  77. package/extends/Hit.js +0 -1509
  78. package/extends/Input.js +0 -699
  79. package/extends/Marshal.js +0 -716
  80. package/extends/Scrolling.js +0 -388
  81. package/extends/Soundmanager2.js +0 -5466
  82. package/extends/Spritesheet.js +0 -196
  83. package/extends/Text.js +0 -366
  84. package/extends/Tiled.js +0 -403
  85. package/extends/Window.js +0 -575
  86. package/extends/gamepad.js +0 -397
  87. package/extends/socket.io.min.js +0 -2
  88. package/extends/swf/soundmanager2.swf +0 -0
  89. package/extends/swf/soundmanager2_debug.swf +0 -0
  90. package/extends/swf/soundmanager2_flash9.swf +0 -0
  91. package/extends/swf/soundmanager2_flash9_debug.swf +0 -0
  92. package/extends/swf/soundmanager2_flash_xdomain.zip +0 -0
  93. package/extends/workers/transition.js +0 -43
  94. package/index.js +0 -46
  95. package/license.txt +0 -19
  96. package/readme.md +0 -483
@@ -0,0 +1,155 @@
1
+ import { isSignal, signal } from "@signe/reactive"
2
+ import { isPrimitive } from "../engine/reactive"
3
+
4
+ /**
5
+ * Converts props into reactive signals if they are primitive values.
6
+ *
7
+ * @param {object} props - The properties to convert.
8
+ * @param {object} [defaults={}] - Default values for properties.
9
+ * @returns {object} An object with reactive signals.
10
+ *
11
+ * @example
12
+ * const props = useProps({ count: 0, name: "John" });
13
+ * console.log(props.count()); // 0
14
+ * props.count.set(1);
15
+ * console.log(props.count()); // 1
16
+ */
17
+ export const useProps = (props, defaults = {}): any => {
18
+ if (isSignal(props)) {
19
+ return props()
20
+ }
21
+ const obj = {}
22
+ for (let key in props) {
23
+ const value = props[key]
24
+ obj[key] = isPrimitive(value) ? signal(value) : value
25
+ }
26
+ for (let key in defaults) {
27
+ if (!(key in obj)) {
28
+ obj[key] = signal(defaults[key])
29
+ }
30
+ }
31
+ return obj
32
+ }
33
+
34
+ type PropType = NumberConstructor | StringConstructor | BooleanConstructor |
35
+ FunctionConstructor | ObjectConstructor | ArrayConstructor |
36
+ null | (new (...args: any[]) => any);
37
+
38
+ interface PropConfig {
39
+ type?: PropType | PropType[];
40
+ required?: boolean;
41
+ default?: any | ((props: any) => any);
42
+ validator?: (value: any, props: any) => boolean;
43
+ }
44
+
45
+ type PropSchema = {
46
+ [key: string]: PropType | PropType[] | PropConfig;
47
+ }
48
+
49
+ /**
50
+ * Validates and defines properties based on a schema.
51
+ *
52
+ * @param {object} props - The properties to validate.
53
+ * @returns {function} A function that takes a schema and returns validated properties.
54
+ *
55
+ * @example
56
+ * const schema = {
57
+ * age: { type: Number, required: true },
58
+ * name: { type: String, default: "Anonymous" }
59
+ * };
60
+ * const validatedProps = useDefineProps({ age: 25 })(schema);
61
+ * console.log(validatedProps.age()); // 25
62
+ * console.log(validatedProps.name()); // "Anonymous"
63
+ */
64
+ export const useDefineProps = (props: any) => {
65
+ return (schema?: PropSchema) => {
66
+ const rawProps = isSignal(props) ? props() : props
67
+ const validatedProps: { [key: string]: any } = {}
68
+
69
+ for (const key in schema) {
70
+ const propConfig = schema[key]
71
+ const value = rawProps[key]
72
+ let validatedValue: any
73
+
74
+ // Handle simple type definition
75
+ if (typeof propConfig === 'function') {
76
+ validateType(key, value, [propConfig])
77
+ validatedValue = value
78
+ }
79
+ // Handle array of types
80
+ else if (Array.isArray(propConfig)) {
81
+ validateType(key, value, propConfig)
82
+ validatedValue = value
83
+ }
84
+ // Handle detailed configuration object
85
+ else if (propConfig && typeof propConfig === 'object') {
86
+ // Check required prop
87
+ if (propConfig.required && value === undefined) {
88
+ throw new Error(`Missing required prop: ${key}`)
89
+ }
90
+
91
+ // Validate type if specified
92
+ if (propConfig.type) {
93
+ const types = Array.isArray(propConfig.type) ? propConfig.type : [propConfig.type]
94
+ validateType(key, value, types)
95
+ }
96
+
97
+ // Run custom validator if provided
98
+ if (propConfig.validator && !propConfig.validator(value, rawProps)) {
99
+ throw new Error(`Invalid prop: custom validation failed for prop "${key}"`)
100
+ }
101
+
102
+ // Set default value if value is undefined
103
+ if (value === undefined && 'default' in propConfig) {
104
+ validatedValue = typeof propConfig.default === 'function'
105
+ ? propConfig.default(rawProps)
106
+ : propConfig.default
107
+ } else {
108
+ validatedValue = value
109
+ }
110
+ }
111
+
112
+ validatedProps[key] = isSignal(validatedValue)
113
+ ? validatedValue
114
+ : signal(validatedValue)
115
+ }
116
+
117
+ return {
118
+ ...useProps(rawProps),
119
+ ...validatedProps
120
+ }
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Validates the type of a property.
126
+ *
127
+ * @param {string} key - The property key.
128
+ * @param {any} value - The property value.
129
+ * @param {any[]} types - The expected types.
130
+ * @throws Will throw an error if the type check fails.
131
+ */
132
+ function validateType(key: string, value: any, types: any[]) {
133
+ if (value === undefined || value === null) return
134
+
135
+ // Si c'est un signal, on vérifie la valeur du signal
136
+ const valueToCheck = isSignal(value) ? value() : value
137
+
138
+ const valid = types.some(type => {
139
+ if (type === Number) return typeof valueToCheck === 'number'
140
+ if (type === String) return typeof valueToCheck === 'string'
141
+ if (type === Boolean) return typeof valueToCheck === 'boolean'
142
+ if (type === Function) return typeof valueToCheck === 'function'
143
+ if (type === Object) return typeof valueToCheck === 'object'
144
+ if (type === Array) return Array.isArray(valueToCheck)
145
+ if (type === null) return valueToCheck === null
146
+ return valueToCheck instanceof type
147
+ })
148
+
149
+ if (!valid) {
150
+ throw new Error(
151
+ `Invalid prop: type check failed for prop "${key}". ` +
152
+ `Expected ${types.map(t => t.name).join(' or ')}`
153
+ )
154
+ }
155
+ }
@@ -0,0 +1,21 @@
1
+ import { Element } from "../engine/reactive";
2
+ import { ComponentInstance } from "../components/DisplayObject";
3
+
4
+ export function useRef(element: Element<ComponentInstance>, ref: string): Element<ComponentInstance> | null {
5
+ const { props } = element;
6
+ if (props.ref == ref) {
7
+ return element;
8
+ }
9
+ // Recursive search for the component with the given id
10
+ if (props.children) {
11
+ for (const child of props.children) {
12
+ const result = useRef(child, ref);
13
+ if (result) {
14
+ return result;
15
+ }
16
+ }
17
+ }
18
+
19
+ // If not found, return undefined
20
+ return null;
21
+ }
package/src/index.ts ADDED
@@ -0,0 +1,13 @@
1
+ import './directives'
2
+ export * from '@signe/reactive'
3
+ export { Howler } from 'howler'
4
+ export * from './components'
5
+ export * from './engine/reactive'
6
+ export * from './engine/signal'
7
+ export * from './engine/trigger'
8
+ export * from './engine/bootstrap'
9
+ export * from './engine/animation'
10
+ export { useProps, useDefineProps } from './hooks/useProps'
11
+ export * from './utils/Ease'
12
+ export * from './utils/RadialGradient'
13
+ export { isObservable } from 'rxjs'
@@ -0,0 +1,33 @@
1
+ import {
2
+ linear,
3
+ easeIn,
4
+ easeInOut,
5
+ easeOut,
6
+ circIn,
7
+ circInOut,
8
+ circOut,
9
+ backIn,
10
+ backInOut,
11
+ backOut,
12
+ anticipate,
13
+ bounceIn,
14
+ bounceInOut,
15
+ bounceOut
16
+ } from "popmotion"
17
+
18
+ export const Easing = {
19
+ linear,
20
+ easeIn,
21
+ easeInOut,
22
+ easeOut,
23
+ circIn,
24
+ circInOut,
25
+ circOut,
26
+ backIn,
27
+ backInOut,
28
+ backOut,
29
+ anticipate,
30
+ bounceIn,
31
+ bounceInOut,
32
+ bounceOut
33
+ }
@@ -0,0 +1,86 @@
1
+ import { Texture, ImageSource, DOMAdapter, Matrix } from "pixi.js";
2
+
3
+ export class RadialGradient {
4
+ private canvas: HTMLCanvasElement;
5
+ private ctx: CanvasRenderingContext2D | null;
6
+ private gradient: CanvasGradient | null = null;
7
+ private texture: Texture | null = null;
8
+ public transform: Matrix;
9
+
10
+ public size = 600;
11
+
12
+ constructor(
13
+ private x0: number,
14
+ private y0: number,
15
+ private x1: number,
16
+ private y1: number,
17
+ private x2: number,
18
+ private y2: number,
19
+ private focalPoint: number = 0
20
+ ) {
21
+ this.size = x0;
22
+ const halfSize = this.size * 0.5;
23
+
24
+ this.canvas = DOMAdapter.get().createCanvas() as any;
25
+ this.canvas.width = this.size;
26
+ this.canvas.height = this.size;
27
+ this.ctx = this.canvas.getContext("2d");
28
+
29
+ if (this.ctx) {
30
+ this.gradient = this.ctx.createRadialGradient(
31
+ halfSize * (1 - focalPoint),
32
+ halfSize,
33
+ 0,
34
+ halfSize,
35
+ halfSize,
36
+ halfSize - 0.5
37
+ );
38
+ }
39
+ }
40
+
41
+ addColorStop(offset: number, color: string) {
42
+ if (this.gradient) {
43
+ this.gradient.addColorStop(offset, color);
44
+ }
45
+ }
46
+
47
+ render({ translate }: { translate?: { x: number; y: number } } = {}) {
48
+ const { x0, y0, x1, y1, x2, y2, focalPoint } = this;
49
+ const defaultSize = this.size;
50
+ if (this.ctx && this.gradient) {
51
+ this.ctx.fillStyle = this.gradient;
52
+ this.ctx.fillRect(0, 0, defaultSize, defaultSize);
53
+
54
+ this.texture = new Texture({
55
+ source: new ImageSource({
56
+ resource: this.canvas,
57
+ addressModeU: "clamp-to-edge",
58
+ addressModeV: "clamp-to-edge",
59
+ }),
60
+ });
61
+
62
+ const m = new Matrix();
63
+ const dx = Math.sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
64
+ const dy = Math.sqrt((x2 - x0) * (x2 - x0) + (y2 - y0) * (y2 - y0));
65
+ const angle = Math.atan2(y1 - y0, x1 - x0);
66
+
67
+ // Calculate the scale factors correctly
68
+ const scaleX = dx / defaultSize;
69
+ const scaleY = dy / defaultSize;
70
+
71
+ // Apply transformations in the correct order
72
+ m.rotate(-angle);
73
+ m.scale(scaleX, scaleY);
74
+ if (translate) {
75
+ m.translate(translate.x, translate.y);
76
+ }
77
+
78
+ this.transform = m;
79
+ }
80
+
81
+ return {
82
+ texture: this.texture,
83
+ matrix: this.transform,
84
+ };
85
+ }
86
+ }
package/.gitattributes DELETED
@@ -1,22 +0,0 @@
1
- # Auto detect text files and perform LF normalization
2
- * text=auto
3
-
4
- # Custom for Visual Studio
5
- *.cs diff=csharp
6
- *.sln merge=union
7
- *.csproj merge=union
8
- *.vbproj merge=union
9
- *.fsproj merge=union
10
- *.dbproj merge=union
11
-
12
- # Standard to msysgit
13
- *.doc diff=astextplain
14
- *.DOC diff=astextplain
15
- *.docx diff=astextplain
16
- *.DOCX diff=astextplain
17
- *.dot diff=astextplain
18
- *.DOT diff=astextplain
19
- *.pdf diff=astextplain
20
- *.PDF diff=astextplain
21
- *.rtf diff=astextplain
22
- *.RTF diff=astextplain
package/.npmignore DELETED
@@ -1,163 +0,0 @@
1
- #################
2
- ## Eclipse
3
- #################
4
-
5
- *.pydevproject
6
- .project
7
- .metadata
8
- bin/
9
- tmp/
10
- *.tmp
11
- *.bak
12
- *.swp
13
- *~.nib
14
- local.properties
15
- .classpath
16
- .settings/
17
- .loadpath
18
-
19
- # External tool builders
20
- .externalToolBuilders/
21
-
22
- # Locally stored "Eclipse launch configurations"
23
- *.launch
24
-
25
- # CDT-specific
26
- .cproject
27
-
28
- # PDT-specific
29
- .buildpath
30
-
31
-
32
- #################
33
- ## Visual Studio
34
- #################
35
-
36
- ## Ignore Visual Studio temporary files, build results, and
37
- ## files generated by popular Visual Studio add-ons.
38
-
39
- # User-specific files
40
- *.suo
41
- *.user
42
- *.sln.docstates
43
-
44
- # Build results
45
- [Dd]ebug/
46
- [Rr]elease/
47
- *_i.c
48
- *_p.c
49
- *.ilk
50
- *.meta
51
- *.obj
52
- *.pch
53
- *.pdb
54
- *.pgc
55
- *.pgd
56
- *.rsp
57
- *.sbr
58
- *.tlb
59
- *.tli
60
- *.tlh
61
- *.tmp
62
- *.vspscc
63
- .builds
64
- *.dotCover
65
-
66
- ## TODO: If you have NuGet Package Restore enabled, uncomment this
67
- #packages/
68
-
69
- # Visual C++ cache files
70
- ipch/
71
- *.aps
72
- *.ncb
73
- *.opensdf
74
- *.sdf
75
-
76
- # Visual Studio profiler
77
- *.psess
78
- *.vsp
79
-
80
- # ReSharper is a .NET coding add-in
81
- _ReSharper*
82
-
83
- # Installshield output folder
84
- [Ee]xpress
85
-
86
- # DocProject is a documentation generator add-in
87
- DocProject/buildhelp/
88
- DocProject/Help/*.HxT
89
- DocProject/Help/*.HxC
90
- DocProject/Help/*.hhc
91
- DocProject/Help/*.hhk
92
- DocProject/Help/*.hhp
93
- DocProject/Help/Html2
94
- DocProject/Help/html
95
-
96
- # Click-Once directory
97
- publish
98
-
99
- # Others
100
- [Bb]in
101
- [Oo]bj
102
- sql
103
- TestResults
104
- *.Cache
105
- ClientBin
106
- stylecop.*
107
- ~$*
108
- *.dbmdl
109
- Generated_Code #added for RIA/Silverlight projects
110
-
111
- # Backup & report files from converting an old project file to a newer
112
- # Visual Studio version. Backup files are not needed, because we have git ;-)
113
- _UpgradeReport_Files/
114
- Backup*/
115
- UpgradeLog*.XML
116
-
117
-
118
-
119
- ############
120
- ## Windows
121
- ############
122
-
123
- # Windows image file caches
124
- Thumbs.db
125
-
126
- # Folder config file
127
- Desktop.ini
128
-
129
-
130
- #############
131
- ## Python
132
- #############
133
-
134
- *.py[co]
135
-
136
- # Packages
137
- *.egg
138
- *.egg-info
139
- dist
140
- build
141
- eggs
142
- parts
143
- bin
144
- var
145
- sdist
146
- develop-eggs
147
- .installed.cfg
148
-
149
- # Installer logs
150
- pip-log.txt
151
-
152
- # Unit test / coverage reports
153
- .coverage
154
- .tox
155
-
156
- #Translations
157
- *.mo
158
-
159
- #Mr Developer
160
- .mr.developer.cfg
161
-
162
- # Mac crap
163
- .DS_Store