animation.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Animations
  2. var animationBar = document.getElementById("animationBar");
  3. var dropdownBtn = document.getElementById("dropdownBtn");
  4. var chevronUp = document.getElementById("chevronUp");
  5. var chevronDown = document.getElementById("chevronDown");
  6. var dropdownLabel = document.getElementById("dropdownLabel");
  7. var dropdownContent = document.getElementById("dropdownContent");
  8. var playBtn = document.getElementById("playBtn");
  9. var slider = document.getElementById("slider");
  10. // event on the dropdown
  11. function formatId(name) {
  12. return "data-" + name.replace(/\s/g, '');
  13. }
  14. function displayDropdownContent(display) {
  15. if (display) {
  16. dropdownContent.style.display = "flex";
  17. chevronDown.style.display = "inline";
  18. chevronUp.style.display = "none";
  19. }
  20. else {
  21. dropdownContent.style.display = "none";
  22. chevronDown.style.display = "none";
  23. chevronUp.style.display = "inline";
  24. }
  25. }
  26. dropdownBtn.addEventListener("click", function() {
  27. if (dropdownContent.style.display === "flex") {
  28. displayDropdownContent(false);
  29. }
  30. else {
  31. displayDropdownContent(true);
  32. }
  33. });
  34. function selectCurrentGroup(group, index, animation) {
  35. if (currentGroupIndex !== undefined) {
  36. document.getElementById(formatId(currentGroup.name + "-" + currentGroupIndex)).classList.remove("active");
  37. }
  38. playBtn.classList.remove("play");
  39. playBtn.classList.add("pause");
  40. // start the new animation group
  41. currentGroup = group;
  42. currentGroupIndex = index;
  43. animation.classList.add("active");
  44. dropdownLabel.innerHTML = currentGroup.name;
  45. dropdownLabel.title = currentGroup.name;
  46. // set the slider
  47. slider.setAttribute("min", currentGroup.from);
  48. slider.setAttribute("max", currentGroup.to);
  49. currentSliderValue = currentGroup.from;
  50. slider.value = currentGroup.from;
  51. }
  52. function createDropdownLink(group, index) {
  53. var animation = document.createElement("a");
  54. animation.innerHTML = group.name;
  55. animation.title = group.name;
  56. animation.setAttribute("id", formatId(group.name + "-" + index));
  57. animation.addEventListener("click", function() {
  58. // stop the current animation group
  59. currentGroup.reset();
  60. currentGroup.stop();
  61. group.play(true);
  62. // hide the content of the dropdown
  63. displayDropdownContent(false);
  64. });
  65. dropdownContent.appendChild(animation);
  66. group.onAnimationGroupPlayObservable.add(function(grp) {
  67. selectCurrentGroup(grp, index, animation);
  68. });
  69. group.onAnimationGroupPauseObservable.add(function(grp) {
  70. playBtn.classList.add("play");
  71. playBtn.classList.remove("pause");
  72. });
  73. }
  74. // event on the play/pause button
  75. playBtn.addEventListener("click", function() {
  76. // click on the button to run the animation
  77. if (this.classList.contains("play")) {
  78. currentGroup.play(true);
  79. }
  80. // click on the button to pause the animation
  81. else {
  82. currentGroup.pause();
  83. }
  84. });
  85. // event on the slider
  86. slider.addEventListener("input", function() {
  87. var value = parseFloat(this.value);
  88. if (playBtn.classList.contains("play")) {
  89. currentGroup.play(true);
  90. currentGroup.goToFrame(value);
  91. currentGroup.pause();
  92. } else {
  93. currentGroup.goToFrame(value);
  94. }
  95. });
  96. var sliderPause = false;
  97. slider.addEventListener("mousedown", function() {
  98. if (playBtn.classList.contains("pause")) {
  99. sliderPause = true;
  100. playBtn.click();
  101. }
  102. });
  103. slider.addEventListener("mouseup", function() {
  104. if (sliderPause) {
  105. sliderPause = false;
  106. playBtn.click();
  107. }
  108. });