all repos — simplyrpg @ 12669c61e2eec0d84ef981688888e4986778c0b8

Fork of https://github.com/harperd1/simplyRPG — multi-map VTT, token visibility, dark theme, stream HUD

libraries/EaselJS_docs/files/easeljs_utils_SpriteSheetBuilder.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/utils/SpriteSheetBuilder.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/utils/SpriteSheetBuilder.js      
200</script>
201<h1 class="file-heading">File:SpriteSheetBuilder.js</h1>
202
203<div class="file">
204    <pre class="code prettyprint linenums">
205/*
206* SpriteSheetBuilder
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 &quot;Software&quot;), 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 &quot;AS IS&quot;, 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/**
234 * @module EaselJS
235 */
236
237// namespace:
238this.createjs = this.createjs||{};
239
240(function() {
241	&quot;use strict&quot;;
242
243
244// constructor:
245	/**
246	 * The SpriteSheetBuilder allows you to generate {{#crossLink &quot;SpriteSheet&quot;}}{{/crossLink}} instances at run time
247	 * from any display object. This can allow you to maintain your assets as vector graphics (for low file size), and
248	 * render them at run time as SpriteSheets for better performance.
249	 *
250	 * SpriteSheets can be built either synchronously, or asynchronously, so that large SpriteSheets can be generated
251	 * without locking the UI.
252	 *
253	 * Note that the &quot;images&quot; used in the generated SpriteSheet are actually canvas elements, and that they will be
254	 * sized to the nearest power of 2 up to the value of {{#crossLink &quot;SpriteSheetBuilder/maxWidth:property&quot;}}{{/crossLink}}
255	 * or {{#crossLink &quot;SpriteSheetBuilder/maxHeight:property&quot;}}{{/crossLink}}.
256	 * @class SpriteSheetBuilder
257	 * @param {Number} [framerate=0] The {{#crossLink &quot;SpriteSheet/framerate:property&quot;}}{{/crossLink}} of
258	 * {{#crossLink &quot;SpriteSheet&quot;}}{{/crossLink}} instances that are created.
259	 * @extends EventDispatcher
260	 * @constructor
261	 **/
262	function SpriteSheetBuilder(framerate) {
263		this.EventDispatcher_constructor();
264		
265	// public properties:
266		/**
267		 * The maximum width for the images (not individual frames) in the generated SpriteSheet. It is recommended to
268		 * use a power of 2 for this value (ex. 1024, 2048, 4096). If the frames cannot all fit within the max
269		 * dimensions, then additional images will be created as needed.
270		 * @property maxWidth
271		 * @type Number
272		 * @default 2048
273		*/
274		this.maxWidth = 2048;
275	
276		/**
277		 * The maximum height for the images (not individual frames) in the generated SpriteSheet. It is recommended to
278		 * use a power of 2 for this value (ex. 1024, 2048, 4096). If the frames cannot all fit within the max
279		 * dimensions, then additional images will be created as needed.
280		 * @property maxHeight
281		 * @type Number
282		 * @default 2048
283		 **/
284		this.maxHeight = 2048;
285	
286		/**
287		 * The SpriteSheet that was generated. This will be null before a build is completed successfully.
288		 * @property spriteSheet
289		 * @type SpriteSheet
290		 **/
291		this.spriteSheet = null;
292	
293		/**
294		 * The scale to apply when drawing all frames to the SpriteSheet. This is multiplied against any scale specified
295		 * in the addFrame call. This can be used, for example, to generate a SpriteSheet at run time that is tailored
296		 * to the a specific device resolution (ex. tablet vs mobile).
297		 * @property scale
298		 * @type Number
299		 * @default 1
300		 **/
301		this.scale = 1;
302	
303		/**
304		* The padding to use between frames. This is helpful to preserve antialiasing on drawn vector content.
305		* @property padding
306		* @type Number
307		* @default 1
308		**/
309		this.padding = 1;
310	
311		/**
312		 * A number from 0.01 to 0.99 that indicates what percentage of time the builder can use. This can be
313		 * thought of as the number of seconds per second the builder will use. For example, with a timeSlice value of 0.3,
314		 * the builder will run 20 times per second, using approximately 15ms per build (30% of available time, or 0.3s per second).
315		 * Defaults to 0.3.
316		 * @property timeSlice
317		 * @type Number
318		 * @default 0.3
319		 **/
320		this.timeSlice = 0.3;
321	
322		/**
323		 * A value between 0 and 1 that indicates the progress of a build, or -1 if a build has not
324		 * been initiated.
325		 * @property progress
326		 * @type Number
327		 * @default -1
328		 * @readonly
329		 */
330		this.progress = -1;
331
332		/**
333		 * A {{#crossLink &quot;SpriteSheet/framerate:property&quot;}}{{/crossLink}} value that will be passed to new {{#crossLink &quot;SpriteSheet&quot;}}{{/crossLink}} instances that are
334		 * created. If no framerate is specified (or it is 0), then SpriteSheets will use the {{#crossLink &quot;Ticker&quot;}}{{/crossLink}}
335		 * framerate.
336		 * @property framerate
337		 * @type Number
338		 * @default 0
339		 */
340		this.framerate = framerate || 0;
341	
342	
343	// private properties:
344		/**
345		 * @property _frames
346		 * @protected
347		 * @type Array
348		 **/
349		this._frames = [];
350	
351		/**
352		 * @property _animations
353		 * @protected
354		 * @type Array
355		 **/
356		this._animations = {};
357	
358		/**
359		 * @property _data
360		 * @protected
361		 * @type Array
362		 **/
363		this._data = null;
364	
365		/**
366		 * @property _nextFrameIndex
367		 * @protected
368		 * @type Number
369		 **/
370		this._nextFrameIndex = 0;
371	
372		/**
373		 * @property _index
374		 * @protected
375		 * @type Number
376		 **/
377		this._index = 0;
378	
379		/**
380		 * @property _timerID
381		 * @protected
382		 * @type Number
383		 **/
384		this._timerID = null;
385	
386		/**
387		 * @property _scale
388		 * @protected
389		 * @type Number
390		 **/
391		this._scale = 1;
392	}
393	var p = createjs.extend(SpriteSheetBuilder, createjs.EventDispatcher);
394
395// constants:
396	SpriteSheetBuilder.ERR_DIMENSIONS = &quot;frame dimensions exceed max spritesheet dimensions&quot;;
397	SpriteSheetBuilder.ERR_RUNNING = &quot;a build is already running&quot;;
398
399// events:
400	/**
401	 * Dispatched when a build completes.
402	 * @event complete
403	 * @param {Object} target The object that dispatched the event.
404	 * @param {String} type The event type.
405	 * @since 0.6.0
406	 */
407
408	/**
409	 * Dispatched when an asynchronous build has progress.
410	 * @event progress
411	 * @param {Object} target The object that dispatched the event.
412	 * @param {String} type The event type.
413	 * @param {Number} progress The current progress value (0-1).
414	 * @since 0.6.0
415	 */
416
417
418// public methods:
419	/**
420	 * Adds a frame to the {{#crossLink &quot;SpriteSheet&quot;}}{{/crossLink}}. Note that the frame will not be drawn until you
421	 * call {{#crossLink &quot;SpriteSheetBuilder/build&quot;}}{{/crossLink}} method. The optional setup params allow you to have
422	 * a function run immediately before the draw occurs. For example, this allows you to add a single source multiple
423	 * times, but manipulate it or its children to change it to generate different frames.
424	 *
425	 * Note that the source&#x27;s transformations (x, y, scale, rotate, alpha) will be ignored, except for regX/Y. To apply
426	 * transforms to a source object and have them captured in the SpriteSheet, simply place it into a {{#crossLink &quot;Container&quot;}}{{/crossLink}}
427	 * and pass in the Container as the source.
428	 * @method addFrame
429	 * @param {DisplayObject} source The source {{#crossLink &quot;DisplayObject&quot;}}{{/crossLink}}  to draw as the frame.
430	 * @param {Rectangle} [sourceRect] A {{#crossLink &quot;Rectangle&quot;}}{{/crossLink}} defining the portion of the
431	 * source to draw to the frame. If not specified, it will look for a &#x60;getBounds&#x60; method, bounds property, or
432	 * &#x60;nominalBounds&#x60; property on the source to use. If one is not found, the frame will be skipped.
433	 * @param {Number} [scale=1] Optional. The scale to draw this frame at. Default is 1.
434	 * @param {Function} [setupFunction] A function to call immediately before drawing this frame. It will be called with two parameters: the source, and setupData.
435	 * @param {Object} [setupData] Arbitrary setup data to pass to setupFunction as the second parameter.
436	 * @return {Number} The index of the frame that was just added, or null if a sourceRect could not be determined.
437	 **/
438	p.addFrame = function(source, sourceRect, scale, setupFunction, setupData) {
439		if (this._data) { throw SpriteSheetBuilder.ERR_RUNNING; }
440		var rect = sourceRect||source.bounds||source.nominalBounds;
441		if (!rect&amp;&amp;source.getBounds) { rect = source.getBounds(); }
442		if (!rect) { return null; }
443		scale = scale||1;
444		return this._frames.push({source:source, sourceRect:rect, scale:scale, funct:setupFunction, data:setupData, index:this._frames.length, height:rect.height*scale})-1;
445	};
446
447	/**
448	 * Adds an animation that will be included in the created {{#crossLink &quot;SpriteSheet&quot;}}{{/crossLink}}.
449	 * @method addAnimation
450	 * @param {String} name The name for the animation.
451	 * @param {Array} frames An array of frame indexes that comprise the animation. Ex. [3,6,5] would describe an animation
452	 * that played frame indexes 3, 6, and 5 in that order.
453	 * @param {String} [next] Specifies the name of the animation to continue to after this animation ends. You can
454	 * also pass false to have the animation stop when it ends. By default it will loop to the start of the same animation.
455	 * @param {Number} [speed] Specifies a frame advance speed for this animation. For example, a value of 0.5 would
456	 * cause the animation to advance every second tick. Note that earlier versions used &#x60;frequency&#x60; instead, which had
457	 * the opposite effect.
458	 **/
459	p.addAnimation = function(name, frames, next, speed) {
460		if (this._data) { throw SpriteSheetBuilder.ERR_RUNNING; }
461		this._animations[name] = {frames:frames, next:next, speed:speed};
462	};
463
464	/**
465	 * This will take a {{#crossLink &quot;MovieClip&quot;}}{{/crossLink}} instance, and add its frames and labels to this
466	 * builder. Labels will be added as an animation running from the label index to the next label. For example, if
467	 * there is a label named &quot;foo&quot; at frame 0 and a label named &quot;bar&quot; at frame 10, in a MovieClip with 15 frames, it
468	 * will add an animation named &quot;foo&quot; that runs from frame index 0 to 9, and an animation named &quot;bar&quot; that runs from
469	 * frame index 10 to 14.
470	 *
471	 * Note that this will iterate through the full MovieClip with {{#crossLink &quot;MovieClip/actionsEnabled:property&quot;}}{{/crossLink}}
472	 * set to &#x60;false&#x60;, ending on the last frame.
473	 * @method addMovieClip
474	 * @param {MovieClip} source The source MovieClip instance to add to the SpriteSheet.
475	 * @param {Rectangle} [sourceRect] A {{#crossLink &quot;Rectangle&quot;}}{{/crossLink}} defining the portion of the source to
476	 * draw to the frame. If not specified, it will look for a {{#crossLink &quot;DisplayObject/getBounds&quot;}}{{/crossLink}}
477	 * method, &#x60;frameBounds&#x60; Array, &#x60;bounds&#x60; property, or &#x60;nominalBounds&#x60; property on the source to use. If one is not
478	 * found, the MovieClip will be skipped.
479	 * @param {Number} [scale=1] The scale to draw the movie clip at.
480	 * @param {Function} [setupFunction] A function to call immediately before drawing each frame. It will be called
481	 * with three parameters: the source, setupData, and the frame index.
482	 * @param {Object} [setupData] Arbitrary setup data to pass to setupFunction as the second parameter.
483	 * @param {Function} [labelFunction] This method will be called for each MovieClip label that is added with four
484	 * parameters: the label name, the source MovieClip instance, the starting frame index (in the movieclip timeline)
485	 * and the end index. It must return a new name for the label/animation, or &#x60;false&#x60; to exclude the label.
486	 **/
487	p.addMovieClip = function(source, sourceRect, scale, setupFunction, setupData, labelFunction) {
488		if (this._data) { throw SpriteSheetBuilder.ERR_RUNNING; }
489		var rects = source.frameBounds;
490		var rect = sourceRect||source.bounds||source.nominalBounds;
491		if (!rect&amp;&amp;source.getBounds) { rect = source.getBounds(); }
492		if (!rect &amp;&amp; !rects) { return; }
493
494		var i, l, baseFrameIndex = this._frames.length;
495		var duration = source.timeline.duration;
496		for (i=0; i&lt;duration; i++) {
497			var r = (rects&amp;&amp;rects[i]) ? rects[i] : rect;
498			this.addFrame(source, r, scale, this._setupMovieClipFrame, {i:i, f:setupFunction, d:setupData});
499		}
500		var labels = source.timeline._labels;
501		var lbls = [];
502		for (var n in labels) {
503			lbls.push({index:labels[n], label:n});
504		}
505		if (lbls.length) {
506			lbls.sort(function(a,b){ return a.index-b.index; });
507			for (i=0,l=lbls.length; i&lt;l; i++) {
508				var label = lbls[i].label;
509				var start = baseFrameIndex+lbls[i].index;
510				var end = baseFrameIndex+((i == l-1) ? duration : lbls[i+1].index);
511				var frames = [];
512				for (var j=start; j&lt;end; j++) { frames.push(j); }
513				if (labelFunction) {
514					label = labelFunction(label, source, start, end);
515					if (!label) { continue; }
516				}
517				this.addAnimation(label, frames, true); // for now, this loops all animations.
518			}
519		}
520	};
521
522	/**
523	 * Builds a {{#crossLink &quot;SpriteSheet&quot;}}{{/crossLink}} instance based on the current frames.
524	 * @method build
525	 * @return {SpriteSheet} The created SpriteSheet instance, or null if a build is already running or an error
526	 * occurred.
527	 **/
528	p.build = function() {
529		if (this._data) { throw SpriteSheetBuilder.ERR_RUNNING; }
530		this._startBuild();
531		while (this._drawNext()) {}
532		this._endBuild();
533		return this.spriteSheet;
534	};
535
536	/**
537	 * Asynchronously builds a {{#crossLink &quot;SpriteSheet&quot;}}{{/crossLink}} instance based on the current frames. It will
538	 * run 20 times per second, using an amount of time defined by &#x60;timeSlice&#x60;. When it is complete it will call the
539	 * specified callback.
540	 * @method buildAsync
541	 * @param {Number} [timeSlice] Sets the timeSlice property on this instance.
542	 **/
543	p.buildAsync = function(timeSlice) {
544		if (this._data) { throw SpriteSheetBuilder.ERR_RUNNING; }
545		this.timeSlice = timeSlice;
546		this._startBuild();
547		var _this = this;
548		this._timerID = setTimeout(function() { _this._run(); }, 50-Math.max(0.01, Math.min(0.99, this.timeSlice||0.3))*50);
549	};
550
551	/**
552	 * Stops the current asynchronous build.
553	 * @method stopAsync
554	 **/
555	p.stopAsync = function() {
556		clearTimeout(this._timerID);
557		this._data = null;
558	};
559
560	/**
561	 * SpriteSheetBuilder instances cannot be cloned.
562	 * @method clone
563	 **/
564	p.clone = function() {
565		throw(&quot;SpriteSheetBuilder cannot be cloned.&quot;);
566	};
567
568	/**
569	 * Returns a string representation of this object.
570	 * @method toString
571	 * @return {String} a string representation of the instance.
572	 **/
573	p.toString = function() {
574		return &quot;[SpriteSheetBuilder]&quot;;
575	};
576
577
578// private methods:
579	/**
580	 * @method _startBuild
581	 * @protected
582	 **/
583	p._startBuild = function() {
584		var pad = this.padding||0;
585		this.progress = 0;
586		this.spriteSheet = null;
587		this._index = 0;
588		this._scale = this.scale;
589		var dataFrames = [];
590		this._data = {
591			images: [],
592			frames: dataFrames,
593			framerate: this.framerate,
594			animations: this._animations // TODO: should we &quot;clone&quot; _animations in case someone adds more animations after a build?
595		};
596
597		var frames = this._frames.slice();
598		frames.sort(function(a,b) { return (a.height&lt;=b.height) ? -1 : 1; });
599
600		if (frames[frames.length-1].height+pad*2 &gt; this.maxHeight) { throw SpriteSheetBuilder.ERR_DIMENSIONS; }
601		var y=0, x=0;
602		var img = 0;
603		while (frames.length) {
604			var o = this._fillRow(frames, y, img, dataFrames, pad);
605			if (o.w &gt; x) { x = o.w; }
606			y += o.h;
607			if (!o.h || !frames.length) {
608				var canvas = createjs.createCanvas?createjs.createCanvas():document.createElement(&quot;canvas&quot;);
609				canvas.width = this._getSize(x,this.maxWidth);
610				canvas.height = this._getSize(y,this.maxHeight);
611				this._data.images[img] = canvas;
612				if (!o.h) {
613					x=y=0;
614					img++;
615				}
616			}
617		}
618	};
619	
620	/**
621	 * @method _setupMovieClipFrame
622	 * @protected
623	 * @return {Number} The width &amp; height of the row.
624	 **/
625	p._setupMovieClipFrame = function(source, data) {
626		var ae = source.actionsEnabled;
627		source.actionsEnabled = false;
628		source.gotoAndStop(data.i);
629		source.actionsEnabled = ae;
630		data.f&amp;&amp;data.f(source, data.d, data.i);
631	};
632
633	/**
634	 * @method _getSize
635	 * @protected
636	 * @return {Number} The width &amp; height of the row.
637	 **/
638	p._getSize = function(size,max) {
639		var pow = 4;
640		while (Math.pow(2,++pow) &lt; size){}
641		return Math.min(max,Math.pow(2,pow));
642	};
643
644	/**
645	 * @method _fillRow
646	 * @param {Array} frames
647	 * @param {Number} y
648	 * @param {HTMLImageElement} img
649	 * @param {Object} dataFrames
650	 * @param {Number} pad
651	 * @protected
652	 * @return {Number} The width &amp; height of the row.
653	 **/
654	p._fillRow = function(frames, y, img, dataFrames, pad) {
655		var w = this.maxWidth;
656		var maxH = this.maxHeight;
657		y += pad;
658		var h = maxH-y;
659		var x = pad;
660		var height = 0;
661		for (var i=frames.length-1; i&gt;=0; i--) {
662			var frame = frames[i];
663			var sc = this._scale*frame.scale;
664			var rect = frame.sourceRect;
665			var source = frame.source;
666			var rx = Math.floor(sc*rect.x-pad);
667			var ry = Math.floor(sc*rect.y-pad);
668			var rh = Math.ceil(sc*rect.height+pad*2);
669			var rw = Math.ceil(sc*rect.width+pad*2);
670			if (rw &gt; w) { throw SpriteSheetBuilder.ERR_DIMENSIONS; }
671			if (rh &gt; h || x+rw &gt; w) { continue; }
672			frame.img = img;
673			frame.rect = new createjs.Rectangle(x,y,rw,rh);
674			height = height || rh;
675			frames.splice(i,1);
676			dataFrames[frame.index] = [x,y,rw,rh,img,Math.round(-rx+sc*source.regX-pad),Math.round(-ry+sc*source.regY-pad)];
677			x += rw;
678		}
679		return {w:x, h:height};
680	};
681
682	/**
683	 * @method _endBuild
684	 * @protected
685	 **/
686	p._endBuild = function() {
687		this.spriteSheet = new createjs.SpriteSheet(this._data);
688		this._data = null;
689		this.progress = 1;
690		this.dispatchEvent(&quot;complete&quot;);
691	};
692
693	/**
694	 * @method _run
695	 * @protected
696	 **/
697	p._run = function() {
698		var ts = Math.max(0.01, Math.min(0.99, this.timeSlice||0.3))*50;
699		var t = (new Date()).getTime()+ts;
700		var complete = false;
701		while (t &gt; (new Date()).getTime()) {
702			if (!this._drawNext()) { complete = true; break; }
703		}
704		if (complete) {
705			this._endBuild();
706		} else {
707			var _this = this;
708			this._timerID = setTimeout(function() { _this._run(); }, 50-ts);
709		}
710		var p = this.progress = this._index/this._frames.length;
711		if (this.hasEventListener(&quot;progress&quot;)) {
712			var evt = new createjs.Event(&quot;progress&quot;);
713			evt.progress = p;
714			this.dispatchEvent(evt);
715		}
716	};
717
718	/**
719	 * @method _drawNext
720	 * @protected
721	 * @return Boolean Returns false if this is the last draw.
722	 **/
723	p._drawNext = function() {
724		var frame = this._frames[this._index];
725		var sc = frame.scale*this._scale;
726		var rect = frame.rect;
727		var sourceRect = frame.sourceRect;
728		var canvas = this._data.images[frame.img];
729		var ctx = canvas.getContext(&quot;2d&quot;);
730		frame.funct&amp;&amp;frame.funct(frame.source, frame.data);
731		ctx.save();
732		ctx.beginPath();
733		ctx.rect(rect.x, rect.y, rect.width, rect.height);
734		ctx.clip();
735		ctx.translate(Math.ceil(rect.x-sourceRect.x*sc), Math.ceil(rect.y-sourceRect.y*sc));
736		ctx.scale(sc,sc);
737		frame.source.draw(ctx); // display object will draw itself.
738		ctx.restore();
739		return (++this._index) &lt; this._frames.length;
740	};
741
742
743	createjs.SpriteSheetBuilder = createjs.promote(SpriteSheetBuilder, &quot;EventDispatcher&quot;);
744}());
745
746    </pre>
747</div>
748
749                    </div>
750                </div>
751            </div>
752        </div>
753    </div>
754</div>
755<script src="../assets/vendor/prettify/prettify-min.js"></script>
756<script>prettyPrint();</script>
757<script src="../assets/js/yui-prettify.js"></script>
758<script src="../assets/../api.js"></script>
759<script src="../assets/js/api-filter.js"></script>
760<script src="../assets/js/api-list.js"></script>
761<script src="../assets/js/api-search.js"></script>
762<script src="../assets/js/apidocs.js"></script>
763</body>
764</html>