stage-js 0.9.5 → 1.0.0-alpha.11

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 (273) hide show
  1. package/.eslintrc.json +36 -0
  2. package/.gitattributes +1 -0
  3. package/.prettierignore +2 -0
  4. package/.prettierrc.json +3 -0
  5. package/CHANGELOG.md +173 -0
  6. package/LICENSE.md +4 -2
  7. package/README.md +12 -631
  8. package/dist/stage.d.ts +558 -0
  9. package/dist/stage.js +3620 -0
  10. package/dist/stage.js.map +1 -0
  11. package/dist/stage.umd.cjs +3624 -0
  12. package/dist/stage.umd.cjs.map +1 -0
  13. package/docs/package-lock.json +12922 -0
  14. package/docs/package.json +18 -0
  15. package/docs/pages/anim.md +34 -0
  16. package/docs/pages/api/classes/Anim.md +1357 -0
  17. package/docs/pages/api/classes/Atlas.md +281 -0
  18. package/docs/pages/api/classes/CanvasTexture.md +317 -0
  19. package/docs/pages/api/classes/ImageTexture.md +256 -0
  20. package/docs/pages/api/classes/Matrix.md +283 -0
  21. package/docs/pages/api/classes/Monotype.md +1291 -0
  22. package/docs/pages/api/classes/Node.md +1030 -0
  23. package/docs/pages/api/classes/Pin.md +41 -0
  24. package/docs/pages/api/classes/PipeTexture.md +231 -0
  25. package/docs/pages/api/classes/ResizableTexture.md +219 -0
  26. package/docs/pages/api/classes/Root.md +1400 -0
  27. package/docs/pages/api/classes/Sprite.md +1285 -0
  28. package/docs/pages/api/classes/Texture.md +175 -0
  29. package/docs/pages/api/classes/TextureSelection.md +61 -0
  30. package/docs/pages/api/classes/Transition.md +417 -0
  31. package/docs/pages/api/functions/anim.md +13 -0
  32. package/docs/pages/api/functions/atlas.md +11 -0
  33. package/docs/pages/api/functions/canvas.md +9 -0
  34. package/docs/pages/api/functions/column.md +11 -0
  35. package/docs/pages/api/functions/component.md +7 -0
  36. package/docs/pages/api/functions/maximize.md +7 -0
  37. package/docs/pages/api/functions/minimize.md +7 -0
  38. package/docs/pages/api/functions/monotype.md +11 -0
  39. package/docs/pages/api/functions/mount.md +11 -0
  40. package/docs/pages/api/functions/pause.md +7 -0
  41. package/docs/pages/api/functions/resume.md +7 -0
  42. package/docs/pages/api/functions/row.md +11 -0
  43. package/docs/pages/api/functions/sprite.md +11 -0
  44. package/docs/pages/api/functions/texture.md +15 -0
  45. package/docs/pages/api/index.md +68 -0
  46. package/docs/pages/api/interfaces/AtlasDefinition.md +109 -0
  47. package/docs/pages/api/interfaces/AtlasTextureDefinition.md +49 -0
  48. package/docs/pages/api/interfaces/MatrixValue.md +37 -0
  49. package/docs/pages/api/interfaces/Vec2Value.md +13 -0
  50. package/docs/pages/api/type-aliases/FitMode.md +7 -0
  51. package/docs/pages/api/type-aliases/NodeEventListener.md +17 -0
  52. package/docs/pages/api/type-aliases/NodeTickListener.md +21 -0
  53. package/docs/pages/api/type-aliases/ResizableTextureMode.md +3 -0
  54. package/docs/pages/api/type-aliases/TextureSelectionInput.md +11 -0
  55. package/docs/pages/api/type-aliases/TextureSelectionInputArray.md +3 -0
  56. package/docs/pages/api/type-aliases/TextureSelectionInputFactory.md +11 -0
  57. package/docs/pages/api/type-aliases/TextureSelectionInputMap.md +3 -0
  58. package/docs/pages/api/type-aliases/TextureSelectionInputOne.md +3 -0
  59. package/docs/pages/api/type-aliases/TransitionEndListener.md +11 -0
  60. package/docs/pages/api/type-aliases/TransitionOptions.md +17 -0
  61. package/docs/pages/api/type-aliases/Viewbox.md +27 -0
  62. package/docs/pages/api/type-aliases/Viewport.md +19 -0
  63. package/docs/pages/api/variables/POINTER_CANCEL.md +3 -0
  64. package/docs/pages/api/variables/POINTER_CLICK.md +3 -0
  65. package/docs/pages/api/variables/POINTER_DOWN.md +3 -0
  66. package/docs/pages/api/variables/POINTER_MOVE.md +3 -0
  67. package/docs/pages/api/variables/POINTER_UP.md +3 -0
  68. package/docs/pages/api/variables/math.md +3 -0
  69. package/docs/pages/atlas.md +42 -0
  70. package/docs/pages/events.md +47 -0
  71. package/docs/pages/hello-world.md +60 -0
  72. package/docs/pages/index.md +15 -0
  73. package/docs/pages/install.md +24 -0
  74. package/docs/pages/layout.md +40 -0
  75. package/docs/pages/loop.md +25 -0
  76. package/docs/pages/monotype.md +32 -0
  77. package/docs/pages/news/v1-0.md +37 -0
  78. package/docs/pages/pin.md +128 -0
  79. package/docs/pages/setup.md +46 -0
  80. package/docs/pages/sprite.md +17 -0
  81. package/docs/pages/texture.md +4 -0
  82. package/docs/pages/transition.md +46 -0
  83. package/docs/pages/tree.md +77 -0
  84. package/docs/styles.css +0 -0
  85. package/docs/tsconfig.json +24 -0
  86. package/docs/vercel.json +8 -0
  87. package/docs/vocs.config.ts +113 -0
  88. package/example/basic-anim/app.js +22 -0
  89. package/example/basic-anim/index.html +14 -0
  90. package/example/basic-drawing/app.js +53 -0
  91. package/example/basic-drawing/index.html +19 -0
  92. package/example/basic-example/app.js +33 -0
  93. package/example/basic-example/index.html +19 -0
  94. package/example/basic-example/sample.png +0 -0
  95. package/example/basic-grid/app.js +38 -0
  96. package/example/basic-grid/index.html +19 -0
  97. package/example/basic-layout/app.js +72 -0
  98. package/example/basic-layout/index.html +19 -0
  99. package/example/basic-matrix/app.js +35 -0
  100. package/example/basic-matrix/box.png +0 -0
  101. package/example/basic-matrix/index.html +19 -0
  102. package/example/basic-mouse/app.js +76 -0
  103. package/example/basic-mouse/index.html +19 -0
  104. package/example/basic-package/app.js +22 -0
  105. package/example/basic-package/index.html +19 -0
  106. package/example/basic-package/main.png +0 -0
  107. package/example/basic-package/main.svg +6 -0
  108. package/example/basic-pinwheel/app.js +24 -0
  109. package/example/basic-pinwheel/index.html +19 -0
  110. package/example/basic-pinwheel/pinwheel.png +0 -0
  111. package/example/basic-row/app.js +27 -0
  112. package/example/basic-row/index.html +19 -0
  113. package/example/basic-row-fix/app.js +33 -0
  114. package/example/basic-row-fix/index.html +19 -0
  115. package/example/basic-stretch/app.js +24 -0
  116. package/example/basic-stretch/index.html +19 -0
  117. package/example/basic-string/app.js +22 -0
  118. package/example/basic-string/index.html +19 -0
  119. package/example/basic-tile/app.js +24 -0
  120. package/example/basic-tile/index.html +19 -0
  121. package/example/basic-tween/app.js +49 -0
  122. package/example/basic-tween/index.html +19 -0
  123. package/example/basic-window/app.js +42 -0
  124. package/example/basic-window/index.html +19 -0
  125. package/example/common/bezier.js +62 -0
  126. package/example/common/example.css +58 -0
  127. package/example/common/example.png +0 -0
  128. package/example/common/example.svg +23 -0
  129. package/example/common/p2.js +10343 -0
  130. package/example/common/pool.js +92 -0
  131. package/example/common/randomize.js +100 -0
  132. package/{ext → example/common}/stage-p2.js +182 -202
  133. package/example/common/texture.js +45 -0
  134. package/example/common/timeout.js +47 -0
  135. package/example/example.svg +23 -0
  136. package/example/game-aero/app.js +284 -0
  137. package/example/game-aero/index.html +21 -0
  138. package/example/game-aero/main.png +0 -0
  139. package/example/game-aero/main.svg +29 -0
  140. package/example/game-asteroids/app.js +444 -0
  141. package/example/game-asteroids/index.html +28 -0
  142. package/example/game-asteroids/main.svg +9 -0
  143. package/example/game-breakout/app.js +627 -0
  144. package/example/game-breakout/bg.png +0 -0
  145. package/example/game-breakout/bg.svg +11 -0
  146. package/example/game-breakout/index.html +19 -0
  147. package/example/game-breakout/logo.svg +31 -0
  148. package/example/game-breakout/main.png +0 -0
  149. package/example/game-breakout/main.svg +45 -0
  150. package/example/game-breakout/texture.js +51 -0
  151. package/example/game-o/app.js +1496 -0
  152. package/example/game-o/index.html +14 -0
  153. package/example/game-o/main.png +0 -0
  154. package/example/game-o/textures.js +203 -0
  155. package/example/game-orbit/app.js +497 -0
  156. package/example/game-orbit/art/android.json +13 -0
  157. package/example/game-orbit/art/bg.svg +62 -0
  158. package/example/game-orbit/art/digit.svg +140 -0
  159. package/example/game-orbit/art/game.json +14 -0
  160. package/example/game-orbit/art/icon.svg +128 -0
  161. package/example/game-orbit/art/ios.json +34 -0
  162. package/example/game-orbit/art/main.svg +56 -0
  163. package/example/game-orbit/art/misc.json +10 -0
  164. package/example/game-orbit/art/splash.svg +77 -0
  165. package/example/game-orbit/index.html +19 -0
  166. package/example/game-orbit/media/bg.png +0 -0
  167. package/example/game-orbit/media/digit.png +0 -0
  168. package/example/game-orbit/media/main.png +0 -0
  169. package/example/game-orbit/textures.js +40 -0
  170. package/example/game-samegame/app.js +244 -0
  171. package/example/game-samegame/index.html +19 -0
  172. package/example/game-samegame/main.png +0 -0
  173. package/example/game-samegame/main.svg +29 -0
  174. package/example/game-samegame/textures.js +15 -0
  175. package/example/game-tictactoe/app.js +183 -0
  176. package/example/game-tictactoe/index.html +19 -0
  177. package/example/game-traffic/app.js +383 -0
  178. package/example/game-traffic/base.svg +4 -0
  179. package/example/game-traffic/bg.png +0 -0
  180. package/example/game-traffic/bg.svg +10 -0
  181. package/example/game-traffic/index.html +19 -0
  182. package/example/game-traffic/main.png +0 -0
  183. package/example/game-traffic/map-1.png +0 -0
  184. package/example/game-traffic/map-1.svg +5 -0
  185. package/example/game-traffic/textures.js +24 -0
  186. package/example/index.html +30 -0
  187. package/index.html +6 -0
  188. package/package.json +46 -22
  189. package/src/common/browser.ts +5 -0
  190. package/src/common/is.ts +18 -0
  191. package/src/common/math.ts +60 -0
  192. package/src/common/matrix.ts +241 -0
  193. package/src/common/stats.ts +8 -0
  194. package/src/common/uid.ts +4 -0
  195. package/src/core/anim.ts +144 -0
  196. package/src/core/core.ts +1191 -0
  197. package/src/core/easing.ts +235 -0
  198. package/src/core/monotype.ts +94 -0
  199. package/src/core/pin.ts +664 -0
  200. package/src/core/pointer.ts +317 -0
  201. package/src/core/root.ts +451 -0
  202. package/src/core/sprite.ts +105 -0
  203. package/src/core/transition.ts +271 -0
  204. package/src/index.ts +3 -0
  205. package/src/main.ts +16 -0
  206. package/src/texture/atlas.ts +218 -0
  207. package/src/texture/canvas.ts +176 -0
  208. package/src/texture/image.ts +103 -0
  209. package/src/texture/index.ts +7 -0
  210. package/src/texture/pipe.ts +47 -0
  211. package/src/texture/resizable.ts +159 -0
  212. package/src/texture/selection.ts +203 -0
  213. package/src/texture/texture.ts +157 -0
  214. package/test/atlas-test.js +112 -97
  215. package/test/event-test.js +48 -50
  216. package/test/main-test.js +12 -15
  217. package/test/mouse-test.js +81 -67
  218. package/test/node-test.js +117 -80
  219. package/test/pin-test.js +57 -59
  220. package/test/transition-test.js +40 -0
  221. package/test/util/expect.js +24 -20
  222. package/test/util/memo.js +5 -5
  223. package/tsconfig.json +18 -0
  224. package/typedoc.json +24 -0
  225. package/vite.config.ts +52 -0
  226. package/dist/stage.cordova.js +0 -4035
  227. package/dist/stage.cordova.js.map +0 -1
  228. package/dist/stage.cordova.min.js +0 -36
  229. package/dist/stage.cordova.min.js.map +0 -1
  230. package/dist/stage.web.js +0 -3984
  231. package/dist/stage.web.js.map +0 -1
  232. package/dist/stage.web.min.js +0 -36
  233. package/dist/stage.web.min.js.map +0 -1
  234. package/ext/stage-matter.js +0 -201
  235. package/ext/stage-physics.js +0 -167
  236. package/ext/stage-planck.js +0 -355
  237. package/ext/stage-sat.js +0 -197
  238. package/lib/addon/easing.js +0 -185
  239. package/lib/addon/mouse.js +0 -206
  240. package/lib/addon/tween.js +0 -189
  241. package/lib/anim.js +0 -135
  242. package/lib/atlas.js +0 -240
  243. package/lib/canvas.js +0 -51
  244. package/lib/core.js +0 -221
  245. package/lib/event.js +0 -3
  246. package/lib/image.js +0 -72
  247. package/lib/index.js +0 -10
  248. package/lib/layout.js +0 -143
  249. package/lib/loader/cordova.js +0 -186
  250. package/lib/loader/web.js +0 -151
  251. package/lib/loop.js +0 -136
  252. package/lib/matrix.js +0 -169
  253. package/lib/pin.js +0 -620
  254. package/lib/root.js +0 -136
  255. package/lib/str.js +0 -81
  256. package/lib/texture.js +0 -103
  257. package/lib/tree.js +0 -410
  258. package/lib/util/await.js +0 -28
  259. package/lib/util/create.js +0 -16
  260. package/lib/util/event.js +0 -71
  261. package/lib/util/extend.js +0 -11
  262. package/lib/util/insert.js +0 -16
  263. package/lib/util/is.js +0 -167
  264. package/lib/util/math.js +0 -42
  265. package/lib/util/memo.js +0 -13
  266. package/lib/util/now.js +0 -13
  267. package/lib/util/once.js +0 -9
  268. package/lib/util/repeat.js +0 -74
  269. package/lib/util/stats.js +0 -1
  270. package/lib/util/string.js +0 -4
  271. package/platform/cordova.js +0 -16
  272. package/platform/web.js +0 -16
  273. package/test/tween-test.js +0 -41
