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,952 +0,0 @@
1
- var fs;
2
- if (typeof(require) !== "undefined") {
3
- fs = require('fs');
4
- }
5
-
6
- function Kernel(class_method, name) {
7
- this.class_method = class_method;
8
- this.class_name = name;
9
- }
10
-
11
- Kernel._extend = function(self, object, clone) {
12
- var o;
13
- if (!(object instanceof Array)) {
14
- object = [object];
15
- }
16
- for (var i=0 ; i < object.length ; i++) {
17
- clone = clone === undefined ? true : clone;
18
- o = object[i];
19
- if (typeof o == "string") {
20
- if (Class.__class_config[o]) {
21
- o = Class.__class_config[o].methods;
22
- }
23
- else {
24
- return self;
25
- }
26
- }
27
-
28
- if (clone) o = CanvasEngine.clone(o);
29
-
30
- for (var key in o) {
31
- self[key] = o[key];
32
- }
33
- }
34
- return self;
35
- }
36
-
37
- Kernel.prototype = {
38
- New: function() { return this["new"].apply(this, arguments) },
39
- "new": function() {
40
- this._class = new Class();
41
- Class.__class[this.class_name] = this._class;
42
- this._construct();
43
- return this._class;
44
- },
45
- _construct: function() {
46
- this._class.extend(this.class_method);
47
- },
48
- _attr_accessor: function(attrs, reader, writer) {
49
- var self = this;
50
- for (var i=0 ; i < attrs.length ; i++) {
51
- this.class_method["_" + attrs[i]] = null;
52
- this.class_method[attrs[i]] = {};
53
- if (reader) {
54
- this.class_method[attrs[i]].set = function(value) {
55
- self.class_method["_" + attrs[i]] = value;
56
- };
57
- }
58
- if (writer) {
59
- this.class_method[attrs[i]].get = function() {
60
- return self.class_method["_" + attrs[i]];
61
- };
62
- }
63
- }
64
- return this;
65
- },
66
- /**
67
- @doc class/
68
- @method attr_accessor Defines the properties that can be read and modified
69
- @params {Array} Properties names in an array
70
- @example
71
-
72
- Class.create("Foo", {
73
-
74
- mymethod: function() {
75
- this.bar.set(5);
76
- console.log(this.bar.get()); // Value of property "bar" is 5
77
- console.log(this._bar); // ditto
78
- }
79
-
80
- }).attr_accessor(["bar"]);
81
-
82
- <jsfiddle>WebCreative5/HzCSm/1</jsfiddle>
83
- @return {Object}
84
- */
85
- attr_accessor: function(attrs) {
86
- return this._attr_accessor(attrs, true, true);
87
- },
88
- /**
89
- @doc class/
90
- @method attr_reader Defines the properties that can be only read
91
- @params {Array} Properties names in an array
92
- @example
93
-
94
- Class.create("Foo", {
95
-
96
- mymethod: function() {
97
- console.log(this.bar.get());
98
- }
99
-
100
- }).attr_reader(["bar"]);
101
-
102
- @return {Object}
103
- */
104
- attr_reader: function(attrs) {
105
- return this._attr_accessor(attrs, true, false);
106
- },
107
- /**
108
- @doc class/
109
- @method attr_writer Defines the properties that can be only modified
110
- @params {Array} Properties names in an array
111
- @example
112
-
113
- Class.create("Foo", {
114
-
115
- mymethod: function() {
116
- this.bar.set(5);
117
- console.log(this._bar);
118
- }
119
-
120
- }).attr_writer(["bar"]);
121
-
122
- @return {Object}
123
- */
124
- attr_writer: function(attrs) {
125
- return this._attr_accessor(attrs, false, true);
126
- },
127
- /**
128
- @doc class/
129
- @method extend add object in this class
130
- @params {Object|String} object or name of existing class
131
- @params {Boolean} clone (optional) Makes a clone of the object (false by default)
132
- @example
133
-
134
- Example 1 :
135
-
136
- Class.create("Foo", {
137
-
138
- mymethod: function() {
139
-
140
- }
141
-
142
- }).extend({
143
- othermethod: function() {
144
-
145
- }
146
- });
147
-
148
- Example 2 :
149
-
150
- Class.create("Bar", {
151
-
152
- initialize: function() {
153
-
154
- }
155
-
156
- });
157
-
158
- Class.create("Foo", {
159
-
160
- mymethod: function() {
161
-
162
- }
163
-
164
- }).extend("Bar");
165
-
166
- @return {Object}
167
- */
168
- extend: function(object, clone) {
169
- Kernel._extend(this.class_method, object, clone);
170
- return this;
171
- },
172
- // TODO
173
- addIn: function(name) {
174
- if (!Class.__class[name]) {
175
- return this;
176
- }
177
- Class.__class[name][this.name] = this;
178
- return this;
179
- }
180
-
181
- }
182
-
183
- function Class() {
184
- this.name = null;
185
- }
186
-
187
- Class.__class = {};
188
- Class.__class_config = {};
189
-
190
- /**
191
- @doc class/
192
- @method get By retrieve the class name
193
- @static
194
- @params {String} name Class name
195
- @return {Kernel} Core class
196
- */
197
- Class.get = function(name) {
198
- return Class.__class[name];
199
- };
200
-
201
- /**
202
- @doc class/
203
- @method create Creating a class. the constructor is the method "initialize"
204
- @static
205
- @params {String} name Class name
206
- @params {Object} methods Methods and properties of the class
207
- @example
208
-
209
- Class.create("Foo", {
210
- bar: null,
211
- initialize: function(bar) {
212
- this.bar = bar;
213
- }
214
- });
215
- var foo = Class.new("Foo", ["Hello World"]);
216
-
217
- <jsfiddle>WebCreative5/cbtFk</jsfiddle>
218
- @return {Kernel} Core class
219
- */
220
- Class.create = function(name, methods, _static) {
221
- var p, _class, _tmp_class;
222
-
223
- /*if (typeof(window) === 'undefined') {
224
- var window = {};
225
- }*/
226
- Class.__class_config[name] = {};
227
- Class.__class[name] = {};
228
- /* Class.__class[name] = function(params) {
229
- // this.__parent = Class;
230
- // this.__parent();
231
- if (this.initialize) {
232
- this.initialize.apply(this, params);
233
- }
234
- };*/
235
-
236
- if (_static) {
237
- p = window[name];
238
- tmp_class = new Class();
239
- for (var key in tmp_class) {
240
- p[key] = tmp_class[key];
241
- }
242
- for (var key in methods) {
243
- p[key] = methods[key];
244
- }
245
- _class = p;
246
- }
247
- else {
248
- //p = Class.__class[name].prototype = methods;
249
- Class.__class_config[name].methods = methods;
250
- var kernel = Class.__class_config[name].kernel = new Kernel(Class.__class_config[name].methods, name);
251
- //p.extend(methods);
252
- }
253
- return kernel;
254
- }
255
-
256
- /**
257
- @doc class/
258
- @method new new class.
259
- @static
260
- @params {String} name Class name
261
- @params {Array} params (optional) Parameters for the constructor
262
- @params {Boolean} initialize (optional) Calls the constructor "initialize" (true by default)
263
- @return {Class}
264
- */
265
- Class.New = function() { return Class["new"].apply(this, arguments) };
266
- Class["new"] = function(name, params, initialize) {
267
- var _class;
268
-
269
- if (typeof params == "boolean") {
270
- initialize = params;
271
- params = [];
272
- }
273
-
274
- if (initialize == undefined) {
275
- initialize = true;
276
- }
277
-
278
- params = params || [];
279
-
280
- if (!Class.__class_config[name]) {
281
- throw name + " class does not exist. Use method \"create\" for build the structure of this class";
282
- }
283
- _class = Class.__class_config[name].kernel["new"]();
284
- if (initialize && _class.initialize) {
285
- _class.initialize.apply(_class, params);
286
- }
287
-
288
- _class.__name__ = name;
289
- return _class;
290
- }
291
-
292
- Class.prototype = {
293
- /**
294
- @method extend add object in this class
295
- @params {Object} object
296
- @parmas {Boolean} clone (optional) Makes a clone of the object (false by default)
297
- @example
298
-
299
- Class.create("Foo", {
300
-
301
- mymethod: function() {
302
-
303
- }
304
-
305
- });
306
- Class.new("Foo").extend({
307
- othermethod: function() {
308
-
309
- }
310
- });
311
-
312
- @return {Object}
313
- */
314
- extend: function(object, clone) {
315
- return Kernel._extend(this, object, clone);
316
- }
317
- }
318
-
319
- var CanvasEngine = {};
320
-
321
- /**
322
- @doc utilities/
323
- @method uniqid Generating a unique identifier by date
324
- @static
325
- @return {String}
326
- */
327
- CanvasEngine.uniqid = function() {
328
- // return new Date().getTime();
329
- return Math.random();
330
- };
331
-
332
- /**
333
- @doc utilities/
334
- @method arraySplice Removes an element in an array by value
335
- @static
336
- @params {Object} val
337
- @params {Array} array
338
- */
339
- CanvasEngine.arraySplice = function(val, array) {
340
- var i;
341
- for (i=0 ; i < array.length ; ++i) {
342
- if (val == array[i]) {
343
- array.splice(i, 1);
344
- return;
345
- }
346
- }
347
- };
348
-
349
- /**
350
- @doc ajax/
351
- @method ajax Perform an asynchronous HTTP (Ajax) request. System uses wire on Node.js
352
- @static
353
- @params {Object} options
354
-
355
- * url {String} File Path
356
- * type {String} (optional) "GET" (default) or "POST"
357
- * data {Object} (optional) Data key/value
358
- * dataType {String} (optional) "text" (default), "json" or "xml"
359
- * success {Function} (optional) Callback if the request was successful
360
- */
361
- CanvasEngine.ajax = function(options) {
362
-
363
- options = CanvasEngine.extend({
364
- url: "./",
365
- type: "GET",
366
- statusCode: {}
367
- }, options);
368
-
369
- options.data = options.data ? JSON.stringify(options.data) : null;
370
-
371
- if (fs) {
372
- fs.readFile('./' + options.url, 'ascii', function (err, ret) {
373
- if (err) throw err;
374
- ret = ret.toString('ascii');
375
- if (options.dataType == 'json') {
376
- ret = CanvasEngine.parseJSON(ret);
377
- }
378
- options.success(ret);
379
- });
380
- return;
381
- }
382
-
383
- var xhr;
384
- try { xhr = new ActiveXObject('Msxml2.XMLHTTP'); }
385
- catch (e)
386
- {
387
- try { xhr = new ActiveXObject('Microsoft.XMLHTTP'); }
388
- catch (e2)
389
- {
390
- try { xhr = new XMLHttpRequest(); }
391
- catch (e3) { xhr = false; }
392
- }
393
- }
394
-
395
- function onSuccess() {
396
- var ret;
397
- if (options.success) {
398
- ret = xhr.responseText;
399
- if (options.dataType == 'json') {
400
- ret = CanvasEngine.parseJSON(ret);
401
- }
402
- else if (options.dataType == 'xml') {
403
- ret = xhr.responseXML;
404
- }
405
- options.success(ret);
406
- }
407
- }
408
-
409
- xhr.onreadystatechange = function() {
410
- if (xhr.readyState == 4) {
411
- if (options.statusCode && options.statusCode[xhr.status]) options.statusCode[xhr.status]();
412
- if (xhr.status == 200) {
413
- onSuccess();
414
- }
415
- else {
416
- if (options.error) options.error(xhr);
417
- }
418
- }
419
- };
420
-
421
- xhr.open(options.type, options.url, true);
422
- if (options.mimeType) {
423
- xhr.overrideMimeType(options.mimeType);
424
- }
425
- xhr.send(options.data);
426
-
427
- }
428
-
429
- /**
430
- @doc ajax/
431
- @method getJSON Load JSON-encoded data from the server using a GET HTTP request.
432
- @static
433
- @params {String} url File Path
434
- @params {String} (optional) data Data key/value
435
- @params {Function} (optional) callback Callback if the request was successful
436
- */
437
- CanvasEngine.getJSON = function(url, data, callback) {
438
- if (typeof data == "function") {
439
- callback = data;
440
- data = null;
441
- }
442
- CanvasEngine.ajax({
443
- url: url,
444
- dataType: 'json',
445
- data: data,
446
- success: callback
447
- });
448
- }
449
-
450
- /**
451
- @doc utilities/
452
- @method parseJSON Takes a well-formed JSON string and returns the resulting JavaScript object.
453
- @static
454
- @params {String} json JSON format
455
- @return {Object}
456
- */
457
- CanvasEngine.parseJSON = function(json) {
458
- return JSON.parse(json);
459
- }
460
-
461
- /**
462
- @doc utilities/
463
- @method each The array is read and sent to a callback function
464
- @static
465
- @params {Array|Integer} array If the value is an integer, it returns to perform a number of loop iteration
466
- @params {Function} callback two parameters :
467
-
468
- * index
469
- * value
470
-
471
- @example
472
-
473
- var foo = ["bar", "test"];
474
- CE.each(foo, function(i, val) {
475
- console.log(val);
476
- });
477
-
478
-
479
- var foo = ["bar", "test"];
480
- CE.each(2, function(i) {
481
- console.log(foo[i]);
482
- });
483
-
484
- */
485
- CanvasEngine.each = function(array, callback) {
486
- var i, l;
487
- if (array instanceof Array) {
488
- l = array.length;
489
- }
490
- else {
491
- l = array;
492
- array = [];
493
- }
494
- for (i=0 ; i < l ; ++i) {
495
- callback.call(array, i, array[i]);
496
- }
497
- }
498
-
499
- /**
500
- @doc utilities/
501
- @method inArray The CE.inArray() method is similar to JavaScript's native .indexOf() method in that it returns -1 when it doesn't find a match. If the first element within the array matches value, CE.inArray() returns 0.
502
-
503
- Because JavaScript treats 0 as loosely equal to false (i.e. 0 == false, but 0 !== false), if we're checking for the presence of value within array, we need to check if it's not equal to (or greater than) -1.
504
- @static
505
- @params {String} val The value to search for.
506
- @params {Array} array An array through which to search.
507
- @return {Integer}
508
- */
509
- CanvasEngine.inArray = function(val, array) {
510
- var i;
511
- for (i=0 ; i < array.length ; ++i) {
512
- if (val == array[i]) {
513
- return i;
514
- }
515
- }
516
- return -1;
517
- };
518
-
519
- /**
520
- @doc engine/
521
- @method clone Clone an object
522
- @static
523
- @params {Object} instance
524
- @return {Object}
525
- */
526
- CanvasEngine.clone = function(srcInstance) {
527
- var i;
528
- if(typeof(srcInstance) != 'object' || srcInstance == null) {
529
- return srcInstance;
530
- }
531
- var newInstance = srcInstance.constructor();
532
- if (newInstance === undefined) {
533
- return srcInstance;
534
- }
535
- for(i in srcInstance){
536
- newInstance[i] = CanvasEngine.clone(srcInstance[i]);
537
- }
538
- return newInstance;
539
- };
540
-
541
-
542
-
543
- /**
544
- @doc utilities/
545
- @method hexaToRGB Converts the hexadecimal value of a color in RGB. Returns an array with 3 colors : [r, g, b]
546
- @static
547
- @params {String} hexa Hexadecimal with or without #
548
- @return {Array}
549
- */
550
- CanvasEngine.hexaToRGB = function(hexa) {
551
- var r, g, b;
552
-
553
- function cutHex(h) {
554
- return (h.charAt(0) == "#") ? h.substring(1,7) : h;
555
- }
556
-
557
- r = parseInt((cutHex(hexa)).substring(0,2),16);
558
- g = parseInt((cutHex(hexa)).substring(2,4),16);
559
- b = parseInt((cutHex(hexa)).substring(4,6),16);
560
-
561
- return [r, g, b];
562
- };
563
-
564
- /**
565
- @doc utilities/
566
- @method rgbToHex Converts the RGB value of a color in Hexadecimal.
567
- @static
568
- @params {String} r Red value (0-255)
569
- @params {String} g Green value (0-255)
570
- @params {String} b Blue value (0-255)
571
- @return {String}
572
- */
573
- CanvasEngine.rgbToHex = function(r, g, b) {
574
- return ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
575
- };
576
-
577
- // Private
578
- CanvasEngine._getRandomColorKey = function() {
579
- var r = Math.round(Math.random() * 255),
580
- g = Math.round(Math.random() * 255),
581
- b = Math.round(Math.random() * 255);
582
- return CanvasEngine.rgbToHex(r, g, b);
583
- };
584
-
585
- /**
586
- @doc utilities/
587
- @method random Random value between `min` and `max`
588
- @static
589
- @params {Integer} min
590
- @params {Integer} max
591
- @return {Integer}
592
- */
593
- CanvasEngine.random = function(min, max) {
594
- return Math.floor((Math.random() * max) + min);
595
- };
596
-
597
- /**
598
- @doc utilities/
599
- @method mobileUserAgent Returns the name of the user agent used
600
- @static
601
- @return {String|Boolean} name of the agent user ("iphone", "ipod", "ipad", "blackberry", "android" or "windows phone") or false if it is not a mobile
602
- @example
603
-
604
- if (CE.mobileUserAgent()) {
605
- // It's a mobile
606
- }
607
- if (CE.mobileUserAgent() == "android") {
608
- // It's a Android mobile
609
- }
610
- */
611
- CanvasEngine.mobileUserAgent = function() {
612
- var ua = navigator.userAgent;
613
- if (ua.match(/(iPhone)/))
614
- return "iphone";
615
- else if (ua.match(/(iPod)/))
616
- return "ipod";
617
- else if (ua.match(/(iPad)/))
618
- return "ipad";
619
- else if (ua.match(/(BlackBerry)/))
620
- return "blackberry";
621
- else if (ua.match(/(Android)/))
622
- return "android";
623
- else if (ua.match(/(Windows Phone)/))
624
- return "windows phone";
625
- else
626
- return false;
627
- };
628
-
629
- CanvasEngine._benchmark = {};
630
- CanvasEngine._interval_benchmark = 60;
631
- CanvasEngine._freq_benchmark = {};
632
- CanvasEngine.microtime = function() {
633
- var now = new Date().getTime() / 1000;
634
- var s = parseInt(now, 10);
635
-
636
- return now * 1000;
637
- };
638
- CanvasEngine.benchmark = function(id) {
639
- var m = this.microtime();
640
- if (this._benchmark[id]) {
641
- console.log("Performance " + id + " : " + (m - this._benchmark[id]) + "ms");
642
- }
643
- this._benchmark[id] = m;
644
-
645
- };
646
-
647
- CanvasEngine.objectSize = function(obj) {
648
- var size = 0, key;
649
- for (key in obj) {
650
- if (obj.hasOwnProperty(key)) size++;
651
- }
652
- return size;
653
- };
654
-
655
- /**
656
- @doc utilities/
657
- @method extend `(>= 1.2.7)` Merging two objects
658
- @static
659
- @params {Object} target An object that will receive the new properties if additional objects are passed in.
660
- @params {Object} obj An object containing additional properties to merge in.
661
- @examples
662
-
663
- CE.extend({a: 1}, {b: 2}); // =>{a: 1, b: 2}
664
-
665
- @return {Object}
666
- */
667
- // TODO : clone
668
- CanvasEngine.extend = function(obj1, obj2, clone) {
669
- if (!obj1) obj1 = {};
670
- if (!obj2) obj2 = {};
671
- return Kernel._extend(obj1, obj2, clone);
672
- }
673
-
674
- /*CanvasEngine.browser = function() {
675
- var ua = navigator.userAgent.toLowerCase();
676
- var browser = {};
677
-
678
- function testNavigator(name) {
679
- var patt = new RegExp (name);
680
- browser[name] = {};
681
- browser[name] = patt.test(ua) && (name == "mozilla" ? !/webkit/.test(ua) : true);
682
- if (browser[name]
683
- }
684
-
685
-
686
- browser.webkit = /webkit/.test(ua);
687
- browser.opera = /opera/.test(ua);
688
- browser.msie = /msie/.test(ua);
689
- /msie\/([^ ]+)/.exec();
690
- return browser;
691
- }*/
692
-
693
- /**
694
- @doc utilities/
695
- @static
696
- @property browser `(>= 1.2.8)` Retrieves the methods and properties related to browser. It contains flags for each of the four most prevalent browser classes (Internet Explorer, Mozilla, Webkit, and Opera) as well as version information.
697
-
698
- * `mozilla` : Tests if the browser is Mozilla
699
- * `webkit` : Tests if the browser is Webkit (Chrome, Safari)
700
- * `opera` : Tests if the browser is Opera
701
- * `msie` : Tests if the browser is Internet Explorer
702
-
703
- Example
704
-
705
- if (CE.browser.msie) {
706
- // if browser is Internet Explorer
707
- }
708
-
709
- Knowing the browser version :
710
-
711
- * `version`
712
-
713
- Example
714
-
715
- if (CE.browser.msie && parseInt(CE.browser.version) == 9) { // crop value
716
- // if browser is Internet Explorer 9
717
- }
718
-
719
- Knowing the browser used by the user :
720
-
721
- * `which()`. Returns the object: `{ua: , version: }`
722
-
723
- Example
724
-
725
- CE.browser.which(); // returns {ua: "mozilla", version: "22.0"}
726
-
727
- @type Object
728
- */
729
-
730
- if (typeof exports == "undefined") {
731
- var _ua = navigator.userAgent.toLowerCase(),
732
- _version = /(chrome|firefox|msie|version)(\/| )([0-9.]+)/.exec(_ua);
733
- CanvasEngine.browser = {
734
- mozilla: /mozilla/.test(_ua) && !/webkit/.test(_ua),
735
- webkit: /webkit/.test(_ua),
736
- opera: /opera/.test(_ua),
737
- msie: /msie/.test(_ua),
738
- version: _version ? _version[3] : null,
739
- which: function() {
740
- var is;
741
- CanvasEngine.each(["mozilla", "webkit", "opera", "msie"], function(i, ua) {
742
- if (CanvasEngine.browser[ua]) {
743
- is = ua;
744
- }
745
- })
746
- return {
747
- ua: is,
748
- version: CanvasEngine.browser.version
749
- };
750
- }
751
- };
752
- }
753
-
754
-
755
-
756
- /**
757
- @doc utilities/
758
- @method moveArray Move one index to another location of an array
759
- @static
760
- @params {&Array} array Array to handle
761
- @params {Integer} pos1 Index of the element to move
762
- @params {Integer} pos2 Destination index
763
- @return {Array}
764
- */
765
- // http://jsperf.com/array-prototype-move
766
- // by Richard Scarrott (http://www.richardscarrott.co.uk)
767
- CanvasEngine.moveArray = function(array, pos1, pos2) {
768
- // local variables
769
- var i, tmp;
770
- // cast input parameters to integers
771
- pos1 = parseInt(pos1, 10);
772
- pos2 = parseInt(pos2, 10);
773
- // if positions are different and inside array
774
- if (pos1 !== pos2 && 0 <= pos1 && pos1 <= array.length && 0 <= pos2 && pos2 <= array.length) {
775
- // save element from position 1
776
- tmp = array[pos1];
777
- // move element down and shift other elements up
778
- if (pos1 < pos2) {
779
- for (i = pos1; i < pos2; i++) {
780
- array[i] = array[i + 1];
781
- }
782
- }
783
-
784
- // move element up and shift other elements down
785
- else {
786
- for (i = pos1; i > pos2; i--) {
787
- array[i] = array[i - 1];
788
- }
789
- }
790
- // put element from position 1 to destination
791
-
792
- array[pos2] = tmp;
793
- }
794
-
795
-
796
-
797
- return array;
798
- }
799
-
800
- /**
801
- @doc utilities/
802
- @method toTimer `(>= 1.3.0)` Converts seconds into a format {hour: "", min: "", sec: ""}
803
- @static
804
- @params {Integer} total_sec Secondes
805
- @return {Array}
806
- @examples
807
-
808
- CE.toTimer(136); // => {hour: "00", min: "02", sec: "16"}
809
-
810
- */
811
- CanvasEngine.toTimer = function(total_sec) {
812
- var hour = "" + Math.floor(total_sec / 60 / 60),
813
- min = "" + Math.floor(total_sec / 60 % 60),
814
- sec = "" + Math.floor(total_sec % 60);
815
- if (hour.length == 1) hour = "0" + hour;
816
- if (min.length == 1) min = "0" + min;
817
- if (sec.length == 1) sec = "0" + sec;
818
- return {
819
- hour: hour,
820
- min: min,
821
- sec: sec
822
- };
823
- }
824
-
825
- CanvasEngine.algo = {
826
-
827
- pascalTriangle: function(max) {
828
-
829
- max = max || 10;
830
-
831
- var enchain = [[1,1], [1,2,1]],
832
- nb_max_move = max - enchain.length;
833
-
834
- for (var i=enchain.length ; i <= nb_max_move ; i++) {
835
- enchain[i] = [1]
836
- for (var j=1 ; j <= i ; j++) {
837
- enchain[i][j] = enchain[i-1][j] + enchain[i-1][j-1];
838
- }
839
- enchain[i][i+1] = 1;
840
- }
841
-
842
- return enchain;
843
-
844
- },
845
-
846
-
847
-
848
- }
849
-
850
- /**
851
- @doc utilities/
852
- @method toMatrix `(>= 1.3.0)` Transforms a one-dimensional array to a table with two Diemension
853
- @static
854
- @params {Array} array The one-dimensional array to convert
855
- @params {Integer} width Width of the matrix created
856
- @params {Integer} height Height of the matrix created
857
- @return {Array}
858
- @examples
859
-
860
- CE.toMatrix([1, 2, 3, 4], 2, 2); // => [[1, 2], [3, 4]]
861
-
862
- */
863
- CanvasEngine.toMatrix = function(array, width, height) {
864
- var matrix = [], k = 0;
865
- for (var j=0 ; j < height ; j++) {
866
- for (var i=0 ; i < width ; i++) {
867
- if (!matrix[i]) matrix[i] = [];
868
- matrix[i][j] = array[k];
869
- k++;
870
- }
871
- }
872
- return matrix;
873
- }
874
-
875
- /**
876
- @doc utilities/
877
- @method rotateMatrix `(>= 1.3.0)` Change the positions of the array elements, the rotation matrix
878
- @static
879
- @params {Array} array The matrix in question
880
- @params {String} rotation (optional) Rotation in degree : `90` or `-90`, `180` (`90` by default)
881
- @return {Array}
882
- @examples
883
-
884
- var matrix = [
885
- [1, 0],
886
- [1, 1],
887
- [1, 0]
888
- ];
889
-
890
- CE.rotateMatrix(matrix);
891
-
892
- // => [
893
- [1, 1, 1],
894
- [0, 1, 0]
895
- ]
896
-
897
- CE.rotateMatrix(matrix, "-90");
898
-
899
- // => [
900
- [0, 1, 0],
901
- [1, 1, 1]
902
- ]
903
-
904
- CE.rotateMatrix(matrix, "180");
905
-
906
- // => [
907
- [0, 1],
908
- [1, 1],
909
- [0, 1]
910
- ]
911
- */
912
- CanvasEngine.rotateMatrix = function(array, rotation) {
913
- var matrix = [], matrix2 = [];
914
-
915
- rotation = rotation || "90";
916
-
917
- if (rotation == "90" || rotation == "-90") {
918
- for (var j=0 ; j < array[0].length ; j++) {
919
- matrix[j] = [];
920
- for (var i=0 ; i < array.length ; i++) {
921
- matrix[j][i] = array[i][j];
922
- }
923
- }
924
- }
925
-
926
- if (rotation == "-90") {
927
- var j=0;
928
- for (var i=matrix.length-1 ; i >= 0 ; i--) {
929
- matrix2[j] = matrix[i];
930
- j++;
931
- }
932
- return matrix2;
933
- }
934
-
935
- if (rotation == "180") {
936
- for (var i=0 ; i < array.length ; i++) {
937
- matrix[i] = array[i].reverse();
938
- }
939
- }
940
-
941
- return matrix;
942
-
943
- }
944
-
945
-
946
- var _CanvasEngine = CanvasEngine;
947
-
948
-
949
- if (typeof(exports) !== "undefined") {
950
- exports.Class = Class;
951
- exports.CanvasEngine = CanvasEngine;
952
- }