-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathMovieClip.js
706 lines (626 loc) · 21.3 KB
/
MovieClip.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
/*
* MovieClip
* Visit http://createjs.com/ for window.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 window.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.
*/
/**
* @module EaselJS
*/
// namespace:
this.createjs = this.createjs||{};
(function() {
"use strict";
// constructor:
/**
* The MovieClip class associates a TweenJS Timeline with an EaselJS {{#crossLink "Container"}}{{/crossLink}}. It allows
* you to create objects which encapsulate timeline animations, state changes, and synched actions. Due to the
* complexities inherent in correctly setting up a MovieClip, it is largely intended for tool output and is not included
* in the main EaselJS library.
*
* Currently MovieClip only works properly if it is tick based (as opposed to time based) though some concessions have
* been made to support time-based timelines in the future.
*
* <h4>Example</h4>
* This example animates two shapes back and forth. The grey shape starts on the left, but we jump to a mid-point in
* the animation using {{#crossLink "MovieClip/gotoAndPlay"}}{{/crossLink}}.
*
* var stage = new createjs.Stage("canvas");
* createjs.Ticker.addEventListener("tick", stage);
*
* var mc = new createjs.MovieClip(null, 0, true, {start:20});
* stage.addChild(mc);
*
* var child1 = new createjs.Shape(
* new createjs.Graphics().beginFill("#999999")
* .drawCircle(30,30,30));
* var child2 = new createjs.Shape(
* new createjs.Graphics().beginFill("#5a9cfb")
* .drawCircle(30,30,30));
*
* mc.timeline.addTween(
* createjs.Tween.get(child1)
* .to({x:0}).to({x:60}, 50).to({x:0}, 50));
* mc.timeline.addTween(
* createjs.Tween.get(child2)
* .to({x:60}).to({x:0}, 50).to({x:60}, 50));
*
* mc.gotoAndPlay("start");
*
* It is recommended to use <code>tween.to()</code> to animate and set properties (use no duration to have it set
* immediately), and the <code>tween.wait()</code> method to create delays between animations. Note that using the
* <code>tween.set()</code> method to affect properties will likely not provide the desired result.
*
* @class MovieClip
* @main MovieClip
* @extends Container
* @constructor
* @param {String} [mode=independent] Initial value for the mode property. One of {{#crossLink "MovieClip/INDEPENDENT:property"}}{{/crossLink}},
* {{#crossLink "MovieClip/SINGLE_FRAME:property"}}{{/crossLink}}, or {{#crossLink "MovieClip/SYNCHED:property"}}{{/crossLink}}.
* The default is {{#crossLink "MovieClip/INDEPENDENT:property"}}{{/crossLink}}.
* @param {Number} [startPosition=0] Initial value for the {{#crossLink "MovieClip/startPosition:property"}}{{/crossLink}}
* property.
* @param {Boolean} [loop=true] Initial value for the {{#crossLink "MovieClip/loop:property"}}{{/crossLink}}
* property. The default is `true`.
* @param {Object} [labels=null] A hash of labels to pass to the {{#crossLink "MovieClip/timeline:property"}}{{/crossLink}}
* instance associated with this MovieClip. Labels only need to be passed if they need to be used.
**/
function MovieClip(mode, startPosition, loop, labels) {
this.Container_constructor();
!MovieClip.inited&&MovieClip.init(); // static init
// public properties:
/**
* Controls how this MovieClip advances its time. Must be one of 0 (INDEPENDENT), 1 (SINGLE_FRAME), or 2 (SYNCHED).
* See each constant for a description of the behaviour.
* @property mode
* @type String
* @default null
**/
this.mode = mode||MovieClip.INDEPENDENT;
/**
* Specifies what the first frame to play in this movieclip, or the only frame to display if mode is SINGLE_FRAME.
* @property startPosition
* @type Number
* @default 0
*/
this.startPosition = startPosition || 0;
/**
* Indicates whether this MovieClip should loop when it reaches the end of its timeline.
* @property loop
* @type Boolean
* @default true
*/
this.loop = loop;
/**
* The current frame of the movieclip.
* @property currentFrame
* @type Number
* @default 0
* @readonly
*/
this.currentFrame = 0;
/**
* The TweenJS Timeline that is associated with this MovieClip. This is created automatically when the MovieClip
* instance is initialized. Animations are created by adding <a href="http://tweenjs.com">TweenJS</a> Tween
* instances to the timeline.
*
* <h4>Example</h4>
*
* var tween = createjs.Tween.get(target).to({x:0}).to({x:100}, 30);
* var mc = new createjs.MovieClip();
* mc.timeline.addTween(tween);
*
* Elements can be added and removed from the timeline by toggling an "_off" property
* using the <code>tweenInstance.to()</code> method. Note that using <code>Tween.set</code> is not recommended to
* create MovieClip animations. The following example will toggle the target off on frame 0, and then back on for
* frame 1. You can use the "visible" property to achieve the same effect.
*
* var tween = createjs.Tween.get(target).to({_off:false})
* .wait(1).to({_off:true})
* .wait(1).to({_off:false});
*
* @property timeline
* @type Timeline
* @default null
*/
this.timeline = new createjs.Timeline(null, labels, {paused:true, position:startPosition, useTicks:true});
/**
* If true, the MovieClip's position will not advance when ticked.
* @property paused
* @type Boolean
* @default false
*/
this.paused = false;
/**
* If true, actions in this MovieClip's tweens will be run when the playhead advances.
* @property actionsEnabled
* @type Boolean
* @default true
*/
this.actionsEnabled = true;
/**
* If true, the MovieClip will automatically be reset to its first frame whenever the timeline adds
* it back onto the display list. This only applies to MovieClip instances with mode=INDEPENDENT.
* <br><br>
* For example, if you had a character animation with a "body" child MovieClip instance
* with different costumes on each frame, you could set body.autoReset = false, so that
* you can manually change the frame it is on, without worrying that it will be reset
* automatically.
* @property autoReset
* @type Boolean
* @default true
*/
this.autoReset = true;
/**
* An array of bounds for each frame in the MovieClip. This is mainly intended for tool output.
* @property frameBounds
* @type Array
* @default null
*/
this.frameBounds = this.frameBounds||null; // TODO: Deprecated. This is for backwards support of Flash/Animate
/**
* By default MovieClip instances advance one frame per tick. Specifying a framerate for the MovieClip
* will cause it to advance based on elapsed time between ticks as appropriate to maintain the target
* framerate.
*
* For example, if a MovieClip with a framerate of 10 is placed on a Stage being updated at 40fps, then the MovieClip will
* advance roughly one frame every 4 ticks. This will not be exact, because the time between each tick will
* vary slightly between frames.
*
* This feature is dependent on the tick event object (or an object with an appropriate "delta" property) being
* passed into {{#crossLink "Stage/update"}}{{/crossLink}}.
* @property framerate
* @type {Number}
* @default null
**/
this.framerate = null;
// private properties:
/**
* @property _synchOffset
* @type Number
* @default 0
* @private
*/
this._synchOffset = 0;
/**
* @property _prevPos
* @type Number
* @default -1
* @private
*/
this._prevPos = -1; // TODO: evaluate using a ._reset Boolean prop instead of -1.
/**
* @property _prevPosition
* @type Number
* @default 0
* @private
*/
this._prevPosition = 0;
/**
* The time remaining from the previous tick, only applicable when .framerate is set.
* @property _t
* @type Number
* @private
*/
this._t = 0;
/**
* List of display objects that are actively being managed by the MovieClip.
* @property _managed
* @type Object
* @private
*/
this._managed = {};
}
var p = createjs.extend(MovieClip, createjs.Container);
// constants:
/**
* The MovieClip will advance independently of its parent, even if its parent is paused.
* This is the default mode.
* @property INDEPENDENT
* @static
* @type String
* @default "independent"
* @readonly
**/
MovieClip.INDEPENDENT = "independent";
/**
* The MovieClip will only display a single frame (as determined by the startPosition property).
* @property SINGLE_FRAME
* @static
* @type String
* @default "single"
* @readonly
**/
MovieClip.SINGLE_FRAME = "single";
/**
* The MovieClip will be advanced only when its parent advances and will be synched to the position of
* the parent MovieClip.
* @property SYNCHED
* @static
* @type String
* @default "synched"
* @readonly
**/
MovieClip.SYNCHED = "synched";
// static properties:
MovieClip.inited = false;
// static methods:
MovieClip.init = function() {
if (MovieClip.inited) { return; }
// plugins introduce some overhead to Tween, so we only install this if an MC is instantiated.
MovieClipPlugin.install();
MovieClip.inited = true;
};
// getter / setters:
/**
* Use the {{#crossLink "MovieClip/labels:property"}}{{/crossLink}} property instead.
* @method getLabels
* @return {Array}
* @deprecated
**/
p.getLabels = function() {
return this.timeline.getLabels();
};
/**
* Use the {{#crossLink "MovieClip/currentLabel:property"}}{{/crossLink}} property instead.
* @method getCurrentLabel
* @return {String}
* @deprecated
**/
p.getCurrentLabel = function() {
this._updateTimeline();
return this.timeline.getCurrentLabel();
};
/**
* Use the {{#crossLink "MovieClip/duration:property"}}{{/crossLink}} property instead.
* @method getDuration
* @return {Number}
* @protected
**/
p.getDuration = function() {
return this.timeline.duration;
};
/**
* Returns an array of objects with label and position (aka frame) properties, sorted by position.
* Shortcut to TweenJS: Timeline.getLabels();
* @property labels
* @type {Array}
* @readonly
**/
/**
* Returns the name of the label on or immediately before the current frame. See TweenJS: Timeline.getCurrentLabel()
* for more information.
* @property currentLabel
* @type {String}
* @readonly
**/
/**
* Returns the duration of this MovieClip in seconds or ticks. Identical to {{#crossLink "MovieClip/duration:property"}}{{/crossLink}}
* and provided for Adobe Flash/Animate API compatibility.
* @property totalFrames
* @type {Number}
* @readonly
**/
/**
* Returns the duration of this MovieClip in seconds or ticks.
* @property duration
* @type {Number}
* @readonly
**/
try {
Object.defineProperties(p, {
labels: { get: p.getLabels },
currentLabel: { get: p.getCurrentLabel },
totalFrames: { get: p.getDuration },
duration: { get: p.getDuration }
});
} catch (e) {}
// public methods:
/**
* Constructor alias for backwards compatibility. This method will be removed in future versions.
* Subclasses should be updated to use {{#crossLink "Utility Methods/extends"}}{{/crossLink}}.
* @method initialize
* @deprecated in favour of `createjs.promote()`
**/
p.initialize = MovieClip; // TODO: Deprecated. This is for backwards support of Adobe Flash/Animate
/**
* Returns true or false indicating whether the display object would be visible if drawn to a canvas.
* This does not account for whether it would be visible within the boundaries of the stage.
* NOTE: This method is mainly for internal use, though it may be useful for advanced uses.
* @method isVisible
* @return {Boolean} Boolean indicating whether the display object would be visible if drawn to a canvas
**/
p.isVisible = function() {
// children are placed in draw, so we can't determine if we have content.
return !!(this.visible && this.alpha > 0 && this.scaleX != 0 && this.scaleY != 0);
};
/**
* Draws the display object into the specified context ignoring its visible, alpha, shadow, and transform.
* Returns true if the draw was handled (useful for overriding functionality).
* NOTE: This method is mainly for internal use, though it may be useful for advanced uses.
* @method draw
* @param {CanvasRenderingContext2D} ctx The canvas 2D context object to draw into.
* @param {Boolean} ignoreCache Indicates whether the draw operation should ignore any current cache.
* For example, used for drawing the cache (to prevent it from simply drawing an existing cache back
* into itself).
**/
p.draw = function(ctx, ignoreCache) {
// draw to cache first:
if (this.DisplayObject_draw(ctx, ignoreCache)) { return true; }
this._updateTimeline();
this.Container_draw(ctx, ignoreCache);
return true;
};
/**
* Sets paused to false.
* @method play
**/
p.play = function() {
this.paused = false;
};
/**
* Sets paused to true.
* @method stop
**/
p.stop = function() {
this.paused = true;
};
/**
* Advances this movie clip to the specified position or label and sets paused to false.
* @method gotoAndPlay
* @param {String|Number} positionOrLabel The animation name or frame number to go to.
**/
p.gotoAndPlay = function(positionOrLabel) {
this.paused = false;
this._goto(positionOrLabel);
};
/**
* Advances this movie clip to the specified position or label and sets paused to true.
* @method gotoAndStop
* @param {String|Number} positionOrLabel The animation or frame name to go to.
**/
p.gotoAndStop = function(positionOrLabel) {
this.paused = true;
this._goto(positionOrLabel);
};
/**
* Advances the playhead. This occurs automatically each tick by default.
* @param [time] {Number} The amount of time in ms to advance by. Only applicable if framerate is set.
* @method advance
*/
p.advance = function(time) {
// TODO: should we worry at all about clips who change their own modes via frame scripts?
var independent = MovieClip.INDEPENDENT;
if (this.mode != independent) { return; }
var o=this, fps = o.framerate;
while ((o = o.parent) && fps == null) {
if (o.mode == independent) { fps = o._framerate; }
}
this._framerate = fps;
var t = (fps != null && fps != -1 && time != null) ? time/(1000/fps) + this._t : 1;
var frames = t|0;
this._t = t-frames; // leftover time
while (!this.paused && frames--) {
this._prevPosition = (this._prevPos < 0) ? 0 : this._prevPosition+1;
this._updateTimeline();
}
};
/**
* MovieClip instances cannot be cloned.
* @method clone
**/
p.clone = function() {
// TODO: add support for this? Need to clone the Timeline & retarget tweens - pretty complex.
throw("MovieClip cannot be cloned.")
};
/**
* Returns a string representation of this object.
* @method toString
* @return {String} a string representation of the instance.
**/
p.toString = function() {
return "[MovieClip (name="+ this.name +")]";
};
// private methods:
/**
* @method _tick
* @param {Object} evtObj An event object that will be dispatched to all tick listeners. This object is reused between dispatchers to reduce construction & GC costs.
* function.
* @protected
**/
p._tick = function(evtObj) {
this.advance(evtObj&&evtObj.delta);
this.Container__tick(evtObj);
};
/**
* @method _goto
* @param {String|Number} positionOrLabel The animation name or frame number to go to.
* @protected
**/
p._goto = function(positionOrLabel) {
var pos = this.timeline.resolve(positionOrLabel);
if (pos == null) { return; }
// prevent _updateTimeline from overwriting the new position because of a reset:
if (this._prevPos == -1) { this._prevPos = NaN; }
this._prevPosition = pos;
this._t = 0;
this._updateTimeline();
};
/**
* @method _reset
* @private
**/
p._reset = function() {
this._prevPos = -1;
this._t = this.currentFrame = 0;
this.paused = false;
};
/**
* @method _updateTimeline
* @protected
**/
p._updateTimeline = function() {
var tl = this.timeline;
var synched = this.mode != MovieClip.INDEPENDENT;
tl.loop = (this.loop==null) ? true : this.loop;
var pos = synched ? this.startPosition + (this.mode==MovieClip.SINGLE_FRAME?0:this._synchOffset) : (this._prevPos < 0 ? 0 : this._prevPosition);
var mode = synched || !this.actionsEnabled ? createjs.Tween.NONE : null;
// pre-assign currentFrame so it is available to frame scripts:
this.currentFrame = tl._calcPosition(pos);
// update timeline position, ignoring actions if this is a graphic.
tl.setPosition(pos, mode);
this._prevPosition = tl._prevPosition;
if (this._prevPos == tl._prevPos) { return; }
this.currentFrame = this._prevPos = tl._prevPos;
for (var n in this._managed) { this._managed[n] = 1; }
var tweens = tl._tweens;
for (var i=0, l=tweens.length; i<l; i++) {
var tween = tweens[i];
var target = tween._target;
if (target == this || tween.passive) { continue; } // TODO: this assumes actions tween has this as the target. Valid?
var offset = tween._stepPosition;
if (target instanceof createjs.DisplayObject) {
// motion tween.
this._addManagedChild(target, offset);
} else {
// state tween.
this._setState(target.state, offset);
}
}
var kids = this.children;
for (i=kids.length-1; i>=0; i--) {
var id = kids[i].id;
if (this._managed[id] == 1) {
this.removeChildAt(i);
delete(this._managed[id]);
}
}
};
/**
* @method _setState
* @param {Array} state
* @param {Number} offset
* @protected
**/
p._setState = function(state, offset) {
if (!state) { return; }
for (var i=state.length-1;i>=0;i--) {
var o = state[i];
var target = o.t;
var props = o.p;
for (var n in props) { target[n] = props[n]; }
this._addManagedChild(target, offset);
}
};
/**
* Adds a child to the timeline, and sets it up as a managed child.
* @method _addManagedChild
* @param {MovieClip} child The child MovieClip to manage
* @param {Number} offset
* @private
**/
p._addManagedChild = function(child, offset) {
if (child._off) { return; }
this.addChildAt(child,0);
if (child instanceof MovieClip) {
child._synchOffset = offset;
// TODO: this does not precisely match Adobe Flash/Animate, which loses track of the clip if it is renamed or removed from the timeline, which causes it to reset.
if (child.mode == MovieClip.INDEPENDENT && child.autoReset && !this._managed[child.id]) { child._reset(); }
}
this._managed[child.id] = 2;
};
/**
* @method _getBounds
* @param {Matrix2D} matrix
* @param {Boolean} ignoreTransform
* @return {Rectangle}
* @protected
**/
p._getBounds = function(matrix, ignoreTransform) {
var bounds = this.DisplayObject_getBounds();
if (!bounds) {
this._updateTimeline();
if (this.frameBounds) { bounds = this._rectangle.copy(this.frameBounds[this.currentFrame]); }
}
if (bounds) { return this._transformBounds(bounds, matrix, ignoreTransform); }
return this.Container__getBounds(matrix, ignoreTransform);
};
createjs.MovieClip = createjs.promote(MovieClip, "Container");
// MovieClipPlugin for TweenJS:
/**
* This plugin works with <a href="http://tweenjs.com" target="_blank">TweenJS</a> to prevent the startPosition
* property from tweening.
* @private
* @class MovieClipPlugin
* @constructor
**/
function MovieClipPlugin() {
throw("MovieClipPlugin cannot be instantiated.")
}
/**
* @method priority
* @private
**/
MovieClipPlugin.priority = 100; // very high priority, should run first
/**
* @method install
* @private
**/
MovieClipPlugin.install = function() {
createjs.Tween.installPlugin(MovieClipPlugin, ["startPosition"]);
};
/**
* @method init
* @param {Tween} tween
* @param {String} prop
* @param {String|Number|Boolean} value
* @private
**/
MovieClipPlugin.init = function(tween, prop, value) {
return value;
};
/**
* @method step
* @private
**/
MovieClipPlugin.step = function() {
// unused.
};
/**
* @method tween
* @param {Tween} tween
* @param {String} prop
* @param {String | Number | Boolean} value
* @param {Array} startValues
* @param {Array} endValues
* @param {Number} ratio
* @param {Object} wait
* @param {Object} end
* @return {*}
*/
MovieClipPlugin.tween = function(tween, prop, value, startValues, endValues, ratio, wait, end) {
if (!(tween.target instanceof MovieClip)) { return value; }
return (ratio == 1 ? endValues[prop] : startValues[prop]);
};
}());