package/README.md CHANGED
@@ -1,641 +1,22 @@
1
- [![Stage](https://s3.amazonaws.com/piqnt.com/stage.js/stage.png)](http://piqnt.com/stage.js/)
1
+ ![Stage.js](https://static.piqnt.com/stage.js/stage.png "Stage.js")
2
2
 
3
- Stage.js is a 2D HTML5 JavaScript library for cross-platform game development, it is lightweight, fast and open-source.
3
+ Stage.js is a lightweight and fast 2D rendering and layout library for web and mobile game development.
4
4
 
5
- [**Check out examples and demos!**](http://piqnt.com/stage.js/)
5
+ Stage.js provides an intuitive api to create interactive graphics on HTM5 2D Canvas.
6
6
 
7
- [Install](#installation) · [Usage](#usage) · [Resources](#resources) · [API Doc](#api-doc) · [Development](#development) · [License](#license)
7
+ ##### Features
8
8
 
9
- [中文手册](https://github.com/shakiba/stage.js/wiki/%E4%B8%AD%E6%96%87%E6%89%8B%E5%86%8C)
9
+ - Optimized rendering loop
10
+ - Pointer events processing, and dispatching events to target components
10
11
 
12
+ - Texture atlas, sprites, and image preloading
11
13
 
12
- ## Introduction
14
+ - Components tree object model
15
+ - Layouts, positioning, and transitions
13
16
 
14
- Canvas is the graphic component of HTML5 game development, but it only has a drawing API and there is no data model like the DOM to compose your application.
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.
17
17
 
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.
18
+ ---
21
19
 
22
- Each rendering cycle consists of ticking and drawing tree nodes. On ticking, nodes adjust themselves to recent updates while on drawing each node transforms according to its pinning and draws its textures.
20
+ [**Documentations**](http://piqnt.com/stage.js/docs/)
23
21
 
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
- });
50
-
51
- // Adding a texture
52
- Stage({
53
- image : 'sample.png',
54
- textures : {
55
- box : { x : 0, y : 0, width : 30, height : 30 }
56
- }
57
- });
58
- ```
59
-
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
- #### NPM
67
-
68
- ```
69
- npm install stage-js --save
70
- ```
71
-
72
- #### Bower
73
-
74
- ```
75
- bower install stage-js --save
76
- ```
77
-
78
-
79
- ## Usage
80
-
81
- #### Browser
82
-
83
- Include an appropriate build file from `dist` directory in your HTML page before your application. For example:
84
-
85
- ```html
86
- <script src="path/to/stage.web.min.js"></script>
87
- <script src="path/to/your-app.js"></script>
88
- ```
89
-
90
- #### Browserify, CommonJS, Node.js
91
- Generally it is preferred to directly include a browser build file in HTML pages, however it is also possible to use CommonJS require instead, for example:
92
-
93
- ```js
94
- var Stage = require('stage-js/platform/web');
95
- ```
96
-
97
- See `platform` directory for other available modules.
98
-
99
-
100
- ## Resources
101
-
102
- [Introduction to Stage.js](http://www.sitepoint.com/introduction-to-stage-js/) by Baljeet Rathi, SitePoint
103
-
104
- [中文手册](https://github.com/shakiba/stage.js/wiki/%E4%B8%AD%E6%96%87%E6%89%8B%E5%86%8C) by Villor 紫刃
105
-
106
-
107
- ## API Doc
108
-
109
- ### Application
110
- An application is created by passing a callback function to `Stage()`.
111
- The library will load, create and initialize all required components and will then call the provided function with the application root node and display the container which is normally a Canvas element.
112
-
113
- ```javascript
114
- // Create and start an application
115
- Stage(function(stage, display) {
116
-
117
- // Set viewbox for stage, see pinning for valid modes
118
- stage.viewbox(width, height, mode = 'in-pad');
119
-
120
- // Listen to view port resize events
121
- stage.on('viewport', function(viewport) {
122
- // `viewport` attributes are `width`, `height` and `ratio`
123
- });
124
-
125
- // Pause playing
126
- stage.pause();
127
-
128
- // Resume playing
129
- stage.resume();
130
- });
131
- ```
132
-
133
-
134
- ### Tree Model
135
- Every app consists of a tree. The tree's root is provided as `stage`.
136
-
137
- ```javascript
138
- // Create a new node instance (with no textures)
139
- var node = Stage.create();
140
-
141
- // Append/prepend child to parent's children (accepts array)
142
- parent.append(child);
143
- parent.prepend(child);
144
-
145
- // Append/prepend child to parent's children
146
- child.appendTo(parent);
147
- child.prependTo(parent);
148
-
149
- // Insert sibling after/before child (accepts array)
150
- child.insertNext(sibling);
151
- child.insertPrev(sibling);
152
-
153
- // Insert sibling after/before child
154
- sibling.insertAfter(child);
155
- sibling.insertBefore(child);
156
-
157
- // Remove child from its parent
158
- child.remove();
159
-
160
- // Remove child from parent (accepts array)
161
- parent.remove(child);
162
-
163
- // Remove all of parent's children
164
- parent.empty();
165
-
166
- // Get parent's first/last (visible) child
167
- parent.first(onlyVisible = false);
168
- parent.last(onlyVisible = false);
169
-
170
- // Get immediate parent
171
- child.parent();
172
-
173
- // Get child's next/prev (visible) sibling
174
- child.next(onlyVisible = false);
175
- child.prev(onlyVisible = false);
176
-
177
- // Get node's visiblity
178
- node.visible();
179
- // Set node's visiblity
180
- node.visible(visible);
181
- node.hide();
182
- node.show();
183
-
184
- // Iterate over parent's children, child can not be removed
185
- for (var child = parent.first(); child; child = child.next()) {
186
- // use child
187
- }
188
-
189
- // Iterate over parent's children, child can be removed
190
- var child, next = parent.first();
191
- while (child = next) {
192
- next = child.next();
193
- // use child
194
- }
195
-
196
- // Visit node's sub-tree including node itself
197
- node.visit({
198
- start : function(node) {
199
- return skipChildren;
200
- },
201
- end : function(node) {
202
- return stopVisit;
203
- },
204
- reverse : reverseChildrenOrder = false,
205
- visible : onlyVisibleNodes = false
206
- });
207
- ```
208
-
209
-
210
- ### Game Loop
211
- Each rendering cycle consists of ticking and redrawing the application tree.
212
- Application and its nodes can be updated during ticking.
213
- Depending on application activities there can be three different follow-ups after ticking:
214
-
215
- * If at least one node is touched then the entire application tree will be redrawn and the game loop will continue.
216
- * If no node is touched but at least one ticking function returns `true` then the game loop will continue but the previous drawing will be retained.
217
- * If no node is touched and no ticking function returns `true` then the application will pause until it is touched directly or indirectly.
218
-
219
- Nodes can be touched directly by calling `node.touch()` but usually they are touched indirectly by other actions such as pinning or tree manipulation.
220
-
221
- ```javascript
222
- // Register a function to be called on ticking
223
- node.tick(function(millisecElapsed) {
224
- return continueGameLoop;
225
- }, beforeChildren = false);
226
-
227
- // Touch node
228
- node.touch();
229
- ```
230
-
231
-
232
- ### Pinning
233
- Pinning specifies how a node is attached to its parent.
234
- Pinning consists of size, transformation, positioning and transparency.
235
-
236
- ```javascript
237
- // Get a pinning value
238
- node.pin(name);
239
-
240
- // Set a pinning value
241
- node.pin(name, value);
242
-
243
- // Set one or more pinning values
244
- node.pin({
245
- name : value,
246
- ...
247
- });
248
- ```
249
-
250
- When `nameX` equals `nameY`, `name` shorthand can be used instead.
251
-
252
- #### Size
253
- For some nodes, such as images, strings, rows and columns, size is set automatically.
254
-
255
- ```javascript
256
- node.pin({
257
- height : height,
258
- width : width
259
- });
260
-
261
- // Shortcut for setting size:
262
- node.size(width, height);
263
- node.width(width);
264
- node.height(height);
265
-
266
- // Shortcut for getting size:
267
- node.width();
268
- node.height();
269
- ```
270
-
271
- #### Transformation
272
- Transformation consists of scaling, skewing and rotating. Rotation is applied after scaling and skewing.
273
-
274
- ```javascript
275
- node.pin({
276
- scaleX : 1,
277
- scaleY : 1,
278
- skewX : 0,
279
- skewY : 0,
280
- rotation : 0
281
- });
282
-
283
- // Shortcut for setting transformation:
284
- node.scale(x, y = x);
285
- node.scale({ x : x, y : y });
286
- node.skew(x, y = x);
287
- node.skew({ x : x, y : y });
288
- node.rotate(angle);
289
- ```
290
-
291
- #### Positioning
292
- When positioning, *handle* point on self is positioned at *offset* distance from *align* point on the parent.
293
- Handle and align are defined as a ratio of width/height, 0 is top/left and 1 is bottom/right.
294
- Handle defaults to the align value when it is not specified.
295
-
296
- ```javascript
297
- node.pin({
298
- alignX : 0,
299
- alignY : 0,
300
- handleX : 0,
301
- handleY : 0,
302
- offsetX : 0,
303
- offsetY : 0
304
- });
305
-
306
- // Shortcut methods for setting positioning:
307
- node.offset(x, y);
308
- node.offset({ x : x, y : y });
309
- ```
310
-
311
- By default an axis-aligned bounding box (AABB) after transformation is used for positioning,
312
- however it is possible to use a non-transformed box by setting a pivot location.
313
- Pivot location is defined as ratio of non-transformed width/height and is used as central point on self for scale, skew and rotation.
314
-
315
- ```javascript
316
- node.pin({
317
- pivotX : 0,
318
- pivotY : 0
319
- });
320
- ```
321
-
322
- #### Transparency
323
- Transparency can be applied to both node textures and subtree nodes or only node textures.
324
-
325
- ```javascript
326
- node.pin({
327
- alpha : 1,
328
- textureAlpha : 1
329
- });
330
-
331
- // Shortcut methods for setting transparency:
332
- node.alpha(alpha);
333
- node.alpha(alpha, textureAlpha);
334
- ```
335
-
336
- #### Scale To
337
- Scale to a new width/height, if mode is set to scale proportionally. Valid modes are:
338
- - `in`: maximum scale which keeps node edges inside the scaleWidth/Height
339
- - `in-pad`: like `in`, but evenly pads node to fill the entire scaleWidth/Height
340
- - `out`: minimum scale which keeps node edges outside scaleWidth/Height
341
- - `out-crop`: like `out`, but evenly crops it to scaleWidth/Height
342
-
343
- ```javascript
344
- node.pin({
345
- scaleMode : mode,
346
- scaleWidth : width,
347
- scaleHeight : height
348
- });
349
-
350
- // Shortcut method:
351
- node.scaleTo(width, height, mode);
352
- ```
353
-
354
-
355
- ### Events
356
- Event listeners can be registered and unregistered to nodes, listeners are called when an event is published to a node.
357
- Some events may be published to multiple nodes, but events do not propagate.
358
-
359
- ```javascript
360
- // Register a listener to node
361
- // Event `name` can be one or an array of strings or spaced separated strings
362
- node.on(name, listener);
363
-
364
- // Unregister a listener from node.
365
- node.off(name, listener);
366
-
367
- // Get listeners registered to node
368
- // Returns an array or undefined
369
- node.listeners(name);
370
-
371
- // Call listeners with args
372
- // Returns number of listeners called
373
- node.publish(name, args);
374
- ```
375
-
376
-
377
- ### Mouse and Touch
378
- Native mouse and touch events are captured, processed and published to application nodes.
379
- Nodes receive mouse events in local coordinates, i.e. mouse location is specified as the distance to the top-left of the node.
380
-
381
- ```javascript
382
- // Add click listener to node
383
- node.on('click', function(point) {
384
- // point.x and point.y are relative to this node left and top
385
- // point.raw is original event
386
- });
387
- ```
388
-
389
- Instead of native click events, syntatic click events are created and published to nodes.
390
- In addition to standard event types, a syntactic `mousecancel` event type is also supported which is similar to `touchcancel` but is published when a `mousedown` is not followed by `mouseup`.
391
-
392
- ```javascript
393
- // Mouse events:
394
- Stage.Mouse.CLICK = 'click';
395
- Stage.Mouse.START = 'touchstart mousedown';
396
- Stage.Mouse.MOVE = 'touchmove mousemove';
397
- Stage.Mouse.END = 'touchend mouseup';
398
- Stage.Mouse.CANCEL = 'touchcancel mousecancel';
399
- ```
400
-
401
-
402
- ### Texture
403
- Textures are drawable objects which are used by tree nodes to draw graphics on the Canvas surface.
404
-
405
- ### Texture Atlas
406
- A texture atlas (sprite sheet) consists of a set of named textures which are referenced by name in an application.
407
-
408
- Atlases are usually created using static image files. Images referenced in atlases are automatically preloaded.
409
-
410
- ```javascript
411
- // Adding texture atlas
412
- Stage({
413
- name : 'mario', // optional
414
- image : {
415
- src : 'mario.png',
416
- ratio : 1, // optional, for high-res images
417
- }
418
- textures : {
419
- stand : { x : 0, y : 0, width : 40, height : 60 },
420
- walk : [
421
- { x : 40, y : 0, width : 40, height : 60 },
422
- { x : 80, y : 0, width : 40, height : 60 },
423
- { x : 120, y : 0, width : 40, height : 60 }
424
- ],
425
- number : {
426
- '0' : { x : 0, y : 60, width : 10, height : 14 },
427
- '1' : { x : 10, y : 60, width : 10, height : 14 },
428
- '2' : { x : 20, y : 60, width : 10, height : 14 },
429
- '3' : { x : 30, y : 60, width : 10, height : 14 },
430
- '4' : { x : 40, y : 60, width : 10, height : 14 },
431
- '5' : { x : 50, y : 60, width : 10, height : 14 },
432
- '6' : { x : 60, y : 60, width : 10, height : 14 },
433
- '7' : { x : 70, y : 60, width : 10, height : 14 },
434
- '8' : { x : 80, y : 60, width : 10, height : 14 },
435
- '9' : { x : 90, y : 60, width : 10, height : 14 }
436
- }
437
- }
438
- });
439
-
440
- Stage.image('mario:stand');
441
-
442
- Stage.anim('mario:walk').play();
443
-
444
- Stage.string('mario:number').value(100);
445
- ```
446
-
447
- If image src starts with `./` it will be resolved relative to the current script URL.
448
-
449
-
450
- ### Image
451
- An image is a node with one texture.
452
-
453
- ```javascript
454
- // Create a new image instance
455
- var image = Stage.image(texture);
456
-
457
- // Change image texture
458
- image.image(texture);
459
-
460
- // Tile/Stretch image when pinning width and/or height
461
- // To define borders add top, bottom, left and right to texture
462
- image.tile();
463
- image.stretch();
464
- ```
465
-
466
-
467
- ### Animation
468
- An animation or anim is a node with an array of textures as frames.
469
-
470
- ```javascript
471
- // Create a new anim instance
472
- var anim = Stage.anim(textures, fps = 15);
473
-
474
- // Get or set animation frame-per-second
475
- anim.fps();
476
- anim.fps(fps);
477
-
478
- // Set animation frames, `textures` can be an array or a texture selector
479
- anim.frames(textures);
480
-
481
- // Go to n-th frame
482
- anim.gotoFrame(n);
483
-
484
- // Move n frames forward (or backward if n is negative)
485
- anim.moveFrame(n);
486
-
487
- // Get number of frames
488
- anim.length();
489
-
490
- // Start playing (from `frameName` if specified)
491
- anim.play(frameName = undefined);
492
-
493
- // Stop playing (and jump to `frameName` if specified)
494
- anim.stop(frameName = undefined);
495
-
496
- // Play `repeat * length` frames and then stop and call `callback`
497
- anim.repeat(repeat, callback = null);
498
- ```
499
-
500
-
501
- ### String
502
- String is a row of images which are dynamically selected from `frames` using characters of a string `value` (or items of an array `value`).
503
-
504
- ```javascript
505
- // Create a new string instance with frames
506
- var string = Stage.string(frames);
507
-
508
- // Set frames, a string referencing a map in an atlas
509
- string.frames("digits");
510
-
511
- // Set frames, a map with textures as values and frame names as keys
512
- string.frames({
513
- '0' : zeroTexture,
514
- '1' : oneTexture,
515
- ...
516
- });
517
-
518
- // Set frames, a function which takes a char (or item) and returns a texture
519
- string.frames(function(char) {
520
- // create a texture for char
521
- return texture;
522
- });
523
-
524
- // Set value, it can be a string (or an array)
525
- // Characters (or items) are used to select frames and create a row of images
526
- string.value(value);
527
-
528
- // Get assigned value
529
- string.value();
530
- ```
531
-
532
-
533
- ### Row and Column
534
- A row/column is a node which organizes its children as a horizontal/vertical sequence.
535
-
536
- ```javascript
537
- // Create a new row/column
538
- var row = Stage.row(childrenAlignY = 0);
539
- var column = Stage.column(childrenAlignX = 0);
540
-
541
- // Make node a row/column
542
- node.row(childrenAlignY = 0);
543
- node.column(childrenAlignX = 0);
544
-
545
- // Add spacing between row/column cells
546
- node.spacing(space);
547
- ```
548
-
549
-
550
- ### Box (experimental)
551
- A box resizes to wrap its children. It can be applied to tiled/stretched
552
- images to create variable size components such as windows and buttons.
553
-
554
- ```javascript
555
- // Create a new box
556
- var box = Stage.box();
557
-
558
- // Make node a box
559
- node = node.box();
560
- ```
561
-
562
-
563
- ### Tweening
564
- Tweening is used to apply smooth transitions to pinning values.
565
-
566
- ```javascript
567
- // Create a tweening entry
568
- // When `append` is true new entry is appended to current entries otherwise replaces them
569
- var tween = node.tween(duration = 400, delay = 0, append = false);
570
-
571
- // Set pinning values and start tweening
572
- // Pinning shortcut methods, such as `.scale()`, can also be used
573
- tween.pin(pinning);
574
-
575
- // Set easing for tweening, it can be either a function or an identifier
576
- // defined as 'name[-mode][(params)]', for example 'quad' or 'poly-in-out(3)'
577
- // Names: linear, quad, cubic, quart, quint, poly(p), sin/sine,
578
- // exp, circle/circ, bounce, elastic(a, p), back(s)
579
- // Modes: in, out, in-out, out-in
580
- tween.ease(easing);
581
-
582
- // Set duration in milliseconds
583
- tween.duration(ms);
584
-
585
- // Set delay in milliseconds
586
- tween.delay(ms);
587
-
588
- // Callback when tweening is done
589
- tween.done(function() {
590
- // this === node
591
- });
592
-
593
- // Remove this node when tweening ends
594
- tween.remove();
595
-
596
- // Hide this node when tweening ends
597
- tween.hide();
598
-
599
- // Create and chain a new tweening to this entry
600
- var nextTween = tween.tween(duration = 400, delay = 0);
601
- ```
602
-
603
-
604
- ### Global Methods
605
- ```javascript
606
-
607
- // Create a new app
608
- Stage(function(stage, display) { });
609
-
610
- // 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);
623
-
624
- // Pause playing all applications
625
- Stage.pause();
626
-
627
- // Resume playing all applications
628
- Stage.resume();
629
- ```
630
-
631
-
632
- ## Development
633
- To try examples with a live build run following command and check output for the URL to open in your browser:
634
- ```
635
- npm run dev
636
- ```
637
-
638
-
639
- ## License
640
- Copyright 2020 Ali Shakiba http://shakiba.me/stage.js
641
- Available under the MIT License
22
+ [**Examples & Demos**](http://piqnt.com/stage.js/)