stage-js 0.9.5 → 1.0.0-alpha.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/.gitattributes +1 -0
- package/CHANGELOG.md +146 -0
- package/LICENSE.md +1 -1
- package/README.md +114 -131
- package/UPGRADE.md +26 -0
- package/dist/stage.cjs +2783 -0
- package/dist/stage.js +2783 -0
- package/dist/stage.umd.cjs +2787 -0
- package/example/basic-anim/app.js +22 -0
- package/example/basic-anim/index.html +12 -0
- package/example/basic-drawing/app.js +43 -0
- package/example/basic-drawing/index.html +15 -0
- package/example/basic-example/app.js +31 -0
- package/example/basic-example/index.html +15 -0
- package/example/basic-example/sample.png +0 -0
- package/example/basic-grid/app.js +37 -0
- package/example/basic-grid/index.html +15 -0
- package/example/basic-layout/app.js +56 -0
- package/example/basic-layout/index.html +15 -0
- package/example/basic-matrix/app.js +20 -0
- package/example/basic-matrix/box.png +0 -0
- package/example/basic-matrix/index.html +15 -0
- package/example/basic-mouse/app.js +60 -0
- package/example/basic-mouse/index.html +15 -0
- package/example/basic-package/app.js +24 -0
- package/example/basic-package/index.html +15 -0
- package/example/basic-package/main.png +0 -0
- package/example/basic-package/main.svg +6 -0
- package/example/basic-pinwheel/app.js +28 -0
- package/example/basic-pinwheel/index.html +15 -0
- package/example/basic-pinwheel/pinwheel.png +0 -0
- package/example/basic-row/app.js +25 -0
- package/example/basic-row/index.html +15 -0
- package/example/basic-row-fix/app.js +31 -0
- package/example/basic-row-fix/index.html +15 -0
- package/example/basic-stretch/app.js +20 -0
- package/example/basic-stretch/index.html +15 -0
- package/example/basic-string/app.js +19 -0
- package/example/basic-string/index.html +15 -0
- package/example/basic-tile/app.js +20 -0
- package/example/basic-tile/index.html +15 -0
- package/example/basic-tween/app.js +28 -0
- package/example/basic-tween/index.html +15 -0
- package/example/basic-window/app.js +29 -0
- package/example/basic-window/index.html +15 -0
- package/example/common/bezier.js +58 -0
- package/example/common/example.css +56 -0
- package/example/common/example.png +0 -0
- package/example/common/example.svg +23 -0
- package/example/common/p2.js +10343 -0
- package/example/common/pool.js +73 -0
- package/example/common/randomize.js +100 -0
- package/{ext → example/common}/stage-p2.js +83 -96
- package/example/common/texture.js +36 -0
- package/example/common/timeout.js +42 -0
- package/example/example.svg +23 -0
- package/example/game-aero/app.js +283 -0
- package/example/game-aero/index.html +15 -0
- package/example/game-aero/main.png +0 -0
- package/example/game-aero/main.svg +29 -0
- package/example/game-asteroids/app.js +454 -0
- package/example/game-asteroids/index.html +19 -0
- package/example/game-asteroids/main.svg +9 -0
- package/example/game-breakout/app.js +600 -0
- package/example/game-breakout/bg.png +0 -0
- package/example/game-breakout/bg.svg +11 -0
- package/example/game-breakout/index.html +13 -0
- package/example/game-breakout/logo.svg +31 -0
- package/example/game-breakout/main.png +0 -0
- package/example/game-breakout/main.svg +45 -0
- package/example/game-breakout/texture.js +51 -0
- package/example/game-o/app.js +1393 -0
- package/example/game-o/index.html +12 -0
- package/example/game-o/main.png +0 -0
- package/example/game-o/textures.js +146 -0
- package/example/game-orbit/app.js +465 -0
- package/example/game-orbit/art/android.json +11 -0
- package/example/game-orbit/art/bg.svg +62 -0
- package/example/game-orbit/art/digit.svg +140 -0
- package/example/game-orbit/art/game.json +10 -0
- package/example/game-orbit/art/icon.svg +128 -0
- package/example/game-orbit/art/ios.json +31 -0
- package/example/game-orbit/art/main.svg +56 -0
- package/example/game-orbit/art/misc.json +8 -0
- package/example/game-orbit/art/splash.svg +77 -0
- package/example/game-orbit/index.html +14 -0
- package/example/game-orbit/media/bg.png +0 -0
- package/example/game-orbit/media/digit.png +0 -0
- package/example/game-orbit/media/main.png +0 -0
- package/example/game-orbit/textures.js +40 -0
- package/example/game-samegame/app.js +229 -0
- package/example/game-samegame/index.html +15 -0
- package/example/game-samegame/main.png +0 -0
- package/example/game-samegame/main.svg +29 -0
- package/example/game-samegame/textures.js +15 -0
- package/example/game-tictactoe/app.js +175 -0
- package/example/game-tictactoe/index.html +15 -0
- package/example/game-traffic/app.js +353 -0
- package/example/game-traffic/base.svg +4 -0
- package/example/game-traffic/bg.png +0 -0
- package/example/game-traffic/bg.svg +10 -0
- package/example/game-traffic/index.html +15 -0
- package/example/game-traffic/main.png +0 -0
- package/example/game-traffic/map-1.png +0 -0
- package/example/game-traffic/map-1.svg +5 -0
- package/example/game-traffic/textures.js +24 -0
- package/example/index.html +31 -0
- package/index.d.ts +449 -0
- package/index.html +7 -0
- package/package.json +25 -20
- package/{lib → src}/anim.js +12 -17
- package/src/core.js +664 -0
- package/src/drawable.js +439 -0
- package/{lib/addon → src}/easing.js +45 -45
- package/src/index-x.js +16 -0
- package/src/index.js +3 -0
- package/{lib → src}/layout.js +20 -26
- package/src/matrix.js +156 -0
- package/src/mouse.js +232 -0
- package/{lib → src}/pin.js +19 -54
- package/src/root.js +325 -0
- package/{lib/util/repeat.js → src/sprite.js} +66 -2
- package/{lib → src}/str.js +12 -16
- package/src/tween.js +194 -0
- package/{lib → src}/util/math.js +13 -8
- package/src/util/stats.js +7 -0
- package/test/atlas-test.js +5 -5
- package/test/event-test.js +2 -2
- package/test/main-test.js +2 -4
- package/test/mouse-test.js +3 -3
- package/test/node-test.js +1 -1
- package/test/pin-test.js +2 -2
- package/test/tween-test.js +2 -2
- package/test/util/expect.js +1 -1
- package/test/util/memo.js +1 -1
- package/vite.config.js +22 -0
- package/dist/stage.cordova.js +0 -4035
- package/dist/stage.cordova.js.map +0 -1
- package/dist/stage.cordova.min.js +0 -36
- package/dist/stage.cordova.min.js.map +0 -1
- package/dist/stage.web.js +0 -3984
- package/dist/stage.web.js.map +0 -1
- package/dist/stage.web.min.js +0 -36
- package/dist/stage.web.min.js.map +0 -1
- package/ext/stage-matter.js +0 -201
- package/ext/stage-physics.js +0 -167
- package/ext/stage-planck.js +0 -355
- package/ext/stage-sat.js +0 -197
- package/lib/addon/mouse.js +0 -206
- package/lib/addon/tween.js +0 -189
- package/lib/atlas.js +0 -240
- package/lib/canvas.js +0 -51
- package/lib/core.js +0 -221
- package/lib/event.js +0 -3
- package/lib/image.js +0 -72
- package/lib/index.js +0 -10
- package/lib/loader/cordova.js +0 -186
- package/lib/loader/web.js +0 -151
- package/lib/loop.js +0 -136
- package/lib/matrix.js +0 -169
- package/lib/root.js +0 -136
- package/lib/texture.js +0 -103
- package/lib/tree.js +0 -410
- package/lib/util/await.js +0 -28
- package/lib/util/create.js +0 -16
- package/lib/util/event.js +0 -71
- package/lib/util/extend.js +0 -11
- package/lib/util/insert.js +0 -16
- package/lib/util/is.js +0 -167
- package/lib/util/memo.js +0 -13
- package/lib/util/now.js +0 -13
- package/lib/util/once.js +0 -9
- package/lib/util/stats.js +0 -1
- package/lib/util/string.js +0 -4
- package/platform/cordova.js +0 -16
- package/platform/web.js +0 -16
package/.gitattributes
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
* text=auto
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
#### v1.0 [work in progress]
|
|
2
|
+
* use ES modules and classes
|
|
3
|
+
* typescript definition added
|
|
4
|
+
* lib directory renamed to src, and folder structure flattened
|
|
5
|
+
* all classes are exported under Stage namespace
|
|
6
|
+
* added Stage.mount()
|
|
7
|
+
* Stage is not callable
|
|
8
|
+
* removed Stage.internal
|
|
9
|
+
* removed Stage() (use other functions to create components)
|
|
10
|
+
* removed Stage({ }), and await Stage.atlas({ }) preloads images
|
|
11
|
+
* removed Stage(function(root, canvas))
|
|
12
|
+
* removed Stage.app(callback)
|
|
13
|
+
* removed script loader `preload(url)`
|
|
14
|
+
* removed relative path resolver for texture images
|
|
15
|
+
* removed cordova/fastcontext loader and build removed
|
|
16
|
+
* removed different platform builds (web, cordova)
|
|
17
|
+
* removed Stage.config()
|
|
18
|
+
* removed _create, _extend functions with js functions
|
|
19
|
+
* Ease() replaced by Ease.get()
|
|
20
|
+
* removed Stage.Math, added Stage.math with different function names
|
|
21
|
+
* renamed sequence to align
|
|
22
|
+
* removed Matrix.reverse (use inverse)
|
|
23
|
+
* removed Mouse.subscribe, replaced by new Mouse().mount
|
|
24
|
+
* renamed Image/image to Sprite/sprite, and .image() to .texture()
|
|
25
|
+
|
|
26
|
+
#### v0.8.2
|
|
27
|
+
* `render.js` renamed to `loop.js`
|
|
28
|
+
* `node.js` renamed to `tree.js`
|
|
29
|
+
* cordova starts by mousemove
|
|
30
|
+
|
|
31
|
+
#### v0.8.1
|
|
32
|
+
* `node.scaleTo()` shortcut pinning method added
|
|
33
|
+
* `node.matrix(true)` returns relative matrix instead of absolute
|
|
34
|
+
|
|
35
|
+
#### v0.8.0
|
|
36
|
+
* **default `.tween()` behavior changed from append to replace**
|
|
37
|
+
* `.tween(duration, delay)` changed to `.tween(duration, delay, append = false)`
|
|
38
|
+
* `tween.clear()` deprecated and no-op
|
|
39
|
+
* `tween.end()` replaced by `tween.done()`
|
|
40
|
+
|
|
41
|
+
#### v0.7.1
|
|
42
|
+
* `tween.then()` replaced by `tween.end()`
|
|
43
|
+
* `tween.remove/hide()` are added
|
|
44
|
+
|
|
45
|
+
#### v0.7.0
|
|
46
|
+
* **images and script urls starting with `./` are resolved as relative**
|
|
47
|
+
|
|
48
|
+
#### v0.6.7
|
|
49
|
+
* Stage.Math extends native Math
|
|
50
|
+
|
|
51
|
+
#### v0.6.6
|
|
52
|
+
* lookup atlas by name before textures
|
|
53
|
+
* `node.width/height()` pinning shortcuts return value
|
|
54
|
+
* preloadScript('url') removed
|
|
55
|
+
|
|
56
|
+
#### v0.6.5
|
|
57
|
+
* **.preload('url.js') added**
|
|
58
|
+
|
|
59
|
+
#### v0.6.4
|
|
60
|
+
* pinning shortcuts are added to nodes
|
|
61
|
+
* entire atlas can be referenced
|
|
62
|
+
|
|
63
|
+
#### v0.6.2
|
|
64
|
+
* `atlas.image.url` renamed to `src`
|
|
65
|
+
* pining shorthand methods added to node
|
|
66
|
+
* tick-based `timeout(fn, delay)` method added to node
|
|
67
|
+
* entire atlas can be used as texture
|
|
68
|
+
|
|
69
|
+
#### v0.6.0
|
|
70
|
+
* **Cut/CutJS renamed to Stage/Stage.js**
|
|
71
|
+
|
|
72
|
+
#### v0.5.0
|
|
73
|
+
* atlas spec changed
|
|
74
|
+
* **.preload(fn) added**
|
|
75
|
+
* node.insert/append/prepend() methods accept array
|
|
76
|
+
* node.id() renamed to node.label()
|
|
77
|
+
* Cut.addTexture() removed
|
|
78
|
+
* image.cropY/cropY() removed
|
|
79
|
+
* anim.gotoLabel() removed
|
|
80
|
+
* Cut.Anim.FPS is not public anymore
|
|
81
|
+
* drawing() replaced with .canvas()
|
|
82
|
+
* **Texture renamed to Atlas, new Texture class replaced Cut.Out**
|
|
83
|
+
* node._cutouts and Cut.cutout() replaced with node._textures and Cut.texture()
|
|
84
|
+
* Cut.texture() returns selection object
|
|
85
|
+
* image-loader spec changed
|
|
86
|
+
|
|
87
|
+
#### v0.4.12
|
|
88
|
+
* root.background method added
|
|
89
|
+
|
|
90
|
+
#### v0.4.10
|
|
91
|
+
* **game loop continues if any tick returns true, but only renders if touched**
|
|
92
|
+
|
|
93
|
+
#### v0.4.9
|
|
94
|
+
* texture.ratio renamed to texture.ppu
|
|
95
|
+
|
|
96
|
+
#### v0.4.8
|
|
97
|
+
* new drawing (experimental) and cutout (internal) api
|
|
98
|
+
|
|
99
|
+
#### v0.4.3
|
|
100
|
+
* pin set/get is refactored
|
|
101
|
+
* resize in/out replace with scale in-pad/out-crop
|
|
102
|
+
* default viewbox mode is 'in-pad'
|
|
103
|
+
|
|
104
|
+
#### v0.4.0
|
|
105
|
+
* **CommonJS/Node.js files and project structure**
|
|
106
|
+
* Source files moved to `lib` and platform files to `platform`
|
|
107
|
+
* extension files moved to ext
|
|
108
|
+
* `lib/main` set as npm `main`
|
|
109
|
+
* texture name made optional in cutout selector: `"[texture:]cutout"`
|
|
110
|
+
* FastContext support added to Cordova, FastCanvas loader removed
|
|
111
|
+
|
|
112
|
+
* `Cut.Out.select` replaced with `Cut.cutout`
|
|
113
|
+
* `Cut.config` and `Cut.start(configs)` are added
|
|
114
|
+
* `Cut.init` and `Loader.loadImage` replaced with `"app-loader"` and `"image-loader"` configs
|
|
115
|
+
* `Loader` merged with `Root`
|
|
116
|
+
* `Loader.start/pause/resume` replaced with `Cut.start/pause/resume`
|
|
117
|
+
* `root._ratio` replaced with `root.viewport().ratio`
|
|
118
|
+
* `_isCut` replaced with `Cut._ensure`
|
|
119
|
+
* `Texture` reorganized
|
|
120
|
+
* `root.resize()` renamed to `viewport()`
|
|
121
|
+
|
|
122
|
+
#### v0.3.1
|
|
123
|
+
* Remove `Mouse(..., captureAnyMove)` and use flag instead
|
|
124
|
+
* Refactoring easing class and moving tween/ease to another file
|
|
125
|
+
|
|
126
|
+
#### v0.3.0
|
|
127
|
+
* **Browserified**
|
|
128
|
+
* Synthetic `Mouse` clicks instead of browser clicks
|
|
129
|
+
* New `off` method to remove listeners
|
|
130
|
+
* New `data` parameter for `visit` methods
|
|
131
|
+
* `_listens` renamed to `_flag`
|
|
132
|
+
* `_isFunc`, `_function`, `_options` and `_status` are removed
|
|
133
|
+
* `_extend` signature changed
|
|
134
|
+
|
|
135
|
+
#### v0.2.1
|
|
136
|
+
* `setImage/Value/Frames/Font` deprecated and replaced by `image/value/frames`
|
|
137
|
+
|
|
138
|
+
#### v0.2.0
|
|
139
|
+
* mouse event listener signature changed from `(rawEvent, {x, y})` to `({x, y, raw})`
|
|
140
|
+
* `viewport` listener signature changed from `(width, height)` to `({width, height})`
|
|
141
|
+
* `Cut.addTexture(...)` is replaced with `Cut(...)`
|
|
142
|
+
* `spy(true)` is replaced with `attr('spy', true)`
|
|
143
|
+
* tweening support for pinning XY-shorthands added
|
|
144
|
+
* `drawing` signature changed from `([name], width, height, [ratio], callback, [def])` to `([name], width, height, [ratio], [callback])` and `(def, [callback])`
|
|
145
|
+
* node.trigger() method added, calls .publish() and returns `this`
|
|
146
|
+
* using deep listeners count instead of `spy` to optimized mouse event distribution
|
package/LICENSE.md
CHANGED
package/README.md
CHANGED
|
@@ -1,101 +1,81 @@
|
|
|
1
1
|
[](http://piqnt.com/stage.js/)
|
|
2
2
|
|
|
3
|
-
Stage.js is a 2D
|
|
3
|
+
Stage.js is a 2D rendering and layout library for HTML5 Canvas. It is lightweight, fast and optimized for web and mobile game development.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
### Features
|
|
6
|
+
- Tree object model, similar to DOM, for composing an application
|
|
7
|
+
- Managed and optimized rendering and game loop
|
|
8
|
+
- Mouse events processing and delivering them to target nodes
|
|
9
|
+
- Positioning and layout
|
|
10
|
+
- Texture atlas and image preloading
|
|
6
11
|
|
|
7
|
-
[
|
|
12
|
+
[**Live Examples and Demos**](http://piqnt.com/stage.js/)
|
|
8
13
|
|
|
9
14
|
[中文手册](https://github.com/shakiba/stage.js/wiki/%E4%B8%AD%E6%96%87%E6%89%8B%E5%86%8C)
|
|
10
15
|
|
|
16
|
+
## v1.0-alpha [Work in Progress] Upgrade and Breaking Changes
|
|
11
17
|
|
|
12
|
-
|
|
18
|
+
Since the initial development of Stage.js in 2013, web technology has changed a lot. The new version of Stage.js is updated to take advantage of new technologies to simplify the API. There are backward incompatible changes in the new version. Please see the [upgrade](./UPGRADE.md) doc for more information.
|
|
13
19
|
|
|
14
|
-
|
|
15
|
-
You need to manually draw your application and manage rendering cycles to play it.
|
|
16
|
-
Moreover, mouse events are only available at the entire Canvas level and they also need to be processed manually.
|
|
20
|
+
## Install
|
|
17
21
|
|
|
18
|
-
Stage.js provides a DOM-like tree data model to compose your application and internally manages rendering cycles and the drawing of your application.
|
|
19
|
-
It also processes and distributes mouse and touch events to targeted tree nodes.
|
|
20
|
-
A Stage.js application consists of a tree of nodes. Each node is pinned (transformed) against its parent and has zero, one or more image textures.
|
|
21
22
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
Rendering is retained and is paused when there is no change.
|
|
25
|
-
|
|
26
|
-
### Example
|
|
27
|
-
|
|
28
|
-
```js
|
|
29
|
-
// Create new app
|
|
30
|
-
Stage(function(stage) {
|
|
31
|
-
|
|
32
|
-
// Set view box
|
|
33
|
-
stage.viewbox(300, 200);
|
|
34
|
-
|
|
35
|
-
// Create an image and append it to stage
|
|
36
|
-
var box = Stage.image('box').appendTo(stage);
|
|
37
|
-
|
|
38
|
-
// Align box to center
|
|
39
|
-
box.pin('align', 0.5);
|
|
40
|
-
|
|
41
|
-
// On mouse click...
|
|
42
|
-
box.on('click', function(point) {
|
|
43
|
-
// ...tween scale values of this node
|
|
44
|
-
this.tween().ease('bounce').pin({
|
|
45
|
-
scaleX : Math.random() + 0.5,
|
|
46
|
-
scaleY : Math.random() + 0.5
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
});
|
|
23
|
+
#### Browser
|
|
50
24
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
});
|
|
25
|
+
```html
|
|
26
|
+
<script src="https://cdn.jsdelivr.net/npm/stage-js@1.0"></script>
|
|
27
|
+
<script>
|
|
28
|
+
const app = Stage.mount();
|
|
29
|
+
</script>
|
|
58
30
|
```
|
|
59
31
|
|
|
60
|
-
|
|
61
|
-
## Installation
|
|
62
|
-
|
|
63
|
-
#### Download
|
|
64
|
-
Latest builds are available in the project [releases page](https://github.com/shakiba/stage.js/releases/latest).
|
|
65
|
-
|
|
66
32
|
#### NPM
|
|
67
33
|
|
|
68
34
|
```
|
|
69
|
-
npm install stage-js
|
|
35
|
+
npm install --save stage-js
|
|
70
36
|
```
|
|
71
37
|
|
|
72
|
-
|
|
73
|
-
|
|
38
|
+
```js
|
|
39
|
+
const Stage = require('stage-js');
|
|
74
40
|
```
|
|
75
|
-
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
import Stage from 'stage-js';
|
|
76
44
|
```
|
|
77
45
|
|
|
46
|
+
### Example
|
|
78
47
|
|
|
79
|
-
|
|
48
|
+
```js
|
|
49
|
+
// Define and preload a texture
|
|
50
|
+
await Stage.atlas({
|
|
51
|
+
image : 'sample.png',
|
|
52
|
+
textures : {
|
|
53
|
+
box : { x : 0, y : 0, width : 30, height : 30 }
|
|
54
|
+
}
|
|
55
|
+
});
|
|
80
56
|
|
|
81
|
-
|
|
57
|
+
// Create and mount a new app
|
|
58
|
+
const app = Stage.mount();
|
|
82
59
|
|
|
83
|
-
|
|
60
|
+
// Set view box
|
|
61
|
+
app.viewbox(300, 200);
|
|
84
62
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
<script src="path/to/your-app.js"></script>
|
|
88
|
-
```
|
|
63
|
+
// Create an sprite and append it to app
|
|
64
|
+
const box = Stage.sprite('box').appendTo(app);
|
|
89
65
|
|
|
90
|
-
|
|
91
|
-
|
|
66
|
+
// Align box to center
|
|
67
|
+
box.pin('align', 0.5);
|
|
92
68
|
|
|
93
|
-
|
|
94
|
-
|
|
69
|
+
// On mouse click...
|
|
70
|
+
box.on('click', function(point) {
|
|
71
|
+
// ...tween scale values of this node
|
|
72
|
+
this.tween().ease('bounce').pin({
|
|
73
|
+
scaleX : Math.random() + 0.5,
|
|
74
|
+
scaleY : Math.random() + 0.5
|
|
75
|
+
});
|
|
76
|
+
});
|
|
95
77
|
```
|
|
96
78
|
|
|
97
|
-
See `platform` directory for other available modules.
|
|
98
|
-
|
|
99
79
|
|
|
100
80
|
## Resources
|
|
101
81
|
|
|
@@ -106,37 +86,42 @@ See `platform` directory for other available modules.
|
|
|
106
86
|
|
|
107
87
|
## API Doc
|
|
108
88
|
|
|
109
|
-
###
|
|
110
|
-
An application is created by
|
|
111
|
-
|
|
89
|
+
### Setup
|
|
90
|
+
An application is created by calling `Stage.mount()`.
|
|
91
|
+
This will set up the root node with a canvas element, starts the application, and returns the root node.
|
|
112
92
|
|
|
113
93
|
```javascript
|
|
114
|
-
// Create and start an application
|
|
115
|
-
Stage(
|
|
94
|
+
// Create and start an application (with a default full page canvas)
|
|
95
|
+
const app = Stage.mount();
|
|
116
96
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
stage.on('viewport', function(viewport) {
|
|
122
|
-
// `viewport` attributes are `width`, `height` and `ratio`
|
|
123
|
-
});
|
|
97
|
+
// Create and start an application with a custom canvas element
|
|
98
|
+
const app = Stage.mount({
|
|
99
|
+
canvas : document.getElementById('game-canvas')
|
|
100
|
+
});
|
|
124
101
|
|
|
125
|
-
|
|
126
|
-
|
|
102
|
+
// Set viewbox for stage, see pinning for valid modes
|
|
103
|
+
app.viewbox(width, height, mode = 'in-pad');
|
|
127
104
|
|
|
128
|
-
|
|
129
|
-
|
|
105
|
+
// Listen to view port resize events
|
|
106
|
+
app.on('viewport', function(viewport) {
|
|
107
|
+
// `viewport` attributes are `width`, `height` and `ratio`
|
|
130
108
|
});
|
|
109
|
+
|
|
110
|
+
// Pause playing
|
|
111
|
+
app.pause();
|
|
112
|
+
|
|
113
|
+
// Resume playing
|
|
114
|
+
app.resume();
|
|
115
|
+
|
|
131
116
|
```
|
|
132
117
|
|
|
133
118
|
|
|
134
119
|
### Tree Model
|
|
135
|
-
Every
|
|
120
|
+
Every application consists of a tree. The tree's root is created and returned by `Stage.mount()`.
|
|
136
121
|
|
|
137
122
|
```javascript
|
|
138
123
|
// Create a new node instance (with no textures)
|
|
139
|
-
|
|
124
|
+
const node = Stage.create();
|
|
140
125
|
|
|
141
126
|
// Append/prepend child to parent's children (accepts array)
|
|
142
127
|
parent.append(child);
|
|
@@ -182,12 +167,12 @@ node.hide();
|
|
|
182
167
|
node.show();
|
|
183
168
|
|
|
184
169
|
// Iterate over parent's children, child can not be removed
|
|
185
|
-
for (
|
|
170
|
+
for (let child = parent.first(); child; child = child.next()) {
|
|
186
171
|
// use child
|
|
187
172
|
}
|
|
188
173
|
|
|
189
174
|
// Iterate over parent's children, child can be removed
|
|
190
|
-
|
|
175
|
+
let child, next = parent.first();
|
|
191
176
|
while (child = next) {
|
|
192
177
|
next = child.next();
|
|
193
178
|
// use child
|
|
@@ -209,7 +194,7 @@ node.visit({
|
|
|
209
194
|
|
|
210
195
|
### Game Loop
|
|
211
196
|
Each rendering cycle consists of ticking and redrawing the application tree.
|
|
212
|
-
|
|
197
|
+
An application and its nodes can be updated during ticking.
|
|
213
198
|
Depending on application activities there can be three different follow-ups after ticking:
|
|
214
199
|
|
|
215
200
|
* If at least one node is touched then the entire application tree will be redrawn and the game loop will continue.
|
|
@@ -403,13 +388,13 @@ Stage.Mouse.CANCEL = 'touchcancel mousecancel';
|
|
|
403
388
|
Textures are drawable objects which are used by tree nodes to draw graphics on the Canvas surface.
|
|
404
389
|
|
|
405
390
|
### Texture Atlas
|
|
406
|
-
A texture atlas (sprite sheet) consists of a set of named textures
|
|
391
|
+
A texture atlas (sprite sheet) consists of a set of named textures that are referenced by name in an application.
|
|
407
392
|
|
|
408
393
|
Atlases are usually created using static image files. Images referenced in atlases are automatically preloaded.
|
|
409
394
|
|
|
410
395
|
```javascript
|
|
411
396
|
// Adding texture atlas
|
|
412
|
-
Stage({
|
|
397
|
+
Stage.atlas({
|
|
413
398
|
name : 'mario', // optional
|
|
414
399
|
image : {
|
|
415
400
|
src : 'mario.png',
|
|
@@ -437,30 +422,27 @@ Stage({
|
|
|
437
422
|
}
|
|
438
423
|
});
|
|
439
424
|
|
|
440
|
-
Stage.
|
|
425
|
+
Stage.sprite('mario:stand');
|
|
441
426
|
|
|
442
427
|
Stage.anim('mario:walk').play();
|
|
443
428
|
|
|
444
429
|
Stage.string('mario:number').value(100);
|
|
445
430
|
```
|
|
446
431
|
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
### Image
|
|
451
|
-
An image is a node with one texture.
|
|
432
|
+
### Sprite
|
|
433
|
+
A sprite is a node with one texture.
|
|
452
434
|
|
|
453
435
|
```javascript
|
|
454
|
-
// Create a new
|
|
455
|
-
|
|
436
|
+
// Create a new sprite instance
|
|
437
|
+
let sprite = Stage.sprite(texture);
|
|
456
438
|
|
|
457
|
-
// Change
|
|
458
|
-
|
|
439
|
+
// Change sprite texture
|
|
440
|
+
sprite.image(texture);
|
|
459
441
|
|
|
460
|
-
// Tile/Stretch
|
|
442
|
+
// Tile/Stretch sprite when pinning width and/or height
|
|
461
443
|
// To define borders add top, bottom, left and right to texture
|
|
462
|
-
|
|
463
|
-
|
|
444
|
+
sprite.tile();
|
|
445
|
+
sprite.stretch();
|
|
464
446
|
```
|
|
465
447
|
|
|
466
448
|
|
|
@@ -469,7 +451,7 @@ An animation or anim is a node with an array of textures as frames.
|
|
|
469
451
|
|
|
470
452
|
```javascript
|
|
471
453
|
// Create a new anim instance
|
|
472
|
-
|
|
454
|
+
const anim = Stage.anim(textures, fps = 15);
|
|
473
455
|
|
|
474
456
|
// Get or set animation frame-per-second
|
|
475
457
|
anim.fps();
|
|
@@ -499,11 +481,11 @@ anim.repeat(repeat, callback = null);
|
|
|
499
481
|
|
|
500
482
|
|
|
501
483
|
### String
|
|
502
|
-
String is a row of
|
|
484
|
+
String is a row of textures that are dynamically selected from `frames` using characters of a string value, or items of an array value.
|
|
503
485
|
|
|
504
486
|
```javascript
|
|
505
487
|
// Create a new string instance with frames
|
|
506
|
-
|
|
488
|
+
const string = Stage.string(frames);
|
|
507
489
|
|
|
508
490
|
// Set frames, a string referencing a map in an atlas
|
|
509
491
|
string.frames("digits");
|
|
@@ -531,12 +513,12 @@ string.value();
|
|
|
531
513
|
|
|
532
514
|
|
|
533
515
|
### Row and Column
|
|
534
|
-
A row/column is a node
|
|
516
|
+
A row/column is a node that aligns its children in a horizontal/vertical direction.
|
|
535
517
|
|
|
536
518
|
```javascript
|
|
537
519
|
// Create a new row/column
|
|
538
|
-
|
|
539
|
-
|
|
520
|
+
const row = Stage.row(childrenAlignY = 0);
|
|
521
|
+
const column = Stage.column(childrenAlignX = 0);
|
|
540
522
|
|
|
541
523
|
// Make node a row/column
|
|
542
524
|
node.row(childrenAlignY = 0);
|
|
@@ -548,25 +530,37 @@ node.spacing(space);
|
|
|
548
530
|
|
|
549
531
|
|
|
550
532
|
### Box (experimental)
|
|
551
|
-
A box resizes to
|
|
552
|
-
images to create variable
|
|
533
|
+
A box resizes to cover its children. It can be applied to tiled/stretched
|
|
534
|
+
images, for example, to create variable-size nodes such as windows and buttons.
|
|
553
535
|
|
|
554
536
|
```javascript
|
|
555
537
|
// Create a new box
|
|
556
|
-
|
|
538
|
+
const box = Stage.box();
|
|
557
539
|
|
|
558
540
|
// Make node a box
|
|
559
541
|
node = node.box();
|
|
560
542
|
```
|
|
561
543
|
|
|
562
544
|
|
|
545
|
+
### Layer (experimental)
|
|
546
|
+
A layer resizes to cover its parent.
|
|
547
|
+
|
|
548
|
+
```javascript
|
|
549
|
+
// Create a new layer
|
|
550
|
+
const box = Stage.box();
|
|
551
|
+
|
|
552
|
+
// Make node a layer
|
|
553
|
+
node = node.layer();
|
|
554
|
+
```
|
|
555
|
+
|
|
556
|
+
|
|
563
557
|
### Tweening
|
|
564
558
|
Tweening is used to apply smooth transitions to pinning values.
|
|
565
559
|
|
|
566
560
|
```javascript
|
|
567
561
|
// Create a tweening entry
|
|
568
562
|
// When `append` is true new entry is appended to current entries otherwise replaces them
|
|
569
|
-
|
|
563
|
+
const tween = node.tween(duration = 400, delay = 0, append = false);
|
|
570
564
|
|
|
571
565
|
// Set pinning values and start tweening
|
|
572
566
|
// Pinning shortcut methods, such as `.scale()`, can also be used
|
|
@@ -597,29 +591,18 @@ tween.remove();
|
|
|
597
591
|
tween.hide();
|
|
598
592
|
|
|
599
593
|
// Create and chain a new tweening to this entry
|
|
600
|
-
|
|
594
|
+
const nextTween = tween.tween(duration = 400, delay = 0);
|
|
601
595
|
```
|
|
602
596
|
|
|
603
597
|
|
|
604
598
|
### Global Methods
|
|
605
599
|
```javascript
|
|
606
600
|
|
|
607
|
-
//
|
|
608
|
-
|
|
601
|
+
// Mount and start a new app
|
|
602
|
+
let app = Stage.mount();
|
|
609
603
|
|
|
610
604
|
// Create and preload a texture atlas
|
|
611
|
-
Stage({ });
|
|
612
|
-
|
|
613
|
-
// A function to be called before starting any app
|
|
614
|
-
// Can be used for preloading application assets
|
|
615
|
-
Stage.preload(function(done) {
|
|
616
|
-
// Call `done` when loaded or failed
|
|
617
|
-
done(error);
|
|
618
|
-
});
|
|
619
|
-
|
|
620
|
-
// Preload and execute a JS file
|
|
621
|
-
// URLs starting with `./` are resolved relative to current script URL
|
|
622
|
-
Stage.preload(src);
|
|
605
|
+
Stage.atlas({ ... });
|
|
623
606
|
|
|
624
607
|
// Pause playing all applications
|
|
625
608
|
Stage.pause();
|
|
@@ -630,12 +613,12 @@ Stage.resume();
|
|
|
630
613
|
|
|
631
614
|
|
|
632
615
|
## Development
|
|
633
|
-
To try examples with a live build run following command and check output for the URL to open in your browser:
|
|
616
|
+
To try examples with a live build run the following command and check the output for the URL to open in your browser:
|
|
634
617
|
```
|
|
635
618
|
npm run dev
|
|
636
619
|
```
|
|
637
620
|
|
|
638
621
|
|
|
639
622
|
## License
|
|
640
|
-
Copyright
|
|
623
|
+
Copyright 2023 Ali Shakiba http://shakiba.me/stage.js
|
|
641
624
|
Available under the MIT License
|
package/UPGRADE.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
|
|
2
|
+
## V1 [Work in Progress] Upgrade and Breaking Changes
|
|
3
|
+
|
|
4
|
+
Since the initial development of Stage.js in 2013, web technology has changed a lot. The new version of Stage.js is updated to take advantage of new web technologies to simplify the API, and there are backward incompatible changes in the new version.
|
|
5
|
+
|
|
6
|
+
#### Importing and Starting
|
|
7
|
+
The first important change is using ES classes and exports. Now all classes are exported under `Stage` namespace, and `Stage` is not a function or a type anymore. In addition, preloading images and creating textures are separated from creating and mounting an app, and you need to use `await` to wait for the preloading to finish.
|
|
8
|
+
|
|
9
|
+
- Use `stage = Stage.mount()` to create and mount a new app, instead of `Stage(function(stage){ }))`.
|
|
10
|
+
- Use `await Stage.atlas({ })` to preload images and create textures before using them, instead of `Stage({ })`.
|
|
11
|
+
- Use `Stage.Node` as the base class for your custom nodes, instead of `Stage`.
|
|
12
|
+
|
|
13
|
+
#### Build
|
|
14
|
+
The second change is build-files. There are multiple build outputs for ES, CommonJS, and UMD.
|
|
15
|
+
|
|
16
|
+
FastContext support is dropped in this version, and there is one build file for all platforms. FastContext was used to take advantage of native GPU rendering in Android devices when browser Canvas rendering was not GPU optimized, however this is not needed anymore.
|
|
17
|
+
|
|
18
|
+
#### Relative Paths
|
|
19
|
+
The second important change is that relative image paths for textures are not resolved relative to the script anymore. You need to use absolute paths or resolve them yourself using a build tool.
|
|
20
|
+
|
|
21
|
+
#### Other API Changes
|
|
22
|
+
There are also smaller API changes:
|
|
23
|
+
- 'Stage.Image` and `Stage.image()` are renamed to `Stage.Sprite` and `Stage.sprite()` (to avoid overlapping with native Image class).
|
|
24
|
+
- `Stage.Math` is renamed to `Stage.math` and extends native `Math` (to avoid overlapping with native Math class).
|
|
25
|
+
|
|
26
|
+
For a complete list of changes, see [CHANGELOG.md](CHANGELOG.md).
|