tweenjs.js 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833
  1. /*!
  2. * TweenJS
  3. * Visit http://createjs.com/ for documentation, updates and examples.
  4. *
  5. * Copyright (c) 2010 gskinner.com, inc.
  6. *
  7. * Permission is hereby granted, free of charge, to any person
  8. * obtaining a copy of this software and associated documentation
  9. * files (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use,
  11. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following
  14. * conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. //##############################################################################
  29. // extend.js
  30. //##############################################################################
  31. this.createjs = this.createjs||{};
  32. /**
  33. * @class Utility Methods
  34. */
  35. /**
  36. * Sets up the prototype chain and constructor property for a new class.
  37. *
  38. * This should be called right after creating the class constructor.
  39. *
  40. * function MySubClass() {}
  41. * createjs.extend(MySubClass, MySuperClass);
  42. * MySubClass.prototype.doSomething = function() { }
  43. *
  44. * var foo = new MySubClass();
  45. * console.log(foo instanceof MySuperClass); // true
  46. * console.log(foo.prototype.constructor === MySubClass); // true
  47. *
  48. * @method extend
  49. * @param {Function} subclass The subclass.
  50. * @param {Function} superclass The superclass to extend.
  51. * @return {Function} Returns the subclass's new prototype.
  52. */
  53. createjs.extend = function(subclass, superclass) {
  54. "use strict";
  55. function o() { this.constructor = subclass; }
  56. o.prototype = superclass.prototype;
  57. return (subclass.prototype = new o());
  58. };
  59. //##############################################################################
  60. // promote.js
  61. //##############################################################################
  62. this.createjs = this.createjs||{};
  63. /**
  64. * @class Utility Methods
  65. */
  66. /**
  67. * Promotes any methods on the super class that were overridden, by creating an alias in the format `prefix_methodName`.
  68. * It is recommended to use the super class's name as the prefix.
  69. * An alias to the super class's constructor is always added in the format `prefix_constructor`.
  70. * This allows the subclass to call super class methods without using `function.call`, providing better performance.
  71. *
  72. * For example, if `MySubClass` extends `MySuperClass`, and both define a `draw` method, then calling `promote(MySubClass, "MySuperClass")`
  73. * would add a `MySuperClass_constructor` method to MySubClass and promote the `draw` method on `MySuperClass` to the
  74. * prototype of `MySubClass` as `MySuperClass_draw`.
  75. *
  76. * This should be called after the class's prototype is fully defined.
  77. *
  78. * function ClassA(name) {
  79. * this.name = name;
  80. * }
  81. * ClassA.prototype.greet = function() {
  82. * return "Hello "+this.name;
  83. * }
  84. *
  85. * function ClassB(name, punctuation) {
  86. * this.ClassA_constructor(name);
  87. * this.punctuation = punctuation;
  88. * }
  89. * createjs.extend(ClassB, ClassA);
  90. * ClassB.prototype.greet = function() {
  91. * return this.ClassA_greet()+this.punctuation;
  92. * }
  93. * createjs.promote(ClassB, "ClassA");
  94. *
  95. * var foo = new ClassB("World", "!?!");
  96. * console.log(foo.greet()); // Hello World!?!
  97. *
  98. * @method promote
  99. * @param {Function} subclass The class to promote super class methods on.
  100. * @param {String} prefix The prefix to add to the promoted method names. Usually the name of the superclass.
  101. * @return {Function} Returns the subclass.
  102. */
  103. createjs.promote = function(subclass, prefix) {
  104. "use strict";
  105. var subP = subclass.prototype, supP = (Object.getPrototypeOf&&Object.getPrototypeOf(subP))||subP.__proto__;
  106. if (supP) {
  107. subP[(prefix+="_") + "constructor"] = supP.constructor; // constructor is not always innumerable
  108. for (var n in supP) {
  109. if (subP.hasOwnProperty(n) && (typeof supP[n] == "function")) { subP[prefix + n] = supP[n]; }
  110. }
  111. }
  112. return subclass;
  113. };
  114. //##############################################################################
  115. // deprecate.js
  116. //##############################################################################
  117. this.createjs = this.createjs||{};
  118. /**
  119. * @class Utility Methods
  120. */
  121. /**
  122. * Wraps deprecated methods so they still be used, but throw warnings to developers.
  123. *
  124. * obj.deprecatedMethod = createjs.deprecate("Old Method Name", obj._fallbackMethod);
  125. *
  126. * The recommended approach for deprecated properties is:
  127. *
  128. * try {
  129. * Obj ect.defineProperties(object, {
  130. * readyOnlyProp: { get: createjs.deprecate("readOnlyProp", function() { return this.alternateProp; }) },
  131. * readWriteProp: {
  132. * get: createjs.deprecate("readOnlyProp", function() { return this.alternateProp; }),
  133. * set: createjs.deprecate("readOnlyProp", function(val) { this.alternateProp = val; })
  134. * });
  135. * } catch (e) {}
  136. *
  137. * @method deprecate
  138. * @param {Function} [fallbackMethod=null] A method to call when the deprecated method is used. See the example for how
  139. * @param {String} [name=null] The name of the method or property to display in the console warning.
  140. * to deprecate properties.
  141. * @return {Function} If a fallbackMethod is supplied, returns a closure that will call the fallback method after
  142. * logging the warning in the console.
  143. */
  144. createjs.deprecate = function(fallbackMethod, name) {
  145. "use strict";
  146. return function() {
  147. var msg = "Deprecated property or method '"+name+"'. See docs for info.";
  148. console && (console.warn ? console.warn(msg) : console.log(msg));
  149. return fallbackMethod && fallbackMethod.apply(this, arguments);
  150. }
  151. };
  152. //##############################################################################
  153. // Event.js
  154. //##############################################################################
  155. this.createjs = this.createjs||{};
  156. (function() {
  157. "use strict";
  158. // constructor:
  159. /**
  160. * Contains properties and methods shared by all events for use with
  161. * {{#crossLink "EventDispatcher"}}{{/crossLink}}.
  162. *
  163. * Note that Event objects are often reused, so you should never
  164. * rely on an event object's state outside of the call stack it was received in.
  165. * @class Event
  166. * @param {String} type The event type.
  167. * @param {Boolean} bubbles Indicates whether the event will bubble through the display list.
  168. * @param {Boolean} cancelable Indicates whether the default behaviour of this event can be cancelled.
  169. * @constructor
  170. **/
  171. function Event(type, bubbles, cancelable) {
  172. // public properties:
  173. /**
  174. * The type of event.
  175. * @property type
  176. * @type String
  177. **/
  178. this.type = type;
  179. /**
  180. * The object that generated an event.
  181. * @property target
  182. * @type Object
  183. * @default null
  184. * @readonly
  185. */
  186. this.target = null;
  187. /**
  188. * The current target that a bubbling event is being dispatched from. For non-bubbling events, this will
  189. * always be the same as target. For example, if childObj.parent = parentObj, and a bubbling event
  190. * is generated from childObj, then a listener on parentObj would receive the event with
  191. * target=childObj (the original target) and currentTarget=parentObj (where the listener was added).
  192. * @property currentTarget
  193. * @type Object
  194. * @default null
  195. * @readonly
  196. */
  197. this.currentTarget = null;
  198. /**
  199. * For bubbling events, this indicates the current event phase:<OL>
  200. * <LI> capture phase: starting from the top parent to the target</LI>
  201. * <LI> at target phase: currently being dispatched from the target</LI>
  202. * <LI> bubbling phase: from the target to the top parent</LI>
  203. * </OL>
  204. * @property eventPhase
  205. * @type Number
  206. * @default 0
  207. * @readonly
  208. */
  209. this.eventPhase = 0;
  210. /**
  211. * Indicates whether the event will bubble through the display list.
  212. * @property bubbles
  213. * @type Boolean
  214. * @default false
  215. * @readonly
  216. */
  217. this.bubbles = !!bubbles;
  218. /**
  219. * Indicates whether the default behaviour of this event can be cancelled via
  220. * {{#crossLink "Event/preventDefault"}}{{/crossLink}}. This is set via the Event constructor.
  221. * @property cancelable
  222. * @type Boolean
  223. * @default false
  224. * @readonly
  225. */
  226. this.cancelable = !!cancelable;
  227. /**
  228. * The epoch time at which this event was created.
  229. * @property timeStamp
  230. * @type Number
  231. * @default 0
  232. * @readonly
  233. */
  234. this.timeStamp = (new Date()).getTime();
  235. /**
  236. * Indicates if {{#crossLink "Event/preventDefault"}}{{/crossLink}} has been called
  237. * on this event.
  238. * @property defaultPrevented
  239. * @type Boolean
  240. * @default false
  241. * @readonly
  242. */
  243. this.defaultPrevented = false;
  244. /**
  245. * Indicates if {{#crossLink "Event/stopPropagation"}}{{/crossLink}} or
  246. * {{#crossLink "Event/stopImmediatePropagation"}}{{/crossLink}} has been called on this event.
  247. * @property propagationStopped
  248. * @type Boolean
  249. * @default false
  250. * @readonly
  251. */
  252. this.propagationStopped = false;
  253. /**
  254. * Indicates if {{#crossLink "Event/stopImmediatePropagation"}}{{/crossLink}} has been called
  255. * on this event.
  256. * @property immediatePropagationStopped
  257. * @type Boolean
  258. * @default false
  259. * @readonly
  260. */
  261. this.immediatePropagationStopped = false;
  262. /**
  263. * Indicates if {{#crossLink "Event/remove"}}{{/crossLink}} has been called on this event.
  264. * @property removed
  265. * @type Boolean
  266. * @default false
  267. * @readonly
  268. */
  269. this.removed = false;
  270. }
  271. var p = Event.prototype;
  272. // public methods:
  273. /**
  274. * Sets {{#crossLink "Event/defaultPrevented"}}{{/crossLink}} to true if the event is cancelable.
  275. * Mirrors the DOM level 2 event standard. In general, cancelable events that have `preventDefault()` called will
  276. * cancel the default behaviour associated with the event.
  277. * @method preventDefault
  278. **/
  279. p.preventDefault = function() {
  280. this.defaultPrevented = this.cancelable&&true;
  281. };
  282. /**
  283. * Sets {{#crossLink "Event/propagationStopped"}}{{/crossLink}} to true.
  284. * Mirrors the DOM event standard.
  285. * @method stopPropagation
  286. **/
  287. p.stopPropagation = function() {
  288. this.propagationStopped = true;
  289. };
  290. /**
  291. * Sets {{#crossLink "Event/propagationStopped"}}{{/crossLink}} and
  292. * {{#crossLink "Event/immediatePropagationStopped"}}{{/crossLink}} to true.
  293. * Mirrors the DOM event standard.
  294. * @method stopImmediatePropagation
  295. **/
  296. p.stopImmediatePropagation = function() {
  297. this.immediatePropagationStopped = this.propagationStopped = true;
  298. };
  299. /**
  300. * Causes the active listener to be removed via removeEventListener();
  301. *
  302. * myBtn.addEventListener("click", function(evt) {
  303. * // do stuff...
  304. * evt.remove(); // removes this listener.
  305. * });
  306. *
  307. * @method remove
  308. **/
  309. p.remove = function() {
  310. this.removed = true;
  311. };
  312. /**
  313. * Returns a clone of the Event instance.
  314. * @method clone
  315. * @return {Event} a clone of the Event instance.
  316. **/
  317. p.clone = function() {
  318. return new Event(this.type, this.bubbles, this.cancelable);
  319. };
  320. /**
  321. * Provides a chainable shortcut method for setting a number of properties on the instance.
  322. *
  323. * @method set
  324. * @param {Object} props A generic object containing properties to copy to the instance.
  325. * @return {Event} Returns the instance the method is called on (useful for chaining calls.)
  326. * @chainable
  327. */
  328. p.set = function(props) {
  329. for (var n in props) { this[n] = props[n]; }
  330. return this;
  331. };
  332. /**
  333. * Returns a string representation of this object.
  334. * @method toString
  335. * @return {String} a string representation of the instance.
  336. **/
  337. p.toString = function() {
  338. return "[Event (type="+this.type+")]";
  339. };
  340. createjs.Event = Event;
  341. }());
  342. //##############################################################################
  343. // EventDispatcher.js
  344. //##############################################################################
  345. this.createjs = this.createjs||{};
  346. (function() {
  347. "use strict";
  348. // constructor:
  349. /**
  350. * EventDispatcher provides methods for managing queues of event listeners and dispatching events.
  351. *
  352. * You can either extend EventDispatcher or mix its methods into an existing prototype or instance by using the
  353. * EventDispatcher {{#crossLink "EventDispatcher/initialize"}}{{/crossLink}} method.
  354. *
  355. * Together with the CreateJS Event class, EventDispatcher provides an extended event model that is based on the
  356. * DOM Level 2 event model, including addEventListener, removeEventListener, and dispatchEvent. It supports
  357. * bubbling / capture, preventDefault, stopPropagation, stopImmediatePropagation, and handleEvent.
  358. *
  359. * EventDispatcher also exposes a {{#crossLink "EventDispatcher/on"}}{{/crossLink}} method, which makes it easier
  360. * to create scoped listeners, listeners that only run once, and listeners with associated arbitrary data. The
  361. * {{#crossLink "EventDispatcher/off"}}{{/crossLink}} method is merely an alias to
  362. * {{#crossLink "EventDispatcher/removeEventListener"}}{{/crossLink}}.
  363. *
  364. * Another addition to the DOM Level 2 model is the {{#crossLink "EventDispatcher/removeAllEventListeners"}}{{/crossLink}}
  365. * method, which can be used to listeners for all events, or listeners for a specific event. The Event object also
  366. * includes a {{#crossLink "Event/remove"}}{{/crossLink}} method which removes the active listener.
  367. *
  368. * <h4>Example</h4>
  369. * Add EventDispatcher capabilities to the "MyClass" class.
  370. *
  371. * EventDispatcher.initialize(MyClass.prototype);
  372. *
  373. * Add an event (see {{#crossLink "EventDispatcher/addEventListener"}}{{/crossLink}}).
  374. *
  375. * instance.addEventListener("eventName", handlerMethod);
  376. * function handlerMethod(event) {
  377. * console.log(event.target + " Was Clicked");
  378. * }
  379. *
  380. * <b>Maintaining proper scope</b><br />
  381. * Scope (ie. "this") can be be a challenge with events. Using the {{#crossLink "EventDispatcher/on"}}{{/crossLink}}
  382. * method to subscribe to events simplifies this.
  383. *
  384. * instance.addEventListener("click", function(event) {
  385. * console.log(instance == this); // false, scope is ambiguous.
  386. * });
  387. *
  388. * instance.on("click", function(event) {
  389. * console.log(instance == this); // true, "on" uses dispatcher scope by default.
  390. * });
  391. *
  392. * If you want to use addEventListener instead, you may want to use function.bind() or a similar proxy to manage
  393. * scope.
  394. *
  395. * <b>Browser support</b>
  396. * The event model in CreateJS can be used separately from the suite in any project, however the inheritance model
  397. * requires modern browsers (IE9+).
  398. *
  399. *
  400. * @class EventDispatcher
  401. * @constructor
  402. **/
  403. function EventDispatcher() {
  404. // private properties:
  405. /**
  406. * @protected
  407. * @property _listeners
  408. * @type Object
  409. **/
  410. this._listeners = null;
  411. /**
  412. * @protected
  413. * @property _captureListeners
  414. * @type Object
  415. **/
  416. this._captureListeners = null;
  417. }
  418. var p = EventDispatcher.prototype;
  419. // static public methods:
  420. /**
  421. * Static initializer to mix EventDispatcher methods into a target object or prototype.
  422. *
  423. * EventDispatcher.initialize(MyClass.prototype); // add to the prototype of the class
  424. * EventDispatcher.initialize(myObject); // add to a specific instance
  425. *
  426. * @method initialize
  427. * @static
  428. * @param {Object} target The target object to inject EventDispatcher methods into. This can be an instance or a
  429. * prototype.
  430. **/
  431. EventDispatcher.initialize = function(target) {
  432. target.addEventListener = p.addEventListener;
  433. target.on = p.on;
  434. target.removeEventListener = target.off = p.removeEventListener;
  435. target.removeAllEventListeners = p.removeAllEventListeners;
  436. target.hasEventListener = p.hasEventListener;
  437. target.dispatchEvent = p.dispatchEvent;
  438. target._dispatchEvent = p._dispatchEvent;
  439. target.willTrigger = p.willTrigger;
  440. };
  441. // public methods:
  442. /**
  443. * Adds the specified event listener. Note that adding multiple listeners to the same function will result in
  444. * multiple callbacks getting fired.
  445. *
  446. * <h4>Example</h4>
  447. *
  448. * displayObject.addEventListener("click", handleClick);
  449. * function handleClick(event) {
  450. * // Click happened.
  451. * }
  452. *
  453. * @method addEventListener
  454. * @param {String} type The string type of the event.
  455. * @param {Function | Object} listener An object with a handleEvent method, or a function that will be called when
  456. * the event is dispatched.
  457. * @param {Boolean} [useCapture] For events that bubble, indicates whether to listen for the event in the capture or bubbling/target phase.
  458. * @return {Function | Object} Returns the listener for chaining or assignment.
  459. **/
  460. p.addEventListener = function(type, listener, useCapture) {
  461. var listeners;
  462. if (useCapture) {
  463. listeners = this._captureListeners = this._captureListeners||{};
  464. } else {
  465. listeners = this._listeners = this._listeners||{};
  466. }
  467. var arr = listeners[type];
  468. if (arr) { this.removeEventListener(type, listener, useCapture); }
  469. arr = listeners[type]; // remove may have deleted the array
  470. if (!arr) { listeners[type] = [listener]; }
  471. else { arr.push(listener); }
  472. return listener;
  473. };
  474. /**
  475. * A shortcut method for using addEventListener that makes it easier to specify an execution scope, have a listener
  476. * only run once, associate arbitrary data with the listener, and remove the listener.
  477. *
  478. * This method works by creating an anonymous wrapper function and subscribing it with addEventListener.
  479. * The wrapper function is returned for use with `removeEventListener` (or `off`).
  480. *
  481. * <b>IMPORTANT:</b> To remove a listener added with `on`, you must pass in the returned wrapper function as the listener, or use
  482. * {{#crossLink "Event/remove"}}{{/crossLink}}. Likewise, each time you call `on` a NEW wrapper function is subscribed, so multiple calls
  483. * to `on` with the same params will create multiple listeners.
  484. *
  485. * <h4>Example</h4>
  486. *
  487. * var listener = myBtn.on("click", handleClick, null, false, {count:3});
  488. * function handleClick(evt, data) {
  489. * data.count -= 1;
  490. * console.log(this == myBtn); // true - scope defaults to the dispatcher
  491. * if (data.count == 0) {
  492. * alert("clicked 3 times!");
  493. * myBtn.off("click", listener);
  494. * // alternately: evt.remove();
  495. * }
  496. * }
  497. *
  498. * @method on
  499. * @param {String} type The string type of the event.
  500. * @param {Function | Object} listener An object with a handleEvent method, or a function that will be called when
  501. * the event is dispatched.
  502. * @param {Object} [scope] The scope to execute the listener in. Defaults to the dispatcher/currentTarget for function listeners, and to the listener itself for object listeners (ie. using handleEvent).
  503. * @param {Boolean} [once=false] If true, the listener will remove itself after the first time it is triggered.
  504. * @param {*} [data] Arbitrary data that will be included as the second parameter when the listener is called.
  505. * @param {Boolean} [useCapture=false] For events that bubble, indicates whether to listen for the event in the capture or bubbling/target phase.
  506. * @return {Function} Returns the anonymous function that was created and assigned as the listener. This is needed to remove the listener later using .removeEventListener.
  507. **/
  508. p.on = function(type, listener, scope, once, data, useCapture) {
  509. if (listener.handleEvent) {
  510. scope = scope||listener;
  511. listener = listener.handleEvent;
  512. }
  513. scope = scope||this;
  514. return this.addEventListener(type, function(evt) {
  515. listener.call(scope, evt, data);
  516. once&&evt.remove();
  517. }, useCapture);
  518. };
  519. /**
  520. * Removes the specified event listener.
  521. *
  522. * <b>Important Note:</b> that you must pass the exact function reference used when the event was added. If a proxy
  523. * function, or function closure is used as the callback, the proxy/closure reference must be used - a new proxy or
  524. * closure will not work.
  525. *
  526. * <h4>Example</h4>
  527. *
  528. * displayObject.removeEventListener("click", handleClick);
  529. *
  530. * @method removeEventListener
  531. * @param {String} type The string type of the event.
  532. * @param {Function | Object} listener The listener function or object.
  533. * @param {Boolean} [useCapture] For events that bubble, indicates whether to listen for the event in the capture or bubbling/target phase.
  534. **/
  535. p.removeEventListener = function(type, listener, useCapture) {
  536. var listeners = useCapture ? this._captureListeners : this._listeners;
  537. if (!listeners) { return; }
  538. var arr = listeners[type];
  539. if (!arr) { return; }
  540. for (var i=0,l=arr.length; i<l; i++) {
  541. if (arr[i] == listener) {
  542. if (l==1) { delete(listeners[type]); } // allows for faster checks.
  543. else { arr.splice(i,1); }
  544. break;
  545. }
  546. }
  547. };
  548. /**
  549. * A shortcut to the removeEventListener method, with the same parameters and return value. This is a companion to the
  550. * .on method.
  551. *
  552. * <b>IMPORTANT:</b> To remove a listener added with `on`, you must pass in the returned wrapper function as the listener. See
  553. * {{#crossLink "EventDispatcher/on"}}{{/crossLink}} for an example.
  554. *
  555. * @method off
  556. * @param {String} type The string type of the event.
  557. * @param {Function | Object} listener The listener function or object.
  558. * @param {Boolean} [useCapture] For events that bubble, indicates whether to listen for the event in the capture or bubbling/target phase.
  559. **/
  560. p.off = p.removeEventListener;
  561. /**
  562. * Removes all listeners for the specified type, or all listeners of all types.
  563. *
  564. * <h4>Example</h4>
  565. *
  566. * // Remove all listeners
  567. * displayObject.removeAllEventListeners();
  568. *
  569. * // Remove all click listeners
  570. * displayObject.removeAllEventListeners("click");
  571. *
  572. * @method removeAllEventListeners
  573. * @param {String} [type] The string type of the event. If omitted, all listeners for all types will be removed.
  574. **/
  575. p.removeAllEventListeners = function(type) {
  576. if (!type) { this._listeners = this._captureListeners = null; }
  577. else {
  578. if (this._listeners) { delete(this._listeners[type]); }
  579. if (this._captureListeners) { delete(this._captureListeners[type]); }
  580. }
  581. };
  582. /**
  583. * Dispatches the specified event to all listeners.
  584. *
  585. * <h4>Example</h4>
  586. *
  587. * // Use a string event
  588. * this.dispatchEvent("complete");
  589. *
  590. * // Use an Event instance
  591. * var event = new createjs.Event("progress");
  592. * this.dispatchEvent(event);
  593. *
  594. * @method dispatchEvent
  595. * @param {Object | String | Event} eventObj An object with a "type" property, or a string type.
  596. * While a generic object will work, it is recommended to use a CreateJS Event instance. If a string is used,
  597. * dispatchEvent will construct an Event instance if necessary with the specified type. This latter approach can
  598. * be used to avoid event object instantiation for non-bubbling events that may not have any listeners.
  599. * @param {Boolean} [bubbles] Specifies the `bubbles` value when a string was passed to eventObj.
  600. * @param {Boolean} [cancelable] Specifies the `cancelable` value when a string was passed to eventObj.
  601. * @return {Boolean} Returns false if `preventDefault()` was called on a cancelable event, true otherwise.
  602. **/
  603. p.dispatchEvent = function(eventObj, bubbles, cancelable) {
  604. if (typeof eventObj == "string") {
  605. // skip everything if there's no listeners and it doesn't bubble:
  606. var listeners = this._listeners;
  607. if (!bubbles && (!listeners || !listeners[eventObj])) { return true; }
  608. eventObj = new createjs.Event(eventObj, bubbles, cancelable);
  609. } else if (eventObj.target && eventObj.clone) {
  610. // redispatching an active event object, so clone it:
  611. eventObj = eventObj.clone();
  612. }
  613. // TODO: it would be nice to eliminate this. Maybe in favour of evtObj instanceof Event? Or !!evtObj.createEvent
  614. try { eventObj.target = this; } catch (e) {} // try/catch allows redispatching of native events
  615. if (!eventObj.bubbles || !this.parent) {
  616. this._dispatchEvent(eventObj, 2);
  617. } else {
  618. var top=this, list=[top];
  619. while (top.parent) { list.push(top = top.parent); }
  620. var i, l=list.length;
  621. // capture & atTarget
  622. for (i=l-1; i>=0 && !eventObj.propagationStopped; i--) {
  623. list[i]._dispatchEvent(eventObj, 1+(i==0));
  624. }
  625. // bubbling
  626. for (i=1; i<l && !eventObj.propagationStopped; i++) {
  627. list[i]._dispatchEvent(eventObj, 3);
  628. }
  629. }
  630. return !eventObj.defaultPrevented;
  631. };
  632. /**
  633. * Indicates whether there is at least one listener for the specified event type.
  634. * @method hasEventListener
  635. * @param {String} type The string type of the event.
  636. * @return {Boolean} Returns true if there is at least one listener for the specified event.
  637. **/
  638. p.hasEventListener = function(type) {
  639. var listeners = this._listeners, captureListeners = this._captureListeners;
  640. return !!((listeners && listeners[type]) || (captureListeners && captureListeners[type]));
  641. };
  642. /**
  643. * Indicates whether there is at least one listener for the specified event type on this object or any of its
  644. * ancestors (parent, parent's parent, etc). A return value of true indicates that if a bubbling event of the
  645. * specified type is dispatched from this object, it will trigger at least one listener.
  646. *
  647. * This is similar to {{#crossLink "EventDispatcher/hasEventListener"}}{{/crossLink}}, but it searches the entire
  648. * event flow for a listener, not just this object.
  649. * @method willTrigger
  650. * @param {String} type The string type of the event.
  651. * @return {Boolean} Returns `true` if there is at least one listener for the specified event.
  652. **/
  653. p.willTrigger = function(type) {
  654. var o = this;
  655. while (o) {
  656. if (o.hasEventListener(type)) { return true; }
  657. o = o.parent;
  658. }
  659. return false;
  660. };
  661. /**
  662. * @method toString
  663. * @return {String} a string representation of the instance.
  664. **/
  665. p.toString = function() {
  666. return "[EventDispatcher]";
  667. };
  668. // private methods:
  669. /**
  670. * @method _dispatchEvent
  671. * @param {Object | Event} eventObj
  672. * @param {Object} eventPhase
  673. * @protected
  674. **/
  675. p._dispatchEvent = function(eventObj, eventPhase) {
  676. var l, arr, listeners = (eventPhase <= 2) ? this._captureListeners : this._listeners;
  677. if (eventObj && listeners && (arr = listeners[eventObj.type]) && (l=arr.length)) {
  678. try { eventObj.currentTarget = this; } catch (e) {}
  679. try { eventObj.eventPhase = eventPhase|0; } catch (e) {}
  680. eventObj.removed = false;
  681. arr = arr.slice(); // to avoid issues with items being removed or added during the dispatch
  682. for (var i=0; i<l && !eventObj.immediatePropagationStopped; i++) {
  683. var o = arr[i];
  684. if (o.handleEvent) { o.handleEvent(eventObj); }
  685. else { o(eventObj); }
  686. if (eventObj.removed) {
  687. this.off(eventObj.type, o, eventPhase==1);
  688. eventObj.removed = false;
  689. }
  690. }
  691. }
  692. if (eventPhase === 2) { this._dispatchEvent(eventObj, 2.1); }
  693. };
  694. createjs.EventDispatcher = EventDispatcher;
  695. }());
  696. //##############################################################################
  697. // Ticker.js
  698. //##############################################################################
  699. this.createjs = this.createjs||{};
  700. (function() {
  701. "use strict";
  702. // constructor:
  703. /**
  704. * The Ticker provides a centralized tick or heartbeat broadcast at a set interval. Listeners can subscribe to the tick
  705. * event to be notified when a set time interval has elapsed.
  706. *
  707. * Note that the interval that the tick event is called is a target interval, and may be broadcast at a slower interval
  708. * when under high CPU load. The Ticker class uses a static interface (ex. `Ticker.framerate = 30;`) and
  709. * can not be instantiated.
  710. *
  711. * <h4>Example</h4>
  712. *
  713. * createjs.Ticker.addEventListener("tick", handleTick);
  714. * function handleTick(event) {
  715. * // Actions carried out each tick (aka frame)
  716. * if (!event.paused) {
  717. * // Actions carried out when the Ticker is not paused.
  718. * }
  719. * }
  720. *
  721. * @class Ticker
  722. * @uses EventDispatcher
  723. * @static
  724. **/
  725. function Ticker() {
  726. throw "Ticker cannot be instantiated.";
  727. }
  728. // constants:
  729. /**
  730. * In this mode, Ticker uses the requestAnimationFrame API, but attempts to synch the ticks to target framerate. It
  731. * uses a simple heuristic that compares the time of the RAF return to the target time for the current frame and
  732. * dispatches the tick when the time is within a certain threshold.
  733. *
  734. * This mode has a higher variance for time between frames than {{#crossLink "Ticker/TIMEOUT:property"}}{{/crossLink}},
  735. * but does not require that content be time based as with {{#crossLink "Ticker/RAF:property"}}{{/crossLink}} while
  736. * gaining the benefits of that API (screen synch, background throttling).
  737. *
  738. * Variance is usually lowest for framerates that are a divisor of the RAF frequency. This is usually 60, so
  739. * framerates of 10, 12, 15, 20, and 30 work well.
  740. *
  741. * Falls back to {{#crossLink "Ticker/TIMEOUT:property"}}{{/crossLink}} if the requestAnimationFrame API is not
  742. * supported.
  743. * @property RAF_SYNCHED
  744. * @static
  745. * @type {String}
  746. * @default "synched"
  747. * @readonly
  748. **/
  749. Ticker.RAF_SYNCHED = "synched";
  750. /**
  751. * In this mode, Ticker passes through the requestAnimationFrame heartbeat, ignoring the target framerate completely.
  752. * Because requestAnimationFrame frequency is not deterministic, any content using this mode should be time based.
  753. * You can leverage {{#crossLink "Ticker/getTime"}}{{/crossLink}} and the {{#crossLink "Ticker/tick:event"}}{{/crossLink}}
  754. * event object's "delta" properties to make this easier.
  755. *
  756. * Falls back on {{#crossLink "Ticker/TIMEOUT:property"}}{{/crossLink}} if the requestAnimationFrame API is not
  757. * supported.
  758. * @property RAF
  759. * @static
  760. * @type {String}
  761. * @default "raf"
  762. * @readonly
  763. **/
  764. Ticker.RAF = "raf";
  765. /**
  766. * In this mode, Ticker uses the setTimeout API. This provides predictable, adaptive frame timing, but does not
  767. * provide the benefits of requestAnimationFrame (screen synch, background throttling).
  768. * @property TIMEOUT
  769. * @static
  770. * @type {String}
  771. * @default "timeout"
  772. * @readonly
  773. **/
  774. Ticker.TIMEOUT = "timeout";
  775. // static events:
  776. /**
  777. * Dispatched each tick. The event will be dispatched to each listener even when the Ticker has been paused using
  778. * {{#crossLink "Ticker/paused:property"}}{{/crossLink}}.
  779. *
  780. * <h4>Example</h4>
  781. *
  782. * createjs.Ticker.addEventListener("tick", handleTick);
  783. * function handleTick(event) {
  784. * console.log("Paused:", event.paused, event.delta);
  785. * }
  786. *
  787. * @event tick
  788. * @param {Object} target The object that dispatched the event.
  789. * @param {String} type The event type.
  790. * @param {Boolean} paused Indicates whether the ticker is currently paused.
  791. * @param {Number} delta The time elapsed in ms since the last tick.
  792. * @param {Number} time The total time in ms since Ticker was initialized.
  793. * @param {Number} runTime The total time in ms that Ticker was not paused since it was initialized. For example,
  794. * you could determine the amount of time that the Ticker has been paused since initialization with `time-runTime`.
  795. * @since 0.6.0
  796. */
  797. // public static properties:
  798. /**
  799. * Specifies the timing api (setTimeout or requestAnimationFrame) and mode to use. See
  800. * {{#crossLink "Ticker/TIMEOUT:property"}}{{/crossLink}}, {{#crossLink "Ticker/RAF:property"}}{{/crossLink}}, and
  801. * {{#crossLink "Ticker/RAF_SYNCHED:property"}}{{/crossLink}} for mode details.
  802. * @property timingMode
  803. * @static
  804. * @type {String}
  805. * @default Ticker.TIMEOUT
  806. **/
  807. Ticker.timingMode = null;
  808. /**
  809. * Specifies a maximum value for the delta property in the tick event object. This is useful when building time
  810. * based animations and systems to prevent issues caused by large time gaps caused by background tabs, system sleep,
  811. * alert dialogs, or other blocking routines. Double the expected frame duration is often an effective value
  812. * (ex. maxDelta=50 when running at 40fps).
  813. *
  814. * This does not impact any other values (ex. time, runTime, etc), so you may experience issues if you enable maxDelta
  815. * when using both delta and other values.
  816. *
  817. * If 0, there is no maximum.
  818. * @property maxDelta
  819. * @static
  820. * @type {number}
  821. * @default 0
  822. */
  823. Ticker.maxDelta = 0;
  824. /**
  825. * When the ticker is paused, all listeners will still receive a tick event, but the <code>paused</code> property
  826. * of the event will be `true`. Also, while paused the `runTime` will not increase. See {{#crossLink "Ticker/tick:event"}}{{/crossLink}},
  827. * {{#crossLink "Ticker/getTime"}}{{/crossLink}}, and {{#crossLink "Ticker/getEventTime"}}{{/crossLink}} for more
  828. * info.
  829. *
  830. * <h4>Example</h4>
  831. *
  832. * createjs.Ticker.addEventListener("tick", handleTick);
  833. * createjs.Ticker.paused = true;
  834. * function handleTick(event) {
  835. * console.log(event.paused,
  836. * createjs.Ticker.getTime(false),
  837. * createjs.Ticker.getTime(true));
  838. * }
  839. *
  840. * @property paused
  841. * @static
  842. * @type {Boolean}
  843. * @default false
  844. **/
  845. Ticker.paused = false;
  846. // mix-ins:
  847. // EventDispatcher methods:
  848. Ticker.removeEventListener = null;
  849. Ticker.removeAllEventListeners = null;
  850. Ticker.dispatchEvent = null;
  851. Ticker.hasEventListener = null;
  852. Ticker._listeners = null;
  853. createjs.EventDispatcher.initialize(Ticker); // inject EventDispatcher methods.
  854. Ticker._addEventListener = Ticker.addEventListener;
  855. Ticker.addEventListener = function() {
  856. !Ticker._inited&&Ticker.init();
  857. return Ticker._addEventListener.apply(Ticker, arguments);
  858. };
  859. // private static properties:
  860. /**
  861. * @property _inited
  862. * @static
  863. * @type {Boolean}
  864. * @private
  865. **/
  866. Ticker._inited = false;
  867. /**
  868. * @property _startTime
  869. * @static
  870. * @type {Number}
  871. * @private
  872. **/
  873. Ticker._startTime = 0;
  874. /**
  875. * @property _pausedTime
  876. * @static
  877. * @type {Number}
  878. * @private
  879. **/
  880. Ticker._pausedTime=0;
  881. /**
  882. * The number of ticks that have passed
  883. * @property _ticks
  884. * @static
  885. * @type {Number}
  886. * @private
  887. **/
  888. Ticker._ticks = 0;
  889. /**
  890. * The number of ticks that have passed while Ticker has been paused
  891. * @property _pausedTicks
  892. * @static
  893. * @type {Number}
  894. * @private
  895. **/
  896. Ticker._pausedTicks = 0;
  897. /**
  898. * @property _interval
  899. * @static
  900. * @type {Number}
  901. * @private
  902. **/
  903. Ticker._interval = 50;
  904. /**
  905. * @property _lastTime
  906. * @static
  907. * @type {Number}
  908. * @private
  909. **/
  910. Ticker._lastTime = 0;
  911. /**
  912. * @property _times
  913. * @static
  914. * @type {Array}
  915. * @private
  916. **/
  917. Ticker._times = null;
  918. /**
  919. * @property _tickTimes
  920. * @static
  921. * @type {Array}
  922. * @private
  923. **/
  924. Ticker._tickTimes = null;
  925. /**
  926. * Stores the timeout or requestAnimationFrame id.
  927. * @property _timerId
  928. * @static
  929. * @type {Number}
  930. * @private
  931. **/
  932. Ticker._timerId = null;
  933. /**
  934. * True if currently using requestAnimationFrame, false if using setTimeout. This may be different than timingMode
  935. * if that property changed and a tick hasn't fired.
  936. * @property _raf
  937. * @static
  938. * @type {Boolean}
  939. * @private
  940. **/
  941. Ticker._raf = true;
  942. // static getter / setters:
  943. /**
  944. * Use the {{#crossLink "Ticker/interval:property"}}{{/crossLink}} property instead.
  945. * @method _setInterval
  946. * @private
  947. * @static
  948. * @param {Number} interval
  949. **/
  950. Ticker._setInterval = function(interval) {
  951. Ticker._interval = interval;
  952. if (!Ticker._inited) { return; }
  953. Ticker._setupTick();
  954. };
  955. // Ticker.setInterval is @deprecated. Remove for 1.1+
  956. Ticker.setInterval = createjs.deprecate(Ticker._setInterval, "Ticker.setInterval");
  957. /**
  958. * Use the {{#crossLink "Ticker/interval:property"}}{{/crossLink}} property instead.
  959. * @method _getInterval
  960. * @private
  961. * @static
  962. * @return {Number}
  963. **/
  964. Ticker._getInterval = function() {
  965. return Ticker._interval;
  966. };
  967. // Ticker.getInterval is @deprecated. Remove for 1.1+
  968. Ticker.getInterval = createjs.deprecate(Ticker._getInterval, "Ticker.getInterval");
  969. /**
  970. * Use the {{#crossLink "Ticker/framerate:property"}}{{/crossLink}} property instead.
  971. * @method _setFPS
  972. * @private
  973. * @static
  974. * @param {Number} value
  975. **/
  976. Ticker._setFPS = function(value) {
  977. Ticker._setInterval(1000/value);
  978. };
  979. // Ticker.setFPS is @deprecated. Remove for 1.1+
  980. Ticker.setFPS = createjs.deprecate(Ticker._setFPS, "Ticker.setFPS");
  981. /**
  982. * Use the {{#crossLink "Ticker/framerate:property"}}{{/crossLink}} property instead.
  983. * @method _getFPS
  984. * @static
  985. * @private
  986. * @return {Number}
  987. **/
  988. Ticker._getFPS = function() {
  989. return 1000/Ticker._interval;
  990. };
  991. // Ticker.getFPS is @deprecated. Remove for 1.1+
  992. Ticker.getFPS = createjs.deprecate(Ticker._getFPS, "Ticker.getFPS");
  993. /**
  994. * Indicates the target time (in milliseconds) between ticks. Default is 50 (20 FPS).
  995. * Note that actual time between ticks may be more than specified depending on CPU load.
  996. * This property is ignored if the ticker is using the `RAF` timing mode.
  997. * @property interval
  998. * @static
  999. * @type {Number}
  1000. **/
  1001. /**
  1002. * Indicates the target frame rate in frames per second (FPS). Effectively just a shortcut to `interval`, where
  1003. * `framerate == 1000/interval`.
  1004. * @property framerate
  1005. * @static
  1006. * @type {Number}
  1007. **/
  1008. try {
  1009. Object.defineProperties(Ticker, {
  1010. interval: { get: Ticker._getInterval, set: Ticker._setInterval },
  1011. framerate: { get: Ticker._getFPS, set: Ticker._setFPS }
  1012. });
  1013. } catch (e) { console.log(e); }
  1014. // public static methods:
  1015. /**
  1016. * Starts the tick. This is called automatically when the first listener is added.
  1017. * @method init
  1018. * @static
  1019. **/
  1020. Ticker.init = function() {
  1021. if (Ticker._inited) { return; }
  1022. Ticker._inited = true;
  1023. Ticker._times = [];
  1024. Ticker._tickTimes = [];
  1025. Ticker._startTime = Ticker._getTime();
  1026. Ticker._times.push(Ticker._lastTime = 0);
  1027. Ticker.interval = Ticker._interval;
  1028. };
  1029. /**
  1030. * Stops the Ticker and removes all listeners. Use init() to restart the Ticker.
  1031. * @method reset
  1032. * @static
  1033. **/
  1034. Ticker.reset = function() {
  1035. if (Ticker._raf) {
  1036. var f = window.cancelAnimationFrame || window.webkitCancelAnimationFrame || window.mozCancelAnimationFrame || window.oCancelAnimationFrame || window.msCancelAnimationFrame;
  1037. f&&f(Ticker._timerId);
  1038. } else {
  1039. clearTimeout(Ticker._timerId);
  1040. }
  1041. Ticker.removeAllEventListeners("tick");
  1042. Ticker._timerId = Ticker._times = Ticker._tickTimes = null;
  1043. Ticker._startTime = Ticker._lastTime = Ticker._ticks = Ticker._pausedTime = 0;
  1044. Ticker._inited = false;
  1045. };
  1046. /**
  1047. * Returns the average time spent within a tick. This can vary significantly from the value provided by getMeasuredFPS
  1048. * because it only measures the time spent within the tick execution stack.
  1049. *
  1050. * Example 1: With a target FPS of 20, getMeasuredFPS() returns 20fps, which indicates an average of 50ms between
  1051. * the end of one tick and the end of the next. However, getMeasuredTickTime() returns 15ms. This indicates that
  1052. * there may be up to 35ms of "idle" time between the end of one tick and the start of the next.
  1053. *
  1054. * Example 2: With a target FPS of 30, {{#crossLink "Ticker/framerate:property"}}{{/crossLink}} returns 10fps, which
  1055. * indicates an average of 100ms between the end of one tick and the end of the next. However, {{#crossLink "Ticker/getMeasuredTickTime"}}{{/crossLink}}
  1056. * returns 20ms. This would indicate that something other than the tick is using ~80ms (another script, DOM
  1057. * rendering, etc).
  1058. * @method getMeasuredTickTime
  1059. * @static
  1060. * @param {Number} [ticks] The number of previous ticks over which to measure the average time spent in a tick.
  1061. * Defaults to the number of ticks per second. To get only the last tick's time, pass in 1.
  1062. * @return {Number} The average time spent in a tick in milliseconds.
  1063. **/
  1064. Ticker.getMeasuredTickTime = function(ticks) {
  1065. var ttl=0, times=Ticker._tickTimes;
  1066. if (!times || times.length < 1) { return -1; }
  1067. // by default, calculate average for the past ~1 second:
  1068. ticks = Math.min(times.length, ticks||(Ticker._getFPS()|0));
  1069. for (var i=0; i<ticks; i++) { ttl += times[i]; }
  1070. return ttl/ticks;
  1071. };
  1072. /**
  1073. * Returns the actual frames / ticks per second.
  1074. * @method getMeasuredFPS
  1075. * @static
  1076. * @param {Number} [ticks] The number of previous ticks over which to measure the actual frames / ticks per second.
  1077. * Defaults to the number of ticks per second.
  1078. * @return {Number} The actual frames / ticks per second. Depending on performance, this may differ
  1079. * from the target frames per second.
  1080. **/
  1081. Ticker.getMeasuredFPS = function(ticks) {
  1082. var times = Ticker._times;
  1083. if (!times || times.length < 2) { return -1; }
  1084. // by default, calculate fps for the past ~1 second:
  1085. ticks = Math.min(times.length-1, ticks||(Ticker._getFPS()|0));
  1086. return 1000/((times[0]-times[ticks])/ticks);
  1087. };
  1088. /**
  1089. * Returns the number of milliseconds that have elapsed since Ticker was initialized via {{#crossLink "Ticker/init"}}.
  1090. * Returns -1 if Ticker has not been initialized. For example, you could use
  1091. * this in a time synchronized animation to determine the exact amount of time that has elapsed.
  1092. * @method getTime
  1093. * @static
  1094. * @param {Boolean} [runTime=false] If true only time elapsed while Ticker was not paused will be returned.
  1095. * If false, the value returned will be total time elapsed since the first tick event listener was added.
  1096. * @return {Number} Number of milliseconds that have elapsed since Ticker was initialized or -1.
  1097. **/
  1098. Ticker.getTime = function(runTime) {
  1099. return Ticker._startTime ? Ticker._getTime() - (runTime ? Ticker._pausedTime : 0) : -1;
  1100. };
  1101. /**
  1102. * Similar to the {{#crossLink "Ticker/getTime"}}{{/crossLink}} method, but returns the time on the most recent {{#crossLink "Ticker/tick:event"}}{{/crossLink}}
  1103. * event object.
  1104. * @method getEventTime
  1105. * @static
  1106. * @param runTime {Boolean} [runTime=false] If true, the runTime property will be returned instead of time.
  1107. * @returns {number} The time or runTime property from the most recent tick event or -1.
  1108. */
  1109. Ticker.getEventTime = function(runTime) {
  1110. return Ticker._startTime ? (Ticker._lastTime || Ticker._startTime) - (runTime ? Ticker._pausedTime : 0) : -1;
  1111. };
  1112. /**
  1113. * Returns the number of ticks that have been broadcast by Ticker.
  1114. * @method getTicks
  1115. * @static
  1116. * @param {Boolean} pauseable Indicates whether to include ticks that would have been broadcast
  1117. * while Ticker was paused. If true only tick events broadcast while Ticker is not paused will be returned.
  1118. * If false, tick events that would have been broadcast while Ticker was paused will be included in the return
  1119. * value. The default value is false.
  1120. * @return {Number} of ticks that have been broadcast.
  1121. **/
  1122. Ticker.getTicks = function(pauseable) {
  1123. return Ticker._ticks - (pauseable ? Ticker._pausedTicks : 0);
  1124. };
  1125. // private static methods:
  1126. /**
  1127. * @method _handleSynch
  1128. * @static
  1129. * @private
  1130. **/
  1131. Ticker._handleSynch = function() {
  1132. Ticker._timerId = null;
  1133. Ticker._setupTick();
  1134. // run if enough time has elapsed, with a little bit of flexibility to be early:
  1135. if (Ticker._getTime() - Ticker._lastTime >= (Ticker._interval-1)*0.97) {
  1136. Ticker._tick();
  1137. }
  1138. };
  1139. /**
  1140. * @method _handleRAF
  1141. * @static
  1142. * @private
  1143. **/
  1144. Ticker._handleRAF = function() {
  1145. Ticker._timerId = null;
  1146. Ticker._setupTick();
  1147. Ticker._tick();
  1148. };
  1149. /**
  1150. * @method _handleTimeout
  1151. * @static
  1152. * @private
  1153. **/
  1154. Ticker._handleTimeout = function() {
  1155. Ticker._timerId = null;
  1156. Ticker._setupTick();
  1157. Ticker._tick();
  1158. };
  1159. /**
  1160. * @method _setupTick
  1161. * @static
  1162. * @private
  1163. **/
  1164. Ticker._setupTick = function() {
  1165. if (Ticker._timerId != null) { return; } // avoid duplicates
  1166. var mode = Ticker.timingMode;
  1167. if (mode == Ticker.RAF_SYNCHED || mode == Ticker.RAF) {
  1168. var f = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame;
  1169. if (f) {
  1170. Ticker._timerId = f(mode == Ticker.RAF ? Ticker._handleRAF : Ticker._handleSynch);
  1171. Ticker._raf = true;
  1172. return;
  1173. }
  1174. }
  1175. Ticker._raf = false;
  1176. Ticker._timerId = setTimeout(Ticker._handleTimeout, Ticker._interval);
  1177. };
  1178. /**
  1179. * @method _tick
  1180. * @static
  1181. * @private
  1182. **/
  1183. Ticker._tick = function() {
  1184. var paused = Ticker.paused;
  1185. var time = Ticker._getTime();
  1186. var elapsedTime = time-Ticker._lastTime;
  1187. Ticker._lastTime = time;
  1188. Ticker._ticks++;
  1189. if (paused) {
  1190. Ticker._pausedTicks++;
  1191. Ticker._pausedTime += elapsedTime;
  1192. }
  1193. if (Ticker.hasEventListener("tick")) {
  1194. var event = new createjs.Event("tick");
  1195. var maxDelta = Ticker.maxDelta;
  1196. event.delta = (maxDelta && elapsedTime > maxDelta) ? maxDelta : elapsedTime;
  1197. event.paused = paused;
  1198. event.time = time;
  1199. event.runTime = time-Ticker._pausedTime;
  1200. Ticker.dispatchEvent(event);
  1201. }
  1202. Ticker._tickTimes.unshift(Ticker._getTime()-time);
  1203. while (Ticker._tickTimes.length > 100) { Ticker._tickTimes.pop(); }
  1204. Ticker._times.unshift(time);
  1205. while (Ticker._times.length > 100) { Ticker._times.pop(); }
  1206. };
  1207. /**
  1208. * @method _getTime
  1209. * @static
  1210. * @private
  1211. **/
  1212. var w=window, now=w.performance.now || w.performance.mozNow || w.performance.msNow || w.performance.oNow || w.performance.webkitNow;
  1213. Ticker._getTime = function() {
  1214. return ((now&&now.call(w.performance))||(new Date().getTime())) - Ticker._startTime;
  1215. };
  1216. createjs.Ticker = Ticker;
  1217. }());
  1218. //##############################################################################
  1219. // AbstractTween.js
  1220. //##############################################################################
  1221. this.createjs = this.createjs||{};
  1222. (function() {
  1223. "use strict";
  1224. // constructor
  1225. /**
  1226. * Base class that both {{#crossLink "Tween"}}{{/crossLink}} and {{#crossLink "Timeline"}}{{/crossLink}} extend. Should not be instantiated directly.
  1227. * @class AbstractTween
  1228. * @param {Object} [props] The configuration properties to apply to this instance (ex. `{loop:-1, paused:true}`).
  1229. * Supported props are listed below. These props are set on the corresponding instance properties except where
  1230. * specified.
  1231. * @param {boolean} [props.useTicks=false] See the {{#crossLink "AbstractTween/useTicks:property"}}{{/crossLink}} property for more information.
  1232. * @param {boolean} [props.ignoreGlobalPause=false] See the {{#crossLink "AbstractTween/ignoreGlobalPause:property"}}{{/crossLink}} for more information.
  1233. * @param {number|boolean} [props.loop=0] See the {{#crossLink "AbstractTween/loop:property"}}{{/crossLink}} for more information.
  1234. * @param {boolean} [props.reversed=false] See the {{#crossLink "AbstractTween/reversed:property"}}{{/crossLink}} for more information.
  1235. * @param {boolean} [props.bounce=false] See the {{#crossLink "AbstractTween/bounce:property"}}{{/crossLink}} for more information.
  1236. * @param {number} [props.timeScale=1] See the {{#crossLink "AbstractTween/timeScale:property"}}{{/crossLink}} for more information.
  1237. * @param {Function} [props.onChange] Adds the specified function as a listener to the {{#crossLink "AbstractTween/change:event"}}{{/crossLink}} event
  1238. * @param {Function} [props.onComplete] Adds the specified function as a listener to the {{#crossLink "AbstractTween/complete:event"}}{{/crossLink}} event
  1239. * @extends EventDispatcher
  1240. * @constructor
  1241. */
  1242. function AbstractTween(props) {
  1243. this.EventDispatcher_constructor();
  1244. // public properties:
  1245. /**
  1246. * Causes this tween to continue playing when a global pause is active. For example, if TweenJS is using {{#crossLink "Ticker"}}{{/crossLink}},
  1247. * then setting this to false (the default) will cause this tween to be paused when `Ticker.paused` is set to
  1248. * `true`. See the {{#crossLink "Tween/tick"}}{{/crossLink}} method for more info. Can be set via the `props`
  1249. * parameter.
  1250. * @property ignoreGlobalPause
  1251. * @type Boolean
  1252. * @default false
  1253. */
  1254. this.ignoreGlobalPause = false;
  1255. /**
  1256. * Indicates the number of times to loop. If set to -1, the tween will loop continuously.
  1257. *
  1258. * Note that a tween must loop at _least_ once to see it play in both directions when `{{#crossLink "AbstractTween/bounce:property"}}{{/crossLink}}`
  1259. * is set to `true`.
  1260. * @property loop
  1261. * @type {Number}
  1262. * @default 0
  1263. */
  1264. this.loop = 0;
  1265. /**
  1266. * Uses ticks for all durations instead of milliseconds. This also changes the behaviour of some actions (such as `call`).
  1267. * Changing this value on a running tween could have unexpected results.
  1268. * @property useTicks
  1269. * @type {Boolean}
  1270. * @default false
  1271. * @readonly
  1272. */
  1273. this.useTicks = false;
  1274. /**
  1275. * Causes the tween to play in reverse.
  1276. * @property reversed
  1277. * @type {Boolean}
  1278. * @default false
  1279. */
  1280. this.reversed = false;
  1281. /**
  1282. * Causes the tween to reverse direction at the end of each loop. Each single-direction play-through of the
  1283. * tween counts as a single bounce. For example, to play a tween once forward, and once back, set the
  1284. * `{{#crossLink "AbstractTween/loop:property"}}{{/crossLink}}` to `1`.
  1285. * @property bounce
  1286. * @type {Boolean}
  1287. * @default false
  1288. */
  1289. this.bounce = false;
  1290. /**
  1291. * Changes the rate at which the tween advances. For example, a `timeScale` value of `2` will double the
  1292. * playback speed, a value of `0.5` would halve it.
  1293. * @property timeScale
  1294. * @type {Number}
  1295. * @default 1
  1296. */
  1297. this.timeScale = 1;
  1298. /**
  1299. * Indicates the duration of this tween in milliseconds (or ticks if `useTicks` is true), irrespective of `loops`.
  1300. * This value is automatically updated as you modify the tween. Changing it directly could result in unexpected
  1301. * behaviour.
  1302. * @property duration
  1303. * @type {Number}
  1304. * @default 0
  1305. * @readonly
  1306. */
  1307. this.duration = 0;
  1308. /**
  1309. * The current normalized position of the tween. This will always be a value between 0 and `duration`.
  1310. * Changing this property directly will have unexpected results, use {{#crossLink "Tween/setPosition"}}{{/crossLink}}.
  1311. * @property position
  1312. * @type {Object}
  1313. * @default 0
  1314. * @readonly
  1315. */
  1316. this.position = 0;
  1317. /**
  1318. * The raw tween position. This value will be between `0` and `loops * duration` while the tween is active, or -1 before it activates.
  1319. * @property rawPosition
  1320. * @type {Number}
  1321. * @default -1
  1322. * @readonly
  1323. */
  1324. this.rawPosition = -1;
  1325. // private properties:
  1326. /**
  1327. * @property _paused
  1328. * @type {Boolean}
  1329. * @default false
  1330. * @protected
  1331. */
  1332. this._paused = true;
  1333. /**
  1334. * @property _next
  1335. * @type {Tween}
  1336. * @default null
  1337. * @protected
  1338. */
  1339. this._next = null;
  1340. /**
  1341. * @property _prev
  1342. * @type {Tween}
  1343. * @default null
  1344. * @protected
  1345. */
  1346. this._prev = null;
  1347. /**
  1348. * @property _parent
  1349. * @type {Object}
  1350. * @default null
  1351. * @protected
  1352. */
  1353. this._parent = null;
  1354. /**
  1355. * @property _labels
  1356. * @type Object
  1357. * @protected
  1358. **/
  1359. this._labels = null;
  1360. /**
  1361. * @property _labelList
  1362. * @type Array[Object]
  1363. * @protected
  1364. **/
  1365. this._labelList = null;
  1366. if (props) {
  1367. this.useTicks = !!props.useTicks;
  1368. this.ignoreGlobalPause = !!props.ignoreGlobalPause;
  1369. this.loop = props.loop === true ? -1 : (props.loop||0);
  1370. this.reversed = !!props.reversed;
  1371. this.bounce = !!props.bounce;
  1372. this.timeScale = props.timeScale||1;
  1373. props.onChange && this.addEventListener("change", props.onChange);
  1374. props.onComplete && this.addEventListener("complete", props.onComplete);
  1375. }
  1376. // while `position` is shared, it needs to happen after ALL props are set, so it's handled in _init()
  1377. };
  1378. var p = createjs.extend(AbstractTween, createjs.EventDispatcher);
  1379. // events:
  1380. /**
  1381. * Dispatched whenever the tween's position changes. It occurs after all tweened properties are updated and actions
  1382. * are executed.
  1383. * @event change
  1384. **/
  1385. /**
  1386. * Dispatched when the tween reaches its end and has paused itself. This does not fire until all loops are complete;
  1387. * tweens that loop continuously will never fire a complete event.
  1388. * @event complete
  1389. **/
  1390. // getter / setters:
  1391. /**
  1392. * Use the {{#crossLink "AbstractTween/paused:property"}}{{/crossLink}} property instead.
  1393. * @method _setPaused
  1394. * @param {Boolean} [value=true] Indicates whether the tween should be paused (`true`) or played (`false`).
  1395. * @return {AbstractTween} This tween instance (for chaining calls)
  1396. * @protected
  1397. * @chainable
  1398. */
  1399. p._setPaused = function(value) {
  1400. createjs.Tween._register(this, value);
  1401. return this;
  1402. };
  1403. p.setPaused = createjs.deprecate(p._setPaused, "AbstractTween.setPaused");
  1404. /**
  1405. * Use the {{#crossLink "AbstractTween/paused:property"}}{{/crossLink}} property instead.
  1406. * @method _getPaused
  1407. * @protected
  1408. */
  1409. p._getPaused = function() {
  1410. return this._paused;
  1411. };
  1412. p.getPaused = createjs.deprecate(p._getPaused, "AbstactTween.getPaused");
  1413. /**
  1414. * Use the {{#crossLink "AbstractTween/currentLabel:property"}}{{/crossLink}} property instead.
  1415. * @method _getCurrentLabel
  1416. * @protected
  1417. * @return {String} The name of the current label or null if there is no label
  1418. **/
  1419. p._getCurrentLabel = function(pos) {
  1420. var labels = this.getLabels();
  1421. if (pos == null) { pos = this.position; }
  1422. for (var i = 0, l = labels.length; i<l; i++) { if (pos < labels[i].position) { break; } }
  1423. return (i===0) ? null : labels[i-1].label;
  1424. };
  1425. p.getCurrentLabel = createjs.deprecate(p._getCurrentLabel, "AbstractTween.getCurrentLabel");
  1426. /**
  1427. * Pauses or unpauses the tween. A paused tween is removed from the global registry and is eligible for garbage
  1428. * collection if no other references to it exist.
  1429. * @property paused
  1430. * @type Boolean
  1431. * @readonly
  1432. **/
  1433. /**
  1434. * Returns the name of the label on or immediately before the current position. For example, given a tween with
  1435. * two labels, "first" on frame index 4, and "second" on frame 8, `currentLabel` would return:
  1436. * <UL>
  1437. * <LI>null if the current position is 2.</LI>
  1438. * <LI>"first" if the current position is 4.</LI>
  1439. * <LI>"first" if the current position is 7.</LI>
  1440. * <LI>"second" if the current position is 15.</LI>
  1441. * </UL>
  1442. * @property currentLabel
  1443. * @type String
  1444. * @readonly
  1445. **/
  1446. try {
  1447. Object.defineProperties(p, {
  1448. paused: { set: p._setPaused, get: p._getPaused },
  1449. currentLabel: { get: p._getCurrentLabel }
  1450. });
  1451. } catch (e) {}
  1452. // public methods:
  1453. /**
  1454. * Advances the tween by a specified amount.
  1455. * @method advance
  1456. * @param {Number} delta The amount to advance in milliseconds (or ticks if useTicks is true). Negative values are supported.
  1457. * @param {Number} [ignoreActions=false] If true, actions will not be executed due to this change in position.
  1458. */
  1459. p.advance = function(delta, ignoreActions) {
  1460. this.setPosition(this.rawPosition+delta*this.timeScale, ignoreActions);
  1461. };
  1462. /**
  1463. * Advances the tween to a specified position.
  1464. * @method setPosition
  1465. * @param {Number} rawPosition The raw position to seek to in milliseconds (or ticks if useTicks is true).
  1466. * @param {Boolean} [ignoreActions=false] If true, do not run any actions that would be triggered by this operation.
  1467. * @param {Boolean} [jump=false] If true, only actions at the new position will be run. If false, actions between the old and new position are run.
  1468. * @param {Function} [callback] Primarily for use with MovieClip, this callback is called after properties are updated, but before actions are run.
  1469. */
  1470. p.setPosition = function(rawPosition, ignoreActions, jump, callback) {
  1471. var d=this.duration, loopCount=this.loop, prevRawPos = this.rawPosition;
  1472. var loop=0, t=0, end=false;
  1473. // normalize position:
  1474. if (rawPosition < 0) { rawPosition = 0; }
  1475. if (d === 0) {
  1476. // deal with 0 length tweens.
  1477. end = true;
  1478. if (prevRawPos !== -1) { return end; } // we can avoid doing anything else if we're already at 0.
  1479. } else {
  1480. loop = rawPosition/d|0;
  1481. t = rawPosition-loop*d;
  1482. end = (loopCount !== -1 && rawPosition >= loopCount*d+d);
  1483. if (end) { rawPosition = (t=d)*(loop=loopCount)+d; }
  1484. if (rawPosition === prevRawPos) { return end; } // no need to update
  1485. var rev = !this.reversed !== !(this.bounce && loop%2); // current loop is reversed
  1486. if (rev) { t = d-t; }
  1487. }
  1488. // set this in advance in case an action modifies position:
  1489. this.position = t;
  1490. this.rawPosition = rawPosition;
  1491. this._updatePosition(jump, end);
  1492. if (end) { this.paused = true; }
  1493. callback&&callback(this);
  1494. if (!ignoreActions) { this._runActions(prevRawPos, rawPosition, jump, !jump && prevRawPos === -1); }
  1495. this.dispatchEvent("change");
  1496. if (end) { this.dispatchEvent("complete"); }
  1497. };
  1498. /**
  1499. * Calculates a normalized position based on a raw position. For example, given a tween with a duration of 3000ms set to loop:
  1500. * console.log(myTween.calculatePosition(3700); // 700
  1501. * @method calculatePosition
  1502. * @param {Number} rawPosition A raw position.
  1503. */
  1504. p.calculatePosition = function(rawPosition) {
  1505. // largely duplicated from setPosition, but necessary to avoid having to instantiate generic objects to pass values (end, loop, position) back.
  1506. var d=this.duration, loopCount=this.loop, loop=0, t=0;
  1507. if (d===0) { return 0; }
  1508. if (loopCount !== -1 && rawPosition >= loopCount*d+d) { t = d; loop = loopCount } // end
  1509. else if (rawPosition < 0) { t = 0; }
  1510. else { loop = rawPosition/d|0; t = rawPosition-loop*d; }
  1511. var rev = !this.reversed !== !(this.bounce && loop%2); // current loop is reversed
  1512. return rev ? d-t : t;
  1513. };
  1514. /**
  1515. * Returns a list of the labels defined on this tween sorted by position.
  1516. * @method getLabels
  1517. * @return {Array[Object]} A sorted array of objects with label and position properties.
  1518. **/
  1519. p.getLabels = function() {
  1520. var list = this._labelList;
  1521. if (!list) {
  1522. list = this._labelList = [];
  1523. var labels = this._labels;
  1524. for (var n in labels) {
  1525. list.push({label:n, position:labels[n]});
  1526. }
  1527. list.sort(function (a,b) { return a.position- b.position; });
  1528. }
  1529. return list;
  1530. };
  1531. /**
  1532. * Defines labels for use with gotoAndPlay/Stop. Overwrites any previously set labels.
  1533. * @method setLabels
  1534. * @param {Object} labels An object defining labels for using {{#crossLink "Timeline/gotoAndPlay"}}{{/crossLink}}/{{#crossLink "Timeline/gotoAndStop"}}{{/crossLink}}
  1535. * in the form `{myLabelName:time}` where time is in milliseconds (or ticks if `useTicks` is `true`).
  1536. **/
  1537. p.setLabels = function(labels) {
  1538. this._labels = labels;
  1539. this._labelList = null;
  1540. };
  1541. /**
  1542. * Adds a label that can be used with {{#crossLink "Timeline/gotoAndPlay"}}{{/crossLink}}/{{#crossLink "Timeline/gotoAndStop"}}{{/crossLink}}.
  1543. * @method addLabel
  1544. * @param {String} label The label name.
  1545. * @param {Number} position The position this label represents.
  1546. **/
  1547. p.addLabel = function(label, position) {
  1548. if (!this._labels) { this._labels = {}; }
  1549. this._labels[label] = position;
  1550. var list = this._labelList;
  1551. if (list) {
  1552. for (var i= 0,l=list.length; i<l; i++) { if (position < list[i].position) { break; } }
  1553. list.splice(i, 0, {label:label, position:position});
  1554. }
  1555. };
  1556. /**
  1557. * Unpauses this timeline and jumps to the specified position or label.
  1558. * @method gotoAndPlay
  1559. * @param {String|Number} positionOrLabel The position in milliseconds (or ticks if `useTicks` is `true`)
  1560. * or label to jump to.
  1561. **/
  1562. p.gotoAndPlay = function(positionOrLabel) {
  1563. this.paused = false;
  1564. this._goto(positionOrLabel);
  1565. };
  1566. /**
  1567. * Pauses this timeline and jumps to the specified position or label.
  1568. * @method gotoAndStop
  1569. * @param {String|Number} positionOrLabel The position in milliseconds (or ticks if `useTicks` is `true`) or label
  1570. * to jump to.
  1571. **/
  1572. p.gotoAndStop = function(positionOrLabel) {
  1573. this.paused = true;
  1574. this._goto(positionOrLabel);
  1575. };
  1576. /**
  1577. * If a numeric position is passed, it is returned unchanged. If a string is passed, the position of the
  1578. * corresponding frame label will be returned, or `null` if a matching label is not defined.
  1579. * @method resolve
  1580. * @param {String|Number} positionOrLabel A numeric position value or label string.
  1581. **/
  1582. p.resolve = function(positionOrLabel) {
  1583. var pos = Number(positionOrLabel);
  1584. if (isNaN(pos)) { pos = this._labels && this._labels[positionOrLabel]; }
  1585. return pos;
  1586. };
  1587. /**
  1588. * Returns a string representation of this object.
  1589. * @method toString
  1590. * @return {String} a string representation of the instance.
  1591. */
  1592. p.toString = function() {
  1593. return "[AbstractTween]";
  1594. };
  1595. /**
  1596. * @method clone
  1597. * @protected
  1598. */
  1599. p.clone = function() {
  1600. throw("AbstractTween can not be cloned.")
  1601. };
  1602. // private methods:
  1603. /**
  1604. * Shared logic that executes at the end of the subclass constructor.
  1605. * @method _init
  1606. * @protected
  1607. */
  1608. p._init = function(props) {
  1609. if (!props || !props.paused) { this.paused = false; }
  1610. if (props&&(props.position!=null)) { this.setPosition(props.position); }
  1611. };
  1612. /**
  1613. * @method _updatePosition
  1614. * @protected
  1615. */
  1616. p._updatePosition = function(jump, end) {
  1617. // abstract.
  1618. };
  1619. /**
  1620. * @method _goto
  1621. * @protected
  1622. **/
  1623. p._goto = function(positionOrLabel) {
  1624. var pos = this.resolve(positionOrLabel);
  1625. if (pos != null) { this.setPosition(pos, false, true); }
  1626. };
  1627. /**
  1628. * @method _runActions
  1629. * @protected
  1630. */
  1631. p._runActions = function(startRawPos, endRawPos, jump, includeStart) {
  1632. // runs actions between startPos & endPos. Separated to support action deferral.
  1633. //console.log(this.passive === false ? " > Tween" : "Timeline", "run", startRawPos, endRawPos, jump, includeStart);
  1634. // if we don't have any actions, and we're not a Timeline, then return:
  1635. // TODO: a cleaner way to handle this would be to override this method in Tween, but I'm not sure it's worth the overhead.
  1636. if (!this._actionHead && !this.tweens) { return; }
  1637. var d=this.duration, reversed=this.reversed, bounce=this.bounce, loopCount=this.loop;
  1638. var loop0, loop1, t0, t1;
  1639. if (d === 0) {
  1640. // deal with 0 length tweens:
  1641. loop0 = loop1 = t0 = t1 = 0;
  1642. reversed = bounce = false;
  1643. } else {
  1644. loop0=startRawPos/d|0;
  1645. loop1=endRawPos/d|0;
  1646. t0=startRawPos-loop0*d;
  1647. t1=endRawPos-loop1*d;
  1648. }
  1649. // catch positions that are past the end:
  1650. if (loopCount !== -1) {
  1651. if (loop1 > loopCount) { t1=d; loop1=loopCount; }
  1652. if (loop0 > loopCount) { t0=d; loop0=loopCount; }
  1653. }
  1654. // special cases:
  1655. if (jump) { return this._runActionsRange(t1, t1, jump, includeStart); } // jump.
  1656. else if (loop0 === loop1 && t0 === t1 && !jump && !includeStart) { return; } // no actions if the position is identical and we aren't including the start
  1657. else if (loop0 === -1) { loop0 = t0 = 0; } // correct the -1 value for first advance, important with useTicks.
  1658. var dir = (startRawPos <= endRawPos), loop = loop0;
  1659. do {
  1660. var rev = !reversed !== !(bounce && loop % 2);
  1661. var start = (loop === loop0) ? t0 : dir ? 0 : d;
  1662. var end = (loop === loop1) ? t1 : dir ? d : 0;
  1663. if (rev) {
  1664. start = d - start;
  1665. end = d - end;
  1666. }
  1667. if (bounce && loop !== loop0 && start === end) { /* bounced onto the same time/frame, don't re-execute end actions */ }
  1668. else if (this._runActionsRange(start, end, jump, includeStart || (loop !== loop0 && !bounce))) { return true; }
  1669. includeStart = false;
  1670. } while ((dir && ++loop <= loop1) || (!dir && --loop >= loop1));
  1671. };
  1672. p._runActionsRange = function(startPos, endPos, jump, includeStart) {
  1673. // abstract
  1674. };
  1675. createjs.AbstractTween = createjs.promote(AbstractTween, "EventDispatcher");
  1676. }());
  1677. //##############################################################################
  1678. // Tween.js
  1679. //##############################################################################
  1680. this.createjs = this.createjs||{};
  1681. (function() {
  1682. "use strict";
  1683. // constructor
  1684. /**
  1685. * Tweens properties for a single target. Methods can be chained to create complex animation sequences:
  1686. *
  1687. * <h4>Example</h4>
  1688. *
  1689. * createjs.Tween.get(target)
  1690. * .wait(500)
  1691. * .to({alpha:0, visible:false}, 1000)
  1692. * .call(handleComplete);
  1693. *
  1694. * Multiple tweens can share a target, however if they affect the same properties there could be unexpected
  1695. * behaviour. To stop all tweens on an object, use {{#crossLink "Tween/removeTweens"}}{{/crossLink}} or pass `override:true`
  1696. * in the props argument.
  1697. *
  1698. * createjs.Tween.get(target, {override:true}).to({x:100});
  1699. *
  1700. * Subscribe to the {{#crossLink "Tween/change:event"}}{{/crossLink}} event to be notified when the tween position changes.
  1701. *
  1702. * createjs.Tween.get(target, {override:true}).to({x:100}).addEventListener("change", handleChange);
  1703. * function handleChange(event) {
  1704. * // The tween changed.
  1705. * }
  1706. *
  1707. * See the {{#crossLink "Tween/get"}}{{/crossLink}} method also.
  1708. * @class Tween
  1709. * @param {Object} target The target object that will have its properties tweened.
  1710. * @param {Object} [props] The configuration properties to apply to this instance (ex. `{loop:-1, paused:true}`).
  1711. * Supported props are listed below. These props are set on the corresponding instance properties except where
  1712. * specified.
  1713. * @param {boolean} [props.useTicks=false] See the {{#crossLink "AbstractTween/useTicks:property"}}{{/crossLink}} property for more information.
  1714. * @param {boolean} [props.ignoreGlobalPause=false] See the {{#crossLink "AbstractTween/ignoreGlobalPause:property"}}{{/crossLink}} for more information.
  1715. * @param {number|boolean} [props.loop=0] See the {{#crossLink "AbstractTween/loop:property"}}{{/crossLink}} for more information.
  1716. * @param {boolean} [props.reversed=false] See the {{#crossLink "AbstractTween/reversed:property"}}{{/crossLink}} for more information.
  1717. * @param {boolean} [props.bounce=false] See the {{#crossLink "AbstractTween/bounce:property"}}{{/crossLink}} for more information.
  1718. * @param {number} [props.timeScale=1] See the {{#crossLink "AbstractTween/timeScale:property"}}{{/crossLink}} for more information.
  1719. * @param {object} [props.pluginData] See the {{#crossLink "Tween/pluginData:property"}}{{/crossLink}} for more information.
  1720. * @param {boolean} [props.paused=false] See the {{#crossLink "AbstractTween/paused:property"}}{{/crossLink}} for more information.
  1721. * @param {number} [props.position=0] The initial position for this tween. See {{#crossLink "AbstractTween/position:property"}}{{/crossLink}}
  1722. * @param {Function} [props.onChange] Adds the specified function as a listener to the {{#crossLink "AbstractTween/change:event"}}{{/crossLink}} event
  1723. * @param {Function} [props.onComplete] Adds the specified function as a listener to the {{#crossLink "AbstractTween/complete:event"}}{{/crossLink}} event
  1724. * @param {boolean} [props.override=false] Removes all existing tweens for the target when set to `true`.
  1725. * </UL>
  1726. * @extends AbstractTween
  1727. * @constructor
  1728. */
  1729. function Tween(target, props) {
  1730. this.AbstractTween_constructor(props);
  1731. // public properties:
  1732. /**
  1733. * Allows you to specify data that will be used by installed plugins. Each plugin uses this differently, but in general
  1734. * you specify data by assigning it to a property of `pluginData` with the same name as the plugin.
  1735. * Note that in many cases, this data is used as soon as the plugin initializes itself for the tween.
  1736. * As such, this data should be set before the first `to` call in most cases.
  1737. * @example
  1738. * myTween.pluginData.SmartRotation = data;
  1739. *
  1740. * Most plugins also support a property to disable them for a specific tween. This is typically the plugin name followed by "_disabled".
  1741. * @example
  1742. * myTween.pluginData.SmartRotation_disabled = true;
  1743. *
  1744. * Some plugins also store working data in this object, usually in a property named `_PluginClassName`.
  1745. * See the documentation for individual plugins for more details.
  1746. * @property pluginData
  1747. * @type {Object}
  1748. */
  1749. this.pluginData = null;
  1750. /**
  1751. * The target of this tween. This is the object on which the tweened properties will be changed.
  1752. * @property target
  1753. * @type {Object}
  1754. * @readonly
  1755. */
  1756. this.target = target;
  1757. /**
  1758. * Indicates the tween's current position is within a passive wait.
  1759. * @property passive
  1760. * @type {Boolean}
  1761. * @default false
  1762. * @readonly
  1763. **/
  1764. this.passive = false;
  1765. // private properties:
  1766. /**
  1767. * @property _stepHead
  1768. * @type {TweenStep}
  1769. * @protected
  1770. */
  1771. this._stepHead = new TweenStep(null, 0, 0, {}, null, true);
  1772. /**
  1773. * @property _stepTail
  1774. * @type {TweenStep}
  1775. * @protected
  1776. */
  1777. this._stepTail = this._stepHead;
  1778. /**
  1779. * The position within the current step. Used by MovieClip.
  1780. * @property _stepPosition
  1781. * @type {Number}
  1782. * @default 0
  1783. * @protected
  1784. */
  1785. this._stepPosition = 0;
  1786. /**
  1787. * @property _actionHead
  1788. * @type {TweenAction}
  1789. * @protected
  1790. */
  1791. this._actionHead = null;
  1792. /**
  1793. * @property _actionTail
  1794. * @type {TweenAction}
  1795. * @protected
  1796. */
  1797. this._actionTail = null;
  1798. /**
  1799. * Plugins added to this tween instance.
  1800. * @property _plugins
  1801. * @type Array[Object]
  1802. * @default null
  1803. * @protected
  1804. */
  1805. this._plugins = null;
  1806. /**
  1807. * Hash for quickly looking up added plugins. Null until a plugin is added.
  1808. * @property _plugins
  1809. * @type Object
  1810. * @default null
  1811. * @protected
  1812. */
  1813. this._pluginIds = null;
  1814. /**
  1815. * Used by plugins to inject new properties.
  1816. * @property _injected
  1817. * @type {Object}
  1818. * @default null
  1819. * @protected
  1820. */
  1821. this._injected = null;
  1822. if (props) {
  1823. this.pluginData = props.pluginData;
  1824. if (props.override) { Tween.removeTweens(target); }
  1825. }
  1826. if (!this.pluginData) { this.pluginData = {}; }
  1827. this._init(props);
  1828. };
  1829. var p = createjs.extend(Tween, createjs.AbstractTween);
  1830. // static properties
  1831. /**
  1832. * Constant returned by plugins to tell the tween not to use default assignment.
  1833. * @property IGNORE
  1834. * @type Object
  1835. * @static
  1836. */
  1837. Tween.IGNORE = {};
  1838. /**
  1839. * @property _listeners
  1840. * @type Array[Tween]
  1841. * @static
  1842. * @protected
  1843. */
  1844. Tween._tweens = [];
  1845. /**
  1846. * @property _plugins
  1847. * @type Object
  1848. * @static
  1849. * @protected
  1850. */
  1851. Tween._plugins = null;
  1852. /**
  1853. * @property _tweenHead
  1854. * @type Tween
  1855. * @static
  1856. * @protected
  1857. */
  1858. Tween._tweenHead = null;
  1859. /**
  1860. * @property _tweenTail
  1861. * @type Tween
  1862. * @static
  1863. * @protected
  1864. */
  1865. Tween._tweenTail = null;
  1866. // static methods
  1867. /**
  1868. * Returns a new tween instance. This is functionally identical to using `new Tween(...)`, but may look cleaner
  1869. * with the chained syntax of TweenJS.
  1870. * <h4>Example</h4>
  1871. *
  1872. * var tween = createjs.Tween.get(target).to({x:100}, 500);
  1873. * // equivalent to:
  1874. * var tween = new createjs.Tween(target).to({x:100}, 500);
  1875. *
  1876. * @method get
  1877. * @param {Object} target The target object that will have its properties tweened.
  1878. * @param {Object} [props] The configuration properties to apply to this instance (ex. `{loop:-1, paused:true}`).
  1879. * Supported props are listed below. These props are set on the corresponding instance properties except where
  1880. * specified.
  1881. * @param {boolean} [props.useTicks=false] See the {{#crossLink "AbstractTween/useTicks:property"}}{{/crossLink}} property for more information.
  1882. * @param {boolean} [props.ignoreGlobalPause=false] See the {{#crossLink "AbstractTween/ignoreGlobalPause:property"}}{{/crossLink}} for more information.
  1883. * @param {number|boolean} [props.loop=0] See the {{#crossLink "AbstractTween/loop:property"}}{{/crossLink}} for more information.
  1884. * @param {boolean} [props.reversed=false] See the {{#crossLink "AbstractTween/reversed:property"}}{{/crossLink}} for more information.
  1885. * @param {boolean} [props.bounce=false] See the {{#crossLink "AbstractTween/bounce:property"}}{{/crossLink}} for more information.
  1886. * @param {number} [props.timeScale=1] See the {{#crossLink "AbstractTween/timeScale:property"}}{{/crossLink}} for more information.
  1887. * @param {object} [props.pluginData] See the {{#crossLink "Tween/pluginData:property"}}{{/crossLink}} for more information.
  1888. * @param {boolean} [props.paused=false] See the {{#crossLink "AbstractTween/paused:property"}}{{/crossLink}} for more information.
  1889. * @param {number} [props.position=0] The initial position for this tween. See {{#crossLink "AbstractTween/position:property"}}{{/crossLink}}
  1890. * @param {Function} [props.onChange] Adds the specified function as a listener to the {{#crossLink "AbstractTween/change:event"}}{{/crossLink}} event
  1891. * @param {Function} [props.onComplete] Adds the specified function as a listener to the {{#crossLink "AbstractTween/complete:event"}}{{/crossLink}} event
  1892. * @param {boolean} [props.override=false] Removes all existing tweens for the target when set to `true`.
  1893. * @return {Tween} A reference to the created tween.
  1894. * @static
  1895. */
  1896. Tween.get = function(target, props) {
  1897. return new Tween(target, props);
  1898. };
  1899. /**
  1900. * Advances all tweens. This typically uses the {{#crossLink "Ticker"}}{{/crossLink}} class, but you can call it
  1901. * manually if you prefer to use your own "heartbeat" implementation.
  1902. * @method tick
  1903. * @param {Number} delta The change in time in milliseconds since the last tick. Required unless all tweens have
  1904. * `useTicks` set to true.
  1905. * @param {Boolean} paused Indicates whether a global pause is in effect. Tweens with {{#crossLink "Tween/ignoreGlobalPause:property"}}{{/crossLink}}
  1906. * will ignore this, but all others will pause if this is `true`.
  1907. * @static
  1908. */
  1909. Tween.tick = function(delta, paused) {
  1910. var tween = Tween._tweenHead;
  1911. while (tween) {
  1912. var next = tween._next; // in case it completes and wipes its _next property
  1913. if ((paused && !tween.ignoreGlobalPause) || tween._paused) { /* paused */ }
  1914. else { tween.advance(tween.useTicks?1:delta); }
  1915. tween = next;
  1916. }
  1917. };
  1918. /**
  1919. * Handle events that result from Tween being used as an event handler. This is included to allow Tween to handle
  1920. * {{#crossLink "Ticker/tick:event"}}{{/crossLink}} events from the createjs {{#crossLink "Ticker"}}{{/crossLink}}.
  1921. * No other events are handled in Tween.
  1922. * @method handleEvent
  1923. * @param {Object} event An event object passed in by the {{#crossLink "EventDispatcher"}}{{/crossLink}}. Will
  1924. * usually be of type "tick".
  1925. * @private
  1926. * @static
  1927. * @since 0.4.2
  1928. */
  1929. Tween.handleEvent = function(event) {
  1930. if (event.type === "tick") {
  1931. this.tick(event.delta, event.paused);
  1932. }
  1933. };
  1934. /**
  1935. * Removes all existing tweens for a target. This is called automatically by new tweens if the `override`
  1936. * property is `true`.
  1937. * @method removeTweens
  1938. * @param {Object} target The target object to remove existing tweens from.
  1939. * @static
  1940. */
  1941. Tween.removeTweens = function(target) {
  1942. if (!target.tweenjs_count) { return; }
  1943. var tween = Tween._tweenHead;
  1944. while (tween) {
  1945. var next = tween._next;
  1946. if (tween.target === target) { Tween._register(tween, true); }
  1947. tween = next;
  1948. }
  1949. target.tweenjs_count = 0;
  1950. };
  1951. /**
  1952. * Stop and remove all existing tweens.
  1953. * @method removeAllTweens
  1954. * @static
  1955. * @since 0.4.1
  1956. */
  1957. Tween.removeAllTweens = function() {
  1958. var tween = Tween._tweenHead;
  1959. while (tween) {
  1960. var next = tween._next;
  1961. tween._paused = true;
  1962. tween.target&&(tween.target.tweenjs_count = 0);
  1963. tween._next = tween._prev = null;
  1964. tween = next;
  1965. }
  1966. Tween._tweenHead = Tween._tweenTail = null;
  1967. };
  1968. /**
  1969. * Indicates whether there are any active tweens on the target object (if specified) or in general.
  1970. * @method hasActiveTweens
  1971. * @param {Object} [target] The target to check for active tweens. If not specified, the return value will indicate
  1972. * if there are any active tweens on any target.
  1973. * @return {Boolean} Indicates if there are active tweens.
  1974. * @static
  1975. */
  1976. Tween.hasActiveTweens = function(target) {
  1977. if (target) { return !!target.tweenjs_count; }
  1978. return !!Tween._tweenHead;
  1979. };
  1980. /**
  1981. * Installs a plugin, which can modify how certain properties are handled when tweened. See the {{#crossLink "SamplePlugin"}}{{/crossLink}}
  1982. * for an example of how to write TweenJS plugins. Plugins should generally be installed via their own `install` method, in order to provide
  1983. * the plugin with an opportunity to configure itself.
  1984. * @method _installPlugin
  1985. * @param {Object} plugin The plugin to install
  1986. * @static
  1987. * @protected
  1988. */
  1989. Tween._installPlugin = function(plugin) {
  1990. var priority = (plugin.priority = plugin.priority||0), arr = (Tween._plugins = Tween._plugins || []);
  1991. for (var i=0,l=arr.length;i<l;i++) {
  1992. if (priority < arr[i].priority) { break; }
  1993. }
  1994. arr.splice(i,0,plugin);
  1995. };
  1996. /**
  1997. * Registers or unregisters a tween with the ticking system.
  1998. * @method _register
  1999. * @param {Tween} tween The tween instance to register or unregister.
  2000. * @param {Boolean} paused If `false`, the tween is registered. If `true` the tween is unregistered.
  2001. * @static
  2002. * @protected
  2003. */
  2004. Tween._register = function(tween, paused) {
  2005. var target = tween.target;
  2006. if (!paused && tween._paused) {
  2007. // TODO: this approach might fail if a dev is using sealed objects
  2008. if (target) { target.tweenjs_count = target.tweenjs_count ? target.tweenjs_count+1 : 1; }
  2009. var tail = Tween._tweenTail;
  2010. if (!tail) { Tween._tweenHead = Tween._tweenTail = tween; }
  2011. else {
  2012. Tween._tweenTail = tail._next = tween;
  2013. tween._prev = tail;
  2014. }
  2015. if (!Tween._inited && createjs.Ticker) { createjs.Ticker.addEventListener("tick", Tween); Tween._inited = true; }
  2016. } else if (paused && !tween._paused) {
  2017. if (target) { target.tweenjs_count--; }
  2018. var next = tween._next, prev = tween._prev;
  2019. if (next) { next._prev = prev; }
  2020. else { Tween._tweenTail = prev; } // was tail
  2021. if (prev) { prev._next = next; }
  2022. else { Tween._tweenHead = next; } // was head.
  2023. tween._next = tween._prev = null;
  2024. }
  2025. tween._paused = paused;
  2026. };
  2027. // events:
  2028. // public methods:
  2029. /**
  2030. * Adds a wait (essentially an empty tween).
  2031. * <h4>Example</h4>
  2032. *
  2033. * //This tween will wait 1s before alpha is faded to 0.
  2034. * createjs.Tween.get(target).wait(1000).to({alpha:0}, 1000);
  2035. *
  2036. * @method wait
  2037. * @param {Number} duration The duration of the wait in milliseconds (or in ticks if `useTicks` is true).
  2038. * @param {Boolean} [passive=false] Tween properties will not be updated during a passive wait. This
  2039. * is mostly useful for use with {{#crossLink "Timeline"}}{{/crossLink}} instances that contain multiple tweens
  2040. * affecting the same target at different times.
  2041. * @return {Tween} This tween instance (for chaining calls).
  2042. * @chainable
  2043. **/
  2044. p.wait = function(duration, passive) {
  2045. if (duration > 0) { this._addStep(+duration, this._stepTail.props, null, passive); }
  2046. return this;
  2047. };
  2048. /**
  2049. * Adds a tween from the current values to the specified properties. Set duration to 0 to jump to these value.
  2050. * Numeric properties will be tweened from their current value in the tween to the target value. Non-numeric
  2051. * properties will be set at the end of the specified duration.
  2052. * <h4>Example</h4>
  2053. *
  2054. * createjs.Tween.get(target).to({alpha:0, visible:false}, 1000);
  2055. *
  2056. * @method to
  2057. * @param {Object} props An object specifying property target values for this tween (Ex. `{x:300}` would tween the x
  2058. * property of the target to 300).
  2059. * @param {Number} [duration=0] The duration of the tween in milliseconds (or in ticks if `useTicks` is true).
  2060. * @param {Function} [ease="linear"] The easing function to use for this tween. See the {{#crossLink "Ease"}}{{/crossLink}}
  2061. * class for a list of built-in ease functions.
  2062. * @return {Tween} This tween instance (for chaining calls).
  2063. * @chainable
  2064. */
  2065. p.to = function(props, duration, ease) {
  2066. if (duration == null || duration < 0) { duration = 0; }
  2067. var step = this._addStep(+duration, null, ease);
  2068. this._appendProps(props, step);
  2069. return this;
  2070. };
  2071. /**
  2072. * Adds a label that can be used with {{#crossLink "Tween/gotoAndPlay"}}{{/crossLink}}/{{#crossLink "Tween/gotoAndStop"}}{{/crossLink}}
  2073. * at the current point in the tween. For example:
  2074. *
  2075. * var tween = createjs.Tween.get(foo)
  2076. * .to({x:100}, 1000)
  2077. * .label("myLabel")
  2078. * .to({x:200}, 1000);
  2079. * // ...
  2080. * tween.gotoAndPlay("myLabel"); // would play from 1000ms in.
  2081. *
  2082. * @method addLabel
  2083. * @param {String} label The label name.
  2084. * @return {Tween} This tween instance (for chaining calls).
  2085. * @chainable
  2086. **/
  2087. p.label = function(name) {
  2088. this.addLabel(name, this.duration);
  2089. return this;
  2090. };
  2091. /**
  2092. * Adds an action to call the specified function.
  2093. * <h4>Example</h4>
  2094. *
  2095. * //would call myFunction() after 1 second.
  2096. * createjs.Tween.get().wait(1000).call(myFunction);
  2097. *
  2098. * @method call
  2099. * @param {Function} callback The function to call.
  2100. * @param {Array} [params]. The parameters to call the function with. If this is omitted, then the function
  2101. * will be called with a single param pointing to this tween.
  2102. * @param {Object} [scope]. The scope to call the function in. If omitted, it will be called in the target's scope.
  2103. * @return {Tween} This tween instance (for chaining calls).
  2104. * @chainable
  2105. */
  2106. p.call = function(callback, params, scope) {
  2107. return this._addAction(scope||this.target, callback, params||[this]);
  2108. };
  2109. /**
  2110. * Adds an action to set the specified props on the specified target. If `target` is null, it will use this tween's
  2111. * target. Note that for properties on the target object, you should consider using a zero duration {{#crossLink "Tween/to"}}{{/crossLink}}
  2112. * operation instead so the values are registered as tweened props.
  2113. * <h4>Example</h4>
  2114. *
  2115. * myTween.wait(1000).set({visible:false}, foo);
  2116. *
  2117. * @method set
  2118. * @param {Object} props The properties to set (ex. `{visible:false}`).
  2119. * @param {Object} [target] The target to set the properties on. If omitted, they will be set on the tween's target.
  2120. * @return {Tween} This tween instance (for chaining calls).
  2121. * @chainable
  2122. */
  2123. p.set = function(props, target) {
  2124. return this._addAction(target||this.target, this._set, [props]);
  2125. };
  2126. /**
  2127. * Adds an action to play (unpause) the specified tween. This enables you to sequence multiple tweens.
  2128. * <h4>Example</h4>
  2129. *
  2130. * myTween.to({x:100}, 500).play(otherTween);
  2131. *
  2132. * @method play
  2133. * @param {Tween} [tween] The tween to play. Defaults to this tween.
  2134. * @return {Tween} This tween instance (for chaining calls).
  2135. * @chainable
  2136. */
  2137. p.play = function(tween) {
  2138. return this._addAction(tween||this, this._set, [{paused:false}]);
  2139. };
  2140. /**
  2141. * Adds an action to pause the specified tween.
  2142. *
  2143. * myTween.pause(otherTween).to({alpha:1}, 1000).play(otherTween);
  2144. *
  2145. * Note that this executes at the end of a tween update, so the tween may advance beyond the time the pause
  2146. * action was inserted at. For example:
  2147. *
  2148. * myTween.to({foo:0}, 1000).pause().to({foo:1}, 1000);
  2149. *
  2150. * At 60fps the tween will advance by ~16ms per tick, if the tween above was at 999ms prior to the current tick, it
  2151. * will advance to 1015ms (15ms into the second "step") and then pause.
  2152. *
  2153. * @method pause
  2154. * @param {Tween} [tween] The tween to pause. Defaults to this tween.
  2155. * @return {Tween} This tween instance (for chaining calls)
  2156. * @chainable
  2157. */
  2158. p.pause = function(tween) {
  2159. return this._addAction(tween||this, this._set, [{paused:true}]);
  2160. };
  2161. // tiny api (primarily for tool output):
  2162. p.w = p.wait;
  2163. p.t = p.to;
  2164. p.c = p.call;
  2165. p.s = p.set;
  2166. /**
  2167. * Returns a string representation of this object.
  2168. * @method toString
  2169. * @return {String} a string representation of the instance.
  2170. */
  2171. p.toString = function() {
  2172. return "[Tween]";
  2173. };
  2174. /**
  2175. * @method clone
  2176. * @protected
  2177. */
  2178. p.clone = function() {
  2179. throw("Tween can not be cloned.")
  2180. };
  2181. // private methods:
  2182. /**
  2183. * Adds a plugin to this tween.
  2184. * @method _addPlugin
  2185. * @param {Object} plugin
  2186. * @protected
  2187. */
  2188. p._addPlugin = function(plugin) {
  2189. var ids = this._pluginIds || (this._pluginIds = {}), id = plugin.ID;
  2190. if (!id || ids[id]) { return; } // already added
  2191. ids[id] = true;
  2192. var plugins = this._plugins || (this._plugins = []), priority = plugin.priority || 0;
  2193. for (var i=0,l=plugins.length; i<l; i++) {
  2194. if (priority < plugins[i].priority) {
  2195. plugins.splice(i,0,plugin);
  2196. return;
  2197. }
  2198. }
  2199. plugins.push(plugin);
  2200. };
  2201. // Docced in AbstractTween
  2202. p._updatePosition = function(jump, end) {
  2203. var step = this._stepHead.next, t=this.position, d=this.duration;
  2204. if (this.target && step) {
  2205. // find our new step index:
  2206. var stepNext = step.next;
  2207. while (stepNext && stepNext.t <= t) { step = step.next; stepNext = step.next; }
  2208. var ratio = end ? d === 0 ? 1 : t/d : (t-step.t)/step.d; // TODO: revisit this.
  2209. this._updateTargetProps(step, ratio, end);
  2210. }
  2211. this._stepPosition = step ? t-step.t : 0;
  2212. };
  2213. /**
  2214. * @method _updateTargetProps
  2215. * @param {Object} step
  2216. * @param {Number} ratio
  2217. * @param {Boolean} end Indicates to plugins that the full tween has ended.
  2218. * @protected
  2219. */
  2220. p._updateTargetProps = function(step, ratio, end) {
  2221. if (this.passive = !!step.passive) { return; } // don't update props.
  2222. var v, v0, v1, ease;
  2223. var p0 = step.prev.props;
  2224. var p1 = step.props;
  2225. if (ease = step.ease) { ratio = ease(ratio,0,1,1); }
  2226. var plugins = this._plugins;
  2227. proploop : for (var n in p0) {
  2228. v0 = p0[n];
  2229. v1 = p1[n];
  2230. // values are different & it is numeric then interpolate:
  2231. if (v0 !== v1 && (typeof(v0) === "number")) {
  2232. v = v0+(v1-v0)*ratio;
  2233. } else {
  2234. v = ratio >= 1 ? v1 : v0;
  2235. }
  2236. if (plugins) {
  2237. for (var i=0,l=plugins.length;i<l;i++) {
  2238. var value = plugins[i].change(this, step, n, v, ratio, end);
  2239. if (value === Tween.IGNORE) { continue proploop; }
  2240. if (value !== undefined) { v = value; }
  2241. }
  2242. }
  2243. this.target[n] = v;
  2244. }
  2245. };
  2246. /**
  2247. * @method _runActionsRange
  2248. * @param {Number} startPos
  2249. * @param {Number} endPos
  2250. * @param {Boolean} includeStart
  2251. * @protected
  2252. */
  2253. p._runActionsRange = function(startPos, endPos, jump, includeStart) {
  2254. var rev = startPos > endPos;
  2255. var action = rev ? this._actionTail : this._actionHead;
  2256. var ePos = endPos, sPos = startPos;
  2257. if (rev) { ePos=startPos; sPos=endPos; }
  2258. var t = this.position;
  2259. while (action) {
  2260. var pos = action.t;
  2261. if (pos === endPos || (pos > sPos && pos < ePos) || (includeStart && pos === startPos)) {
  2262. action.funct.apply(action.scope, action.params);
  2263. if (t !== this.position) { return true; }
  2264. }
  2265. action = rev ? action.prev : action.next;
  2266. }
  2267. };
  2268. /**
  2269. * @method _appendProps
  2270. * @param {Object} props
  2271. * @protected
  2272. */
  2273. p._appendProps = function(props, step, stepPlugins) {
  2274. var initProps = this._stepHead.props, target = this.target, plugins = Tween._plugins;
  2275. var n, i, value, initValue, inject;
  2276. var oldStep = step.prev, oldProps = oldStep.props;
  2277. var stepProps = step.props || (step.props = this._cloneProps(oldProps));
  2278. var cleanProps = {}; // TODO: is there some way to avoid this additional object?
  2279. for (n in props) {
  2280. if (!props.hasOwnProperty(n)) { continue; }
  2281. cleanProps[n] = stepProps[n] = props[n];
  2282. if (initProps[n] !== undefined) { continue; }
  2283. initValue = undefined; // accessing missing properties on DOMElements when using CSSPlugin is INSANELY expensive, so we let the plugin take a first swing at it.
  2284. if (plugins) {
  2285. for (i = plugins.length-1; i >= 0; i--) {
  2286. value = plugins[i].init(this, n, initValue);
  2287. if (value !== undefined) { initValue = value; }
  2288. if (initValue === Tween.IGNORE) {
  2289. delete(stepProps[n]);
  2290. delete(cleanProps[n]);
  2291. break;
  2292. }
  2293. }
  2294. }
  2295. if (initValue !== Tween.IGNORE) {
  2296. if (initValue === undefined) { initValue = target[n]; }
  2297. oldProps[n] = (initValue === undefined) ? null : initValue;
  2298. }
  2299. }
  2300. for (n in cleanProps) {
  2301. value = props[n];
  2302. // propagate old value to previous steps:
  2303. var o, prev=oldStep;
  2304. while ((o = prev) && (prev = o.prev)) {
  2305. if (prev.props === o.props) { continue; } // wait step
  2306. if (prev.props[n] !== undefined) { break; } // already has a value, we're done.
  2307. prev.props[n] = oldProps[n];
  2308. }
  2309. }
  2310. if (stepPlugins !== false && (plugins = this._plugins)) {
  2311. for (i = plugins.length-1; i >= 0; i--) {
  2312. plugins[i].step(this, step, cleanProps);
  2313. }
  2314. }
  2315. if (inject = this._injected) {
  2316. this._injected = null;
  2317. this._appendProps(inject, step, false);
  2318. }
  2319. };
  2320. /**
  2321. * Used by plugins to inject properties onto the current step. Called from within `Plugin.step` calls.
  2322. * For example, a plugin dealing with color, could read a hex color, and inject red, green, and blue props into the tween.
  2323. * See the SamplePlugin for more info.
  2324. * @method _injectProp
  2325. * @param {String} name
  2326. * @param {Object} value
  2327. * @protected
  2328. */
  2329. p._injectProp = function(name, value) {
  2330. var o = this._injected || (this._injected = {});
  2331. o[name] = value;
  2332. };
  2333. /**
  2334. * @method _addStep
  2335. * @param {Number} duration
  2336. * @param {Object} props
  2337. * @param {Function} ease
  2338. * @param {Boolean} passive
  2339. * @protected
  2340. */
  2341. p._addStep = function(duration, props, ease, passive) {
  2342. var step = new TweenStep(this._stepTail, this.duration, duration, props, ease, passive||false);
  2343. this.duration += duration;
  2344. return this._stepTail = (this._stepTail.next = step);
  2345. };
  2346. /**
  2347. * @method _addAction
  2348. * @param {Object} scope
  2349. * @param {Function} funct
  2350. * @param {Array} params
  2351. * @protected
  2352. */
  2353. p._addAction = function(scope, funct, params) {
  2354. var action = new TweenAction(this._actionTail, this.duration, scope, funct, params);
  2355. if (this._actionTail) { this._actionTail.next = action; }
  2356. else { this._actionHead = action; }
  2357. this._actionTail = action;
  2358. return this;
  2359. };
  2360. /**
  2361. * @method _set
  2362. * @param {Object} props
  2363. * @protected
  2364. */
  2365. p._set = function(props) {
  2366. for (var n in props) {
  2367. this[n] = props[n];
  2368. }
  2369. };
  2370. /**
  2371. * @method _cloneProps
  2372. * @param {Object} props
  2373. * @protected
  2374. */
  2375. p._cloneProps = function(props) {
  2376. var o = {};
  2377. for (var n in props) { o[n] = props[n]; }
  2378. return o;
  2379. };
  2380. createjs.Tween = createjs.promote(Tween, "AbstractTween");
  2381. function TweenStep(prev, t, d, props, ease, passive) {
  2382. this.next = null;
  2383. this.prev = prev;
  2384. this.t = t;
  2385. this.d = d;
  2386. this.props = props;
  2387. this.ease = ease;
  2388. this.passive = passive;
  2389. this.index = prev ? prev.index+1 : 0;
  2390. };
  2391. function TweenAction(prev, t, scope, funct, params) {
  2392. this.next = null;
  2393. this.prev = prev;
  2394. this.t = t;
  2395. this.d = 0;
  2396. this.scope = scope;
  2397. this.funct = funct;
  2398. this.params = params;
  2399. };
  2400. }());
  2401. //##############################################################################
  2402. // Timeline.js
  2403. //##############################################################################
  2404. this.createjs = this.createjs||{};
  2405. (function() {
  2406. "use strict";
  2407. // constructor
  2408. /**
  2409. * The Timeline class synchronizes multiple tweens and allows them to be controlled as a group. Please note that if a
  2410. * timeline is looping, the tweens on it may appear to loop even if the "loop" property of the tween is false.
  2411. *
  2412. * NOTE: Timeline currently also accepts a param list in the form: `tweens, labels, props`. This is for backwards
  2413. * compatibility only and will be removed in the future. Include tweens and labels as properties on the props object.
  2414. * @class Timeline
  2415. * @param {Object} [props] The configuration properties to apply to this instance (ex. `{loop:-1, paused:true}`).
  2416. * Supported props are listed below. These props are set on the corresponding instance properties except where
  2417. * specified.<UL>
  2418. * <LI> `useTicks`</LI>
  2419. * <LI> `ignoreGlobalPause`</LI>
  2420. * <LI> `loop`</LI>
  2421. * <LI> `reversed`</LI>
  2422. * <LI> `bounce`</LI>
  2423. * <LI> `timeScale`</LI>
  2424. * <LI> `paused`</LI>
  2425. * <LI> `position`: indicates the initial position for this tween.</LI>
  2426. * <LI> `onChange`: adds the specified function as a listener to the `change` event</LI>
  2427. * <LI> `onComplete`: adds the specified function as a listener to the `complete` event</LI>
  2428. * </UL>
  2429. * @extends AbstractTween
  2430. * @constructor
  2431. **/
  2432. function Timeline(props) {
  2433. var tweens, labels;
  2434. // handle old params (tweens, labels, props):
  2435. // TODO: deprecated.
  2436. if (props instanceof Array || (props == null && arguments.length > 1)) {
  2437. tweens = props;
  2438. labels = arguments[1];
  2439. props = arguments[2];
  2440. } else if (props) {
  2441. tweens = props.tweens;
  2442. labels = props.labels;
  2443. }
  2444. this.AbstractTween_constructor(props);
  2445. // private properties:
  2446. /**
  2447. * The array of tweens in the timeline. It is *strongly* recommended that you use
  2448. * {{#crossLink "Tween/addTween"}}{{/crossLink}} and {{#crossLink "Tween/removeTween"}}{{/crossLink}},
  2449. * rather than accessing this directly, but it is included for advanced uses.
  2450. * @property tweens
  2451. * @type Array
  2452. **/
  2453. this.tweens = [];
  2454. if (tweens) { this.addTween.apply(this, tweens); }
  2455. this.setLabels(labels);
  2456. this._init(props);
  2457. };
  2458. var p = createjs.extend(Timeline, createjs.AbstractTween);
  2459. // events:
  2460. // docced in AbstractTween.
  2461. // public methods:
  2462. /**
  2463. * Adds one or more tweens (or timelines) to this timeline. The tweens will be paused (to remove them from the
  2464. * normal ticking system) and managed by this timeline. Adding a tween to multiple timelines will result in
  2465. * unexpected behaviour.
  2466. * @method addTween
  2467. * @param {Tween} ...tween The tween(s) to add. Accepts multiple arguments.
  2468. * @return {Tween} The first tween that was passed in.
  2469. **/
  2470. p.addTween = function(tween) {
  2471. if (tween._parent) { tween._parent.removeTween(tween); }
  2472. var l = arguments.length;
  2473. if (l > 1) {
  2474. for (var i=0; i<l; i++) { this.addTween(arguments[i]); }
  2475. return arguments[l-1];
  2476. } else if (l === 0) { return null; }
  2477. this.tweens.push(tween);
  2478. tween._parent = this;
  2479. tween.paused = true;
  2480. var d = tween.duration;
  2481. if (tween.loop > 0) { d *= tween.loop+1; }
  2482. if (d > this.duration) { this.duration = d; }
  2483. if (this.rawPosition >= 0) { tween.setPosition(this.rawPosition); }
  2484. return tween;
  2485. };
  2486. /**
  2487. * Removes one or more tweens from this timeline.
  2488. * @method removeTween
  2489. * @param {Tween} ...tween The tween(s) to remove. Accepts multiple arguments.
  2490. * @return Boolean Returns `true` if all of the tweens were successfully removed.
  2491. **/
  2492. p.removeTween = function(tween) {
  2493. var l = arguments.length;
  2494. if (l > 1) {
  2495. var good = true;
  2496. for (var i=0; i<l; i++) { good = good && this.removeTween(arguments[i]); }
  2497. return good;
  2498. } else if (l === 0) { return true; }
  2499. var tweens = this.tweens;
  2500. var i = tweens.length;
  2501. while (i--) {
  2502. if (tweens[i] === tween) {
  2503. tweens.splice(i, 1);
  2504. tween._parent = null;
  2505. if (tween.duration >= this.duration) { this.updateDuration(); }
  2506. return true;
  2507. }
  2508. }
  2509. return false;
  2510. };
  2511. /**
  2512. * Recalculates the duration of the timeline. The duration is automatically updated when tweens are added or removed,
  2513. * but this method is useful if you modify a tween after it was added to the timeline.
  2514. * @method updateDuration
  2515. **/
  2516. p.updateDuration = function() {
  2517. this.duration = 0;
  2518. for (var i=0,l=this.tweens.length; i<l; i++) {
  2519. var tween = this.tweens[i];
  2520. var d = tween.duration;
  2521. if (tween.loop > 0) { d *= tween.loop+1; }
  2522. if (d > this.duration) { this.duration = d; }
  2523. }
  2524. };
  2525. /**
  2526. * Returns a string representation of this object.
  2527. * @method toString
  2528. * @return {String} a string representation of the instance.
  2529. **/
  2530. p.toString = function() {
  2531. return "[Timeline]";
  2532. };
  2533. /**
  2534. * @method clone
  2535. * @protected
  2536. **/
  2537. p.clone = function() {
  2538. throw("Timeline can not be cloned.")
  2539. };
  2540. // private methods:
  2541. // Docced in AbstractTween
  2542. p._updatePosition = function(jump, end) {
  2543. var t = this.position;
  2544. for (var i=0, l=this.tweens.length; i<l; i++) {
  2545. this.tweens[i].setPosition(t, true, jump); // actions will run after all the tweens update.
  2546. }
  2547. };
  2548. // Docced in AbstractTween
  2549. p._runActionsRange = function(startPos, endPos, jump, includeStart) {
  2550. //console.log(" range", startPos, endPos, jump, includeStart);
  2551. var t = this.position;
  2552. for (var i=0, l=this.tweens.length; i<l; i++) {
  2553. this.tweens[i]._runActions(startPos, endPos, jump, includeStart);
  2554. if (t !== this.position) { return true; } // an action changed this timeline's position.
  2555. }
  2556. };
  2557. createjs.Timeline = createjs.promote(Timeline, "AbstractTween");
  2558. }());
  2559. //##############################################################################
  2560. // Ease.js
  2561. //##############################################################################
  2562. this.createjs = this.createjs||{};
  2563. (function() {
  2564. "use strict";
  2565. /**
  2566. * The Ease class provides a collection of easing functions for use with TweenJS. It does not use the standard 4 param
  2567. * easing signature. Instead it uses a single param which indicates the current linear ratio (0 to 1) of the tween.
  2568. *
  2569. * Most methods on Ease can be passed directly as easing functions:
  2570. *
  2571. * Tween.get(target).to({x:100}, 500, Ease.linear);
  2572. *
  2573. * However, methods beginning with "get" will return an easing function based on parameter values:
  2574. *
  2575. * Tween.get(target).to({y:200}, 500, Ease.getPowIn(2.2));
  2576. *
  2577. * Please see the <a href="http://www.createjs.com/Demos/TweenJS/Tween_SparkTable">spark table demo</a> for an
  2578. * overview of the different ease types on <a href="http://tweenjs.com">TweenJS.com</a>.
  2579. *
  2580. * <em>Equations derived from work by Robert Penner.</em>
  2581. * @class Ease
  2582. * @static
  2583. **/
  2584. function Ease() {
  2585. throw "Ease cannot be instantiated.";
  2586. }
  2587. // static methods and properties
  2588. /**
  2589. * @method linear
  2590. * @param {Number} t
  2591. * @static
  2592. * @return {Number}
  2593. **/
  2594. Ease.linear = function(t) { return t; };
  2595. /**
  2596. * Identical to linear.
  2597. * @method none
  2598. * @param {Number} t
  2599. * @static
  2600. * @return {Number}
  2601. **/
  2602. Ease.none = Ease.linear;
  2603. /**
  2604. * Mimics the simple -100 to 100 easing in Adobe Flash/Animate.
  2605. * @method get
  2606. * @param {Number} amount A value from -1 (ease in) to 1 (ease out) indicating the strength and direction of the ease.
  2607. * @static
  2608. * @return {Function}
  2609. **/
  2610. Ease.get = function(amount) {
  2611. if (amount < -1) { amount = -1; }
  2612. else if (amount > 1) { amount = 1; }
  2613. return function(t) {
  2614. if (amount==0) { return t; }
  2615. if (amount<0) { return t*(t*-amount+1+amount); }
  2616. return t*((2-t)*amount+(1-amount));
  2617. };
  2618. };
  2619. /**
  2620. * Configurable exponential ease.
  2621. * @method getPowIn
  2622. * @param {Number} pow The exponent to use (ex. 3 would return a cubic ease).
  2623. * @static
  2624. * @return {Function}
  2625. **/
  2626. Ease.getPowIn = function(pow) {
  2627. return function(t) {
  2628. return Math.pow(t,pow);
  2629. };
  2630. };
  2631. /**
  2632. * Configurable exponential ease.
  2633. * @method getPowOut
  2634. * @param {Number} pow The exponent to use (ex. 3 would return a cubic ease).
  2635. * @static
  2636. * @return {Function}
  2637. **/
  2638. Ease.getPowOut = function(pow) {
  2639. return function(t) {
  2640. return 1-Math.pow(1-t,pow);
  2641. };
  2642. };
  2643. /**
  2644. * Configurable exponential ease.
  2645. * @method getPowInOut
  2646. * @param {Number} pow The exponent to use (ex. 3 would return a cubic ease).
  2647. * @static
  2648. * @return {Function}
  2649. **/
  2650. Ease.getPowInOut = function(pow) {
  2651. return function(t) {
  2652. if ((t*=2)<1) return 0.5*Math.pow(t,pow);
  2653. return 1-0.5*Math.abs(Math.pow(2-t,pow));
  2654. };
  2655. };
  2656. /**
  2657. * @method quadIn
  2658. * @param {Number} t
  2659. * @static
  2660. * @return {Number}
  2661. **/
  2662. Ease.quadIn = Ease.getPowIn(2);
  2663. /**
  2664. * @method quadOut
  2665. * @param {Number} t
  2666. * @static
  2667. * @return {Number}
  2668. **/
  2669. Ease.quadOut = Ease.getPowOut(2);
  2670. /**
  2671. * @method quadInOut
  2672. * @param {Number} t
  2673. * @static
  2674. * @return {Number}
  2675. **/
  2676. Ease.quadInOut = Ease.getPowInOut(2);
  2677. /**
  2678. * @method cubicIn
  2679. * @param {Number} t
  2680. * @static
  2681. * @return {Number}
  2682. **/
  2683. Ease.cubicIn = Ease.getPowIn(3);
  2684. /**
  2685. * @method cubicOut
  2686. * @param {Number} t
  2687. * @static
  2688. * @return {Number}
  2689. **/
  2690. Ease.cubicOut = Ease.getPowOut(3);
  2691. /**
  2692. * @method cubicInOut
  2693. * @param {Number} t
  2694. * @static
  2695. * @return {Number}
  2696. **/
  2697. Ease.cubicInOut = Ease.getPowInOut(3);
  2698. /**
  2699. * @method quartIn
  2700. * @param {Number} t
  2701. * @static
  2702. * @return {Number}
  2703. **/
  2704. Ease.quartIn = Ease.getPowIn(4);
  2705. /**
  2706. * @method quartOut
  2707. * @param {Number} t
  2708. * @static
  2709. * @return {Number}
  2710. **/
  2711. Ease.quartOut = Ease.getPowOut(4);
  2712. /**
  2713. * @method quartInOut
  2714. * @param {Number} t
  2715. * @static
  2716. * @return {Number}
  2717. **/
  2718. Ease.quartInOut = Ease.getPowInOut(4);
  2719. /**
  2720. * @method quintIn
  2721. * @param {Number} t
  2722. * @static
  2723. * @return {Number}
  2724. **/
  2725. Ease.quintIn = Ease.getPowIn(5);
  2726. /**
  2727. * @method quintOut
  2728. * @param {Number} t
  2729. * @static
  2730. * @return {Number}
  2731. **/
  2732. Ease.quintOut = Ease.getPowOut(5);
  2733. /**
  2734. * @method quintInOut
  2735. * @param {Number} t
  2736. * @static
  2737. * @return {Number}
  2738. **/
  2739. Ease.quintInOut = Ease.getPowInOut(5);
  2740. /**
  2741. * @method sineIn
  2742. * @param {Number} t
  2743. * @static
  2744. * @return {Number}
  2745. **/
  2746. Ease.sineIn = function(t) {
  2747. return 1-Math.cos(t*Math.PI/2);
  2748. };
  2749. /**
  2750. * @method sineOut
  2751. * @param {Number} t
  2752. * @static
  2753. * @return {Number}
  2754. **/
  2755. Ease.sineOut = function(t) {
  2756. return Math.sin(t*Math.PI/2);
  2757. };
  2758. /**
  2759. * @method sineInOut
  2760. * @param {Number} t
  2761. * @static
  2762. * @return {Number}
  2763. **/
  2764. Ease.sineInOut = function(t) {
  2765. return -0.5*(Math.cos(Math.PI*t) - 1);
  2766. };
  2767. /**
  2768. * Configurable "back in" ease.
  2769. * @method getBackIn
  2770. * @param {Number} amount The strength of the ease.
  2771. * @static
  2772. * @return {Function}
  2773. **/
  2774. Ease.getBackIn = function(amount) {
  2775. return function(t) {
  2776. return t*t*((amount+1)*t-amount);
  2777. };
  2778. };
  2779. /**
  2780. * @method backIn
  2781. * @param {Number} t
  2782. * @static
  2783. * @return {Number}
  2784. **/
  2785. Ease.backIn = Ease.getBackIn(1.7);
  2786. /**
  2787. * Configurable "back out" ease.
  2788. * @method getBackOut
  2789. * @param {Number} amount The strength of the ease.
  2790. * @static
  2791. * @return {Function}
  2792. **/
  2793. Ease.getBackOut = function(amount) {
  2794. return function(t) {
  2795. return (--t*t*((amount+1)*t + amount) + 1);
  2796. };
  2797. };
  2798. /**
  2799. * @method backOut
  2800. * @param {Number} t
  2801. * @static
  2802. * @return {Number}
  2803. **/
  2804. Ease.backOut = Ease.getBackOut(1.7);
  2805. /**
  2806. * Configurable "back in out" ease.
  2807. * @method getBackInOut
  2808. * @param {Number} amount The strength of the ease.
  2809. * @static
  2810. * @return {Function}
  2811. **/
  2812. Ease.getBackInOut = function(amount) {
  2813. amount*=1.525;
  2814. return function(t) {
  2815. if ((t*=2)<1) return 0.5*(t*t*((amount+1)*t-amount));
  2816. return 0.5*((t-=2)*t*((amount+1)*t+amount)+2);
  2817. };
  2818. };
  2819. /**
  2820. * @method backInOut
  2821. * @param {Number} t
  2822. * @static
  2823. * @return {Number}
  2824. **/
  2825. Ease.backInOut = Ease.getBackInOut(1.7);
  2826. /**
  2827. * @method circIn
  2828. * @param {Number} t
  2829. * @static
  2830. * @return {Number}
  2831. **/
  2832. Ease.circIn = function(t) {
  2833. return -(Math.sqrt(1-t*t)- 1);
  2834. };
  2835. /**
  2836. * @method circOut
  2837. * @param {Number} t
  2838. * @static
  2839. * @return {Number}
  2840. **/
  2841. Ease.circOut = function(t) {
  2842. return Math.sqrt(1-(--t)*t);
  2843. };
  2844. /**
  2845. * @method circInOut
  2846. * @param {Number} t
  2847. * @static
  2848. * @return {Number}
  2849. **/
  2850. Ease.circInOut = function(t) {
  2851. if ((t*=2) < 1) return -0.5*(Math.sqrt(1-t*t)-1);
  2852. return 0.5*(Math.sqrt(1-(t-=2)*t)+1);
  2853. };
  2854. /**
  2855. * @method bounceIn
  2856. * @param {Number} t
  2857. * @static
  2858. * @return {Number}
  2859. **/
  2860. Ease.bounceIn = function(t) {
  2861. return 1-Ease.bounceOut(1-t);
  2862. };
  2863. /**
  2864. * @method bounceOut
  2865. * @param {Number} t
  2866. * @static
  2867. * @return {Number}
  2868. **/
  2869. Ease.bounceOut = function(t) {
  2870. if (t < 1/2.75) {
  2871. return (7.5625*t*t);
  2872. } else if (t < 2/2.75) {
  2873. return (7.5625*(t-=1.5/2.75)*t+0.75);
  2874. } else if (t < 2.5/2.75) {
  2875. return (7.5625*(t-=2.25/2.75)*t+0.9375);
  2876. } else {
  2877. return (7.5625*(t-=2.625/2.75)*t +0.984375);
  2878. }
  2879. };
  2880. /**
  2881. * @method bounceInOut
  2882. * @param {Number} t
  2883. * @static
  2884. * @return {Number}
  2885. **/
  2886. Ease.bounceInOut = function(t) {
  2887. if (t<0.5) return Ease.bounceIn (t*2) * .5;
  2888. return Ease.bounceOut(t*2-1)*0.5+0.5;
  2889. };
  2890. /**
  2891. * Configurable elastic ease.
  2892. * @method getElasticIn
  2893. * @param {Number} amplitude
  2894. * @param {Number} period
  2895. * @static
  2896. * @return {Function}
  2897. **/
  2898. Ease.getElasticIn = function(amplitude,period) {
  2899. var pi2 = Math.PI*2;
  2900. return function(t) {
  2901. if (t==0 || t==1) return t;
  2902. var s = period/pi2*Math.asin(1/amplitude);
  2903. return -(amplitude*Math.pow(2,10*(t-=1))*Math.sin((t-s)*pi2/period));
  2904. };
  2905. };
  2906. /**
  2907. * @method elasticIn
  2908. * @param {Number} t
  2909. * @static
  2910. * @return {Number}
  2911. **/
  2912. Ease.elasticIn = Ease.getElasticIn(1,0.3);
  2913. /**
  2914. * Configurable elastic ease.
  2915. * @method getElasticOut
  2916. * @param {Number} amplitude
  2917. * @param {Number} period
  2918. * @static
  2919. * @return {Function}
  2920. **/
  2921. Ease.getElasticOut = function(amplitude,period) {
  2922. var pi2 = Math.PI*2;
  2923. return function(t) {
  2924. if (t==0 || t==1) return t;
  2925. var s = period/pi2 * Math.asin(1/amplitude);
  2926. return (amplitude*Math.pow(2,-10*t)*Math.sin((t-s)*pi2/period )+1);
  2927. };
  2928. };
  2929. /**
  2930. * @method elasticOut
  2931. * @param {Number} t
  2932. * @static
  2933. * @return {Number}
  2934. **/
  2935. Ease.elasticOut = Ease.getElasticOut(1,0.3);
  2936. /**
  2937. * Configurable elastic ease.
  2938. * @method getElasticInOut
  2939. * @param {Number} amplitude
  2940. * @param {Number} period
  2941. * @static
  2942. * @return {Function}
  2943. **/
  2944. Ease.getElasticInOut = function(amplitude,period) {
  2945. var pi2 = Math.PI*2;
  2946. return function(t) {
  2947. var s = period/pi2 * Math.asin(1/amplitude);
  2948. if ((t*=2)<1) return -0.5*(amplitude*Math.pow(2,10*(t-=1))*Math.sin( (t-s)*pi2/period ));
  2949. return amplitude*Math.pow(2,-10*(t-=1))*Math.sin((t-s)*pi2/period)*0.5+1;
  2950. };
  2951. };
  2952. /**
  2953. * @method elasticInOut
  2954. * @param {Number} t
  2955. * @static
  2956. * @return {Number}
  2957. **/
  2958. Ease.elasticInOut = Ease.getElasticInOut(1,0.3*1.5);
  2959. createjs.Ease = Ease;
  2960. }());
  2961. //##############################################################################
  2962. // MotionGuidePlugin.js
  2963. //##############################################################################
  2964. this.createjs = this.createjs||{};
  2965. (function() {
  2966. "use strict";
  2967. /**
  2968. * A TweenJS plugin for working with motion guides. Defined paths which objects can follow or orient along.
  2969. *
  2970. * To use the plugin, install the plugin after TweenJS has loaded. To define a path, add
  2971. *
  2972. * createjs.MotionGuidePlugin.install();
  2973. *
  2974. * <h4>Example</h4>
  2975. *
  2976. * // Using a Motion Guide
  2977. * createjs.Tween.get(target).to({guide:{ path:[0,0, 0,200,200,200, 200,0,0,0] }},7000);
  2978. * // Visualizing the line
  2979. * graphics.moveTo(0,0).curveTo(0,200,200,200).curveTo(200,0,0,0);
  2980. *
  2981. * Each path needs pre-computation to ensure there's fast performance. Because of the pre-computation there's no
  2982. * built in support for path changes mid tween. These are the Guide Object's properties:<UL>
  2983. * <LI> path: Required, Array : The x/y points used to draw the path with a moveTo and 1 to n curveTo calls.</LI>
  2984. * <LI> start: Optional, 0-1 : Initial position, default 0 except for when continuing along the same path.</LI>
  2985. * <LI> end: Optional, 0-1 : Final position, default 1 if not specified.</LI>
  2986. * <LI> orient: Optional, string : "fixed"/"auto"/"cw"/"ccw"<UL>
  2987. * <LI>"fixed" forces the object to face down the path all movement (relative to start rotation),</LI>
  2988. * <LI>"auto" rotates the object along the path relative to the line.</LI>
  2989. * <LI>"cw"/"ccw" force clockwise or counter clockwise rotations including Adobe Flash/Animate-like
  2990. * behaviour. This may override your end rotation value.</LI>
  2991. * </UL></LI>
  2992. * </UL>
  2993. * Guide objects should not be shared between tweens even if all properties are identical, the library stores
  2994. * information on these objects in the background and sharing them can cause unexpected behaviour. Values
  2995. * outside 0-1 range of tweens will be a "best guess" from the appropriate part of the defined curve.
  2996. *
  2997. * @class MotionGuidePlugin
  2998. * @constructor
  2999. */
  3000. function MotionGuidePlugin() {
  3001. throw("MotionGuidePlugin cannot be instantiated.")
  3002. }
  3003. var s = MotionGuidePlugin;
  3004. // static properties:
  3005. /**
  3006. * @property priority
  3007. * @protected
  3008. * @static
  3009. */
  3010. s.priority = 0; // high priority, should run sooner
  3011. /**
  3012. * READ-ONLY. A unique identifying string for this plugin. Used by TweenJS to ensure duplicate plugins are not installed on a tween.
  3013. * @property ID
  3014. * @type {String}
  3015. * @static
  3016. * @readonly
  3017. */
  3018. s.ID = "MotionGuide";
  3019. // static methods
  3020. /**
  3021. * Installs this plugin for use with TweenJS. Call this once after TweenJS is loaded to enable this plugin.
  3022. * @method install
  3023. * @static
  3024. */
  3025. s.install = function() {
  3026. createjs.Tween._installPlugin(MotionGuidePlugin);
  3027. return createjs.Tween.IGNORE;
  3028. };
  3029. /**
  3030. * Called by TweenJS when a new property initializes on a tween.
  3031. * See {{#crossLink "SamplePlugin/init"}}{{/crossLink}} for more info.
  3032. * @method init
  3033. * @param {Tween} tween
  3034. * @param {String} prop
  3035. * @param {any} value
  3036. * @return {any}
  3037. * @static
  3038. */
  3039. s.init = function(tween, prop, value) {
  3040. if(prop == "guide") {
  3041. tween._addPlugin(s);
  3042. }
  3043. };
  3044. /**
  3045. * Called when a new step is added to a tween (ie. a new "to" action is added to a tween).
  3046. * See {{#crossLink "SamplePlugin/step"}}{{/crossLink}} for more info.
  3047. * @method step
  3048. * @param {Tween} tween
  3049. * @param {TweenStep} step
  3050. * @param {Object} props
  3051. * @static
  3052. */
  3053. s.step = function(tween, step, props) {
  3054. for (var n in props) {
  3055. if(n !== "guide") { continue; }
  3056. var guideData = step.props.guide;
  3057. var error = s._solveGuideData(props.guide, guideData);
  3058. guideData.valid = !error;
  3059. var end = guideData.endData;
  3060. tween._injectProp("x", end.x);
  3061. tween._injectProp("y", end.y);
  3062. if(error || !guideData.orient) { break; }
  3063. var initRot = step.prev.props.rotation === undefined ? (tween.target.rotation || 0) : step.prev.props.rotation;
  3064. guideData.startOffsetRot = initRot - guideData.startData.rotation;
  3065. if(guideData.orient == "fixed") {
  3066. // controlled rotation
  3067. guideData.endAbsRot = end.rotation + guideData.startOffsetRot;
  3068. guideData.deltaRotation = 0;
  3069. } else {
  3070. // interpreted rotation
  3071. var finalRot = props.rotation === undefined ? (tween.target.rotation || 0) : props.rotation;
  3072. var deltaRot = (finalRot - guideData.endData.rotation) - guideData.startOffsetRot;
  3073. var modRot = deltaRot % 360;
  3074. guideData.endAbsRot = finalRot;
  3075. switch(guideData.orient) {
  3076. case "auto":
  3077. guideData.deltaRotation = deltaRot;
  3078. break;
  3079. case "cw":
  3080. guideData.deltaRotation = ((modRot + 360) % 360) + (360 * Math.abs((deltaRot/360) |0));
  3081. break;
  3082. case "ccw":
  3083. guideData.deltaRotation = ((modRot - 360) % 360) + (-360 * Math.abs((deltaRot/360) |0));
  3084. break;
  3085. }
  3086. }
  3087. tween._injectProp("rotation", guideData.endAbsRot);
  3088. }
  3089. };
  3090. /**
  3091. * Called before a property is updated by the tween.
  3092. * See {{#crossLink "SamplePlugin/change"}}{{/crossLink}} for more info.
  3093. * @method change
  3094. * @param {Tween} tween
  3095. * @param {TweenStep} step
  3096. * @param {String} prop
  3097. * @param {any} value
  3098. * @param {Number} ratio
  3099. * @param {Boolean} end
  3100. * @return {any}
  3101. * @static
  3102. */
  3103. s.change = function(tween, step, prop, value, ratio, end) {
  3104. var guideData = step.props.guide;
  3105. if(
  3106. !guideData || // Missing data
  3107. (step.props === step.prev.props) || // In a wait()
  3108. (guideData === step.prev.props.guide) // Guide hasn't changed
  3109. ) {
  3110. return; // have no business making decisions
  3111. }
  3112. if(
  3113. (prop === "guide" && !guideData.valid) || // this data is broken
  3114. (prop == "x" || prop == "y") || // these always get over-written
  3115. (prop === "rotation" && guideData.orient) // currently over-written
  3116. ){
  3117. return createjs.Tween.IGNORE;
  3118. }
  3119. s._ratioToPositionData(ratio, guideData, tween.target);
  3120. };
  3121. // public methods
  3122. /**
  3123. * Provide potentially useful debugging information, like running the error detection system, and rendering the path
  3124. * defined in the guide data.
  3125. *
  3126. * NOTE: you will need to transform your context 2D to the local space of the guide if you wish to line it up.
  3127. * @param {Object} guideData All the information describing the guide to be followed.
  3128. * @param {DrawingContext2D} [ctx=undefined] The context to draw the object into.
  3129. * @param {Array} [higlight=undefined] Array of ratio positions to highlight
  3130. * @returns {undefined|String}
  3131. */
  3132. s.debug = function(guideData, ctx, higlight) {
  3133. guideData = guideData.guide || guideData;
  3134. // errors
  3135. var err = s._findPathProblems(guideData);
  3136. if(err) {
  3137. console.error("MotionGuidePlugin Error found: \n" + err);
  3138. }
  3139. // drawing
  3140. if(!ctx){ return err; }
  3141. var i;
  3142. var path = guideData.path;
  3143. var pathLength = path.length;
  3144. var width = 3;
  3145. var length = 9;
  3146. ctx.save();
  3147. //ctx.resetTransform();
  3148. ctx.lineCap = "round";
  3149. ctx.lineJoin = "miter";
  3150. ctx.beginPath();
  3151. // curve
  3152. ctx.moveTo(path[0], path[1]);
  3153. for(i=2; i < pathLength; i+=4) {
  3154. ctx.quadraticCurveTo(
  3155. path[i], path[i+1],
  3156. path[i+2], path[i+3]
  3157. );
  3158. }
  3159. ctx.strokeStyle = "black";
  3160. ctx.lineWidth = width*1.5;
  3161. ctx.stroke();
  3162. ctx.strokeStyle = "white";
  3163. ctx.lineWidth = width;
  3164. ctx.stroke();
  3165. ctx.closePath();
  3166. // highlights
  3167. var hiCount = higlight.length;
  3168. if(higlight && hiCount) {
  3169. var tempStore = {};
  3170. var tempLook = {};
  3171. s._solveGuideData(guideData, tempStore);
  3172. for(var i=0; i<hiCount; i++){
  3173. tempStore.orient = "fixed";
  3174. s._ratioToPositionData(higlight[i], tempStore, tempLook);
  3175. ctx.beginPath();
  3176. ctx.moveTo(tempLook.x, tempLook.y);
  3177. ctx.lineTo(
  3178. tempLook.x + Math.cos(tempLook.rotation * 0.0174533) * length,
  3179. tempLook.y + Math.sin(tempLook.rotation * 0.0174533) * length
  3180. );
  3181. ctx.strokeStyle = "black";
  3182. ctx.lineWidth = width*1.5;
  3183. ctx.stroke();
  3184. ctx.strokeStyle = "red";
  3185. ctx.lineWidth = width;
  3186. ctx.stroke();
  3187. ctx.closePath();
  3188. }
  3189. }
  3190. // end draw
  3191. ctx.restore();
  3192. return err;
  3193. };
  3194. // private methods
  3195. /**
  3196. * Calculate and store optimization data about the desired path to improve performance and accuracy of positions.
  3197. * @param {Object} source The guide data provided to the tween call
  3198. * @param {Object} storage the guide data used by the step calls and plugin to do the job, will be overwritten
  3199. * @returns {undefined|String} Can return an error if unable to generate the data.
  3200. * @private
  3201. */
  3202. s._solveGuideData = function(source, storage) {
  3203. var err = undefined;
  3204. if(err = s.debug(source)) { return err; }
  3205. var path = storage.path = source.path;
  3206. var orient = storage.orient = source.orient;
  3207. storage.subLines = [];
  3208. storage.totalLength = 0;
  3209. storage.startOffsetRot = 0;
  3210. storage.deltaRotation = 0;
  3211. storage.startData = {ratio: 0};
  3212. storage.endData = {ratio: 1};
  3213. storage.animSpan = 1;
  3214. var pathLength = path.length;
  3215. var precision = 10;
  3216. var sx,sy, cx,cy, ex,ey, i,j, len, temp = {};
  3217. sx = path[0]; sy = path[1];
  3218. // get the data for each curve
  3219. for(i=2; i < pathLength; i+=4) {
  3220. cx = path[i]; cy = path[i+1];
  3221. ex = path[i+2]; ey = path[i+3];
  3222. var subLine = {
  3223. weightings: [],
  3224. estLength: 0,
  3225. portion: 0
  3226. };
  3227. var subX = sx, subY = sy;
  3228. // get the distance data for each point
  3229. for(j=1; j <= precision;j++) { // we need to evaluate t = 1 not t = 0
  3230. s._getParamsForCurve(sx,sy, cx,cy, ex,ey, j/precision, false, temp);
  3231. var dx = temp.x - subX, dy = temp.y - subY;
  3232. len = Math.sqrt(dx*dx + dy*dy);
  3233. subLine.weightings.push(len);
  3234. subLine.estLength += len;
  3235. subX = temp.x;
  3236. subY = temp.y;
  3237. }
  3238. // figure out full lengths
  3239. storage.totalLength += subLine.estLength;
  3240. // use length to figure out proportional weightings
  3241. for(j=0; j < precision; j++) {
  3242. len = subLine.estLength;
  3243. subLine.weightings[j] = subLine.weightings[j] / len;
  3244. }
  3245. storage.subLines.push(subLine);
  3246. sx = ex;
  3247. sy = ey;
  3248. }
  3249. // use length to figure out proportional weightings
  3250. len = storage.totalLength;
  3251. var l = storage.subLines.length;
  3252. for(i=0; i<l; i++) {
  3253. storage.subLines[i].portion = storage.subLines[i].estLength / len;
  3254. }
  3255. // determine start and end data
  3256. var startRatio = isNaN(source.start) ? 0 : source.start;
  3257. var endRatio = isNaN(source.end) ? 1 : source.end;
  3258. s._ratioToPositionData(startRatio, storage, storage.startData);
  3259. s._ratioToPositionData(endRatio, storage, storage.endData);
  3260. // this has to be done last else the prev ratios will be out of place
  3261. storage.startData.ratio = startRatio;
  3262. storage.endData.ratio = endRatio;
  3263. storage.animSpan = storage.endData.ratio - storage.startData.ratio;
  3264. };
  3265. /**
  3266. * Convert a percentage along the line into, a local line (start, control, end) t-value for calculation.
  3267. * @param {Number} ratio The (euclidean distance) percentage into the whole curve.
  3268. * @param {Object} guideData All the information describing the guide to be followed.
  3269. * @param {Object} output Object to save output properties of x,y, and rotation onto.
  3270. * @returns {Object} The output object, useful for isolated calls.
  3271. * @private
  3272. */
  3273. s._ratioToPositionData = function(ratio, guideData, output) {
  3274. var lineSegments = guideData.subLines;
  3275. var i,l, t, test, target;
  3276. var look = 0;
  3277. var precision = 10;
  3278. var effRatio = (ratio * guideData.animSpan) + guideData.startData.ratio;
  3279. // find subline
  3280. l = lineSegments.length;
  3281. for(i=0; i<l; i++) {
  3282. test = lineSegments[i].portion;
  3283. if(look + test >= effRatio){ target = i; break; }
  3284. look += test;
  3285. }
  3286. if(target === undefined) { target = l-1; look -= test; }
  3287. // find midline weighting
  3288. var subLines = lineSegments[target].weightings;
  3289. var portion = test;
  3290. l = subLines.length;
  3291. for(i=0; i<l; i++) {
  3292. test = subLines[i] * portion;
  3293. if(look + test >= effRatio){ break; }
  3294. look += test;
  3295. }
  3296. // translate the subline index into a position in the path data
  3297. target = (target*4) + 2;
  3298. // take the distance we've covered in our ratio, and scale it to distance into the weightings
  3299. t = (i/precision) + (((effRatio-look) / test) * (1/precision));
  3300. // position
  3301. var pathData = guideData.path;
  3302. s._getParamsForCurve(
  3303. pathData[target-2], pathData[target-1],
  3304. pathData[target], pathData[target+1],
  3305. pathData[target+2], pathData[target+3],
  3306. t,
  3307. guideData.orient,
  3308. output
  3309. );
  3310. if(guideData.orient) {
  3311. if(ratio >= 0.99999 && ratio <= 1.00001 && guideData.endAbsRot !== undefined) {
  3312. output.rotation = guideData.endAbsRot;
  3313. } else {
  3314. output.rotation += guideData.startOffsetRot + (ratio * guideData.deltaRotation);
  3315. }
  3316. }
  3317. return output;
  3318. };
  3319. /**
  3320. * For a given quadratic bezier t-value, what is the position and rotation. Save it onto the output object.
  3321. * @param {Number} sx Start x.
  3322. * @param {Number} sy Start y.
  3323. * @param {Number} cx Control x.
  3324. * @param {Number} cy Control y.
  3325. * @param {Number} ex End x.
  3326. * @param {Number} ey End y.
  3327. * @param {Number} t T value (parametric distance into curve).
  3328. * @param {Boolean} orient Save rotation data.
  3329. * @param {Object} output Object to save output properties of x,y, and rotation onto.
  3330. * @private
  3331. */
  3332. s._getParamsForCurve = function(sx,sy, cx,cy, ex,ey, t, orient, output) {
  3333. var inv = 1 - t;
  3334. // finding a point on a bezier curve
  3335. output.x = inv*inv * sx + 2 * inv * t * cx + t*t * ex;
  3336. output.y = inv*inv * sy + 2 * inv * t * cy + t*t * ey;
  3337. // finding an angle on a bezier curve
  3338. if(orient) {
  3339. // convert from radians back to degrees
  3340. output.rotation = 57.2957795 * Math.atan2(
  3341. (cy - sy)*inv + (ey - cy)*t,
  3342. (cx - sx)*inv + (ex - cx)*t
  3343. );
  3344. }
  3345. };
  3346. /**
  3347. * Perform a check to validate path information so plugin can avoid later error checking.
  3348. * @param {Object} guideData All the information describing the guide to be followed.
  3349. * @returns {undefined|String} The problem found, or undefined if no problems.
  3350. * @private
  3351. */
  3352. s._findPathProblems = function(guideData) {
  3353. var path = guideData.path;
  3354. var valueCount = (path && path.length) || 0; // ensure this is a number to simplify later logic
  3355. if(valueCount < 6 || (valueCount-2) % 4) {
  3356. var message = "\tCannot parse 'path' array due to invalid number of entries in path. ";
  3357. message += "There should be an odd number of points, at least 3 points, and 2 entries per point (x & y). ";
  3358. message += "See 'CanvasRenderingContext2D.quadraticCurveTo' for details as 'path' models a quadratic bezier.\n\n";
  3359. message += "Only [ "+ valueCount +" ] values found. Expected: "+ Math.max(Math.ceil((valueCount-2)/4)*4+2, 6); //6, 10, 14,...
  3360. return message;
  3361. }
  3362. for(var i=0; i<valueCount; i++) {
  3363. if(isNaN(path[i])){
  3364. return "All data in path array must be numeric";
  3365. }
  3366. }
  3367. var start = guideData.start;
  3368. if(isNaN(start) && !(start === undefined)/* || start < 0 || start > 1*/) { // outside 0-1 is unpredictable, but not breaking
  3369. return "'start' out of bounds. Expected 0 to 1, got: "+ start;
  3370. }
  3371. var end = guideData.end;
  3372. if(isNaN(end) && (end !== undefined)/* || end < 0 || end > 1*/) { // outside 0-1 is unpredictable, but not breaking
  3373. return "'end' out of bounds. Expected 0 to 1, got: "+ end;
  3374. }
  3375. var orient = guideData.orient;
  3376. if(orient) { // mirror the check used elsewhere
  3377. if(orient != "fixed" && orient != "auto" && orient != "cw" && orient != "ccw") {
  3378. return 'Invalid orientation value. Expected ["fixed", "auto", "cw", "ccw", undefined], got: '+ orient;
  3379. }
  3380. }
  3381. return undefined;
  3382. };
  3383. createjs.MotionGuidePlugin = MotionGuidePlugin;
  3384. }());
  3385. //##############################################################################
  3386. // version.js
  3387. //##############################################################################
  3388. this.createjs = this.createjs || {};
  3389. (function() {
  3390. "use strict";
  3391. /**
  3392. * Static class holding library specific information such as the version and buildDate of
  3393. * the library.
  3394. * @class TweenJS
  3395. **/
  3396. var s = createjs.TweenJS = createjs.TweenJS || {};
  3397. /**
  3398. * The version string for this release.
  3399. * @property version
  3400. * @type String
  3401. * @static
  3402. **/
  3403. s.version = /*=version*/"1.0.0"; // injected by build process
  3404. /**
  3405. * The build date for this release in UTC format.
  3406. * @property buildDate
  3407. * @type String
  3408. * @static
  3409. **/
  3410. s.buildDate = /*=date*/"Thu, 14 Sep 2017 19:47:47 GMT"; // injected by build process
  3411. })();