Animation.js 43 KB

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