Animation.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. /*global define*/
  2. define([
  3. '../../Core/Color',
  4. '../../Core/defined',
  5. '../../Core/defineProperties',
  6. '../../Core/destroyObject',
  7. '../../Core/DeveloperError',
  8. '../getElement',
  9. '../subscribeAndEvaluate'
  10. ], function(
  11. Color,
  12. defined,
  13. defineProperties,
  14. destroyObject,
  15. DeveloperError,
  16. getElement,
  17. subscribeAndEvaluate) {
  18. "use strict";
  19. var svgNS = "http://www.w3.org/2000/svg";
  20. var xlinkNS = "http://www.w3.org/1999/xlink";
  21. var widgetForDrag;
  22. var gradientEnabledColor0 = Color.fromCssColorString('rgba(247,250,255,0.384)');
  23. var gradientEnabledColor1 = Color.fromCssColorString('rgba(143,191,255,0.216)');
  24. var gradientEnabledColor2 = Color.fromCssColorString('rgba(153,197,255,0.098)');
  25. var gradientEnabledColor3 = Color.fromCssColorString('rgba(255,255,255,0.086)');
  26. var gradientDisabledColor0 = Color.fromCssColorString('rgba(255,255,255,0.267)');
  27. var gradientDisabledColor1 = Color.fromCssColorString('rgba(255,255,255,0)');
  28. var gradientKnobColor = Color.fromCssColorString('rgba(66,67,68,0.3)');
  29. var gradientPointerColor = Color.fromCssColorString('rgba(0,0,0,0.5)');
  30. function getElementColor(element) {
  31. return Color.fromCssColorString(window.getComputedStyle(element).getPropertyValue('color'));
  32. }
  33. //Dynamically builds an SVG element from a JSON object.
  34. function svgFromObject(obj) {
  35. var ele = document.createElementNS(svgNS, obj.tagName);
  36. for ( var field in obj) {
  37. if (obj.hasOwnProperty(field) && field !== 'tagName') {
  38. if (field === 'children') {
  39. var i;
  40. var len = obj.children.length;
  41. for (i = 0; i < len; ++i) {
  42. ele.appendChild(svgFromObject(obj.children[i]));
  43. }
  44. } else if (field.indexOf('xlink:') === 0) {
  45. ele.setAttributeNS(xlinkNS, field.substring(6), obj[field]);
  46. } else if (field === 'textContent') {
  47. ele.textContent = obj[field];
  48. } else {
  49. ele.setAttribute(field, obj[field]);
  50. }
  51. }
  52. }
  53. return ele;
  54. }
  55. function svgText(x, y, msg) {
  56. var text = document.createElementNS(svgNS, 'text');
  57. text.setAttribute('x', x);
  58. text.setAttribute('y', y);
  59. text.setAttribute('class', 'cesium-animation-svgText');
  60. var tspan = document.createElementNS(svgNS, 'tspan');
  61. tspan.textContent = msg;
  62. text.appendChild(tspan);
  63. return text;
  64. }
  65. function setShuttleRingPointer(shuttleRingPointer, knobOuter, angle) {
  66. shuttleRingPointer.setAttribute('transform', 'translate(100,100) rotate(' + angle + ')');
  67. knobOuter.setAttribute('transform', 'rotate(' + angle + ')');
  68. }
  69. var makeColorStringScratch = new Color();
  70. function makeColorString(background, gradient) {
  71. var gradientAlpha = gradient.alpha;
  72. var backgroundAlpha = 1.0 - gradientAlpha;
  73. makeColorStringScratch.red = (background.red * backgroundAlpha) + (gradient.red * gradientAlpha);
  74. makeColorStringScratch.green = (background.green * backgroundAlpha) + (gradient.green * gradientAlpha);
  75. makeColorStringScratch.blue = (background.blue * backgroundAlpha) + (gradient.blue * gradientAlpha);
  76. return makeColorStringScratch.toCssColorString();
  77. }
  78. function rectButton(x, y, path) {
  79. var button = {
  80. tagName : 'g',
  81. 'class' : 'cesium-animation-rectButton',
  82. transform : 'translate(' + x + ',' + y + ')',
  83. children : [{
  84. tagName : 'rect',
  85. 'class' : 'cesium-animation-buttonGlow',
  86. width : 32,
  87. height : 32,
  88. rx : 2,
  89. ry : 2
  90. }, {
  91. tagName : 'rect',
  92. 'class' : 'cesium-animation-buttonMain',
  93. width : 32,
  94. height : 32,
  95. rx : 4,
  96. ry : 4
  97. }, {
  98. tagName : 'use',
  99. 'class' : 'cesium-animation-buttonPath',
  100. 'xlink:href' : path
  101. }, {
  102. tagName : 'title',
  103. textContent : ''
  104. }]
  105. };
  106. return svgFromObject(button);
  107. }
  108. function wingButton(x, y, path) {
  109. var button = {
  110. tagName : 'g',
  111. 'class' : 'cesium-animation-rectButton',
  112. transform : 'translate(' + x + ',' + y + ')',
  113. children : [{
  114. tagName : 'use',
  115. 'class' : 'cesium-animation-buttonGlow',
  116. 'xlink:href' : '#animation_pathWingButton'
  117. }, {
  118. tagName : 'use',
  119. 'class' : 'cesium-animation-buttonMain',
  120. 'xlink:href' : '#animation_pathWingButton'
  121. }, {
  122. tagName : 'use',
  123. 'class' : 'cesium-animation-buttonPath',
  124. 'xlink:href' : path
  125. }, {
  126. tagName : 'title',
  127. textContent : ''
  128. }]
  129. };
  130. return svgFromObject(button);
  131. }
  132. function setShuttleRingFromMouseOrTouch(widget, e) {
  133. var viewModel = widget._viewModel;
  134. var shuttleRingDragging = viewModel.shuttleRingDragging;
  135. if (shuttleRingDragging && (widgetForDrag !== widget)) {
  136. return;
  137. }
  138. if (e.type === 'mousedown' || (shuttleRingDragging && e.type === 'mousemove') ||
  139. (e.type === 'touchstart' && e.touches.length === 1) ||
  140. (shuttleRingDragging && e.type === 'touchmove' && e.touches.length === 1)) {
  141. var centerX = widget._centerX;
  142. var centerY = widget._centerY;
  143. var svg = widget._svgNode;
  144. var rect = svg.getBoundingClientRect();
  145. var clientX;
  146. var clientY;
  147. if (e.type === 'touchstart' || e.type === 'touchmove') {
  148. clientX = e.touches[0].clientX;
  149. clientY = e.touches[0].clientY;
  150. } else {
  151. clientX = e.clientX;
  152. clientY = e.clientY;
  153. }
  154. if (!shuttleRingDragging &&
  155. (clientX > rect.right ||
  156. clientX < rect.left ||
  157. clientY < rect.top ||
  158. clientY > rect.bottom)) {
  159. return;
  160. }
  161. var pointerRect = widget._shuttleRingPointer.getBoundingClientRect();
  162. var x = clientX - centerX - rect.left;
  163. var y = clientY - centerY - rect.top;
  164. var angle = Math.atan2(y, x) * 180 / Math.PI + 90;
  165. if (angle > 180) {
  166. angle -= 360;
  167. }
  168. var shuttleRingAngle = viewModel.shuttleRingAngle;
  169. if (shuttleRingDragging || (clientX < pointerRect.right && clientX > pointerRect.left && clientY > pointerRect.top && clientY < pointerRect.bottom)) {
  170. widgetForDrag = widget;
  171. viewModel.shuttleRingDragging = true;
  172. viewModel.shuttleRingAngle = angle;
  173. } else if (angle < shuttleRingAngle) {
  174. viewModel.slower();
  175. } else if (angle > shuttleRingAngle) {
  176. viewModel.faster();
  177. }
  178. e.preventDefault();
  179. } else {
  180. if (widget === widgetForDrag) {
  181. widgetForDrag = undefined;
  182. }
  183. viewModel.shuttleRingDragging = false;
  184. }
  185. }
  186. //This is a private class for treating an SVG element like a button.
  187. //If we ever need a general purpose SVG button, we can make this generic.
  188. var SvgButton = function(svgElement, viewModel) {
  189. this._viewModel = viewModel;
  190. this.svgElement = svgElement;
  191. this._enabled = undefined;
  192. this._toggled = undefined;
  193. var that = this;
  194. this._clickFunction = function() {
  195. var command = that._viewModel.command;
  196. if (command.canExecute) {
  197. command();
  198. }
  199. };
  200. svgElement.addEventListener('click', this._clickFunction, true);
  201. //TODO: Since the animation widget uses SVG and has no HTML backing,
  202. //we need to wire everything up manually. Knockout can supposedly
  203. //bind to SVG, so we we figure that out we can modify our SVG
  204. //to include the binding information directly.
  205. this._subscriptions = [//
  206. subscribeAndEvaluate(viewModel, 'toggled', this.setToggled, this),//
  207. subscribeAndEvaluate(viewModel, 'tooltip', this.setTooltip, this),//
  208. subscribeAndEvaluate(viewModel.command, 'canExecute', this.setEnabled, this)];
  209. };
  210. SvgButton.prototype.destroy = function() {
  211. this.svgElement.removeEventListener('click', this._clickFunction, true);
  212. var subscriptions = this._subscriptions;
  213. for ( var i = 0, len = subscriptions.length; i < len; i++) {
  214. subscriptions[i].dispose();
  215. }
  216. destroyObject(this);
  217. };
  218. SvgButton.prototype.isDestroyed = function() {
  219. return false;
  220. };
  221. SvgButton.prototype.setEnabled = function(enabled) {
  222. if (this._enabled !== enabled) {
  223. this._enabled = enabled;
  224. if (!enabled) {
  225. this.svgElement.setAttribute('class', 'cesium-animation-buttonDisabled');
  226. return;
  227. }
  228. if (this._toggled) {
  229. this.svgElement.setAttribute('class', 'cesium-animation-rectButton cesium-animation-buttonToggled');
  230. return;
  231. }
  232. this.svgElement.setAttribute('class', 'cesium-animation-rectButton');
  233. }
  234. };
  235. SvgButton.prototype.setToggled = function(toggled) {
  236. if (this._toggled !== toggled) {
  237. this._toggled = toggled;
  238. if (this._enabled) {
  239. if (toggled) {
  240. this.svgElement.setAttribute('class', 'cesium-animation-rectButton cesium-animation-buttonToggled');
  241. } else {
  242. this.svgElement.setAttribute('class', 'cesium-animation-rectButton');
  243. }
  244. }
  245. }
  246. };
  247. SvgButton.prototype.setTooltip = function(tooltip) {
  248. this.svgElement.getElementsByTagName('title')[0].textContent = tooltip;
  249. };
  250. /**
  251. * <span style="display: block; text-align: center;">
  252. * <img src="images/AnimationWidget.png" width="211" height="142" alt="" style="border: none; border-radius: 5px;" />
  253. * <br />Animation widget
  254. * </span>
  255. * <br /><br />
  256. * The Animation widget provides buttons for play, pause, and reverse, along with the
  257. * current time and date, surrounded by a "shuttle ring" for controlling the speed of animation.
  258. * <br /><br />
  259. * The "shuttle ring" concept is borrowed from video editing, where typically a
  260. * "jog wheel" can be rotated to move past individual animation frames very slowly, and
  261. * a surrounding shuttle ring can be twisted to control direction and speed of fast playback.
  262. * Cesium typically treats time as continuous (not broken into pre-defined animation frames),
  263. * so this widget offers no jog wheel. Instead, the shuttle ring is capable of both fast and
  264. * very slow playback. Click and drag the shuttle ring pointer itself (shown above in green),
  265. * or click in the rest of the ring area to nudge the pointer to the next preset speed in that direction.
  266. * <br /><br />
  267. * The Animation widget also provides a "realtime" button (in the upper-left) that keeps
  268. * animation time in sync with the end user's system clock, typically displaying
  269. * "today" or "right now." This mode is not available in {@link ClockRange.CLAMPED} or
  270. * {@link ClockRange.LOOP_STOP} mode if the current time is outside of {@link Clock}'s startTime and endTime.
  271. *
  272. * @alias Animation
  273. * @constructor
  274. *
  275. * @param {Element|String} container The DOM element or ID that will contain the widget.
  276. * @param {AnimationViewModel} viewModel The view model used by this widget.
  277. *
  278. * @exception {DeveloperError} Element with id "container" does not exist in the document.
  279. *
  280. * @see AnimationViewModel
  281. * @see Clock
  282. *
  283. * @example
  284. * // In HTML head, include a link to Animation.css stylesheet,
  285. * // and in the body, include: <div id="animationContainer"></div>
  286. *
  287. * var clock = new Cesium.Clock();
  288. * var clockViewModel = new Cesium.ClockViewModel(clock);
  289. * var viewModel = new Cesium.AnimationViewModel(clockViewModel);
  290. * var widget = new Cesium.Animation('animationContainer', viewModel);
  291. *
  292. * function tick() {
  293. * clock.tick();
  294. * Cesium.requestAnimationFrame(tick);
  295. * }
  296. * Cesium.requestAnimationFrame(tick);
  297. */
  298. var Animation = function(container, viewModel) {
  299. //>>includeStart('debug', pragmas.debug);
  300. if (!defined(container)) {
  301. throw new DeveloperError('container is required.');
  302. }
  303. if (!defined(viewModel)) {
  304. throw new DeveloperError('viewModel is required.');
  305. }
  306. //>>includeEnd('debug');
  307. container = getElement(container);
  308. this._viewModel = viewModel;
  309. this._container = container;
  310. this._centerX = 0;
  311. this._centerY = 0;
  312. this._defsElement = undefined;
  313. this._svgNode = undefined;
  314. this._topG = undefined;
  315. this._lastHeight = undefined;
  316. this._lastWidth = undefined;
  317. // Firefox requires SVG references to be included directly, not imported from external CSS.
  318. // Also, CSS minifiers get confused by this being in an external CSS file.
  319. var cssStyle = document.createElement('style');
  320. cssStyle.textContent = '.cesium-animation-rectButton .cesium-animation-buttonGlow { filter: url(#animation_blurred); }\
  321. .cesium-animation-rectButton .cesium-animation-buttonMain { fill: url(#animation_buttonNormal); }\
  322. .cesium-animation-buttonToggled .cesium-animation-buttonMain { fill: url(#animation_buttonToggled); }\
  323. .cesium-animation-rectButton:hover .cesium-animation-buttonMain { fill: url(#animation_buttonHovered); }\
  324. .cesium-animation-buttonDisabled .cesium-animation-buttonMain { fill: url(#animation_buttonDisabled); }\
  325. .cesium-animation-shuttleRingG .cesium-animation-shuttleRingSwoosh { fill: url(#animation_shuttleRingSwooshGradient); }\
  326. .cesium-animation-shuttleRingG:hover .cesium-animation-shuttleRingSwoosh { fill: url(#animation_shuttleRingSwooshHovered); }\
  327. .cesium-animation-shuttleRingPointer { fill: url(#animation_shuttleRingPointerGradient); }\
  328. .cesium-animation-shuttleRingPausePointer { fill: url(#animation_shuttleRingPointerPaused); }\
  329. .cesium-animation-knobOuter { fill: url(#animation_knobOuter); }\
  330. .cesium-animation-knobInner { fill: url(#animation_knobInner); }';
  331. document.head.insertBefore(cssStyle, document.head.childNodes[0]);
  332. var themeEle = document.createElement('div');
  333. themeEle.className = 'cesium-animation-theme';
  334. themeEle.innerHTML = '<div class="cesium-animation-themeNormal"></div>\
  335. <div class="cesium-animation-themeHover"></div>\
  336. <div class="cesium-animation-themeSelect"></div>\
  337. <div class="cesium-animation-themeDisabled"></div>\
  338. <div class="cesium-animation-themeKnob"></div>\
  339. <div class="cesium-animation-themePointer"></div>\
  340. <div class="cesium-animation-themeSwoosh"></div>\
  341. <div class="cesium-animation-themeSwooshHover"></div>';
  342. this._theme = themeEle;
  343. this._themeNormal = themeEle.childNodes[0];
  344. this._themeHover = themeEle.childNodes[1];
  345. this._themeSelect = themeEle.childNodes[2];
  346. this._themeDisabled = themeEle.childNodes[3];
  347. this._themeKnob = themeEle.childNodes[4];
  348. this._themePointer = themeEle.childNodes[5];
  349. this._themeSwoosh = themeEle.childNodes[6];
  350. this._themeSwooshHover = themeEle.childNodes[7];
  351. var svg = document.createElementNS(svgNS, 'svg:svg');
  352. this._svgNode = svg;
  353. // Define the XLink namespace that SVG uses
  354. svg.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xlink', xlinkNS);
  355. var topG = document.createElementNS(svgNS, 'g');
  356. this._topG = topG;
  357. this._realtimeSVG = new SvgButton(wingButton(3, 4, '#animation_pathClock'), viewModel.playRealtimeViewModel);
  358. this._playReverseSVG = new SvgButton(rectButton(44, 99, '#animation_pathPlayReverse'), viewModel.playReverseViewModel);
  359. this._playForwardSVG = new SvgButton(rectButton(124, 99, '#animation_pathPlay'), viewModel.playForwardViewModel);
  360. this._pauseSVG = new SvgButton(rectButton(84, 99, '#animation_pathPause'), viewModel.pauseViewModel);
  361. var buttonsG = document.createElementNS(svgNS, 'g');
  362. buttonsG.appendChild(this._realtimeSVG.svgElement);
  363. buttonsG.appendChild(this._playReverseSVG.svgElement);
  364. buttonsG.appendChild(this._playForwardSVG.svgElement);
  365. buttonsG.appendChild(this._pauseSVG.svgElement);
  366. var shuttleRingBackPanel = svgFromObject({
  367. tagName : 'circle',
  368. 'class' : 'cesium-animation-shuttleRingBack',
  369. cx : 100,
  370. cy : 100,
  371. r : 99
  372. });
  373. this._shuttleRingBackPanel = shuttleRingBackPanel;
  374. var shuttleRingSwooshG = svgFromObject({
  375. tagName : 'g',
  376. 'class' : 'cesium-animation-shuttleRingSwoosh',
  377. children : [{
  378. tagName : 'use',
  379. transform : 'translate(100,97) scale(-1,1)',
  380. 'xlink:href' : '#animation_pathSwooshFX'
  381. }, {
  382. tagName : 'use',
  383. transform : 'translate(100,97)',
  384. 'xlink:href' : '#animation_pathSwooshFX'
  385. }, {
  386. tagName : 'line',
  387. x1 : 100,
  388. y1 : 8,
  389. x2 : 100,
  390. y2 : 22
  391. }]
  392. });
  393. this._shuttleRingSwooshG = shuttleRingSwooshG;
  394. this._shuttleRingPointer = svgFromObject({
  395. tagName : 'use',
  396. 'class' : 'cesium-animation-shuttleRingPointer',
  397. 'xlink:href' : '#animation_pathPointer'
  398. });
  399. var knobG = svgFromObject({
  400. tagName : 'g',
  401. transform : 'translate(100,100)'
  402. });
  403. this._knobOuter = svgFromObject({
  404. tagName : 'circle',
  405. 'class' : 'cesium-animation-knobOuter',
  406. cx : 0,
  407. cy : 0,
  408. r : 71
  409. });
  410. var knobInnerAndShieldSize = 61;
  411. var knobInner = svgFromObject({
  412. tagName : 'circle',
  413. 'class' : 'cesium-animation-knobInner',
  414. cx : 0,
  415. cy : 0,
  416. r : knobInnerAndShieldSize
  417. });
  418. this._knobDate = svgText(0, -24, '');
  419. this._knobTime = svgText(0, -7, '');
  420. this._knobStatus = svgText(0, -41, '');
  421. // widget shield catches clicks on the knob itself (even while DOM elements underneath are changing).
  422. var knobShield = svgFromObject({
  423. tagName : 'circle',
  424. 'class' : 'cesium-animation-blank',
  425. cx : 0,
  426. cy : 0,
  427. r : knobInnerAndShieldSize
  428. });
  429. var shuttleRingBackG = document.createElementNS(svgNS, 'g');
  430. shuttleRingBackG.setAttribute('class', 'cesium-animation-shuttleRingG');
  431. container.appendChild(themeEle);
  432. topG.appendChild(shuttleRingBackG);
  433. topG.appendChild(knobG);
  434. topG.appendChild(buttonsG);
  435. shuttleRingBackG.appendChild(shuttleRingBackPanel);
  436. shuttleRingBackG.appendChild(shuttleRingSwooshG);
  437. shuttleRingBackG.appendChild(this._shuttleRingPointer);
  438. knobG.appendChild(this._knobOuter);
  439. knobG.appendChild(knobInner);
  440. knobG.appendChild(this._knobDate);
  441. knobG.appendChild(this._knobTime);
  442. knobG.appendChild(this._knobStatus);
  443. knobG.appendChild(knobShield);
  444. svg.appendChild(topG);
  445. container.appendChild(svg);
  446. var that = this;
  447. var mouseCallback = function(e) {
  448. setShuttleRingFromMouseOrTouch(that, e);
  449. };
  450. this._mouseCallback = mouseCallback;
  451. shuttleRingBackPanel.addEventListener('mousedown', mouseCallback, true);
  452. shuttleRingBackPanel.addEventListener('touchstart', mouseCallback, true);
  453. shuttleRingSwooshG.addEventListener('mousedown', mouseCallback, true);
  454. shuttleRingSwooshG.addEventListener('touchstart', mouseCallback, true);
  455. document.addEventListener('mousemove', mouseCallback, true);
  456. document.addEventListener('touchmove', mouseCallback, true);
  457. document.addEventListener('mouseup', mouseCallback, true);
  458. document.addEventListener('touchend', mouseCallback, true);
  459. this._shuttleRingPointer.addEventListener('mousedown', mouseCallback, true);
  460. this._shuttleRingPointer.addEventListener('touchstart', mouseCallback, true);
  461. this._knobOuter.addEventListener('mousedown', mouseCallback, true);
  462. this._knobOuter.addEventListener('touchstart', mouseCallback, true);
  463. //TODO: Since the animation widget uses SVG and has no HTML backing,
  464. //we need to wire everything up manually. Knockout can supposedly
  465. //bind to SVG, so we we figure that out we can modify our SVG
  466. //to include the binding information directly.
  467. var timeNode = this._knobTime.childNodes[0];
  468. var dateNode = this._knobDate.childNodes[0];
  469. var statusNode = this._knobStatus.childNodes[0];
  470. var isPaused;
  471. this._subscriptions = [//
  472. subscribeAndEvaluate(viewModel.pauseViewModel, 'toggled', function(value) {
  473. if (isPaused !== value) {
  474. isPaused = value;
  475. if (isPaused) {
  476. that._shuttleRingPointer.setAttribute('class', 'cesium-animation-shuttleRingPausePointer');
  477. } else {
  478. that._shuttleRingPointer.setAttribute('class', 'cesium-animation-shuttleRingPointer');
  479. }
  480. }
  481. }),
  482. subscribeAndEvaluate(viewModel, 'shuttleRingAngle', function(value) {
  483. setShuttleRingPointer(that._shuttleRingPointer, that._knobOuter, value);
  484. }),
  485. subscribeAndEvaluate(viewModel, 'dateLabel', function(value) {
  486. if (dateNode.textContent !== value) {
  487. dateNode.textContent = value;
  488. }
  489. }),
  490. subscribeAndEvaluate(viewModel, 'timeLabel', function(value) {
  491. if (timeNode.textContent !== value) {
  492. timeNode.textContent = value;
  493. }
  494. }),
  495. subscribeAndEvaluate(viewModel, 'multiplierLabel', function(value) {
  496. if (statusNode.textContent !== value) {
  497. statusNode.textContent = value;
  498. }
  499. })];
  500. this.applyThemeChanges();
  501. this.resize();
  502. };
  503. defineProperties(Animation.prototype, {
  504. /**
  505. * Gets the parent container.
  506. *
  507. * @memberof Animation.prototype
  508. * @type {Element}
  509. */
  510. container : {
  511. get : function() {
  512. return this._container;
  513. }
  514. },
  515. /**
  516. * Gets the view model.
  517. *
  518. * @memberof Animation.prototype
  519. * @type {AnimationViewModel}
  520. */
  521. viewModel : {
  522. get : function() {
  523. return this._viewModel;
  524. }
  525. }
  526. });
  527. /**
  528. * @returns {Boolean} true if the object has been destroyed, false otherwise.
  529. */
  530. Animation.prototype.isDestroyed = function() {
  531. return false;
  532. };
  533. /**
  534. * Destroys the animation widget. Should be called if permanently
  535. * removing the widget from layout.
  536. */
  537. Animation.prototype.destroy = function() {
  538. var mouseCallback = this._mouseCallback;
  539. this._shuttleRingBackPanel.removeEventListener('mousedown', mouseCallback, true);
  540. this._shuttleRingBackPanel.removeEventListener('touchstart', mouseCallback, true);
  541. this._shuttleRingSwooshG.removeEventListener('mousedown', mouseCallback, true);
  542. this._shuttleRingSwooshG.removeEventListener('touchstart', mouseCallback, true);
  543. document.removeEventListener('mousemove', mouseCallback, true);
  544. document.removeEventListener('touchmove', mouseCallback, true);
  545. document.removeEventListener('mouseup', mouseCallback, true);
  546. document.removeEventListener('touchend', mouseCallback, true);
  547. this._shuttleRingPointer.removeEventListener('mousedown', mouseCallback, true);
  548. this._shuttleRingPointer.removeEventListener('touchstart', mouseCallback, true);
  549. this._knobOuter.removeEventListener('mousedown', mouseCallback, true);
  550. this._knobOuter.removeEventListener('touchstart', mouseCallback, true);
  551. this._container.removeChild(this._svgNode);
  552. this._container.removeChild(this._theme);
  553. this._realtimeSVG.destroy();
  554. this._playReverseSVG.destroy();
  555. this._playForwardSVG.destroy();
  556. this._pauseSVG.destroy();
  557. var subscriptions = this._subscriptions;
  558. for ( var i = 0, len = subscriptions.length; i < len; i++) {
  559. subscriptions[i].dispose();
  560. }
  561. return destroyObject(this);
  562. };
  563. /**
  564. * Resizes the widget to match the container size.
  565. * This function should be called whenever the container size is changed.
  566. */
  567. Animation.prototype.resize = function() {
  568. var parentWidth = this._container.clientWidth;
  569. var parentHeight = this._container.clientHeight;
  570. if (parentWidth === this._lastWidth && parentHeight === this._lastHeight) {
  571. return;
  572. }
  573. var svg = this._svgNode;
  574. //The width and height as the SVG was originally drawn.
  575. var baseWidth = 200;
  576. var baseHeight = 132;
  577. var width = parentWidth;
  578. var height = parentHeight;
  579. if (parentWidth === 0 && parentHeight === 0) {
  580. width = baseWidth;
  581. height = baseHeight;
  582. } else if (parentWidth === 0) {
  583. height = parentHeight;
  584. width = baseWidth * (parentHeight / baseHeight);
  585. } else if (parentHeight === 0) {
  586. width = parentWidth;
  587. height = baseHeight * (parentWidth / baseWidth);
  588. }
  589. var scaleX = width / baseWidth;
  590. var scaleY = height / baseHeight;
  591. svg.style.cssText = 'width: ' + width + 'px; height: ' + height + 'px; position: absolute; bottom: 0; left: 0; overflow: hidden;';
  592. svg.setAttribute('width', width);
  593. svg.setAttribute('height', height);
  594. svg.setAttribute('viewBox', '0 0 ' + width + ' ' + height);
  595. this._topG.setAttribute('transform', 'scale(' + scaleX + ',' + scaleY + ')');
  596. this._centerX = Math.max(1, 100.0 * scaleX);
  597. this._centerY = Math.max(1, 100.0 * scaleY);
  598. this._lastHeight = parentWidth;
  599. this._lastWidth = parentHeight;
  600. };
  601. /**
  602. * Updates the widget to reflect any modified CSS rules for theming.
  603. *
  604. * @example
  605. * //Switch to the cesium-lighter theme.
  606. * document.body.className = 'cesium-lighter';
  607. * animation.applyThemeChanges();
  608. */
  609. Animation.prototype.applyThemeChanges = function() {
  610. var buttonNormalBackColor = getElementColor(this._themeNormal);
  611. var buttonHoverBackColor = getElementColor(this._themeHover);
  612. var buttonToggledBackColor = getElementColor(this._themeSelect);
  613. var buttonDisabledBackColor = getElementColor(this._themeDisabled);
  614. var knobBackColor = getElementColor(this._themeKnob);
  615. var pointerColor = getElementColor(this._themePointer);
  616. var swooshColor = getElementColor(this._themeSwoosh);
  617. var swooshHoverColor = getElementColor(this._themeSwooshHover);
  618. var defsElement = svgFromObject({
  619. tagName : 'defs',
  620. children : [{
  621. id : 'animation_buttonNormal',
  622. tagName : 'linearGradient',
  623. x1 : '50%',
  624. y1 : '0%',
  625. x2 : '50%',
  626. y2 : '100%',
  627. children : [
  628. //add a 'stop-opacity' field to make translucent.
  629. {
  630. tagName : 'stop',
  631. offset : '0%',
  632. 'stop-color' : makeColorString(buttonNormalBackColor, gradientEnabledColor0)
  633. }, {
  634. tagName : 'stop',
  635. offset : '12%',
  636. 'stop-color' : makeColorString(buttonNormalBackColor, gradientEnabledColor1)
  637. }, {
  638. tagName : 'stop',
  639. offset : '46%',
  640. 'stop-color' : makeColorString(buttonNormalBackColor, gradientEnabledColor2)
  641. }, {
  642. tagName : 'stop',
  643. offset : '81%',
  644. 'stop-color' : makeColorString(buttonNormalBackColor, gradientEnabledColor3)
  645. }]
  646. }, {
  647. id : 'animation_buttonHovered',
  648. tagName : 'linearGradient',
  649. x1 : '50%',
  650. y1 : '0%',
  651. x2 : '50%',
  652. y2 : '100%',
  653. children : [{
  654. tagName : 'stop',
  655. offset : '0%',
  656. 'stop-color' : makeColorString(buttonHoverBackColor, gradientEnabledColor0)
  657. }, {
  658. tagName : 'stop',
  659. offset : '12%',
  660. 'stop-color' : makeColorString(buttonHoverBackColor, gradientEnabledColor1)
  661. }, {
  662. tagName : 'stop',
  663. offset : '46%',
  664. 'stop-color' : makeColorString(buttonHoverBackColor, gradientEnabledColor2)
  665. }, {
  666. tagName : 'stop',
  667. offset : '81%',
  668. 'stop-color' : makeColorString(buttonHoverBackColor, gradientEnabledColor3)
  669. }]
  670. }, {
  671. id : 'animation_buttonToggled',
  672. tagName : 'linearGradient',
  673. x1 : '50%',
  674. y1 : '0%',
  675. x2 : '50%',
  676. y2 : '100%',
  677. children : [{
  678. tagName : 'stop',
  679. offset : '0%',
  680. 'stop-color' : makeColorString(buttonToggledBackColor, gradientEnabledColor0)
  681. }, {
  682. tagName : 'stop',
  683. offset : '12%',
  684. 'stop-color' : makeColorString(buttonToggledBackColor, gradientEnabledColor1)
  685. }, {
  686. tagName : 'stop',
  687. offset : '46%',
  688. 'stop-color' : makeColorString(buttonToggledBackColor, gradientEnabledColor2)
  689. }, {
  690. tagName : 'stop',
  691. offset : '81%',
  692. 'stop-color' : makeColorString(buttonToggledBackColor, gradientEnabledColor3)
  693. }]
  694. }, {
  695. id : 'animation_buttonDisabled',
  696. tagName : 'linearGradient',
  697. x1 : '50%',
  698. y1 : '0%',
  699. x2 : '50%',
  700. y2 : '100%',
  701. children : [{
  702. tagName : 'stop',
  703. offset : '0%',
  704. 'stop-color' : makeColorString(buttonDisabledBackColor, gradientDisabledColor0)
  705. }, {
  706. tagName : 'stop',
  707. offset : '75%',
  708. 'stop-color' : makeColorString(buttonDisabledBackColor, gradientDisabledColor1)
  709. }]
  710. }, {
  711. id : 'animation_blurred',
  712. tagName : 'filter',
  713. width : '200%',
  714. height : '200%',
  715. x : '-50%',
  716. y : '-50%',
  717. children : [{
  718. tagName : 'feGaussianBlur',
  719. stdDeviation : 4,
  720. 'in' : 'SourceGraphic'
  721. }]
  722. }, {
  723. id : 'animation_shuttleRingSwooshGradient',
  724. tagName : 'linearGradient',
  725. x1 : '50%',
  726. y1 : '0%',
  727. x2 : '50%',
  728. y2 : '100%',
  729. children : [{
  730. tagName : 'stop',
  731. offset : '0%',
  732. 'stop-opacity' : 0.2,
  733. 'stop-color' : swooshColor.toCssColorString()
  734. }, {
  735. tagName : 'stop',
  736. offset : '85%',
  737. 'stop-opacity' : 0.85,
  738. 'stop-color' : swooshColor.toCssColorString()
  739. }, {
  740. tagName : 'stop',
  741. offset : '95%',
  742. 'stop-opacity' : 0.05,
  743. 'stop-color' : swooshColor.toCssColorString()
  744. }]
  745. }, {
  746. id : 'animation_shuttleRingSwooshHovered',
  747. tagName : 'linearGradient',
  748. x1 : '50%',
  749. y1 : '0%',
  750. x2 : '50%',
  751. y2 : '100%',
  752. children : [{
  753. tagName : 'stop',
  754. offset : '0%',
  755. 'stop-opacity' : 0.2,
  756. 'stop-color' : swooshHoverColor.toCssColorString()
  757. }, {
  758. tagName : 'stop',
  759. offset : '85%',
  760. 'stop-opacity' : 0.85,
  761. 'stop-color' : swooshHoverColor.toCssColorString()
  762. }, {
  763. tagName : 'stop',
  764. offset : '95%',
  765. 'stop-opacity' : 0.05,
  766. 'stop-color' : swooshHoverColor.toCssColorString()
  767. }]
  768. }, {
  769. id : 'animation_shuttleRingPointerGradient',
  770. tagName : 'linearGradient',
  771. x1 : '0%',
  772. y1 : '50%',
  773. x2 : '100%',
  774. y2 : '50%',
  775. children : [{
  776. tagName : 'stop',
  777. offset : '0%',
  778. 'stop-color' : pointerColor.toCssColorString()
  779. }, {
  780. tagName : 'stop',
  781. offset : '40%',
  782. 'stop-color' : pointerColor.toCssColorString()
  783. }, {
  784. tagName : 'stop',
  785. offset : '60%',
  786. 'stop-color' : makeColorString(pointerColor, gradientPointerColor)
  787. }, {
  788. tagName : 'stop',
  789. offset : '100%',
  790. 'stop-color' : makeColorString(pointerColor, gradientPointerColor)
  791. }]
  792. }, {
  793. id : 'animation_shuttleRingPointerPaused',
  794. tagName : 'linearGradient',
  795. x1 : '0%',
  796. y1 : '50%',
  797. x2 : '100%',
  798. y2 : '50%',
  799. children : [{
  800. tagName : 'stop',
  801. offset : '0%',
  802. 'stop-color' : '#CCC'
  803. }, {
  804. tagName : 'stop',
  805. offset : '40%',
  806. 'stop-color' : '#CCC'
  807. }, {
  808. tagName : 'stop',
  809. offset : '60%',
  810. 'stop-color' : '#555'
  811. }, {
  812. tagName : 'stop',
  813. offset : '100%',
  814. 'stop-color' : '#555'
  815. }]
  816. }, {
  817. id : 'animation_knobOuter',
  818. tagName : 'linearGradient',
  819. x1 : '20%',
  820. y1 : '0%',
  821. x2 : '90%',
  822. y2 : '100%',
  823. children : [{
  824. tagName : 'stop',
  825. offset : '5%',
  826. 'stop-color' : makeColorString(knobBackColor, gradientEnabledColor0)
  827. }, {
  828. tagName : 'stop',
  829. offset : '60%',
  830. 'stop-color' : makeColorString(knobBackColor, gradientKnobColor)
  831. }, {
  832. tagName : 'stop',
  833. offset : '85%',
  834. 'stop-color' : makeColorString(knobBackColor, gradientEnabledColor1)
  835. }]
  836. }, {
  837. id : 'animation_knobInner',
  838. tagName : 'linearGradient',
  839. x1 : '20%',
  840. y1 : '0%',
  841. x2 : '90%',
  842. y2 : '100%',
  843. children : [{
  844. tagName : 'stop',
  845. offset : '5%',
  846. 'stop-color' : makeColorString(knobBackColor, gradientKnobColor)
  847. }, {
  848. tagName : 'stop',
  849. offset : '60%',
  850. 'stop-color' : makeColorString(knobBackColor, gradientEnabledColor0)
  851. }, {
  852. tagName : 'stop',
  853. offset : '85%',
  854. 'stop-color' : makeColorString(knobBackColor, gradientEnabledColor3)
  855. }]
  856. }, {
  857. id : 'animation_pathReset',
  858. tagName : 'path',
  859. transform : 'translate(16,16) scale(0.85) translate(-16,-16)',
  860. d : 'M24.316,5.318,9.833,13.682,9.833,5.5,5.5,5.5,5.5,25.5,9.833,25.5,9.833,17.318,24.316,25.682z'
  861. }, {
  862. id : 'animation_pathPause',
  863. tagName : 'path',
  864. transform : 'translate(16,16) scale(0.85) translate(-16,-16)',
  865. d : 'M13,5.5,7.5,5.5,7.5,25.5,13,25.5zM24.5,5.5,19,5.5,19,25.5,24.5,25.5z'
  866. }, {
  867. id : 'animation_pathPlay',
  868. tagName : 'path',
  869. transform : 'translate(16,16) scale(0.85) translate(-16,-16)',
  870. d : 'M6.684,25.682L24.316,15.5L6.684,5.318V25.682z'
  871. }, {
  872. id : 'animation_pathPlayReverse',
  873. tagName : 'path',
  874. transform : 'translate(16,16) scale(-0.85,0.85) translate(-16,-16)',
  875. d : 'M6.684,25.682L24.316,15.5L6.684,5.318V25.682z'
  876. }, {
  877. id : 'animation_pathLoop',
  878. tagName : 'path',
  879. transform : 'translate(16,16) scale(0.85) translate(-16,-16)',
  880. d : 'M24.249,15.499c-0.009,4.832-3.918,8.741-8.75,8.75c-2.515,0-4.768-1.064-6.365-2.763l2.068-1.442l-7.901-3.703l0.744,8.694l2.193-1.529c2.244,2.594,5.562,4.242,9.26,4.242c6.767,0,12.249-5.482,12.249-12.249H24.249zM15.499,6.75c2.516,0,4.769,1.065,6.367,2.764l-2.068,1.443l7.901,3.701l-0.746-8.693l-2.192,1.529c-2.245-2.594-5.562-4.245-9.262-4.245C8.734,3.25,3.25,8.734,3.249,15.499H6.75C6.758,10.668,10.668,6.758,15.499,6.75z'
  881. }, {
  882. id : 'animation_pathClock',
  883. tagName : 'path',
  884. transform : 'translate(16,16) scale(0.85) translate(-16,-15.5)',
  885. d : 'M15.5,2.374C8.251,2.375,2.376,8.251,2.374,15.5C2.376,22.748,8.251,28.623,15.5,28.627c7.249-0.004,13.124-5.879,13.125-13.127C28.624,8.251,22.749,2.375,15.5,2.374zM15.5,25.623C9.909,25.615,5.385,21.09,5.375,15.5C5.385,9.909,9.909,5.384,15.5,5.374c5.59,0.01,10.115,4.535,10.124,10.125C25.615,21.09,21.091,25.615,15.5,25.623zM8.625,15.5c-0.001-0.552-0.448-0.999-1.001-1c-0.553,0-1,0.448-1,1c0,0.553,0.449,1,1,1C8.176,16.5,8.624,16.053,8.625,15.5zM8.179,18.572c-0.478,0.277-0.642,0.889-0.365,1.367c0.275,0.479,0.889,0.641,1.365,0.365c0.479-0.275,0.643-0.887,0.367-1.367C9.27,18.461,8.658,18.297,8.179,18.572zM9.18,10.696c-0.479-0.276-1.09-0.112-1.366,0.366s-0.111,1.09,0.365,1.366c0.479,0.276,1.09,0.113,1.367-0.366C9.821,11.584,9.657,10.973,9.18,10.696zM22.822,12.428c0.478-0.275,0.643-0.888,0.366-1.366c-0.275-0.478-0.89-0.642-1.366-0.366c-0.479,0.278-0.642,0.89-0.366,1.367C21.732,12.54,22.344,12.705,22.822,12.428zM12.062,21.455c-0.478-0.275-1.089-0.111-1.366,0.367c-0.275,0.479-0.111,1.09,0.366,1.365c0.478,0.277,1.091,0.111,1.365-0.365C12.704,22.344,12.54,21.732,12.062,21.455zM12.062,9.545c0.479-0.276,0.642-0.888,0.366-1.366c-0.276-0.478-0.888-0.642-1.366-0.366s-0.642,0.888-0.366,1.366C10.973,9.658,11.584,9.822,12.062,9.545zM22.823,18.572c-0.48-0.275-1.092-0.111-1.367,0.365c-0.275,0.479-0.112,1.092,0.367,1.367c0.477,0.275,1.089,0.113,1.365-0.365C23.464,19.461,23.3,18.848,22.823,18.572zM19.938,7.813c-0.477-0.276-1.091-0.111-1.365,0.366c-0.275,0.48-0.111,1.091,0.366,1.367s1.089,0.112,1.366-0.366C20.581,8.702,20.418,8.089,19.938,7.813zM23.378,14.5c-0.554,0.002-1.001,0.45-1.001,1c0.001,0.552,0.448,1,1.001,1c0.551,0,1-0.447,1-1C24.378,14.949,23.929,14.5,23.378,14.5zM15.501,6.624c-0.552,0-1,0.448-1,1l-0.466,7.343l-3.004,1.96c-0.478,0.277-0.642,0.889-0.365,1.365c0.275,0.479,0.889,0.643,1.365,0.367l3.305-1.676C15.39,16.99,15.444,17,15.501,17c0.828,0,1.5-0.671,1.5-1.5l-0.5-7.876C16.501,7.072,16.053,6.624,15.501,6.624zM15.501,22.377c-0.552,0-1,0.447-1,1s0.448,1,1,1s1-0.447,1-1S16.053,22.377,15.501,22.377zM18.939,21.455c-0.479,0.277-0.643,0.889-0.366,1.367c0.275,0.477,0.888,0.643,1.366,0.365c0.478-0.275,0.642-0.889,0.366-1.365C20.028,21.344,19.417,21.18,18.939,21.455z'
  886. }, {
  887. id : 'animation_pathWingButton',
  888. tagName : 'path',
  889. d : 'm 4.5,0.5 c -2.216,0 -4,1.784 -4,4 l 0,24 c 0,2.216 1.784,4 4,4 l 13.71875,0 C 22.478584,27.272785 27.273681,22.511272 32.5,18.25 l 0,-13.75 c 0,-2.216 -1.784,-4 -4,-4 l -24,0 z'
  890. }, {
  891. id : 'animation_pathPointer',
  892. tagName : 'path',
  893. d : 'M-15,-65,-15,-55,15,-55,15,-65,0,-95z'
  894. }, {
  895. id : 'animation_pathSwooshFX',
  896. tagName : 'path',
  897. d : 'm 85,0 c 0,16.617 -4.813944,35.356 -13.131081,48.4508 h 6.099803 c 8.317138,-13.0948 13.13322,-28.5955 13.13322,-45.2124 0,-46.94483 -38.402714,-85.00262 -85.7743869,-85.00262 -1.0218522,0 -2.0373001,0.0241 -3.0506131,0.0589 45.958443,1.59437 82.723058,35.77285 82.723058,81.70532 z'
  898. }]
  899. });
  900. if (!defined(this._defsElement)) {
  901. this._svgNode.appendChild(defsElement);
  902. } else {
  903. this._svgNode.replaceChild(defsElement, this._defsElement);
  904. }
  905. this._defsElement = defsElement;
  906. };
  907. return Animation;
  908. });