-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFL.js
1620 lines (1436 loc) · 34.3 KB
/
FL.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
(function(win){
var FL = win.FL = {};
try{
win.log = function(){
console.log.apply(console,arguments);
};
}
catch(e){
win.log = function(){
console.dir(arguments);
}
}
win.DEG_TO_RAD = Math.PI/180;
FL.getUrlParams = function()
{
var params = {};
var url = window.location.href;
var idx = url.indexOf("?");
if(idx > 0)
{
var queryStr = url.substring(idx + 1);
var args = queryStr.split("&");
for(var i = 0, a, nv; a = args[i]; i++)
{
nv = args[i] = a.split("=");
params[nv[0]] = nv.length > 1 ? nv[1] : true;
}
}
return params;
};
FL.ns = function(str)
{
var arr = str.split(".");
var obj = win;
for(var i = 0, l = arr.length;i < l;i ++)
{
obj[arr[i]] = obj[arr[i]] || {};
obj = obj[arr[i]];
}
return obj;
};
/**
* modules: "Movieclip, Utils..."
*/
FL.import = function(from, modules){
var str = "";
modules.replace(/\s/g,"").split(",").forEach(function(obj){
str += "var " + obj + "=" + from + "." + obj + ";";
});
return str;
};
FL.params = FL.getUrlParams();
FL.debug = FL.params.debug&&FL.params.debug!=0;
})(window);
(function(){
var Utils = FL.Utils = {};
Utils.extends = function(childClass, parentClass)
{
childClass.prototype = Object.create(parentClass.prototype);
childClass.prototype.superClass = parentClass.prototype;
childClass.prototype.constructor = childClass;
};
Utils.getElementOffset = function(elem)
{
var x = elem.offsetLeft, y = elem.offsetTop;
while((elem = elem.offsetParent) && elem != document.body && elem != document)
{
x += elem.offsetLeft;
y += elem.offsetTop;
}
return {x:x, y:y};
};
Utils.merge = function(obj, props)
{
for(var key in props)
{
obj[key] = props[key];
}
return obj;
};
Utils.getUrlParams = function()
{
var params = {};
var url = window.location.href;
var idx = url.indexOf("?");
if(idx > 0)
{
var queryStr = url.substring(idx + 1);
var args = queryStr.split("&");
for(var i = 0, a, nv; a = args[i]; i++)
{
nv = args[i] = a.split("=");
params[nv[0]] = nv.length > 1 ? nv[1] : true;
}
}
return params;
};
Utils.getRandom = function(min, max, isInt){
if(min > max){
min = max + min;
max = min - max;
min = min - max;
}
var num = Math.random()*(max-min) + min
return isInt?num>>0:num;
};
})();
(function(win){
var Vector = win.Vector = function(x, y)
{
this.x = x||0;
this.y = y||0;
}
Vector.prototype.set = function(x, y)
{
this.x = x;
this.y = y;
}
Vector.prototype.getClone = function()
{
return new Vector(this.x,this.y);
}
Vector.prototype.cut = function(max)
{
var r = Math.min(max, this.getLength());
this.setLength(r);
}
Vector.prototype.cutNew = function(max)
{
var r= Math.min(max, this.getLength());
var v = this.getClone();
v.setLength(r);
return v;
}
Vector.prototype.equals = function(v)
{
return (this.x==v.x && this.y==v.y);
}
Vector.prototype.plus = function(v)
{
this.x += v.x;
this.y += v.y;
return this;
}
Vector.prototype.plusNew = function(v)
{
return new Vector(this.x+v.x,this.y+v.y);
}
Vector.prototype.minus = function(v)
{
this.x -= v.x;
this.y -= v.y;
return this;
}
Vector.prototype.minusNew = function(v)
{
return new Vector(this.x-v.x,this.y-v.y);
}
Vector.prototype.negate = function()
{
this.x = - this.x;
this.y = - this.y;
}
Vector.prototype.negateNew = function()
{
return new Vector(-this.x,-this.y);
}
Vector.prototype.scale = function(s)
{
this.x *= s;
this.y *= s;
}
Vector.prototype.scaleNew = function(s)
{
return new Vector(this.x * s, this.y * s);
}
Vector.prototype.getLength = function()
{
return Math.sqrt(this.x*this.x + this.y*this.y);
}
Vector.prototype.setLength = function(len)
{
var r = this.getLength();
if (r) this.scale (len / r);
else this.x = len;
}
Vector.prototype.getAngle = function()
{
return Math.atan2(this.y, this.x);
}
Vector.prototype.setAngle = function(ang)
{
var r = this.getLength();
this.x = r * Math.cos (ang);
this.y = r * Math.sin (ang);
}
/**
* angle || cos, sin
*/
Vector.prototype.rotate = function()
{
var cos, sin;
var a = arguments;
if(a.length == 1)
{
cos = Math.cos(a[0]);
sin = Math.sin(a[0]);
}
else
{
cos = a[0]
sin = a[1]
}
var rx = this.x * cos - this.y * sin;
var ry = this.x * sin + this.y * cos;
this.x = rx;
this.y = ry;
}
Vector.prototype.rotateNew = function(ang)
{
var v=new Vector(this.x,this.y);
v.rotate(ang);
return v;
}
Vector.prototype.cross = function(v)
{
return this.x*v.y - v.x*this.y;
};
Vector.prototype.dot = function(v)
{
return this.x * v.x + this.y * v.y;
}
Vector.prototype.getNormal = function()
{
return new Vector(-this.y,this.x);
}
Vector.prototype.isPerpTo = function(v)
{
return (this.dot (v) == 0);
}
Vector.prototype.angleBetween = function(v)
{
var dp = this.dot (v);
var cosAngle = dp / (this.getLength() * v.getLength());
return Math.acos (cosAngle);
}
Vector.prototype.getLength2 = function()
{
return this.x*this.x + this.y*this.y;
}
})(window);
(function(){
var Rect = FL.Rect = function(x, y, width, height){
this.set(x, y, width, height);
};
Rect.prototype.set = function(x, y, width, height)
{
this.left = this.x = x||0;
this.top = this.y = y||0;
this.width = width||this.width||0;
this.height = height||this.height||0;
this.right = this.x + this.width;
this.bottom = this.y + this.height;
};
Rect.prototype.intersects = function(rect){
return rect.x <= this.right && this.x <= rect.right&&
rect.y <= this.bottom && this.y <= rect.bottom;
};
Rect.prototype.hitTestPoint = function(x, y){
return x>=this.x && x<=this.right&&
y>=this.y && y<=this.bottom;
}
})();
(function(){
var Rect = FL.Rect;
var min = Math.min;
var max = Math.max;
var abs = Math.abs;
var Line = FL.Line = function(v0, v1)
{
this.p0 = v0||new Vector();
this.p1 = v1||new Vector();
};
/**
*获取t百分比的点
*/
Line.prototype.getPoint = function(t){
var x = (this.p1.x - this.p0.x) * t + this.p0.x;
var y = (this.p1.y - this.p0.y) * t + this.p0.y;
return new Vector(x, y);
};
Line.prototype.getAngle = function(){
var x = this.p1.x - this.p0.x;
var y = this.p1.y - this.p0.y;
return Math.atan2(y, x);
};
Line.prototype.getY = function(x){
this.lx = this.lx||min(this.p0.x, this.p1.x);
this.rx = this.rx||max(this.p0.x, this.p1.x);
if(x < this.lx || x > this.rx) return null;
this._getY = this._getY || function(x){
var x1 = this.p0.x;
var y1 = this.p0.y;
var x2 = this.p1.x;
var y2 = this.p1.y;
if(x1 == x2) return Math.min(y1, y2);
if(y1 == y2) return y1;
return (x-x1)/(x2-x1)*(y2-y1) + y1
}
return this._getY(x);
};
Line.prototype.createPoints = function()
{
var points = [];
this.lx = this.lx||min(this.p0.x, this.p1.x);
this.rx = this.rx||max(this.p0.x, this.p1.x);
var ang = this.getAngle();
for(var i = this.lx>>0;i <= this.rx; i ++)
{
if(this.getY(i) != null) points.push({x:i, y:this.getY(i), ang:ang});
}
return points;
}
Line.prototype.hitTestPoint = function(x, y)
{
var p = new Vector(x, y);
p.minus(this.p0);
return p.cross(this.p1.minusNew(this.p0))==0&&
getBounds(this).hitTestPoint(p.x, p.y);
};
Line.prototype.intersects = function(line)
{
if(getBounds(line).intersects(getBounds(this)))
{
var p0 = this.p0;
var p1 = this.p1;
var q0 = line.p0;
var q1 = line.p1;
var p10 = p1.minusNew(p0);
var q10 = q1.minusNew(q0);
return q0.minusNew(p0).cross(p10)*p10.cross(q1.minusNew(p0))>=0&&
p0.minusNew(q0).cross(q10)*q10.cross(p1.minusNew(q0))>= 0
}
return false;
};
function getBounds(line)
{
var x = min(line.p0.x, line.p1.x);
var y = min(line.p0.y, line.p1.y);
var w = line.p0.x - line.p1.x;
var h = line.p0.y - line.p1.y;
return new Rect(x, y, abs(w), abs(h));
}
})();
(function(){
var Line = FL.Line;
var Polygon = FL.Polygon = function(points)
{
this.points = points;
}
Polygon.prototype.hitTestPoint = function(x, y)
{
var line = new Line(new Vector(x, y), new Vector(-1000, y));
var n = 0;
var points = this.points;
for(var i = 0, l = points.length;i < l;i ++)
{
var path = new Line(points[i], points[(i+1)%l]);
if(path.hitTestPoint(x, y)) return true;
if(path.p0.x != path.p1.x)
{
if(line.hitTestPoint(path.p0)) n ++;
else if(!line.hitTestPoint(path.p1) && line.intersects(path)) n++;
}
}
return n%2 == 1;
}
})();
(function(){
var TOTAL_SIMPSON_STEP = 100;
var Point = FL.Point = function(x, y){
this.x = x;
this.y = y;
};
var Bezier = FL.Bezier = function(){
var args = arguments;
if(args.length == 4){
this.p0 = args[0];
this.p1 = args[1];
this.p2 = args[2];
this.p3 = args[3];
}
else if(args.length == 8){
this.p0 = new Point(args[0], args[1]);
this.p1 = new Point(args[2], args[3]);
this.p2 = new Point(args[4], args[5]);
this.p3 = new Point(args[6], args[7]);
}
};
Bezier.prototype.getSpeedLength = function(t){
var it = 1-t, it2 = it*it, t2 = t*t;
var x = -3*this.p0.x*it2 + 3*this.p1.x*it2 - 6*this.p1.x*it*t + 6*this.p2.x*it*t - 3*this.p2.x*t2 + 3*this.p3.x*t2;
var y = -3*this.p0.y*it2 + 3*this.p1.y*it2 - 6*this.p1.y*it*t + 6*this.p2.y*it*t - 3*this.p2.y*t2 + 3*this.p3.y*t2;
return Math.sqrt(x*x + y*y);
};
/*
*获取线条长度
*/
Bezier.prototype.getLength = function(t){
var stepCounts = parseInt(TOTAL_SIMPSON_STEP*t), i;
if(stepCounts & 1) stepCounts++;
if(stepCounts==0) return 0.0;
var halfCounts = stepCounts>>1;
var sum1=0, sum2=0;
var dStep = t/stepCounts;
for(i=0; i<halfCounts; i++){
sum1 += this.getSpeedLength((2*i+1)*dStep);
}
for(i = 1; i<halfCounts; i++){
sum2 += this.getSpeedLength((2*i)*dStep);
}
return (this.getSpeedLength(0)+this.getSpeedLength(1)+2*sum2+4*sum1)*dStep/3;
};
Bezier.prototype.getPointByTime = function(t){
var it = 1-t, it2 = it*it, it3 = it2*it;
var t2 = t*t, t3 = t2*t;
return new Vector(it3*this.p0.x + 3*it2*t*this.p1.x + 3*it*t2*this.p2.x + t3*this.p3.x,
it3*this.p0.y + 3*it2*t*this.p1.y + 3*it*t2*this.p2.y + t3*this.p3.y
);
};
Bezier.prototype.getPointByLen = function(len){
if(len > this.length){
return this.p3;
}
var t1 = len/this.length, t2;
do
{
t2 = t1 - (this.getLength(t1)-len)/this.getSpeedLength(t1);
if(Math.abs(t1-t2)<0.01) break;
t1=t2;
}while(true);
return this.getPointByTime(t2);
};
/**
*获取等距点,距离为step,默认1
*/
Bezier.prototype.getPointsByLen = function(step){
step = step||1;
var len = this.getLength();
var i = 0;
var points = [];
while(i < len)
{
points.push(this.getPointByLen(i));
i+=step;
}
points.push(this.getPointByLen(len))
return points;
};
/**
*获取等比例点,比例为step,默认.01
*/
Bezier.prototype.getPointsByTime = function(step){
step = step||.01;
var t = 0;
var points = [];
while(t < 1)
{
points.push(this.getPointByTime(t));
t += step;
}
points.push(this.getPointByTime(1));
return points;
};
})();
(function(){
var EventDispatcher = FL.EventDispatcher = function()
{
this.eventListeners = {};
};
EventDispatcher.prototype.addEventListener = function(type, listener)
{
if(!this.eventListeners[type]) this.eventListeners[type] = [];
if(this.eventListeners[type].indexOf(listener)==-1) this.eventListeners[type].push(listener);
};
EventDispatcher.prototype.removeEventListener = function(type, listener)
{
if(!this.eventListeners[type]) return;
var index = this.eventListeners[type].indexOf(listener);
if(index != -1) this.eventListeners[type].splice(index, 1);
};
EventDispatcher.prototype.removeAllEventListener = function()
{
this.eventListeners = {};
}
/**
* e:{type:String, data:{}}
*/
EventDispatcher.prototype.dispatchEvent = function(e)
{
var arr = this.eventListeners[e.type];
e.target = this;
if(arr && arr.length > 0)
{
arr = arr.slice(0);
for(var i = 0,l = arr.length;i < l;i ++)
{
arr[i].call(this, e);
}
}
};
})();
;(function(){
var Utils = FL.Utils;
var EventDispatcher = FL.EventDispatcher;
var ImageLoader = FL.ImageLoader = function()
{
EventDispatcher.call(this);
this.init();
}
Utils.extends(ImageLoader, EventDispatcher);
ImageLoader.prototype.init = function()
{
this.images = {};
this.totalSize = 0;
this.loadSize = 0;
};
ImageLoader.prototype.load = function(arr)
{
var that = this;
this.init();
var images = this.images;
for(var i = 0, l = arr.length;i < l;i++)
{
var image = new Image();
image.onload = function(e)
{
that._onCompleteHander(e);
};
this.totalSize += arr[i].size;
images[arr[i].id] = image;
image.size = arr[i].size;
image.src = arr[i].src;
image.id = arr[i].id;
// Utils.merge(image, arr[i]);
}
};
ImageLoader.prototype._onCompleteHander = function(e)
{
this.loadSize += e.target.size;
if(this.loadSize - this.totalSize > -.1)
{
this.dispatchEvent({type:"complete"});
}
else
{
this.dispatchEvent({type:"progress"});
}
log("load:" + e.target.id + ", "+Math.floor(this.loadSize)+"/"+Math.floor(this.totalSize));
}
})();
(function(){
var Utils = FL.Utils;
var Mouse = FL.Mouse = {
stage:null,
x:0,
y:0,
init:function(stage){
this.stage = stage;
this.element = document;
this.x = 0;
this.y = 0;
this.isDown = false;
var offset = Utils.getElementOffset(this.element);
this.offsetX = offset.x;
this.offsetY =offset.y;
this.addEvent();
},
addEvent:function(){
var elem = this.element;
var names = ["mousedown","mousemove","mouseup"];
var events = "ontouchstart" in window?["touchstart", "touchmove", "touchend"]:names;
var that = this;
names.forEach(function(name, i){
elem.addEventListener(name, function(e)
{
e.preventDefault();
if(i == 0) that.isDown = true;
else if(i == 2) that.isDown = false;
var x = e.changedTouches?e.changedTouches[0].pageX:e.pageX;
var y = e.changedTouches?e.changedTouches[0].pageY:e.pageY;
that.x = x - that.offsetX;
that.y = y - that.offsetY;
that.stage.dispatchEvent({type:name,x:that.x,y:that.y});
});
});
}
};
})();
(function(){
var Utils = FL.Utils;
var Keyboard = FL.Keyboard = {
init:function(){
this.elem = document;
this.key = {};
this.time = {};
this.delay = {};
this.addEvent();
},
addEvent:function(){
var elem = this.elem;
var key = this.key;
var time = this.time;
var delay = this.delay;
var that = this;
elem.addEventListener("keydown", function(e){
e.preventDefault();
if(key[e.keyCode] !== true)
{
key[e.keyCode] = true;
delay[e.keyCode] = new Date() - time[e.keyCode];
time[e.keyCode] = new Date();
that.keyDown();
}
});
elem.addEventListener("keyup", function(e){
e.preventDefault();
key[e.keyCode] = false;
that.keyUp();
});
},
getIsDown:function(key){
return this.key[KEY[key]];
},
getKeyDelay:function(key){
return this.delay[KEY[key]];
},
keyDown:function(){},
keyUp:function(){}
};
var KEY = {
BACKSPACE : 8,
TAB : 9,
NUM_CENTER : 12,
ENTER : 13,
RETURN : 13,
SHIFT : 16,
CTRL : 17,
ALT : 18,
PAUSE : 19,
CAPS_LOCK : 20,
ESC : 27,
ESCAPE : 27,
SPACE : 32,
PAGE_UP : 33,
PAGE_DOWN : 34,
END : 35,
HOME : 36,
LEFT : 37,
UP : 38,
RIGHT : 39,
DOWN : 40,
PRINT_SCREEN : 44,
INSERT : 45,
DELETE : 46,
ZERO : 48,
ONE : 49,
TWO : 50,
THREE : 51,
FOUR : 52,
FIVE : 53,
SIX : 54,
SEVEN : 55,
EIGHT : 56,
NINE : 57,
A : 65,
B : 66,
C : 67,
D : 68,
E : 69,
F : 70,
G : 71,
H : 72,
I : 73,
J : 74,
K : 75,
L : 76,
M : 77,
N : 78,
O : 79,
P : 80,
Q : 81,
R : 82,
S : 83,
T : 84,
U : 85,
V : 86,
W : 87,
X : 88,
Y : 89,
Z : 90,
CONTEXT_MENU : 93,
NUM_ZERO : 96,
NUM_ONE : 97,
NUM_TWO : 98,
NUM_THREE : 99,
NUM_FOUR : 100,
NUM_FIVE : 101,
NUM_SIX : 102,
NUM_SEVEN : 103,
NUM_EIGHT : 104,
NUM_NINE : 105,
NUM_MULTIPLY : 106,
NUM_PLUS : 107,
NUM_MINUS : 109,
NUM_PERIOD : 110,
NUM_DIVISION : 111,
F1 : 112,
F2 : 113,
F3 : 114,
F4 : 115,
F5 : 116,
F6 : 117,
F7 : 118,
F8 : 119,
F9 : 120,
F10 : 121,
F11 : 122,
F12 : 123
};
})();
(function(){
var Utils = FL.Utils;
var EventDispatcher = FL.EventDispatcher;
var Rect = FL.Rect;
var Polygon = FL.Polygon;
var DisplayObject = FL.DisplayObject = function(x, y)
{
this.x = x||0;
this.y = y||0;
this.originX = 0;
this.originY = 0;
this.scaleX = 1;
this.scaleY = 1;
this.width = 0;
this.height = 0;
this.angle = 0;
this.alpha = 1;
this.timeStep = 0;
this.mouseEnable = false;
this.visible = true;
this.rect = new Rect();
EventDispatcher.call(this);
// var height = 0;
// var that = this;
// Object.defineProperty(this, "height", {
// get:function(){
// return that.scaleY*height;
// },
// set:function(value){
// height = value
// }
// })
};
Utils.extends(DisplayObject, EventDispatcher);
DisplayObject.prototype.render = function(ctx)
{
this.isInStage = this._inStage();
if(!this.visible||!ctx || !this.isInStage) return;
ctx.save();
ctx.globalAlpha = this.alpha;
ctx.translate(this.x, this.y);
ctx.rotate(this.angle);
ctx.scale(this.scaleX, this.scaleY);
this._draw(ctx);
ctx.restore();
if(FL.debug)
{
this._debugDraw(ctx);
}
};
DisplayObject.prototype._debugDraw = function(ctx)
{
ctx.save();
var rect = this.getBounds();
ctx.strokeStyle = "#f00";
ctx.lineWidth = .5;
ctx.beginPath();
ctx.moveTo(this.hitPoints[0].x, this.hitPoints[0].y)
for(var i = 1, l = this.hitPoints.length;i < l;i ++)
{
ctx.lineTo(this.hitPoints[i].x, this.hitPoints[i].y);
}
ctx.closePath();
ctx.stroke();
ctx.strokeStyle = "#00f";
ctx.beginPath();
ctx.strokeRect(rect.x, rect.y, rect.width, rect.height);
ctx.stroke();
ctx.restore();
}
DisplayObject.prototype.setCenter = function()
{
this.originX = this.width >>1;
this.originY = this.height >>1;
}
DisplayObject.prototype._draw = function(ctx)
{
};
DisplayObject.prototype.initStage = function(stage, timeStep)
{
this.stage = stage;
this.timeStep = timeStep;
}
DisplayObject.prototype._inStage =function()
{
return this.stage && this.x + this.width*2 > 0 && this.y + this.height*2 > 0 && this.x - 2*this.width< this.stage.width && this.y - 2*this.height< this.stage.height;
}
DisplayObject.prototype.getBounds = function()
{
var scaleX = Math.abs(this.scaleX);
var scaleY = Math.abs(this.scaleY);
var cos = Math.cos(this.angle);
var sin = Math.sin(this.angle);
var originX = this.originX*scaleX;
var originY = this.originY*scaleY;
var minx, miny, maxx, maxy;
var points = [[0, 0], [0, this.height*scaleY], [this.width*scaleX, this.height*scaleY], [this.width*scaleX, 0]];
for(var i = 3;i >= 0;i --)
{
var tx = points[i][0] - originX;
var ty = points[i][1] - originY;
var ex = tx * cos - ty * sin + this.x;
var ey = tx * sin + ty * cos + this.y;
points[i] = new Vector(ex, ey);
if(i == 3)
{
minx = maxx = ex;
miny = maxy = ey;
}
else
{
minx > ex && (minx = ex);
miny > ey && (miny = ey);
maxx < ex && (maxx = ex);
maxy < ey && (maxy = ey);
}
}
this.hitPoints = points;
return new Rect(minx, miny, maxx - minx, maxy - miny);
};
/**
* copy from https://github.com/quark-dev-team/quarkjs/blob/master/src/base/display/DisplayObject.js
* 获得一个对象相对于其某个祖先(默认即舞台)的连接矩阵。
* @private
*/
DisplayObject.prototype.getConcatenatedMatrix = function(ancestor)
{
var mtx = new FL.Matrix(1, 0, 0, 1, 0, 0);
if(ancestor == this) return mtx;
for(var o = this; o.parent != null && o.parent != ancestor; o = o.parent)
{
var cos = 1, sin = 0;
if(o.rotation%360 != 0)
{
var r = o.rotation * DEG_TO_RAD;
cos = Math.cos(r);
sin = Math.sin(r);
}
if(o.regX != 0) mtx.tx -= o.regX;
if(o.regY != 0) mtx.ty -= o.regY;
mtx.concat(new FL.Matrix(cos*o.scaleX, sin*o.scaleX, -sin*o.scaleY, cos*o.scaleY, o.x, o.y));
}
return mtx;
};
DisplayObject.prototype.hitTestPoint = function(x, y)
{
if(this.angle == 0)
{
var scaleX = Math.abs(this.scaleX);
var scaleY = Math.abs(this.scaleY);
return new Rect(this.x - this.originX*scaleX, this.y - this.originY*scaleY, this.width*scaleX, this.height*scaleY).hitTestPoint(x, y);
}
if(this.getBounds().hitTestPoint(x, y))
{
return new Polygon(this.hitPoints).hitTestPoint(x, y);
}
return false;
};
DisplayObject.prototype.hitTestObject = function(obj)
{