-
Notifications
You must be signed in to change notification settings - Fork 40
/
flash.js
2651 lines (2225 loc) · 82.4 KB
/
flash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Core utils are a part of FlashJS engine
*
* http://flashjs.com
*
* Copyright (c) 2010 - 2012 pixelsresearch.com,
*/
if (Function.prototype.bind === undefined) {
Function.prototype.bind = function () {
var fn = this,
args = [].slice.call(arguments),
object = args.shift();
return function () {
return fn.apply(object, args.concat([].slice.call(arguments)));
};
};
}
if (Array.prototype.remove === undefined) {
Array.prototype.remove = function (s) {
for (var i = 0; i < this.length; i++) {
if (s === this[i]) this.splice(i, 1);
}
}
}
function replaceAll(txt, replace, with_this) {
return txt.replace(new RegExp(replace, 'g'), with_this);
}
function getFileExtension(filename) {
return (/[.]/.exec(filename))[0] ? /[^.]+$/.exec(filename)[0] : '';
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function cloneObject(obj) {
if (obj == null || typeof(obj) != 'object')
return obj;
var temp = obj.constructor(); // changed
for (var key in obj)
temp[key] = cloneObject(obj[key]);
return temp;
}
function defineGetterSetter(variableParent, variableName, getterFunction, setterFunction) {
if (Object.defineProperty) {
Object.defineProperty(variableParent, variableName, {
get: getterFunction,
set: setterFunction
});
}
else if (document.__defineGetter__) {
variableParent.__defineGetter__(variableName, getterFunction);
variableParent.__defineSetter__(variableName, setterFunction);
}
variableParent["get" + variableName] = getterFunction;
variableParent["set" + variableName] = setterFunction;
}
(function (w) {
//Namespace initialization
w.flash = w.flash || {};
w.flash.currentId = 0;
//UID functions to have unique id for all DisplayObjects
var UID = function () {
return w.flash.currentId++;
}
var ajax = function (URL, callback, errorCallback) {
if (w.XMLHttpRequest) {
var xmlhttp = new XMLHttpRequest()
} else if (w.ActiveXObject) {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200 || xmlhttp.status == 0) {
callback(JSON.parse(xmlhttp.response || xmlhttp.responseText));
} else {
errorCallback(xmlhttp.response || xmlhttp.responseText);
}
}
};
xmlhttp.open("GET", URL, true);
xmlhttp.send(null);
}
w.flash.extend = function (destination, source) {
if (destination === undefined) destination = new Object();
if (destination !== undefined) {
for (var k in source) {
if (source.hasOwnProperty(k)) {
destination[k] = source[k];
}
}
} else {
destination = source;
}
return destination;
}
var requestAnimFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function (/* function */ callback, /* DOMElement */ element) {
window.setTimeout(callback, 1000 / 60);
};
})();
var onEnterFrame = function () {
if (w.flash.stages !== undefined) {
var now = Date.now();
for (var i in w.flash.stages) {
var delta = now - w.flash.stages[i].lastFrameTime;
if (w.flash.stages[i].enabled === true && delta > w.flash.stages[i].interval) {
if (w.flash.stages[i].onEnterFrame) {
var timesRepeat = Math.floor(delta / w.flash.stages[i].interval);
timesRepeat = Math.min(timesRepeat, 50);
for (timesRepeat; timesRepeat > 0; timesRepeat--) {
w.flash.stages[i].onEnterFrame();
}
}
w.flash.stages[i].lastFrameTime = Date.now();
w.flash.stages[i].render();
}
}
}
if (w.flash.hooks.length > 0) {
var hooksLength = w.flash.hooks.length;
for (var i = 0; i < hooksLength; i++) {
w.flash.hooks[i]();
}
}
window.requestAnimFrame(w.onEnterFrame);
//TO DO flash.stage.trigger({type: flash.events.Event.ENTER_FRAME});
}
w.flash.subscribeToEnterFrame = function (handler) {
for (var i = 0; i < w.flash.hooks.length; i++) {
if (w.flash.hooks[i] === handler) {
return;
}
}
w.flash.hooks.push(handler);
}
w.flash.unsubscribeFromEnterFrame = function (handler) {
w.flash.hooks.remove(handler);
}
//Function to clone object into needed namespaces, every class description have to ends with this
w.flash.cloneToNamespaces = function (obj, name) {
w[name] = w.flash[name] = obj;
}
w.flash.hooks = [];
var showBrowserIsNotSupportedWindow = function (container) {
container.style.backgroundColor = '#ffffff';
container.color = '#000000'
container.innerHTML = "<div style='margin: 45px 45px 45px 45px; font-weight: bold; font-family: Arial; font-size: 40px; line-height: 36px;'>" +
"YOUR BROWSER<br>IS NOT SUPPORTED" +
"<p style='font-size: 24px;'>TRY ONE OF THESE:</p>" +
"<p style='font-size: 24px;'><a href='https://www.google.com/intl/en/chrome/browser/'>Chrome</a>," +
" <a href='http://windows.microsoft.com/en-US/internet-explorer/downloads/ie-9/worldwide-languages'>Internet explorer 9</a>, " +
"<a href='http://support.apple.com/downloads/#safari'>Safari</a>, " +
"<a href='http://www.mozilla.org/'>Firefox</a></p>" +
"</div>";
}
w.flash.cloneToNamespaces(showBrowserIsNotSupportedWindow, 'showBrowserIsNotSupportedWindow');
w.flash.cloneToNamespaces(navigator.userAgent.match(/(iPad|iPhone|iPod)/i) ? true : false, 'iOS');
w.flash.cloneToNamespaces(navigator.userAgent.match(/(iPad)/i) ? true : false, 'iPad');
w.flash.cloneToNamespaces(navigator.userAgent.match(/(iPhone)/i) ? true : false, 'iPhone')
w.flash.cloneToNamespaces(navigator.userAgent.match(/(iPod)/i) ? true : false, 'iPod');
w.flash.cloneToNamespaces(navigator.userAgent.match(/(iPad)/i) && (window.devicePixelRatio) && (window.devicePixelRatio >= 2) ? true : false, 'iPad3');
w.flash.cloneToNamespaces(navigator.userAgent.toLowerCase().indexOf("android") > -1 ? true : false, 'Android');
w.flash.cloneToNamespaces(ajax, 'ajax');
w.flash.cloneToNamespaces(UID, 'UID');
w.flash.cloneToNamespaces(requestAnimFrame, 'requestAnimFrame');
w.flash.cloneToNamespaces(onEnterFrame, 'onEnterFrame');
w.flash.onWindowFocus = function () {
clearInterval(w.flash.checkFocusInterval);
for (var i in w.flash.stages) {
w.flash.stages[i].lastFrameTime = Date.now();
}
}
w.addEventListener('blur', function () {
w.flash.windowFocusLeft = true;
w.flash.checkFocusInterval = setInterval(w.flash.onWindowFocus, 1000);
})
w.onEnterFrame();
})(window);
/*
* Matrix2D
* Visit http://createjs.com/ for documentation, updates and examples.
*
* Copyright (c) 2010 gskinner.com, inc.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
(function (ns) {
/**
* Represents an affine transformation matrix, and provides tools for constructing and concatenating matrixes.
* @class Matrix2D
* @constructor
* @param {Number} a Specifies the a property for the new matrix.
* @param {Number} b Specifies the b property for the new matrix.
* @param {Number} c Specifies the c property for the new matrix.
* @param {Number} d Specifies the d property for the new matrix.
* @param {Number} tx Specifies the tx property for the new matrix.
* @param {Number} ty Specifies the ty property for the new matrix.
**/
var Matrix2D = function (a, b, c, d, tx, ty) {
this.initialize(a, b, c, d, tx, ty);
}
var p = Matrix2D.prototype;
// static public properties:
/**
* An identity matrix, representing a null transformation. Read-only.
* @property identity
* @static
* @type Matrix2D
**/
Matrix2D.identity = null; // set at bottom of class definition.
/**
* Multiplier for converting degrees to radians. Used internally by Matrix2D. Read-only.
* @property DEG_TO_RAD
* @static
* @final
* @type Number
**/
Matrix2D.DEG_TO_RAD = Math.PI / 180;
// public properties:
/**
* Position (0, 0) in a 3x3 affine transformation matrix.
* @property a
* @type Number
**/
p.a = 1;
/**
* Position (0, 1) in a 3x3 affine transformation matrix.
* @property b
* @type Number
**/
p.b = 0;
/**
* Position (1, 0) in a 3x3 affine transformation matrix.
* @property c
* @type Number
**/
p.c = 0;
/**
* Position (1, 1) in a 3x3 affine transformation matrix.
* @property d
* @type Number
**/
p.d = 1;
/**
* Position (2, 0) in a 3x3 affine transformation matrix.
* @property atx
* @type Number
**/
p.tx = 0;
/**
* Position (2, 1) in a 3x3 affine transformation matrix.
* @property ty
* @type Number
**/
p.ty = 0;
/**
* Property representing the alpha that will be applied to a display-canvas object. This is not part of matrix
* operations, but is used for operations like getConcatenatedMatrix to provide concatenated alpha values.
* @property alpha
* @type Number
**/
p.alpha = 1;
/**
* Property representing the shadow that will be applied to a display-canvas object. This is not part of matrix
* operations, but is used for operations like getConcatenatedMatrix to provide concatenated shadow values.
* @property shadow
* @type Shadow
**/
p.shadow = null;
/**
* Property representing the compositeOperation that will be applied to a display-canvas object. This is not part of
* matrix operations, but is used for operations like getConcatenatedMatrix to provide concatenated
* compositeOperation values. You can find a list of valid composite operations at:
* <a href="https://developer.mozilla.org/en/Canvas_tutorial/Compositing">https://developer.mozilla.org/en/Canvas_tutorial/Compositing</a>
* @property compositeOperation
* @type String
**/
p.compositeOperation = null;
// constructor:
/**
* Initialization method.
* @method initialize
* @protected
* @return {Matrix2D} This matrix. Useful for chaining method calls.
*/
p.initialize = function (a, b, c, d, tx, ty) {
if (a != null) {
this.a = a;
}
this.b = b || 0;
this.c = c || 0;
if (d != null) {
this.d = d;
}
this.tx = tx || 0;
this.ty = ty || 0;
return this;
}
// public methods:
/**
* Concatenates the specified matrix properties with this matrix. All parameters are required.
* @method prepend
* @param {Number} a
* @param {Number} b
* @param {Number} c
* @param {Number} d
* @param {Number} tx
* @param {Number} ty
* @return {Matrix2D} This matrix. Useful for chaining method calls.
**/
p.prepend = function (a, b, c, d, tx, ty) {
var tx1 = this.tx;
if (a != 1 || b != 0 || c != 0 || d != 1) {
var a1 = this.a;
var c1 = this.c;
this.a = a1 * a + this.b * c;
this.b = a1 * b + this.b * d;
this.c = c1 * a + this.d * c;
this.d = c1 * b + this.d * d;
}
this.tx = tx1 * a + this.ty * c + tx;
this.ty = tx1 * b + this.ty * d + ty;
return this;
}
/**
* Appends the specified matrix properties with this matrix. All parameters are required.
* @method append
* @param {Number} a
* @param {Number} b
* @param {Number} c
* @param {Number} d
* @param {Number} tx
* @param {Number} ty
* @return {Matrix2D} This matrix. Useful for chaining method calls.
**/
p.append = function (a, b, c, d, tx, ty) {
var a1 = this.a;
var b1 = this.b;
var c1 = this.c;
var d1 = this.d;
this.a = a * a1 + b * c1;
this.b = a * b1 + b * d1;
this.c = c * a1 + d * c1;
this.d = c * b1 + d * d1;
this.tx = tx * a1 + ty * c1 + this.tx;
this.ty = tx * b1 + ty * d1 + this.ty;
return this;
}
/**
* Prepends the specified matrix with this matrix.
* @method prependMatrix
* @param {Matrix2D} matrix
**/
p.prependMatrix = function (matrix) {
this.prepend(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty);
this.prependProperties(matrix.alpha, matrix.shadow, matrix.compositeOperation);
return this;
}
/**
* Appends the specified matrix with this matrix.
* @method appendMatrix
* @param {Matrix2D} matrix
* @return {Matrix2D} This matrix. Useful for chaining method calls.
**/
p.appendMatrix = function (matrix) {
this.append(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty);
this.appendProperties(matrix.alpha, matrix.shadow, matrix.compositeOperation);
return this;
}
/**
* Generates matrix properties from the specified display-canvas object transform properties, and prepends them with this matrix.
* For example, you can use this to generate a matrix from a display-canvas object: var mtx = new Matrix2D();
* mtx.prependTransform(o.x, o.y, o.scaleX, o.scaleY, o.rotation);
* @method prependTransform
* @param {Number} x
* @param {Number} y
* @param {Number} scaleX
* @param {Number} scaleY
* @param {Number} rotation
* @param {Number} skewX
* @param {Number} skewY
* @param {Number} regX Optional.
* @param {Number} regY Optional.
* @return {Matrix2D} This matrix. Useful for chaining method calls.
**/
p.prependTransform = function (x, y, scaleX, scaleY, rotation, skewX, skewY, regX, regY) {
if (rotation % 360) {
var r = rotation * Matrix2D.DEG_TO_RAD;
var cos = Math.cos(r);
var sin = Math.sin(r);
} else {
cos = 1;
sin = 0;
}
if (regX || regY) {
// append the registration offset:
this.tx -= regX;
this.ty -= regY;
}
if (skewX || skewY) {
// TODO: can this be combined into a single prepend operation?
skewX *= Matrix2D.DEG_TO_RAD;
skewY *= Matrix2D.DEG_TO_RAD;
this.prepend(cos * scaleX, sin * scaleX, -sin * scaleY, cos * scaleY, 0, 0);
this.prepend(Math.cos(skewY), Math.sin(skewY), -Math.sin(skewX), Math.cos(skewX), x, y);
} else {
this.prepend(cos * scaleX, sin * scaleX, -sin * scaleY, cos * scaleY, x, y);
}
return this;
}
/**
* Generates matrix properties from the specified display-canvas object transform properties, and appends them with this matrix.
* For example, you can use this to generate a matrix from a display-canvas object: var mtx = new Matrix2D();
* mtx.appendTransform(o.x, o.y, o.scaleX, o.scaleY, o.rotation);
* @method appendTransform
* @param {Number} x
* @param {Number} y
* @param {Number} scaleX
* @param {Number} scaleY
* @param {Number} rotation
* @param {Number} skewX
* @param {Number} skewY
* @param {Number} regX Optional.
* @param {Number} regY Optional.
* @return {Matrix2D} This matrix. Useful for chaining method calls.
**/
p.appendTransform = function (x, y, scaleX, scaleY, rotation, skewX, skewY, regX, regY) {
if (rotation % 360) {
var r = rotation * Matrix2D.DEG_TO_RAD;
var cos = Math.cos(r);
var sin = Math.sin(r);
} else {
cos = 1;
sin = 0;
}
if (skewX || skewY) {
// TODO: can this be combined into a single append?
skewX *= Matrix2D.DEG_TO_RAD;
skewY *= Matrix2D.DEG_TO_RAD;
this.append(Math.cos(skewY), Math.sin(skewY), -Math.sin(skewX), Math.cos(skewX), x, y);
this.append(cos * scaleX, sin * scaleX, -sin * scaleY, cos * scaleY, 0, 0);
} else {
this.append(cos * scaleX, sin * scaleX, -sin * scaleY, cos * scaleY, x, y);
}
if (regX || regY) {
// prepend the registration offset:
this.tx -= regX * this.a + regY * this.c;
this.ty -= regX * this.b + regY * this.d;
}
return this;
}
/**
* Applies a rotation transformation to the matrix.
* @method rotate
* @param {Number} angle The angle in degrees.
* @return {Matrix2D} This matrix. Useful for chaining method calls.
**/
p.rotate = function (angle) {
var cos = Math.cos(angle);
var sin = Math.sin(angle);
var a1 = this.a;
var c1 = this.c;
var tx1 = this.tx;
this.a = a1 * cos - this.b * sin;
this.b = a1 * sin + this.b * cos;
this.c = c1 * cos - this.d * sin;
this.d = c1 * sin + this.d * cos;
this.tx = tx1 * cos - this.ty * sin;
this.ty = tx1 * sin + this.ty * cos;
return this;
}
/**
* Applies a skew transformation to the matrix.
* @method skew
* @param {Number} skewX The amount to skew horizontally in degrees.
* @param {Number} skewY The amount to skew vertically in degrees.
* @return {Matrix2D} This matrix. Useful for chaining method calls.
*/
p.skew = function (skewX, skewY) {
skewX = skewX * Matrix2D.DEG_TO_RAD;
skewY = skewY * Matrix2D.DEG_TO_RAD;
this.append(Math.cos(skewY), Math.sin(skewY), -Math.sin(skewX), Math.cos(skewX), 0, 0);
return this;
}
/**
* Applies a scale transformation to the matrix.
* @method scale
* @param {Number} x
* @param {Number} y
* @return {Matrix2D} This matrix. Useful for chaining method calls.
**/
p.scale = function (x, y) {
this.a *= x;
this.d *= y;
this.tx *= x;
this.ty *= y;
return this;
}
/**
* Translates the matrix on the x and y axes.
* @method translate
* @param {Number} x
* @param {Number} y
* @return {Matrix2D} This matrix. Useful for chaining method calls.
**/
p.translate = function (x, y) {
this.tx += x;
this.ty += y;
return this;
}
/**
* Sets the properties of the matrix to those of an identity matrix (one that applies a null transformation).
* @method identity
* @return {Matrix2D} This matrix. Useful for chaining method calls.
**/
p.identity = function () {
this.alpha = this.a = this.d = 1;
this.b = this.c = this.tx = this.ty = 0;
this.shadow = this.compositeOperation = null;
return this;
}
/**
* Inverts the matrix, causing it to perform the opposite transformation.
* @method invert
* @return {Matrix2D} This matrix. Useful for chaining method calls.
**/
p.invert = function () {
var a1 = this.a;
var b1 = this.b;
var c1 = this.c;
var d1 = this.d;
var tx1 = this.tx;
var n = a1 * d1 - b1 * c1;
this.a = d1 / n;
this.b = -b1 / n;
this.c = -c1 / n;
this.d = a1 / n;
this.tx = (c1 * this.ty - d1 * tx1) / n;
this.ty = -(a1 * this.ty - b1 * tx1) / n;
return this;
}
/**
* Returns true if the matrix is an identity matrix.
* @method isIdentity
* @returns Boolean
**/
p.isIdentity = function () {
return this.tx == 0 && this.ty == 0 && this.a == 1 && this.b == 0 && this.c == 0 && this.d == 1;
}
/**
* Decomposes the matrix into transform properties (x, y, scaleX, scaleY, and rotation). Note that this these values
* may not match the transform properties you used to generate the matrix, though they will produce the same visual
* results.
* @method decompose
* @param {Object} target The object to apply the transform properties to. If null, then a new object will be returned.
* @return {Matrix2D} This matrix. Useful for chaining method calls.
*/
p.decompose = function (target) {
// TODO: it would be nice to be able to solve for whether the matrix can be decomposed into only scale/rotation
// even when scale is negative
if (target == null) {
target = {};
}
target.x = this.tx;
target.y = this.ty;
target.scaleX = Math.sqrt(this.a * this.a + this.b * this.b);
target.scaleY = Math.sqrt(this.c * this.c + this.d * this.d);
var skewX = Math.atan2(-this.c, this.d);
var skewY = Math.atan2(this.b, this.a);
if (skewX == skewY) {
target.rotation = skewY / Matrix2D.DEG_TO_RAD;
if (this.a < 0 && this.d >= 0) {
target.rotation += (target.rotation <= 0) ? 180 : -180;
}
target.skewX = target.skewY = 0;
} else {
target.skewX = skewX / Matrix2D.DEG_TO_RAD;
target.skewY = skewY / Matrix2D.DEG_TO_RAD;
}
return target;
}
/**
* Reinitializes all matrix properties to those specified.
* @method appendProperties
* @param {Number} a
* @param {Number} b
* @param {Number} c
* @param {Number} d
* @param {Number} tx
* @param {Number} ty
* @param {Number} alpha desired alpha value
* @param {Shadow} shadow desired shadow value
* @param {String} compositeOperation desired composite operation value
* @return {Matrix2D} This matrix. Useful for chaining method calls.
*/
p.reinitialize = function (a, b, c, d, tx, ty, alpha, shadow, compositeOperation) {
this.initialize(a, b, c, d, tx, ty);
this.alpha = alpha || 1;
this.shadow = shadow;
this.compositeOperation = compositeOperation;
return this;
}
/**
* Appends the specified visual properties to the current matrix.
* @method appendProperties
* @param {Number} alpha desired alpha value
* @param {Shadow} shadow desired shadow value
* @param {String} compositeOperation desired composite operation value
* @return {Matrix2D} This matrix. Useful for chaining method calls.
*/
p.appendProperties = function (alpha, shadow, compositeOperation) {
this.alpha *= alpha;
this.shadow = shadow || this.shadow;
this.compositeOperation = compositeOperation || this.compositeOperation;
return this;
}
/**
* Prepends the specified visual properties to the current matrix.
* @method prependProperties
* @param {Number} alpha desired alpha value
* @param {Shadow} shadow desired shadow value
* @param {String} compositeOperation desired composite operation value
* @return {Matrix2D} This matrix. Useful for chaining method calls.
*/
p.prependProperties = function (alpha, shadow, compositeOperation) {
this.alpha *= alpha;
this.shadow = this.shadow || shadow;
this.compositeOperation = this.compositeOperation || compositeOperation;
return this;
}
/**
* Returns a clone of the Matrix2D instance.
* @method clone
* @return {Matrix2D} a clone of the Matrix2D instance.
**/
p.clone = function () {
var mtx = new Matrix2D(this.a, this.b, this.c, this.d, this.tx, this.ty);
mtx.shadow = this.shadow;
mtx.alpha = this.alpha;
mtx.compositeOperation = this.compositeOperation;
return mtx;
}
/**
* Returns a string representation of this object.
* @method toString
* @return {String} a string representation of the instance.
**/
p.toString = function () {
return "[Matrix2D (a=" + this.a + " b=" + this.b + " c=" + this.c + " d=" + this.d + " tx=" + this.tx + " ty=" + this.ty + ")]";
}
// this has to be populated after the class is defined:
Matrix2D.identity = new Matrix2D(1, 0, 0, 1, 0, 0);
ns.Matrix2D = Matrix2D;
}(window));
/*
* DisplayObject is a part of FlashJS engine
*
* http://flashjs.com
*
* Copyright (c) 2011 - 2012 pixelsresearch.com,
*/
(function (w) {
var DisplayObject = function (image, width, height, stage) {
this.init();
if (image !== undefined) {
if ((typeof image) === 'string') {
image = new ImageLoader(image, undefined, (function () {
this.setImage(image, width, height);
}).bind(this));
} else {
this.setImage(image);
}
}
if (stage !== undefined) {
this.setStage(stage);
}
}
var p = DisplayObject.prototype;
p.cacheCanvas = null;
p.id = -1;
p.name = null;
p.parent = null;
p.zindexCache = 0;
p.x = 0;
p.y = 0;
p.regX = 0;
p.regY = 0;
p.width = 0;
p.height = 0;
p._width = 0;
p._height = 0;
p.scaleX = 1;
p.scaleY = 1;
p.rotation = 0;
p.alpha = 1;
p._matrix = null;
p.visible = true;
p.mouseEnabled = true;
p.snapToPixel = false;
p.handlers = [];
//Handlers
p.onPress = null;
p.onClick = null;
p.onMousOver = null;
p.onMouseOut = null;
p.onTick = null;
p.init = function () {
this.id = w.UID();
this._matrix = new w.Matrix2D();
}
p.setZIndex = function (zIndex) {
this.zindexCache = zIndex;
this.updateMaxZIndex(zIndex);
}
p.getZIndex = function () {
return this.zindexCache;
}
//zIndex
defineGetterSetter(p, "zIndex", p.getZIndex, p.setZIndex);
p.updateMaxZIndex = function (zindex, object) {
if (this._stage !== undefined) {
if (zindex === undefined) zindex = this.zindexCache;
this._stage.maxzindex = Math.max(this._stage.maxzindex, zindex + 1);
}
}
p.setWidth = function (width) {
this._originalWidth = width;
if (this._stage !== undefined) {
this._width = this._originalWidth / this._stage.pixelScale;
} else {
this._width = this._originalWidth;
}
this._halfWidth = this._width / 2;
}
p.getWidth = function () {
return this._width;
}
defineGetterSetter(p, 'width', p.getWidth, p.setWidth);
p.setHeight = function (height) {
this._originalHeight = height;
if (this._stage !== undefined) {
this._height = this._originalHeight / this._stage.pixelScale;
} else {
this._height = this._originalHeight;
}
this._halfHeight = this._height / 2;
}
p.getHeight = function () {
return this._height;
}
defineGetterSetter(p, 'height', p.getHeight, p.setHeight);
p.setStage = function (stage) {
this._stage = stage;
this.refreshDimensions();
this.refreshHalfDimensions();
this.updateMaxZIndex();
}
p.getStage = function () {
return this._stage;
}
defineGetterSetter(p, 'stage', p.getStage, p.setStage);
p.setImage = function (image, width, height) {
if (this.cacheCanvas === null) this.cacheCanvas = document.createElement("canvas");
this.width = width || image.width;
this.height = height || image.height;
this.cacheCanvas.width = this._originalWidth;
this.cacheCanvas.height = this._originalHeight;
var ctx = this.cacheCanvas.getContext('2d');
ctx.drawImage(image, 0, 0, this._originalWidth, this._originalHeight);
}
p.setRotationCenter = function () {
this.regX = this.cacheCanvas.width / 2;
this.regY = this.cacheCanvas.height / 2;
}
p.refreshHalfDimensions = function () {
this._halfHeight = this._height / 2;
this._halfWidth = this._width / 2;
}
p.refreshDimensions = function () {
this._height = this._originalHeight / this._stage.pixelScale;
this._width = this._originalWidth / this._stage.pixelScale;
}
p.isVisible = function () {
return this.visible && this.alpha > 0 && this.scaleX != 0 && this.scaleY != 0;
}
p.draw = function (ctx, ignoreCache, zIndex) {
if (!this.isVisible()) return;
if (zIndex !== undefined && this.zindexCache !== zIndex && this.childs === undefined) {
return;
}
if (ignoreCache || !this.cacheCanvas) {
return false;
}
ctx.drawImage(this.cacheCanvas, 0, 0, this._originalWidth, this._originalHeight);
return true;
}
p.updateContext = function (ctx, zIndex) {
if (!this.isVisible()) return;
if (zIndex !== undefined && this.zindexCache !== zIndex && this.childs === undefined) {
return;
}
if (this._stage === undefined) {
this.setStage(this.getStage());
}
//TO DO MASK
var mtx;
var x = this.x;
var y = this.y;
if (this._stage !== undefined) {
x = this.x * this._stage.pixelScale;
y = this.y * this._stage.pixelScale;
}
mtx = this._matrix.identity().appendTransform(x, y, this.scaleX, this.scaleY, this.rotation, this.skewX, this.skewY, this.regX, this.regY);
ctx.transform(mtx.a, mtx.b, mtx.c, mtx.d, mtx.tx + 0.5 | 0, mtx.ty + 0.5 | 0);
ctx.globalAlpha *= this.alpha;
}
p.getStage = function () {
if (this.canvas !== undefined) {
return this;
}
var stage = undefined;
var o = this.parent;
while (stage === undefined && o !== undefined) {
stage = o.stage;
o = this.parent;
}
return stage;
}
p.getConcatenatedMatrix = function (matrix) {
if (matrix) {
matrix.identity();
}
else {
matrix = new w.Matrix2D();
}
var o = this;
while (o != null) {
matrix.prependTransform(o.x, o.y, o.scaleX, o.scaleY, o.rotation, o.skewX, o.skewY, o.regX, o.regY).prependProperties(o.alpha, o.shadow, o.compositeOperation);
o = o.parent;
}
return matrix;
}
p.addEventListener = function (type, handler) {
handler = handler.bind(this);