canvasengine 1.0.8 → 2.0.0-beta.1
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/.cursorrules +0 -0
- package/README.md +71 -0
- package/dist/compiler/vite.js +119 -0
- package/dist/compiler/vite.js.map +1 -0
- package/dist/index.d.ts +846 -0
- package/dist/index.js +3340 -0
- package/dist/index.js.map +1 -0
- package/logo.png +0 -0
- package/package.json +88 -15
- package/src/compiler/grammar.pegjs +180 -0
- package/src/compiler/vite.ts +166 -0
- package/src/components/Canvas.ts +134 -0
- package/src/components/Container.ts +46 -0
- package/src/components/DisplayObject.ts +458 -0
- package/src/components/DrawMap/index.ts +65 -0
- package/src/components/Graphic.ts +147 -0
- package/src/components/NineSliceSprite.ts +46 -0
- package/src/components/ParticleEmitter.ts +39 -0
- package/src/components/Scene.ts +6 -0
- package/src/components/Sprite.ts +493 -0
- package/src/components/Text.ts +145 -0
- package/src/components/Tilemap/Tile.ts +79 -0
- package/src/components/Tilemap/TileGroup.ts +207 -0
- package/src/components/Tilemap/TileLayer.ts +163 -0
- package/src/components/Tilemap/TileSet.ts +41 -0
- package/src/components/Tilemap/index.ts +80 -0
- package/src/components/TilingSprite.ts +39 -0
- package/src/components/Viewport.ts +159 -0
- package/src/components/index.ts +12 -0
- package/src/components/types/DisplayObject.ts +68 -0
- package/src/components/types/MouseEvent.ts +3 -0
- package/src/components/types/Spritesheet.ts +389 -0
- package/src/components/types/index.ts +4 -0
- package/src/directives/Drag.ts +84 -0
- package/src/directives/KeyboardControls.ts +922 -0
- package/src/directives/Scheduler.ts +112 -0
- package/src/directives/Sound.ts +91 -0
- package/src/directives/Transition.ts +45 -0
- package/src/directives/ViewportCull.ts +40 -0
- package/src/directives/ViewportFollow.ts +26 -0
- package/src/directives/index.ts +7 -0
- package/src/engine/animation.ts +113 -0
- package/src/engine/bootstrap.ts +19 -0
- package/src/engine/directive.ts +23 -0
- package/src/engine/reactive.ts +379 -0
- package/src/engine/signal.ts +138 -0
- package/src/engine/trigger.ts +40 -0
- package/src/engine/utils.ts +135 -0
- package/src/hooks/addContext.ts +6 -0
- package/src/hooks/useProps.ts +155 -0
- package/src/hooks/useRef.ts +21 -0
- package/src/index.ts +14 -0
- package/src/presets/Bar.ts +89 -0
- package/src/presets/Button.ts +0 -0
- package/src/presets/Joystick.ts +286 -0
- package/src/presets/NightAmbiant.ts +122 -0
- package/src/presets/Particle.ts +53 -0
- package/src/utils/Ease.ts +33 -0
- package/src/utils/RadialGradient.ts +86 -0
- package/starter/assets/logo.png +0 -0
- package/starter/components/app.ce +18 -0
- package/starter/components/hello.ce +34 -0
- package/starter/index.html +21 -0
- package/starter/main.ts +4 -0
- package/starter/package.json +16 -0
- package/starter/vite.config.ts +12 -0
- package/tsconfig.json +32 -0
- package/tsconfig.node.json +10 -0
- package/tsup.config.ts +28 -0
- package/vitest.config.ts +12 -0
- package/core/ModelServer.js +0 -251
- package/core/engine-common.js +0 -534
- package/examples/server/client.html +0 -31
- package/examples/server/server.js +0 -11
- package/extends/Gleed2d.js +0 -248
- package/extends/Marshal.js +0 -662
- package/extends/Tiled.js +0 -293
- package/index.js +0 -15
- package/license.txt +0 -19
- package/readme.md +0 -69
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import * as PIXI from "pixi.js";
|
|
2
|
+
import { FX } from "revolt-fx";
|
|
3
|
+
import { h, mount, tick } from "../engine/signal";
|
|
4
|
+
import { Container } from "../components";
|
|
5
|
+
import { on } from "../engine/trigger";
|
|
6
|
+
import { useProps } from "../hooks/useProps";
|
|
7
|
+
|
|
8
|
+
export function Particle(options) {
|
|
9
|
+
const { emit, settings = {} } = options;
|
|
10
|
+
const { name } = useProps(options);
|
|
11
|
+
const fx = new FX();
|
|
12
|
+
let element;
|
|
13
|
+
|
|
14
|
+
PIXI.Assets.add({ alias: "fx_settings", src: "/default-bundle.json" });
|
|
15
|
+
PIXI.Assets.add({
|
|
16
|
+
alias: "fx_spritesheet",
|
|
17
|
+
src: "/revoltfx-spritesheet.json",
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
tick(({deltaRatio}) => {
|
|
21
|
+
fx.update(deltaRatio);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
mount(async (_element) => {
|
|
25
|
+
element = _element;
|
|
26
|
+
|
|
27
|
+
const data = await PIXI.Assets.load(["fx_settings", "fx_spritesheet"]);
|
|
28
|
+
let fxSettings = {...data.fx_settings};
|
|
29
|
+
|
|
30
|
+
if (settings.emitters) {
|
|
31
|
+
const lastId = 10000;
|
|
32
|
+
const emittersWithIds = settings.emitters.map((emitter, index) => ({
|
|
33
|
+
...emitter,
|
|
34
|
+
id: lastId + index
|
|
35
|
+
}));
|
|
36
|
+
|
|
37
|
+
fxSettings.emitters = [
|
|
38
|
+
...fxSettings.emitters,
|
|
39
|
+
...emittersWithIds,
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
fx.initBundle(fxSettings, true);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
on(emit, () => {
|
|
48
|
+
const emitter = fx.getParticleEmitter(name());
|
|
49
|
+
emitter.init(element.componentInstance);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return h(Container);
|
|
53
|
+
}
|
|
@@ -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
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<Canvas
|
|
2
|
+
backgroundColor="#fff"
|
|
3
|
+
width="100%"
|
|
4
|
+
height="100%"
|
|
5
|
+
antialias="true"
|
|
6
|
+
>
|
|
7
|
+
<Container
|
|
8
|
+
width="100%"
|
|
9
|
+
height="100%"
|
|
10
|
+
justifyContent="center"
|
|
11
|
+
alignItems="center">
|
|
12
|
+
<HelloWorld text="CanvasEngine" />
|
|
13
|
+
</Container>
|
|
14
|
+
</Canvas>
|
|
15
|
+
|
|
16
|
+
<script>
|
|
17
|
+
import HelloWorld from "./hello.ce";
|
|
18
|
+
</script>
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
<Container flexDirection="column" width="500px">
|
|
2
|
+
<Sprite
|
|
3
|
+
image="/assets/logo.png"
|
|
4
|
+
anchor="0.5"
|
|
5
|
+
rotation
|
|
6
|
+
scale
|
|
7
|
+
@pointerenter={onEnter}
|
|
8
|
+
@pointerleave={onLeave}
|
|
9
|
+
/>
|
|
10
|
+
<Text text size="70" fontFamily="Helvetica" x="90" y="-30" />
|
|
11
|
+
</Container>
|
|
12
|
+
|
|
13
|
+
<script>
|
|
14
|
+
import { signal, tick, animatedSignal, Easing } from "canvasengine";
|
|
15
|
+
|
|
16
|
+
const { text } = defineProps();
|
|
17
|
+
const rotation = signal(0);
|
|
18
|
+
const scale = animatedSignal(1, {
|
|
19
|
+
duration: 300,
|
|
20
|
+
ease: Easing.easeInOut,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
tick(() => {
|
|
24
|
+
rotation.update(rotation => rotation + 0.01);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const onEnter = () => {
|
|
28
|
+
scale.set(1.5);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const onLeave = () => {
|
|
32
|
+
scale.set(1);
|
|
33
|
+
};
|
|
34
|
+
</script>
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<title>Canvas Engine</title>
|
|
7
|
+
<style>
|
|
8
|
+
body, html, #root {
|
|
9
|
+
margin: 0;
|
|
10
|
+
padding: 0;
|
|
11
|
+
height: 100vh;
|
|
12
|
+
width: 100vw;
|
|
13
|
+
overflow: hidden;
|
|
14
|
+
}
|
|
15
|
+
</style>
|
|
16
|
+
</head>
|
|
17
|
+
<body>
|
|
18
|
+
<div id="root"></div>
|
|
19
|
+
<script type="module" src="/main.ts"></script>
|
|
20
|
+
</body>
|
|
21
|
+
</html>
|
package/starter/main.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "template",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "vite"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [],
|
|
10
|
+
"author": "",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"vite": "^6.0.3"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { defineConfig } from 'vite'
|
|
2
|
+
import vitePluginCe from 'canvasengine/compiler'
|
|
3
|
+
|
|
4
|
+
// https://vitejs.dev/config/
|
|
5
|
+
export default defineConfig({
|
|
6
|
+
plugins: [vitePluginCe()],
|
|
7
|
+
resolve: {
|
|
8
|
+
alias: {
|
|
9
|
+
path: 'rollup-plugin-node-polyfills/polyfills/path',
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"lib": ["dom", "esnext"],
|
|
5
|
+
"module": "ES2020",
|
|
6
|
+
"outDir": "./lib",
|
|
7
|
+
"rootDir": "src",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"sourceMap": true,
|
|
10
|
+
"strictNullChecks": true,
|
|
11
|
+
"strictPropertyInitialization": false,
|
|
12
|
+
"moduleResolution": "Bundler",
|
|
13
|
+
"esModuleInterop": true,
|
|
14
|
+
"removeComments": false,
|
|
15
|
+
"noUnusedParameters": false,
|
|
16
|
+
"noUnusedLocals": false,
|
|
17
|
+
"noImplicitThis": false,
|
|
18
|
+
"noImplicitAny": false,
|
|
19
|
+
"noImplicitReturns": false,
|
|
20
|
+
"declaration": true,
|
|
21
|
+
"experimentalDecorators": true,
|
|
22
|
+
"emitDecoratorMetadata": false,
|
|
23
|
+
"stripInternal": true,
|
|
24
|
+
"skipLibCheck": true,
|
|
25
|
+
"types": ["node"]
|
|
26
|
+
},
|
|
27
|
+
"include": [
|
|
28
|
+
"src",
|
|
29
|
+
"sample"
|
|
30
|
+
],
|
|
31
|
+
|
|
32
|
+
}
|
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { defineConfig } from 'tsup'
|
|
2
|
+
|
|
3
|
+
export default [
|
|
4
|
+
defineConfig({
|
|
5
|
+
format: ['esm'],
|
|
6
|
+
target: 'es2020',
|
|
7
|
+
splitting: true,
|
|
8
|
+
clean: true,
|
|
9
|
+
shims: false,
|
|
10
|
+
dts: true,
|
|
11
|
+
sourcemap: true,
|
|
12
|
+
entry: ['src/index.ts'],
|
|
13
|
+
outDir: 'dist'
|
|
14
|
+
}),
|
|
15
|
+
|
|
16
|
+
defineConfig({
|
|
17
|
+
format: ['esm'],
|
|
18
|
+
target: 'node20',
|
|
19
|
+
splitting: true,
|
|
20
|
+
clean: false,
|
|
21
|
+
shims: false,
|
|
22
|
+
dts: true,
|
|
23
|
+
sourcemap: true,
|
|
24
|
+
entry: ['src/compiler/vite.ts'],
|
|
25
|
+
outDir: 'dist/compiler',
|
|
26
|
+
external: ['vite', 'acorn', 'peggy', 'typescript'],
|
|
27
|
+
})
|
|
28
|
+
]
|
package/vitest.config.ts
ADDED
package/core/ModelServer.js
DELETED
|
@@ -1,251 +0,0 @@
|
|
|
1
|
-
exports.Class = function(Class, CE) {
|
|
2
|
-
var io = CE.io;
|
|
3
|
-
/**
|
|
4
|
-
@doc server
|
|
5
|
-
@class ModelServer Model used for the server. Socket.io resumes with a notion of class
|
|
6
|
-
1. Import CanvasEngine
|
|
7
|
-
2. Listen a port
|
|
8
|
-
3. Initialize the model with a main class
|
|
9
|
-
<code>
|
|
10
|
-
var CE = require("canvasengine").listen(8336);
|
|
11
|
-
|
|
12
|
-
CE.Model.init("Main", {
|
|
13
|
-
|
|
14
|
-
initialize: function() {
|
|
15
|
-
// Code
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
</code>
|
|
19
|
-
*/
|
|
20
|
-
Class.create("ModelServer", {
|
|
21
|
-
/**
|
|
22
|
-
@doc server/
|
|
23
|
-
@property socket The socket user (socket.io)
|
|
24
|
-
@type Object
|
|
25
|
-
@default null
|
|
26
|
-
*/
|
|
27
|
-
socket: null,
|
|
28
|
-
_models: {},
|
|
29
|
-
/**
|
|
30
|
-
@doc server/
|
|
31
|
-
@method init Initializes the connection and call the class
|
|
32
|
-
@params {String} name Class name
|
|
33
|
-
@params {Array} name (optional) Name of the methods that can be called by the client
|
|
34
|
-
@params {Object} model (optional) Properties and methods of this class
|
|
35
|
-
- the "initialize" method is called automatically when the connection
|
|
36
|
-
- the "disconnect" method is called when the user logs out
|
|
37
|
-
@example
|
|
38
|
-
<code>
|
|
39
|
-
var CE = require("canvasengine").listen(8336);
|
|
40
|
-
|
|
41
|
-
CE.Model.init("Main", {
|
|
42
|
-
|
|
43
|
-
initialize: function() {
|
|
44
|
-
// Code
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
</code>
|
|
48
|
-
@return {ModelServer}
|
|
49
|
-
*/
|
|
50
|
-
init: function(name, events, model) {
|
|
51
|
-
if (!(events instanceof Array)) {
|
|
52
|
-
model = events;
|
|
53
|
-
events = false;
|
|
54
|
-
}
|
|
55
|
-
if (!events) {
|
|
56
|
-
events = [];
|
|
57
|
-
}
|
|
58
|
-
var self = this;
|
|
59
|
-
events.push("disconnect");
|
|
60
|
-
this.create(name, events, model);
|
|
61
|
-
io.sockets.on('connection', function (socket) {
|
|
62
|
-
self.socket = socket;
|
|
63
|
-
self.new(name);
|
|
64
|
-
});
|
|
65
|
-
return this;
|
|
66
|
-
},
|
|
67
|
-
/**
|
|
68
|
-
@doc server/
|
|
69
|
-
@method create Create a model
|
|
70
|
-
@params {String} name Class name
|
|
71
|
-
@params {Array} name (optional) Name of the methods that can be called by the client
|
|
72
|
-
@params {Object} model (optional) Properties and methods of this class
|
|
73
|
-
@example
|
|
74
|
-
Module in "/mods/myclass.js" :
|
|
75
|
-
<code>
|
|
76
|
-
exports.Class = function(CE) {
|
|
77
|
-
return CE.Model.create("MyClass", {
|
|
78
|
-
|
|
79
|
-
initialize: function() {
|
|
80
|
-
// Code
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
</code>
|
|
86
|
-
In main JS file :
|
|
87
|
-
<code>
|
|
88
|
-
var CE = require("canvasengine").listen(8336);
|
|
89
|
-
CE.Model.init("Main", {
|
|
90
|
-
|
|
91
|
-
initialize: function() {
|
|
92
|
-
var Class = require('./mods/myclass').Class(CE),
|
|
93
|
-
myclass = Class.new("MyClass");
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
</code>
|
|
97
|
-
@return {Class}
|
|
98
|
-
*/
|
|
99
|
-
create: function(name, events, model) {
|
|
100
|
-
if (!(events instanceof Array)) {
|
|
101
|
-
model = events;
|
|
102
|
-
events = false;
|
|
103
|
-
}
|
|
104
|
-
var self = this;
|
|
105
|
-
this._models[name] = events;
|
|
106
|
-
return Class.create(name, model).extend({
|
|
107
|
-
/**
|
|
108
|
-
@doc server/
|
|
109
|
-
@property scene Sends data to the scene
|
|
110
|
-
@type Object
|
|
111
|
-
@example
|
|
112
|
-
Server :
|
|
113
|
-
<code>
|
|
114
|
-
var CE = require("canvasengine").listen(8336);
|
|
115
|
-
CE.Model.init("Main", {
|
|
116
|
-
initialize: function() {
|
|
117
|
-
this.scene.emit("foo", "Hello");
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
</code>
|
|
121
|
-
Client :
|
|
122
|
-
var Model = io.connect('http://127.0.0.1:8333');
|
|
123
|
-
|
|
124
|
-
var canvas = CE.defines("canvas").
|
|
125
|
-
ready(function() {
|
|
126
|
-
canvas.Scene.call("MyScene");
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
canvas.Scene.new({
|
|
130
|
-
name: "MyScene",
|
|
131
|
-
model: Model,
|
|
132
|
-
events: ["foo"],
|
|
133
|
-
ready: function(stage) {
|
|
134
|
-
|
|
135
|
-
},
|
|
136
|
-
foo: function(text) {
|
|
137
|
-
console.log(text);
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
*/
|
|
141
|
-
scene: {
|
|
142
|
-
/**
|
|
143
|
-
@doc server/
|
|
144
|
-
@method scene.emit (View Socket.io)
|
|
145
|
-
*/
|
|
146
|
-
emit: function(name, value) {
|
|
147
|
-
self.socket.emit(name, value);
|
|
148
|
-
},
|
|
149
|
-
/**
|
|
150
|
-
@doc server/
|
|
151
|
-
@method scene.broadcast.emit (View Socket.io)
|
|
152
|
-
*/
|
|
153
|
-
/**
|
|
154
|
-
@doc server/
|
|
155
|
-
@method scene.broadcast.to (View Socket.io)
|
|
156
|
-
*/
|
|
157
|
-
broadcast: {
|
|
158
|
-
emit: function(name, value) {
|
|
159
|
-
self.socket.broadcast.emit(name, value);
|
|
160
|
-
},
|
|
161
|
-
to: function(room, name, value) {
|
|
162
|
-
self.socket.broadcast.to(room).emit(name, value);
|
|
163
|
-
}
|
|
164
|
-
},
|
|
165
|
-
/**
|
|
166
|
-
@doc server/
|
|
167
|
-
@method scene.join (View Socket.io)
|
|
168
|
-
*/
|
|
169
|
-
join: function(room) {
|
|
170
|
-
self.socket.join(room);
|
|
171
|
-
},
|
|
172
|
-
/**
|
|
173
|
-
@doc server/
|
|
174
|
-
@method scene.leave (View Socket.io)
|
|
175
|
-
*/
|
|
176
|
-
leave: function(room) {
|
|
177
|
-
self.socket.leave(room);
|
|
178
|
-
},
|
|
179
|
-
/**
|
|
180
|
-
@doc server/
|
|
181
|
-
@method scene.disconnect (View Socket.io)
|
|
182
|
-
*/
|
|
183
|
-
disconnect: function(data) {
|
|
184
|
-
io.sockets.emit(data);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
},
|
|
190
|
-
/**
|
|
191
|
-
@doc server/
|
|
192
|
-
@method new Instantiating a model
|
|
193
|
-
@params {String} name Class name
|
|
194
|
-
@example
|
|
195
|
-
Module in "/mods/myclass.js" :
|
|
196
|
-
<code>
|
|
197
|
-
exports.Class = function(CE) {
|
|
198
|
-
var Class = CE.Model.create("MyClass", {
|
|
199
|
-
|
|
200
|
-
initialize: function() {
|
|
201
|
-
// Code
|
|
202
|
-
},
|
|
203
|
-
|
|
204
|
-
foo: function() {
|
|
205
|
-
console.log("Hello World");
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
});
|
|
209
|
-
return Class.new("MyClass");
|
|
210
|
-
}
|
|
211
|
-
</code>
|
|
212
|
-
In main JS file :
|
|
213
|
-
<code>
|
|
214
|
-
var CE = require("canvasengine").listen(8336);
|
|
215
|
-
CE.Model.init("Main", {
|
|
216
|
-
|
|
217
|
-
initialize: function() {
|
|
218
|
-
var myclass = require('./mods/myclass').Class(CE);
|
|
219
|
-
myclass.foo();
|
|
220
|
-
}
|
|
221
|
-
});
|
|
222
|
-
</code>
|
|
223
|
-
@return {Class}
|
|
224
|
-
*/
|
|
225
|
-
new: function(name) {
|
|
226
|
-
var self = this;
|
|
227
|
-
var _class = Class.new(name);
|
|
228
|
-
var events = this._models[name];
|
|
229
|
-
if (events) {
|
|
230
|
-
for (var key in events) {
|
|
231
|
-
obj = _class[events[key]];
|
|
232
|
-
if (obj) {
|
|
233
|
-
setEvent.call(this, _class, key, obj);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
function setEvent(_class, key, obj) {
|
|
239
|
-
this.socket.on(events[key], function(data) {
|
|
240
|
-
if (!data) {
|
|
241
|
-
data = {};
|
|
242
|
-
}
|
|
243
|
-
if (obj) obj.call(_class, data);
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
return _class;
|
|
248
|
-
}
|
|
249
|
-
});
|
|
250
|
-
return Class.new("ModelServer");
|
|
251
|
-
}
|