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
@@ -1,910 +0,0 @@
1
- /*
2
- Copyright (C) 2012 by Samuel Ronce
3
-
4
- Permission is hereby granted, free of charge, to any person obtaining a copy
5
- of this software and associated documentation files (the "Software"), to deal
6
- in the Software without restriction, including without limitation the rights
7
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- copies of the Software, and to permit persons to whom the Software is
9
- furnished to do so, subject to the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be included in
12
- all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
- THE SOFTWARE.
21
- */
22
-
23
- /*
24
- * x: next value
25
- t: current time
26
- b: last value
27
- c: next value - last value
28
- d: duration
29
- */
30
- var Ease = {
31
- linear: function(x, t, b, c, d) {
32
- return c*(t/=d) + b;
33
- },
34
- easeInQuad: function (x, t, b, c, d) {
35
- return c*(t/=d)*t + b;
36
- },
37
- easeOutQuad: function (x, t, b, c, d) {
38
- return -c *(t/=d)*(t-2) + b;
39
- },
40
- easeInOutQuad: function (x, t, b, c, d) {
41
- if ((t/=d/2) < 1) return c/2*t*t + b;
42
- return -c/2 * ((--t)*(t-2) - 1) + b;
43
- },
44
- easeInCubic: function (x, t, b, c, d) {
45
- return c*(t/=d)*t*t + b;
46
- },
47
- easeOutCubic: function (x, t, b, c, d) {
48
- return c*((t=t/d-1)*t*t + 1) + b;
49
- },
50
- easeInOutCubic: function (x, t, b, c, d) {
51
- if ((t/=d/2) < 1) return c/2*t*t*t + b;
52
- return c/2*((t-=2)*t*t + 2) + b;
53
- },
54
- easeInQuart: function (x, t, b, c, d) {
55
- return c*(t/=d)*t*t*t + b;
56
- },
57
- easeOutQuart: function (x, t, b, c, d) {
58
- return -c * ((t=t/d-1)*t*t*t - 1) + b;
59
- },
60
- easeInOutQuart: function (x, t, b, c, d) {
61
- if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
62
- return -c/2 * ((t-=2)*t*t*t - 2) + b;
63
- },
64
- easeInQuint: function (x, t, b, c, d) {
65
- return c*(t/=d)*t*t*t*t + b;
66
- },
67
- easeOutQuint: function (x, t, b, c, d) {
68
- return c*((t=t/d-1)*t*t*t*t + 1) + b;
69
- },
70
- easeInOutQuint: function (x, t, b, c, d) {
71
- if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
72
- return c/2*((t-=2)*t*t*t*t + 2) + b;
73
- },
74
- easeInSine: function (x, t, b, c, d) {
75
- return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
76
- },
77
- easeOutSine: function (x, t, b, c, d) {
78
- return c * Math.sin(t/d * (Math.PI/2)) + b;
79
- },
80
- easeInOutSine: function (x, t, b, c, d) {
81
- return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
82
- },
83
- easeInExpo: function (x, t, b, c, d) {
84
- return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
85
- },
86
- easeOutExpo: function (x, t, b, c, d) {
87
- return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
88
- },
89
- easeInOutExpo: function (x, t, b, c, d) {
90
- if (t==0) return b;
91
- if (t==d) return b+c;
92
- if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
93
- return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
94
- },
95
- easeInCirc: function (x, t, b, c, d) {
96
- return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
97
- },
98
- easeOutCirc: function (x, t, b, c, d) {
99
- return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
100
- },
101
- easeInOutCirc: function (x, t, b, c, d) {
102
- if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
103
- return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
104
- },
105
- easeInElastic: function (x, t, b, c, d) {
106
- var s=1.70158;var p=0;var a=c;
107
- if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
108
- if (a < Math.abs(c)) { a=c; var s=p/4; }
109
- else var s = p/(2*Math.PI) * Math.asin (c/a);
110
- return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
111
- },
112
- easeOutElastic: function (x, t, b, c, d) {
113
- var s=1.70158;var p=0;var a=c;
114
- if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
115
- if (a < Math.abs(c)) { a=c; var s=p/4; }
116
- else var s = p/(2*Math.PI) * Math.asin (c/a);
117
- return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
118
- },
119
- easeInOutElastic: function (x, t, b, c, d) {
120
- var s=1.70158;var p=0;var a=c;
121
- if (t==0) return b; if ((t/=d/2)==2) return b+c; if (!p) p=d*(.3*1.5);
122
- if (a < Math.abs(c)) { a=c; var s=p/4; }
123
- else var s = p/(2*Math.PI) * Math.asin (c/a);
124
- if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
125
- return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
126
- },
127
- easeInBack: function (x, t, b, c, d, s) {
128
- if (s == undefined) s = 1.70158;
129
- return c*(t/=d)*t*((s+1)*t - s) + b;
130
- },
131
- easeOutBack: function (x, t, b, c, d, s) {
132
- if (s == undefined) s = 1.70158;
133
- return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
134
- },
135
- easeInOutBack: function (x, t, b, c, d, s) {
136
- if (s == undefined) s = 1.70158;
137
- if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
138
- return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
139
- },
140
- easeInBounce: function (x, t, b, c, d) {
141
- return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
142
- },
143
- easeOutBounce: function (x, t, b, c, d) {
144
- if ((t/=d) < (1/2.75)) {
145
- return c*(7.5625*t*t) + b;
146
- } else if (t < (2/2.75)) {
147
- return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
148
- } else if (t < (2.5/2.75)) {
149
- return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
150
- } else {
151
- return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
152
- }
153
- },
154
- easeInOutBounce: function (x, t, b, c, d) {
155
- if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
156
- return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
157
- }
158
- }
159
-
160
-
161
- Class.create("Timeline", {
162
- _timeline: {},
163
- _frequence: 0,
164
- _stop: false,
165
- _propreties: [],
166
- _key_times: [],
167
- _onFinish: null,
168
- _varTime: {},
169
- initialize: function(el) {
170
- this._frequence = 0;
171
- this.el = el;
172
- this.addProprety(["opacity", "x", "y", "scaleX", "scaleY", "rotation"]);
173
- this._loop();
174
- },
175
- /**
176
- @doc timeline/
177
- @method to The properties of the elements will change over a predefined period
178
- @param {Object} attr Property values :
179
-
180
- * opacity
181
- * x
182
- * y
183
- * scaleX
184
- * scaleY
185
- * rotation
186
-
187
- Example :
188
-
189
- {x: 200, scaleX: 2}
190
-
191
- @param {Integer} frames Duration in frames
192
- @param {Function} ease (optional) Effect.
193
-
194
- Use `Ease` object that has constants :
195
-
196
- * easeInQuad
197
- * easeOutQuad
198
- * easeInCubic
199
- * easeOutCubic
200
- * easeInOutCubic
201
- * easeInQuart
202
- * easeOutQuart
203
- * easeInOutQuart
204
- * easeInQuint
205
- * easeOutQuint
206
- * easeInOutQuint
207
- * easeInSine
208
- * easeOutSine
209
- * easeInOutSine
210
- * easeInExpo
211
- * easeOutExpo
212
- * easeInOutExpo
213
- * easeInCirc
214
- * easeOutCirc
215
- * easeInOutCirc
216
- * easeInElastic
217
- * easeOutElastic
218
- * easeInOutElastic
219
- * easeInBack
220
- * easeOutBack
221
- * easeInOutBack
222
- * easeInBounce
223
- * easeOutBounce
224
- * easeInOutBounce
225
-
226
- See example below. You can see the effects on [http://gsgd.co.uk/sandbox/jquery/easing/](http://gsgd.co.uk/sandbox/jquery/easing/)
227
-
228
- @example
229
-
230
- var timeline = canvas.Timeline.new(el);
231
- timeline.to({x: 100}, 70, Ease.easeOutElastic).call();
232
-
233
- <jsfiddle>MAdmq/8</jsfiddle>
234
-
235
- @return {CanvasEngine.Timeline}
236
- */
237
- to: function(attr, frames, ease, _cal) {
238
- if (ease) {
239
- attr._ease_ = ease;
240
- }
241
- if (!_cal) {
242
- _cal = "set";
243
- }
244
- this._key_times.push(frames);
245
- this._timeline[frames] = attr;
246
- this._timeline[frames]._cal = _cal;
247
- return this;
248
- },
249
- /**
250
- @doc timeline/
251
- @method wait Wait a number of frames before you start the next animation
252
- @param {Integer} frames Duration in frames
253
- Example
254
-
255
- var timeline = canvas.Timeline.new(el);
256
- timeline.wait(10)
257
- .to({x: 100}, 70, Ease.easeOutElastic)
258
- .call();
259
-
260
- @return {CanvasEngine.Timeline}
261
- */
262
- wait: function(frame) {
263
- var last_key = this.getLastKey();
264
- this.to(last_key, frame, false, "wait");
265
- return this;
266
- },
267
- /**
268
- @doc timeline/
269
- @method getLastKey Retrieve the properties of the last key
270
- @return {Object}
271
- */
272
- getLastKey: function() {
273
- var last_time = this._key_times[this._key_times.length-1];
274
- if (!last_time) {
275
- var obj = {};
276
- for (var i=0 ; i < this._propreties.length ; i++) {
277
- obj[this._propreties[i]] = this.el[this._propreties[i]];
278
- }
279
- return obj;
280
- }
281
- return this._timeline[last_time];
282
- },
283
- /**
284
- @doc timeline/
285
- @method add Adds values to the properties of a period.
286
- @param {Object} attr Property values :
287
-
288
- * opacity
289
- * x
290
- * y
291
- * scaleX
292
- * scaleY
293
- * rotation
294
-
295
- Example :
296
-
297
- {x: 200, scaleX: 2}
298
-
299
- @param {Integer} frames Duration in frames
300
- @param {Function} ease (optional) Effect. See [to()](?p=extends.timeline.to)
301
- @example
302
-
303
- var timeline = canvas.Timeline.new(el);
304
- timeline.add({x: 100}, 70, Ease.easeOutElastic).call();
305
-
306
- @return {CanvasEngine.Timeline}
307
- */
308
- add: function(attr, frames, ease) {
309
- return this.to(attr, frames, ease, "add");
310
- },
311
- /**
312
- @doc timeline/
313
- @method addProprety Adds properties to changes
314
- @param {String|Array} name Property name
315
- @example
316
-
317
- var timeline = canvas.Timeline.new(el);
318
- tileline.addProprety("foo");
319
- timeline.to({foo: 100}, 70).call();
320
-
321
- */
322
- addProprety: function(name) {
323
- if (!(name instanceof Array)) {
324
- name = [name];
325
- }
326
- for (var i=0 ; i < name.length ; i++) {
327
- this._propreties.push(name[i]);
328
- }
329
- },
330
- /**
331
- @doc timeline/
332
- @method loop Run continuously timeline
333
- @example
334
-
335
- var timeline = canvas.Timeline.new(el);
336
- timeline.add({x: 2}, 60).loop();
337
-
338
- */
339
- loop: function() {
340
- var self = this;
341
- this.call(function() {
342
- self._stop = false;
343
- return true;
344
- });
345
- },
346
- _initVar: function() {
347
- this._varTime = {
348
- freq: this._frequence,
349
- time: 0,
350
- time_tmp: 0,
351
- last_t: 0,
352
- next_t: 0,
353
- find_next: false
354
- };
355
-
356
- },
357
- _loop: function() {
358
- var self = this, ret;
359
-
360
- this.el.addLoopListener(function() {
361
-
362
- if (self._varTime.time === undefined) return;
363
-
364
-
365
- var freq = self._varTime._frequence,
366
- time = self._varTime.time,
367
- time_tmp = self._varTime.time_tmp,
368
- last_t = self._varTime.last_t,
369
- next_t = self._varTime.next_t,
370
- find_next = self._varTime.find_next;
371
-
372
- function initValue() {
373
- var obj = {};
374
- for (var i=0 ; i < self._propreties.length ; i++) {
375
- obj[self._propreties[i]] = this[self._propreties[i]];
376
- }
377
- self._timeline["0"] = obj;
378
- }
379
-
380
- function endValue(type) {
381
- var _cal, result_val;
382
- if (self._timeline[next_t][type] === undefined) {
383
- return this[type];
384
- }
385
- _cal = self._timeline[next_t]._cal;
386
- result_val = self._timeline[next_t][type];
387
- switch (_cal) {
388
- case "add":
389
- result_val += self._timeline[last_t][type];
390
- break;
391
- }
392
- return result_val;
393
- }
394
-
395
- function newValue(type) {
396
- var ease, value, _cal, next_val, result_val;
397
- if (self._timeline[next_t][type] === undefined) {
398
- return this[type];
399
- }
400
- ease = self._timeline[next_t]._ease_;
401
- _cal = self._timeline[next_t]._cal;
402
- result_val = self._timeline[next_t][type];
403
- switch (_cal) {
404
- case "add":
405
- result_val += self._timeline[last_t][type];
406
- break;
407
- }
408
- if (!ease) {
409
- ease = Ease.linear;
410
- }
411
- value = ease(
412
- result_val,
413
- (next_t - last_t) - time_tmp,
414
- self._timeline[last_t][type],
415
- result_val - self._timeline[last_t][type],
416
- next_t - last_t
417
- );
418
- return value;
419
- }
420
- freq = 0;
421
-
422
-
423
-
424
- var t;
425
- if (self._stop) {
426
- return;
427
- }
428
-
429
- freq++;
430
- if (freq >= self._frequence) {
431
- if (time == 0) {
432
- initValue.call(this);
433
- }
434
- if (time_tmp == 0) {
435
- find_next = false;
436
- for (var key in self._timeline) {
437
- if (key > time) {
438
- last_t = next_t ? next_t : 0;
439
- next_t = key;
440
- find_next = true;
441
- break;
442
- }
443
- }
444
-
445
- if (!find_next) {
446
- self._stop = true;
447
- time = 0;
448
- last_t = 0;
449
- time_tmp = 0;
450
- if (self._onFinish) {
451
- ret = self._onFinish.call(this);
452
- }
453
- if (!ret) {
454
- return;
455
- }
456
- else {
457
- initValue.call(this);
458
- next_t = 0;
459
- }
460
- }
461
- }
462
-
463
- time_tmp = next_t - time;
464
- if (time_tmp != 0) {
465
- if (self._timeline[next_t]._cal != "wait") {
466
- if (time_tmp == 1) {
467
- for (var i=0 ; i < self._propreties.length ; i++) {
468
- this[self._propreties[i]] = endValue.call(this, self._propreties[i]);
469
- }
470
- }
471
- else {
472
- for (var i=0 ; i < self._propreties.length ; i++) {
473
- this[self._propreties[i]] = newValue.call(this, self._propreties[i]);
474
- }
475
- }
476
- }
477
- }
478
- time++;
479
-
480
- }
481
-
482
-
483
-
484
- self._varTime._frequence = freq;
485
- self._varTime.time = time;
486
- self._varTime.time_tmp = time_tmp;
487
- self._varTime.last_t = last_t;
488
- self._varTime.next_t = next_t;
489
- self._varTime.find_next = find_next;
490
-
491
-
492
- });
493
- },
494
- /**
495
- @doc timeline/
496
- @method call Run timeline
497
- @param {Function} onFinish (optional) Calls the function when the animation is completed
498
- @example
499
-
500
- var timeline = canvas.Timeline.new(el);
501
- timeline.to({x: 20}, 60).call(function() {
502
- console.log("finish !");
503
- });
504
-
505
- */
506
- call: function(onFinish) {
507
- this._initVar();
508
- this._onFinish = onFinish;
509
- this._stop = false;
510
- }
511
- });
512
-
513
- Class.create("Animation", {
514
- _images: [],
515
- _frames: {},
516
- _animations: {},
517
- _frequence: 0,
518
- _stop: false,
519
- _timeline: null,
520
- _onFinish: null,
521
- _seq: null,
522
- _loop: false,
523
- _els: null,
524
- el: null,
525
- initialize: function(options) {
526
- this._options = options;
527
- this._images = options.images;
528
- this._animations = options.animations;
529
- this._timeline = options.timeline;
530
- if (options.addIn) {
531
- this.el = options.addIn.scene.createElement();
532
- options.addIn.append(this.el);
533
- this.add();
534
- }
535
- },
536
- /**
537
- @doc animation/
538
- @method add Adds a loop listener in a element to perform an animation (with addLoopListener). From the method call, the loop is started on the animation stop. Use the play method to start the animation
539
- @param {CanvasEngine.Element} el
540
- @event animation:draw Call each sequence. Id parameter sequence
541
- */
542
- add: function(el) {
543
-
544
- if (el) this.el = el;
545
-
546
- var self = this;
547
- var i = 0;
548
- var freq = null;
549
- var img_seq = 0;
550
-
551
- this.stop();
552
-
553
- this.el.addLoopListener(function() {
554
- var t;
555
-
556
-
557
-
558
- var seq = self._animations[self._seq], loop = self._loop == "loop";
559
-
560
- function seqSize(img) {
561
- if (seq.size) return seq.size;
562
- var data_img = Global_CE.Materials.get(img);
563
- seq.size = {
564
- width: data_img.width,
565
- height: data_img.height
566
- };
567
- }
568
-
569
- if (seq && !seq.frequence) seq.frequence = 0;
570
-
571
- if (freq == null && seq) {
572
- freq = seq.frequence;
573
- }
574
-
575
- if (self._stop) {
576
- if (seq) freq = seq.frequence;
577
- i = 0;
578
- return;
579
- }
580
- freq++;
581
-
582
- if (freq >= seq.frequence) {
583
- if (self._images instanceof Array) {
584
- var img = self._images[img_seq];
585
- seqSize(img);
586
- this.drawImage(img);
587
- img_seq++;
588
- if (img_seq >= self._images.length) {
589
- img_seq = 0;
590
- if (!loop) {
591
- self._stop = true;
592
- }
593
- }
594
-
595
- }
596
- else {
597
- var img = Global_CE.Materials.get(self._images), sx = 0, sy = 0;
598
- seqSize(self._images);
599
- var currentSeq;
600
- var nx = img.width / seq.size.width;
601
- var ny = img.height / seq.size.height;
602
- var id;
603
- var children;
604
-
605
- function drawImage(_el, id) {
606
- var _img = self._images;
607
- if (seq.patternSize) {
608
- seq.size = {
609
- width: img.width / seq.patternSize.width,
610
- height: img.height / seq.patternSize.height
611
- };
612
- }
613
-
614
- sy = parseInt(id / Math.round(img.width / seq.size.width));
615
- sx = (id % Math.round(img.width / seq.size.width));
616
-
617
- var w = seq.size.width * sx,
618
- h = seq.size.height * sy;
619
-
620
- _el.trigger("animation:draw", id);
621
- if (seq.image) {
622
- _img = seq.image;
623
- }
624
- if (!seq.position) seq.position = {};
625
- if (!seq.position.left) seq.position.left = 0;
626
- if (!seq.position.top) seq.position.top = 0;
627
-
628
- _el.drawImage(_img, w, h, seq.size.width, seq.size.height, seq.position.left, seq.position.top, seq.size.width, seq.size.height);
629
- }
630
-
631
- function finish() {
632
- if (self._loop == "stop") {
633
- if (seq.finish) seq.finish.call(self);
634
- self.stop();
635
- return true;
636
- }
637
- else if (self._loop == "remove") {
638
- if (self._options.addIn) {
639
- this.empty();
640
- }
641
- else {
642
- this.remove();
643
- }
644
- if (seq.finish) seq.finish.call(self);
645
- self.stop();
646
- return true;
647
- }
648
- return false;
649
- }
650
-
651
-
652
- if (seq.frames[0] instanceof Array) {
653
-
654
- if (seq.frames[i] === undefined) {
655
- i = 0;
656
- if (finish.call(this)) return;
657
- }
658
- this.empty();
659
-
660
- seq.framesDefault = seq.framesDefault || {};
661
- if (!seq.framesDefault.x) seq.framesDefault.x = 0;
662
- if (!seq.framesDefault.y) seq.framesDefault.y = 0;
663
- if (!seq.framesDefault.zoom) seq.framesDefault.zoom = 100;
664
- if (!seq.framesDefault.opacity) seq.framesDefault.opacity = 255;
665
- if (!seq.framesDefault.rotation) seq.framesDefault.rotation = 0;
666
-
667
- if (!seq.frames[i]) {
668
- i++;
669
- return;
670
- }
671
-
672
- for (var j=0 ; j < seq.frames[i].length ; j++) {
673
- currentSeq = seq.frames[i][j];
674
- if (currentSeq) {
675
- children = this.scene.createElement(seq.size.width, seq.size.height);
676
- id = currentSeq.pattern-1;
677
- children.setOriginPoint("middle");
678
- children.x = currentSeq.x != undefined ? currentSeq.x : seq.framesDefault.x;
679
- children.y = currentSeq.y != undefined ? currentSeq.y : seq.framesDefault.y;
680
- children.scaleX = currentSeq.zoom != undefined ? currentSeq.zoom / 100 : seq.framesDefault.zoom / 100;
681
- children.scaleY = currentSeq.zoom != undefined ? currentSeq.zoom / 100 : seq.framesDefault.zoom / 100;
682
- children.opacity = currentSeq.opacity != undefined ? currentSeq.opacity / 255 : seq.framesDefault.opacity / 255;
683
- children.rotation = currentSeq.rotation != undefined ? currentSeq.rotation : seq.framesDefault.rotation;
684
- drawImage(children, id);
685
- this.append(children);
686
- }
687
- }
688
-
689
- }
690
- else {
691
- id = seq.frames[0] + i;
692
-
693
- if (id > seq.frames[1]) {
694
- i = 0;
695
- finish.call(this);
696
- drawImage(this, seq.frames[0]);
697
- }
698
- else {
699
- drawImage(this, id);
700
- }
701
-
702
- }
703
-
704
- i++;
705
- }
706
- freq = 0;
707
- }
708
- });
709
-
710
-
711
- },
712
- /**
713
- @doc animation/
714
- @method stop Stop animation
715
- */
716
- stop: function() {
717
- this._stop = true;
718
- },
719
- /**
720
- @doc animation/
721
- @method play Adds a loop listener in a element to perform an animation (with addLoopListener). From the method call, the loop is started on the animation stop. Use the play method to start the animation
722
- @param {String} seq Name of the sequence corresponding to the key in the initialization of the class
723
- @param {String} type (optional) Choose from :
724
-
725
- * loop : Animation loop
726
- * stop : The animation is done once and stops
727
- * remove : The animation is done once and removed at the end
728
- */
729
- play: function(seq, type) {
730
- this._loop = type;
731
- this._seq = seq;
732
- this._stop = false;
733
- }
734
- });
735
-
736
-
737
-
738
-
739
- var Animation = {
740
- /**
741
- @doc timeline
742
- @class Timeline Create a temporal animation. See [to()](?p=extends.timeline.to)
743
- @param {Element} el
744
- @example
745
-
746
- var timeline = canvas.Timeline.new(el);
747
- timeline.to({x: 100}, 70, Ease.easeOutElastic).call();
748
-
749
- */
750
- Timeline: {
751
- New: function() { return this["new"].apply(this, arguments); },
752
- "new": function(el) {
753
- return Class["new"]("Timeline", [el]);
754
- }
755
- },
756
-
757
- /**
758
-
759
- @doc animation
760
- @class Animation View an animation from an image
761
- @param {Object} options
762
-
763
- * images {String|Array} : identifying the image. If the value is an array. The different images are chained
764
- * addIn {CanvasEngine.Element} : allows you to add animation to an existing element
765
- * animations {Object} : Includes all animations. The key is the identifier of the animation. The value parameters (view example)
766
- * frames {Array} : Array with two elements: the first is to play the first frame, the second frame is the arrival
767
- * size {Object} : Set the width and height of the sequence (`width` and `height` keys )
768
- * patternSize {Object} `(>= 1.2.5)` : Number of patterns :
769
-
770
- Example :
771
-
772
- patternSize: {
773
- width: 4,
774
- height: 4
775
- }
776
-
777
- If the image measurement 100*100px, this amounts to:
778
-
779
- size: {
780
- width: 100 / 4,
781
- height: 100 / 4
782
- }
783
-
784
-
785
- * frequence {integer} : Frequency display. The higher the value the more high frequency is low
786
- * finish {Function} : Callback when the animation is finished
787
- * image {String} : identifying the image specific to this animation
788
- * position {Object} : offset of the animation display ("left" and "top" keys)
789
-
790
- @example
791
-
792
- In method `ready` of the scene :
793
-
794
- var el = this.createElement();
795
- var animation = canvas.Animation.new({
796
- images: "chara",
797
- animations: {
798
- run: {
799
- frames: [0, 5],
800
- size: {
801
- width: 42,
802
- height: 42
803
- },
804
- frequence: 3,
805
- finish: function() {
806
-
807
- }
808
- }
809
- }
810
- });
811
- stage.append(el);
812
- animation.add(el);
813
- animation.play("run", "loop");
814
-
815
-
816
- <jsfiddle>WebCreative5/77wUh/3</jsfiddle>
817
-
818
- You can also chaining multiple different images:
819
-
820
- var el = this.createElement(),
821
- animation = canvas.Animation.new({
822
- images: ["foo", "bar"],
823
- animations: {
824
- _default: {
825
- frequence: 7
826
- }
827
- }
828
- });
829
- animation.add(el);
830
- animation.play("_default", "loop");
831
-
832
-
833
- <jsfiddle>WebCreative5/E2vVW</jsfiddle>
834
-
835
- ## Example of an image with several sequences
836
-
837
- Here is the picture:
838
-
839
- ![](http://canvasengine.net/examples/img/fire.png)
840
-
841
- You can retrieve sequences and create a particular animation :
842
-
843
- canvas.Animation.New({
844
- images: "fire",
845
- addIn: stage,
846
- animations: {
847
- _default: {
848
- position: {
849
- top: 75/2,
850
- left: 74/2
851
- },
852
- frequence: 8,
853
- frames: [
854
- [{"pattern":7}],
855
- [{"pattern":8}],
856
- [{"pattern":9}]
857
- ],
858
- size: {
859
- width: 75,
860
- height: 74
861
- }
862
- }
863
- }
864
- });
865
-
866
- * position : Position of the point of origin
867
- * frequence : Frequency
868
- * frames : Different sequences for each frame. A frame is composed of an array with several sequences
869
- * pattern : Identifying the pattern. The identifier starts at 1 and increments in the direction of West Reading
870
- * x : Position X
871
- * y : Position Y
872
- * zoom : Scaling between 0 and 100
873
- * opacity : Opacity between 0 and 255
874
- * rotation : Rotation between 0 and 360
875
-
876
- <jsfiddle>WebCreative5/ePwtq</jsfiddle>
877
-
878
- It is possible to provide default values width `framesDefault` that will apply to all sequences that do not have the property :
879
-
880
- canvas.Animation.New({
881
- images: "fire",
882
- addIn: stage,
883
- animations: {
884
- _default: {
885
- framesDefault: {
886
- rotation: 20
887
- },
888
- frames: [
889
- [{"pattern":7}],
890
- [{"pattern":8}],
891
- [{"pattern":9}]
892
- ],
893
- size: {
894
- width: 75,
895
- height: 74
896
- }
897
- }
898
- }
899
- });
900
-
901
-
902
- */
903
- Animation: {
904
- New: function() { return this["new"].apply(this, arguments); },
905
- "new": function(options) {
906
- return Class["new"]("Animation", [options]);
907
- }
908
- }
909
- };
910
-