libraries/EaselJS_docs/files/easeljs_display_Container.js.html (view raw)
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="utf-8">
5 <link rel="stylesheet" href="../assets/css/yui-cssgrids-min.css">
6 <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
7 <link rel="stylesheet" href="../assets/css/main.css" id="site_styles">
8 <link rel="shortcut icon" type="image/x-icon" href="../assets/favicon.ico">
9 <script src="../assets/js/yui-source.js"></script>
10 <title>EaselJS v1.0.0 API Documentation : easeljs/display/Container.js</title>
11</head>
12<body class="yui3-skin-sam">
13
14<div id="doc">
15 <div id="hd" class="yui3-g header">
16 <div class="yui3-u-3-4">
17
18 <h1><a href="http://www.createjs.com/easeljs"><img src="../assets/docs-icon-EaselJS.png" title="EaselJS"></a></h1>
19
20 </div>
21 <div class="version">
22 <em>API Documentation for: 1.0.0</em>
23 </div>
24 </div>
25 <div id="bd" class="yui3-g">
26
27 <div class="yui3-u-1-4">
28 <div id="docs-sidebar" class="sidebar apidocs">
29 <div id="api-list">
30 <h2 class="off-left">APIs</h2>
31 <div id="api-tabview" class="tabview">
32 <ul class="tabs">
33 <li><a href="#api-classes">Classes</a></li>
34 </ul>
35
36 <div id="api-tabview-filter">
37 <input type="search" id="api-filter" placeholder="Type to filter APIs">
38 </div>
39
40 <div id="api-tabview-panel">
41 <ul id="api-classes" class="apis classes">
42
43 <li><a href="../classes/AlphaMapFilter.html">AlphaMapFilter</a></li>
44
45 <li><a href="../classes/AlphaMaskFilter.html">AlphaMaskFilter</a></li>
46
47 <li><a href="../classes/Bitmap.html">Bitmap</a></li>
48
49 <li><a href="../classes/BitmapCache.html">BitmapCache</a></li>
50
51 <li><a href="../classes/BitmapText.html">BitmapText</a></li>
52
53 <li><a href="../classes/BlurFilter.html">BlurFilter</a></li>
54
55 <li><a href="../classes/ButtonHelper.html">ButtonHelper</a></li>
56
57 <li><a href="../classes/ColorFilter.html">ColorFilter</a></li>
58
59 <li><a href="../classes/ColorMatrix.html">ColorMatrix</a></li>
60
61 <li><a href="../classes/ColorMatrixFilter.html">ColorMatrixFilter</a></li>
62
63 <li><a href="../classes/Container.html">Container</a></li>
64
65 <li><a href="../classes/DisplayObject.html">DisplayObject</a></li>
66
67 <li><a href="../classes/DisplayProps.html">DisplayProps</a></li>
68
69 <li><a href="../classes/DOMElement.html">DOMElement</a></li>
70
71 <li><a href="../classes/EaselJS.html">EaselJS</a></li>
72
73 <li><a href="../classes/Event.html">Event</a></li>
74
75 <li><a href="../classes/EventDispatcher.html">EventDispatcher</a></li>
76
77 <li><a href="../classes/Filter.html">Filter</a></li>
78
79 <li><a href="../classes/Graphics.html">Graphics</a></li>
80
81 <li><a href="../classes/Graphics.Arc.html">Graphics.Arc</a></li>
82
83 <li><a href="../classes/Graphics.ArcTo.html">Graphics.ArcTo</a></li>
84
85 <li><a href="../classes/Graphics.BeginPath.html">Graphics.BeginPath</a></li>
86
87 <li><a href="../classes/Graphics.BezierCurveTo.html">Graphics.BezierCurveTo</a></li>
88
89 <li><a href="../classes/Graphics.Circle.html">Graphics.Circle</a></li>
90
91 <li><a href="../classes/Graphics.ClosePath.html">Graphics.ClosePath</a></li>
92
93 <li><a href="../classes/Graphics.Ellipse.html">Graphics.Ellipse</a></li>
94
95 <li><a href="../classes/Graphics.Fill.html">Graphics.Fill</a></li>
96
97 <li><a href="../classes/Graphics.LineTo.html">Graphics.LineTo</a></li>
98
99 <li><a href="../classes/Graphics.MoveTo.html">Graphics.MoveTo</a></li>
100
101 <li><a href="../classes/Graphics.PolyStar.html">Graphics.PolyStar</a></li>
102
103 <li><a href="../classes/Graphics.QuadraticCurveTo.html">Graphics.QuadraticCurveTo</a></li>
104
105 <li><a href="../classes/Graphics.Rect.html">Graphics.Rect</a></li>
106
107 <li><a href="../classes/Graphics.RoundRect.html">Graphics.RoundRect</a></li>
108
109 <li><a href="../classes/Graphics.Stroke.html">Graphics.Stroke</a></li>
110
111 <li><a href="../classes/Graphics.StrokeDash.html">Graphics.StrokeDash</a></li>
112
113 <li><a href="../classes/Graphics.StrokeStyle.html">Graphics.StrokeStyle</a></li>
114
115 <li><a href="../classes/Matrix2D.html">Matrix2D</a></li>
116
117 <li><a href="../classes/MouseEvent.html">MouseEvent</a></li>
118
119 <li><a href="../classes/MovieClip.html">MovieClip</a></li>
120
121 <li><a href="../classes/MovieClipPlugin.html">MovieClipPlugin</a></li>
122
123 <li><a href="../classes/Point.html">Point</a></li>
124
125 <li><a href="../classes/Rectangle.html">Rectangle</a></li>
126
127 <li><a href="../classes/Shadow.html">Shadow</a></li>
128
129 <li><a href="../classes/Shape.html">Shape</a></li>
130
131 <li><a href="../classes/Sprite.html">Sprite</a></li>
132
133 <li><a href="../classes/SpriteSheet.html">SpriteSheet</a></li>
134
135 <li><a href="../classes/SpriteSheetBuilder.html">SpriteSheetBuilder</a></li>
136
137 <li><a href="../classes/SpriteSheetUtils.html">SpriteSheetUtils</a></li>
138
139 <li><a href="../classes/Stage.html">Stage</a></li>
140
141 <li><a href="../classes/StageGL.html">StageGL</a></li>
142
143 <li><a href="../classes/Text.html">Text</a></li>
144
145 <li><a href="../classes/Ticker.html">Ticker</a></li>
146
147 <li><a href="../classes/Touch.html">Touch</a></li>
148
149 <li><a href="../classes/UID.html">UID</a></li>
150
151 <li><a href="../classes/Utility Methods.html">Utility Methods</a></li>
152
153 <li><a href="../classes/VideoBuffer.html">VideoBuffer</a></li>
154
155 <li><a href="../classes/WebGLInspector.html">WebGLInspector</a></li>
156
157 </ul>
158
159 <ul id="api-modules" class="apis modules">
160
161 <li><a href="../modules/CreateJS.html">CreateJS</a></li>
162
163 <li><a href="../modules/EaselJS.html">EaselJS</a></li>
164
165 </ul>
166 </div>
167 </div>
168</div>
169
170 </div>
171 </div>
172 <div class="yui3-u-3-4">
173 <div id="api-options">
174 Show:
175 <label for="api-show-inherited">
176 <input type="checkbox" id="api-show-inherited" checked>
177 Inherited
178 </label>
179
180 <label for="api-show-protected">
181 <input type="checkbox" id="api-show-protected">
182 Protected
183 </label>
184
185 <label for="api-show-private">
186 <input type="checkbox" id="api-show-private">
187 Private
188 </label>
189 <label for="api-show-deprecated">
190 <input type="checkbox" id="api-show-deprecated">
191 Deprecated
192 </label>
193
194 </div>
195 <div class="apidocs">
196 <div id="docs-main">
197 <div class="content source-content">
198 <script>
199 var name = easeljs/display/Container.js
200</script>
201<h1 class="file-heading">File:Container.js</h1>
202
203<div class="file">
204 <pre class="code prettyprint linenums">
205/*
206* Container
207* Visit http://createjs.com/ for documentation, updates and examples.
208*
209* Copyright (c) 2010 gskinner.com, inc.
210*
211* Permission is hereby granted, free of charge, to any person
212* obtaining a copy of this software and associated documentation
213* files (the "Software"), to deal in the Software without
214* restriction, including without limitation the rights to use,
215* copy, modify, merge, publish, distribute, sublicense, and/or sell
216* copies of the Software, and to permit persons to whom the
217* Software is furnished to do so, subject to the following
218* conditions:
219*
220* The above copyright notice and this permission notice shall be
221* included in all copies or substantial portions of the Software.
222*
223* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
224* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
225* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
226* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
227* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
228* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
229* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
230* OTHER DEALINGS IN THE SOFTWARE.
231*/
232
233// namespace:
234this.createjs = this.createjs||{};
235
236(function() {
237 "use strict";
238
239
240// constructor:
241/**
242 * A Container is a nestable display list that allows you to work with compound display elements. For example you could
243 * group arm, leg, torso and head {{#crossLink "Bitmap"}}{{/crossLink}} instances together into a Person Container, and
244 * transform them as a group, while still being able to move the individual parts relative to each other. Children of
245 * containers have their <code>transform</code> and <code>alpha</code> properties concatenated with their parent
246 * Container.
247 *
248 * For example, a {{#crossLink "Shape"}}{{/crossLink}} with x=100 and alpha=0.5, placed in a Container with <code>x=50</code>
249 * and <code>alpha=0.7</code> will be rendered to the canvas at <code>x=150</code> and <code>alpha=0.35</code>.
250 * Containers have some overhead, so you generally shouldn't create a Container to hold a single child.
251 *
252 * <h4>Example</h4>
253 *
254 * var container = new createjs.Container();
255 * container.addChild(bitmapInstance, shapeInstance);
256 * container.x = 100;
257 *
258 * @class Container
259 * @extends DisplayObject
260 * @constructor
261 **/
262 function Container() {
263 this.DisplayObject_constructor();
264
265 // public properties:
266 /**
267 * The array of children in the display list. You should usually use the child management methods such as
268 * {{#crossLink "Container/addChild"}}{{/crossLink}}, {{#crossLink "Container/removeChild"}}{{/crossLink}},
269 * {{#crossLink "Container/swapChildren"}}{{/crossLink}}, etc, rather than accessing this directly, but it is
270 * included for advanced uses.
271 * @property children
272 * @type Array
273 * @default null
274 **/
275 this.children = [];
276
277 /**
278 * Indicates whether the children of this container are independently enabled for mouse/pointer interaction.
279 * If false, the children will be aggregated under the container - for example, a click on a child shape would
280 * trigger a click event on the container.
281 * @property mouseChildren
282 * @type Boolean
283 * @default true
284 **/
285 this.mouseChildren = true;
286
287 /**
288 * If false, the tick will not be propagated to children of this Container. This can provide some performance benefits.
289 * In addition to preventing the "tick" event from being dispatched, it will also prevent tick related updates
290 * on some display objects (ex. Sprite & MovieClip frame advancing, DOMElement visibility handling).
291 * @property tickChildren
292 * @type Boolean
293 * @default true
294 **/
295 this.tickChildren = true;
296 }
297 var p = createjs.extend(Container, createjs.DisplayObject);
298
299
300// getter / setters:
301 /**
302 * Use the {{#crossLink "Container/numChildren:property"}}{{/crossLink}} property instead.
303 * @method _getNumChildren
304 * @protected
305 * @return {Number}
306 **/
307 p._getNumChildren = function() {
308 return this.children.length;
309 };
310 // Container.getNumChildren is @deprecated. Remove for 1.1+
311 p.getNumChildren = createjs.deprecate(p._getNumChildren, "Container.getNumChildren");
312
313 /**
314 * Returns the number of children in the container.
315 * @property numChildren
316 * @type {Number}
317 * @readonly
318 **/
319 try {
320 Object.defineProperties(p, {
321 numChildren: { get: p._getNumChildren }
322 });
323 } catch (e) {}
324
325
326// public methods:
327 /**
328 * Constructor alias for backwards compatibility. This method will be removed in future versions.
329 * Subclasses should be updated to use {{#crossLink "Utility Methods/extends"}}{{/crossLink}}.
330 * @method initialize
331 * @deprecated in favour of `createjs.promote()`
332 **/
333 p.initialize = Container; // TODO: deprecated.
334
335 /**
336 * Returns true or false indicating whether the display object would be visible if drawn to a canvas.
337 * This does not account for whether it would be visible within the boundaries of the stage.
338 *
339 * NOTE: This method is mainly for internal use, though it may be useful for advanced uses.
340 * @method isVisible
341 * @return {Boolean} Boolean indicating whether the display object would be visible if drawn to a canvas
342 **/
343 p.isVisible = function() {
344 var hasContent = this.cacheCanvas || this.children.length;
345 return !!(this.visible && this.alpha > 0 && this.scaleX != 0 && this.scaleY != 0 && hasContent);
346 };
347
348 /**
349 * Draws the display object into the specified context ignoring its visible, alpha, shadow, and transform.
350 * Returns true if the draw was handled (useful for overriding functionality).
351 *
352 * NOTE: This method is mainly for internal use, though it may be useful for advanced uses.
353 * @method draw
354 * @param {CanvasRenderingContext2D} ctx The canvas 2D context object to draw into.
355 * @param {Boolean} [ignoreCache=false] Indicates whether the draw operation should ignore any current cache.
356 * For example, used for drawing the cache (to prevent it from simply drawing an existing cache back
357 * into itself).
358 **/
359 p.draw = function(ctx, ignoreCache) {
360 if (this.DisplayObject_draw(ctx, ignoreCache)) { return true; }
361
362 // this ensures we don't have issues with display list changes that occur during a draw:
363 var list = this.children.slice();
364 for (var i=0,l=list.length; i<l; i++) {
365 var child = list[i];
366 if (!child.isVisible()) { continue; }
367
368 // draw the child:
369 ctx.save();
370 child.updateContext(ctx);
371 child.draw(ctx);
372 ctx.restore();
373 }
374 return true;
375 };
376
377 /**
378 * Adds a child to the top of the display list.
379 *
380 * <h4>Example</h4>
381 *
382 * container.addChild(bitmapInstance);
383 *
384 * You can also add multiple children at once:
385 *
386 * container.addChild(bitmapInstance, shapeInstance, textInstance);
387 *
388 * @method addChild
389 * @param {DisplayObject} child The display object to add.
390 * @return {DisplayObject} The child that was added, or the last child if multiple children were added.
391 **/
392 p.addChild = function(child) {
393 if (child == null) { return child; }
394 var l = arguments.length;
395 if (l > 1) {
396 for (var i=0; i<l; i++) { this.addChild(arguments[i]); }
397 return arguments[l-1];
398 }
399 // Note: a lot of duplication with addChildAt, but push is WAY faster than splice.
400 var par=child.parent, silent = par === this;
401 par&&par._removeChildAt(createjs.indexOf(par.children, child), silent);
402 child.parent = this;
403 this.children.push(child);
404 if (!silent) { child.dispatchEvent("added"); }
405 return child;
406 };
407
408 /**
409 * Adds a child to the display list at the specified index, bumping children at equal or greater indexes up one, and
410 * setting its parent to this Container.
411 *
412 * <h4>Example</h4>
413 *
414 * addChildAt(child1, index);
415 *
416 * You can also add multiple children, such as:
417 *
418 * addChildAt(child1, child2, ..., index);
419 *
420 * The index must be between 0 and numChildren. For example, to add myShape under otherShape in the display list,
421 * you could use:
422 *
423 * container.addChildAt(myShape, container.getChildIndex(otherShape));
424 *
425 * This would also bump otherShape's index up by one. Fails silently if the index is out of range.
426 *
427 * @method addChildAt
428 * @param {DisplayObject} child The display object to add.
429 * @param {Number} index The index to add the child at.
430 * @return {DisplayObject} Returns the last child that was added, or the last child if multiple children were added.
431 **/
432 p.addChildAt = function(child, index) {
433 var l = arguments.length;
434 var indx = arguments[l-1]; // can't use the same name as the index param or it replaces arguments[1]
435 if (indx < 0 || indx > this.children.length) { return arguments[l-2]; }
436 if (l > 2) {
437 for (var i=0; i<l-1; i++) { this.addChildAt(arguments[i], indx+i); }
438 return arguments[l-2];
439 }
440 var par=child.parent, silent = par === this;
441 par&&par._removeChildAt(createjs.indexOf(par.children, child), silent);
442 child.parent = this;
443 this.children.splice(index, 0, child);
444 if (!silent) { child.dispatchEvent("added"); }
445 return child;
446 };
447
448 /**
449 * Removes the specified child from the display list. Note that it is faster to use removeChildAt() if the index is
450 * already known.
451 *
452 * <h4>Example</h4>
453 *
454 * container.removeChild(child);
455 *
456 * You can also remove multiple children:
457 *
458 * removeChild(child1, child2, ...);
459 *
460 * Returns true if the child (or children) was removed, or false if it was not in the display list.
461 * @method removeChild
462 * @param {DisplayObject} child The child to remove.
463 * @return {Boolean} true if the child (or children) was removed, or false if it was not in the display list.
464 **/
465 p.removeChild = function(child) {
466 var l = arguments.length;
467 if (l > 1) {
468 var good = true;
469 for (var i=0; i<l; i++) { good = good && this.removeChild(arguments[i]); }
470 return good;
471 }
472 return this._removeChildAt(createjs.indexOf(this.children, child));
473 };
474
475 /**
476 * Removes the child at the specified index from the display list, and sets its parent to null.
477 *
478 * <h4>Example</h4>
479 *
480 * container.removeChildAt(2);
481 *
482 * You can also remove multiple children:
483 *
484 * container.removeChild(2, 7, ...)
485 *
486 * Returns true if the child (or children) was removed, or false if any index was out of range.
487 * @method removeChildAt
488 * @param {Number} index The index of the child to remove.
489 * @return {Boolean} true if the child (or children) was removed, or false if any index was out of range.
490 **/
491 p.removeChildAt = function(index) {
492 var l = arguments.length;
493 if (l > 1) {
494 var a = [];
495 for (var i=0; i<l; i++) { a[i] = arguments[i]; }
496 a.sort(function(a, b) { return b-a; });
497 var good = true;
498 for (var i=0; i<l; i++) { good = good && this._removeChildAt(a[i]); }
499 return good;
500 }
501 return this._removeChildAt(index);
502 };
503
504 /**
505 * Removes all children from the display list.
506 *
507 * <h4>Example</h4>
508 *
509 * container.removeAllChildren();
510 *
511 * @method removeAllChildren
512 **/
513 p.removeAllChildren = function() {
514 var kids = this.children;
515 while (kids.length) { this._removeChildAt(0); }
516 };
517
518 /**
519 * Returns the child at the specified index.
520 *
521 * <h4>Example</h4>
522 *
523 * container.getChildAt(2);
524 *
525 * @method getChildAt
526 * @param {Number} index The index of the child to return.
527 * @return {DisplayObject} The child at the specified index. Returns null if there is no child at the index.
528 **/
529 p.getChildAt = function(index) {
530 return this.children[index];
531 };
532
533 /**
534 * Returns the child with the specified name.
535 * @method getChildByName
536 * @param {String} name The name of the child to return.
537 * @return {DisplayObject} The child with the specified name.
538 **/
539 p.getChildByName = function(name) {
540 var kids = this.children;
541 for (var i=0,l=kids.length;i<l;i++) {
542 if(kids[i].name == name) { return kids[i]; }
543 }
544 return null;
545 };
546
547 /**
548 * Performs an array sort operation on the child list.
549 *
550 * <h4>Example: Display children with a higher y in front.</h4>
551 *
552 * var sortFunction = function(obj1, obj2, options) {
553 * if (obj1.y > obj2.y) { return 1; }
554 * if (obj1.y < obj2.y) { return -1; }
555 * return 0;
556 * }
557 * container.sortChildren(sortFunction);
558 *
559 * @method sortChildren
560 * @param {Function} sortFunction the function to use to sort the child list. See JavaScript's <code>Array.sort</code>
561 * documentation for details.
562 **/
563 p.sortChildren = function(sortFunction) {
564 this.children.sort(sortFunction);
565 };
566
567 /**
568 * Returns the index of the specified child in the display list, or -1 if it is not in the display list.
569 *
570 * <h4>Example</h4>
571 *
572 * var index = container.getChildIndex(child);
573 *
574 * @method getChildIndex
575 * @param {DisplayObject} child The child to return the index of.
576 * @return {Number} The index of the specified child. -1 if the child is not found.
577 **/
578 p.getChildIndex = function(child) {
579 return createjs.indexOf(this.children, child);
580 };
581
582 /**
583 * Swaps the children at the specified indexes. Fails silently if either index is out of range.
584 * @method swapChildrenAt
585 * @param {Number} index1
586 * @param {Number} index2
587 **/
588 p.swapChildrenAt = function(index1, index2) {
589 var kids = this.children;
590 var o1 = kids[index1];
591 var o2 = kids[index2];
592 if (!o1 || !o2) { return; }
593 kids[index1] = o2;
594 kids[index2] = o1;
595 };
596
597 /**
598 * Swaps the specified children's depth in the display list. Fails silently if either child is not a child of this
599 * Container.
600 * @method swapChildren
601 * @param {DisplayObject} child1
602 * @param {DisplayObject} child2
603 **/
604 p.swapChildren = function(child1, child2) {
605 var kids = this.children;
606 var index1,index2;
607 for (var i=0,l=kids.length;i<l;i++) {
608 if (kids[i] == child1) { index1 = i; }
609 if (kids[i] == child2) { index2 = i; }
610 if (index1 != null && index2 != null) { break; }
611 }
612 if (i==l) { return; } // TODO: throw error?
613 kids[index1] = child2;
614 kids[index2] = child1;
615 };
616
617 /**
618 * Changes the depth of the specified child. Fails silently if the child is not a child of this container, or the index is out of range.
619 * @param {DisplayObject} child
620 * @param {Number} index
621 * @method setChildIndex
622 **/
623 p.setChildIndex = function(child, index) {
624 var kids = this.children, l=kids.length;
625 if (child.parent != this || index < 0 || index >= l) { return; }
626 for (var i=0;i<l;i++) {
627 if (kids[i] == child) { break; }
628 }
629 if (i==l || i == index) { return; }
630 kids.splice(i,1);
631 kids.splice(index,0,child);
632 };
633
634 /**
635 * Returns true if the specified display object either is this container or is a descendent (child, grandchild, etc)
636 * of this container.
637 * @method contains
638 * @param {DisplayObject} child The DisplayObject to be checked.
639 * @return {Boolean} true if the specified display object either is this container or is a descendent.
640 **/
641 p.contains = function(child) {
642 while (child) {
643 if (child == this) { return true; }
644 child = child.parent;
645 }
646 return false;
647 };
648
649 /**
650 * Tests whether the display object intersects the specified local point (ie. draws a pixel with alpha > 0 at the
651 * specified position). This ignores the alpha, shadow and compositeOperation of the display object, and all
652 * transform properties including regX/Y.
653 * @method hitTest
654 * @param {Number} x The x position to check in the display object's local coordinates.
655 * @param {Number} y The y position to check in the display object's local coordinates.
656 * @return {Boolean} A Boolean indicating whether there is a visible section of a DisplayObject that overlaps the specified
657 * coordinates.
658 **/
659 p.hitTest = function(x, y) {
660 // TODO: optimize to use the fast cache check where possible.
661 return (this.getObjectUnderPoint(x, y) != null);
662 };
663
664 /**
665 * Returns an array of all display objects under the specified coordinates that are in this container's display
666 * list. This routine ignores any display objects with {{#crossLink "DisplayObject/mouseEnabled:property"}}{{/crossLink}}
667 * set to `false`. The array will be sorted in order of visual depth, with the top-most display object at index 0.
668 * This uses shape based hit detection, and can be an expensive operation to run, so it is best to use it carefully.
669 * For example, if testing for objects under the mouse, test on tick (instead of on {{#crossLink "DisplayObject/mousemove:event"}}{{/crossLink}}),
670 * and only if the mouse's position has changed.
671 *
672 * <ul>
673 * <li>By default (mode=0) this method evaluates all display objects.</li>
674 * <li>By setting the `mode` parameter to `1`, the {{#crossLink "DisplayObject/mouseEnabled:property"}}{{/crossLink}}
675 * and {{#crossLink "mouseChildren:property"}}{{/crossLink}} properties will be respected.</li>
676 * <li>Setting the `mode` to `2` additionally excludes display objects that do not have active mouse event
677 * listeners or a {{#crossLink "DisplayObject:cursor:property"}}{{/crossLink}} property. That is, only objects
678 * that would normally intercept mouse interaction will be included. This can significantly improve performance
679 * in some cases by reducing the number of display objects that need to be tested.</li>
680 * </li>
681 *
682 * This method accounts for both {{#crossLink "DisplayObject/hitArea:property"}}{{/crossLink}} and {{#crossLink "DisplayObject/mask:property"}}{{/crossLink}}.
683 * @method getObjectsUnderPoint
684 * @param {Number} x The x position in the container to test.
685 * @param {Number} y The y position in the container to test.
686 * @param {Number} [mode=0] The mode to use to determine which display objects to include. 0-all, 1-respect mouseEnabled/mouseChildren, 2-only mouse opaque objects.
687 * @return {Array} An Array of DisplayObjects under the specified coordinates.
688 **/
689 p.getObjectsUnderPoint = function(x, y, mode) {
690 var arr = [];
691 var pt = this.localToGlobal(x, y);
692 this._getObjectsUnderPoint(pt.x, pt.y, arr, mode>0, mode==1);
693 return arr;
694 };
695
696 /**
697 * Similar to {{#crossLink "Container/getObjectsUnderPoint"}}{{/crossLink}}, but returns only the top-most display
698 * object. This runs significantly faster than <code>getObjectsUnderPoint()</code>, but is still potentially an expensive
699 * operation. See {{#crossLink "Container/getObjectsUnderPoint"}}{{/crossLink}} for more information.
700 * @method getObjectUnderPoint
701 * @param {Number} x The x position in the container to test.
702 * @param {Number} y The y position in the container to test.
703 * @param {Number} mode The mode to use to determine which display objects to include. 0-all, 1-respect mouseEnabled/mouseChildren, 2-only mouse opaque objects.
704 * @return {DisplayObject} The top-most display object under the specified coordinates.
705 **/
706 p.getObjectUnderPoint = function(x, y, mode) {
707 var pt = this.localToGlobal(x, y);
708 return this._getObjectsUnderPoint(pt.x, pt.y, null, mode>0, mode==1);
709 };
710
711 /**
712 * Docced in superclass.
713 */
714 p.getBounds = function() {
715 return this._getBounds(null, true);
716 };
717
718
719 /**
720 * Docced in superclass.
721 */
722 p.getTransformedBounds = function() {
723 return this._getBounds();
724 };
725
726 /**
727 * Returns a clone of this Container. Some properties that are specific to this instance's current context are
728 * reverted to their defaults (for example .parent).
729 * @method clone
730 * @param {Boolean} [recursive=false] If true, all of the descendants of this container will be cloned recursively. If false, the
731 * properties of the container will be cloned, but the new instance will not have any children.
732 * @return {Container} A clone of the current Container instance.
733 **/
734 p.clone = function(recursive) {
735 var o = this._cloneProps(new Container());
736 if (recursive) { this._cloneChildren(o); }
737 return o;
738 };
739
740 /**
741 * Returns a string representation of this object.
742 * @method toString
743 * @return {String} a string representation of the instance.
744 **/
745 p.toString = function() {
746 return "[Container (name="+ this.name +")]";
747 };
748
749
750// private methods:
751 /**
752 * @method _tick
753 * @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.
754 * @protected
755 **/
756 p._tick = function(evtObj) {
757 if (this.tickChildren) {
758 for (var i=this.children.length-1; i>=0; i--) {
759 var child = this.children[i];
760 if (child.tickEnabled && child._tick) { child._tick(evtObj); }
761 }
762 }
763 this.DisplayObject__tick(evtObj);
764 };
765
766 /**
767 * Recursively clones all children of this container, and adds them to the target container.
768 * @method cloneChildren
769 * @protected
770 * @param {Container} o The target container.
771 **/
772 p._cloneChildren = function(o) {
773 if (o.children.length) { o.removeAllChildren(); }
774 var arr = o.children;
775 for (var i=0, l=this.children.length; i<l; i++) {
776 var clone = this.children[i].clone(true);
777 clone.parent = o;
778 arr.push(clone);
779 }
780 };
781
782 /**
783 * Removes the child at the specified index from the display list, and sets its parent to null.
784 * Used by `removeChildAt`, `addChild`, and `addChildAt`.
785 * @method removeChildAt
786 * @protected
787 * @param {Number} index The index of the child to remove.
788 * @param {Boolean} [silent] Prevents dispatch of `removed` event if true.
789 * @return {Boolean} true if the child (or children) was removed, or false if any index was out of range.
790 **/
791 p._removeChildAt = function(index, silent) {
792 if (index < 0 || index > this.children.length-1) { return false; }
793 var child = this.children[index];
794 if (child) { child.parent = null; }
795 this.children.splice(index, 1);
796 if (!silent) { child.dispatchEvent("removed"); }
797 return true;
798 };
799
800 /**
801 * @method _getObjectsUnderPoint
802 * @param {Number} x
803 * @param {Number} y
804 * @param {Array} arr
805 * @param {Boolean} mouse If true, it will respect mouse interaction properties like mouseEnabled, mouseChildren, and active listeners.
806 * @param {Boolean} activeListener If true, there is an active mouse event listener on a parent object.
807 * @param {Number} currentDepth Indicates the current depth of the search.
808 * @return {DisplayObject}
809 * @protected
810 **/
811 p._getObjectsUnderPoint = function(x, y, arr, mouse, activeListener, currentDepth) {
812 currentDepth = currentDepth || 0;
813 if (!currentDepth && !this._testMask(this, x, y)) { return null; }
814 var mtx, ctx = createjs.DisplayObject._hitTestContext;
815 activeListener = activeListener || (mouse&&this._hasMouseEventListener());
816
817 // draw children one at a time, and check if we get a hit:
818 var children = this.children, l = children.length;
819 for (var i=l-1; i>=0; i--) {
820 var child = children[i];
821 var hitArea = child.hitArea;
822 if (!child.visible || (!hitArea && !child.isVisible()) || (mouse && !child.mouseEnabled)) { continue; }
823 if (!hitArea && !this._testMask(child, x, y)) { continue; }
824
825 // if a child container has a hitArea then we only need to check its hitAre2a, so we can treat it as a normal DO:
826 if (!hitArea && child instanceof Container) {
827 var result = child._getObjectsUnderPoint(x, y, arr, mouse, activeListener, currentDepth+1);
828 if (!arr && result) { return (mouse && !this.mouseChildren) ? this : result; }
829 } else {
830 if (mouse && !activeListener && !child._hasMouseEventListener()) { continue; }
831
832 // TODO: can we pass displayProps forward, to avoid having to calculate this backwards every time? It's kind of a mixed bag. When we're only hunting for DOs with event listeners, it may not make sense.
833 var props = child.getConcatenatedDisplayProps(child._props);
834 mtx = props.matrix;
835
836 if (hitArea) {
837 mtx.appendMatrix(hitArea.getMatrix(hitArea._props.matrix));
838 props.alpha = hitArea.alpha;
839 }
840
841 ctx.globalAlpha = props.alpha;
842 ctx.setTransform(mtx.a, mtx.b, mtx.c, mtx.d, mtx.tx-x, mtx.ty-y);
843 (hitArea||child).draw(ctx);
844 if (!this._testHit(ctx)) { continue; }
845 ctx.setTransform(1, 0, 0, 1, 0, 0);
846 ctx.clearRect(0, 0, 2, 2);
847 if (arr) { arr.push(child); }
848 else { return (mouse && !this.mouseChildren) ? this : child; }
849 }
850 }
851 return null;
852 };
853
854 /**
855 * @method _testMask
856 * @param {DisplayObject} target
857 * @param {Number} x
858 * @param {Number} y
859 * @return {Boolean} Indicates whether the x/y is within the masked region.
860 * @protected
861 **/
862 p._testMask = function(target, x, y) {
863 var mask = target.mask;
864 if (!mask || !mask.graphics || mask.graphics.isEmpty()) { return true; }
865
866 var mtx = this._props.matrix, parent = target.parent;
867 mtx = parent ? parent.getConcatenatedMatrix(mtx) : mtx.identity();
868 mtx = mask.getMatrix(mask._props.matrix).prependMatrix(mtx);
869
870 var ctx = createjs.DisplayObject._hitTestContext;
871 ctx.setTransform(mtx.a, mtx.b, mtx.c, mtx.d, mtx.tx-x, mtx.ty-y);
872
873 // draw the mask as a solid fill:
874 mask.graphics.drawAsPath(ctx);
875 ctx.fillStyle = "#000";
876 ctx.fill();
877
878 if (!this._testHit(ctx)) { return false; }
879 ctx.setTransform(1, 0, 0, 1, 0, 0);
880 ctx.clearRect(0, 0, 2, 2);
881
882 return true;
883 };
884
885 /**
886 * @method _getBounds
887 * @param {Matrix2D} matrix
888 * @param {Boolean} ignoreTransform If true, does not apply this object's transform.
889 * @return {Rectangle}
890 * @protected
891 **/
892 p._getBounds = function(matrix, ignoreTransform) {
893 var bounds = this.DisplayObject_getBounds();
894 if (bounds) { return this._transformBounds(bounds, matrix, ignoreTransform); }
895
896 var mtx = this._props.matrix;
897 mtx = ignoreTransform ? mtx.identity() : this.getMatrix(mtx);
898 if (matrix) { mtx.prependMatrix(matrix); }
899
900 var l = this.children.length, rect=null;
901 for (var i=0; i<l; i++) {
902 var child = this.children[i];
903 if (!child.visible || !(bounds = child._getBounds(mtx))) { continue; }
904 if (rect) { rect.extend(bounds.x, bounds.y, bounds.width, bounds.height); }
905 else { rect = bounds.clone(); }
906 }
907 return rect;
908 };
909
910
911 createjs.Container = createjs.promote(Container, "DisplayObject");
912}());
913 </pre>
914</div>
915
916 </div>
917 </div>
918 </div>
919 </div>
920 </div>
921</div>
922<script src="../assets/vendor/prettify/prettify-min.js"></script>
923<script>prettyPrint();</script>
924<script src="../assets/js/yui-prettify.js"></script>
925<script src="../assets/../api.js"></script>
926<script src="../assets/js/api-filter.js"></script>
927<script src="../assets/js/api-list.js"></script>
928<script src="../assets/js/api-search.js"></script>
929<script src="../assets/js/apidocs.js"></script>
930</body>
931</html>