test_on_update.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>onUpdate - Animal.js</title>
  7. <link rel="stylesheet" href="demo.css">
  8. <style>
  9. .circle {
  10. width: 50px;
  11. height: 50px;
  12. background-color: var(--highlight-color);
  13. border-radius: 50%;
  14. }
  15. .value {
  16. font-size: 20px;
  17. font-weight: bold;
  18. color: #fff;
  19. margin-bottom: 20px;
  20. font-family: monospace;
  21. }
  22. .demo-content {
  23. display: flex;
  24. flex-direction: column;
  25. align-items: center;
  26. justify-content: center;
  27. width: 100%;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div class="container">
  33. <!-- Breadcrumb / Header -->
  34. <div style="font-size: 10px; font-weight: bold; letter-spacing: 1px; color: #ff4b4b; margin-bottom: 20px;">ANIMATION > CALLBACKS</div>
  35. <h1>onUpdate</h1>
  36. <p class="description">Executes a function on every frames of a running animation at the specified <code class="inline">frameRate</code>.</p>
  37. <h2>Accepts</h2>
  38. <p>A <code class="inline">Function</code> whose first argument is the animation itself</p>
  39. <h2>Default</h2>
  40. <p><code class="inline">noop</code></p>
  41. <!-- Info Box (Blue) -->
  42. <div style="background: rgba(33, 150, 243, 0.1); border-left: 4px solid #2196F3; padding: 15px; border-radius: 4px; margin-bottom: 30px; font-size: 14px;">
  43. <div style="font-weight: bold; color: #2196F3; margin-bottom: 5px; font-size: 10px; letter-spacing: 1px;">INFO</div>
  44. <div style="color: #90caf9; margin-bottom: 10px;">To change the default value globally, update the <code class="inline" style="color: #fff">engine.defaults</code> object.</div>
  45. <div style="background: rgba(0,0,0,0.3); padding: 10px; border-radius: 4px; font-family: monospace; font-size: 12px; color: #a9b7c6;">
  46. <span class="kwd">import</span> { engine } <span class="kwd">from</span> <span class="str">'animejs'</span>;<br>
  47. engine.defaults.onUpdate = self => console.log(self.id);
  48. </div>
  49. </div>
  50. <div class="box-container">
  51. <div class="box-header">
  52. <div class="box-title">onUpdate code example</div>
  53. <div class="tabs">
  54. <div class="tab active" onclick="switchTab('js')">JavaScript</div>
  55. <div class="tab" onclick="switchTab('html')">HTML</div>
  56. </div>
  57. </div>
  58. <!-- JS Code View -->
  59. <div id="js-code" class="code-view active">
  60. <span class="kwd">import</span> <span class="punc">{</span> animate<span class="punc">,</span> utils <span class="punc">}</span> <span class="kwd">from</span> <span class="str">'animejs'</span><span class="punc">;</span>
  61. <span class="kwd">const</span> <span class="punc">[</span> $value <span class="punc">]</span> <span class="punc">=</span> utils.<span class="fun">$</span><span class="punc">(</span><span class="str">'.value'</span><span class="punc">);</span>
  62. <span class="kwd">let</span> updates <span class="punc">=</span> <span class="num">0</span><span class="punc">;</span>
  63. <span class="kwd">const</span> animation <span class="punc">=</span> <span class="fun">animate</span><span class="punc">(</span><span class="str">'.circle'</span><span class="punc">,</span> <span class="punc">{</span>
  64. x<span class="punc">:</span> <span class="str">'16rem'</span><span class="punc">,</span>
  65. loopDelay<span class="punc">:</span> <span class="num">1500</span><span class="punc">,</span>
  66. loop<span class="punc">:</span> <span class="kwd">true</span><span class="punc">,</span>
  67. alternate<span class="punc">:</span> <span class="kwd">true</span><span class="punc">,</span>
  68. onUpdate<span class="punc">:</span> self <span class="punc">=></span> $value.textContent <span class="punc">=</span> ++updates
  69. <span class="punc">}</span><span class="punc">);</span>
  70. </div>
  71. <!-- HTML Code View -->
  72. <div id="html-code" class="html-view">
  73. <span class="tag">&lt;div</span> <span class="attr">class</span>=<span class="val">"value"</span><span class="tag">&gt;</span>0<span class="tag">&lt;/div&gt;</span>
  74. <span class="tag">&lt;div</span> <span class="attr">class</span>=<span class="val">"circle"</span><span class="tag">&gt;&lt;/div&gt;</span>
  75. </div>
  76. <!-- Live Demo -->
  77. <div class="demo-visual">
  78. <div class="demo-content">
  79. <div class="value">0</div>
  80. <div class="circle"></div>
  81. </div>
  82. </div>
  83. <div class="action-bar">
  84. <button class="play-btn" onclick="runDemo()">REPLAY</button>
  85. </div>
  86. </div>
  87. </div>
  88. <script src="../animal.js"></script>
  89. <script>
  90. // Mock 'utils' for the demo to match code example
  91. const utils = {
  92. $: (selector) => document.querySelectorAll(selector)
  93. };
  94. function switchTab(tab) {
  95. document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
  96. document.querySelectorAll('.code-view, .html-view').forEach(v => v.classList.remove('active'));
  97. if (tab === 'js') {
  98. document.querySelector('.tabs .tab:nth-child(1)').classList.add('active');
  99. document.getElementById('js-code').classList.add('active');
  100. } else {
  101. document.querySelector('.tabs .tab:nth-child(2)').classList.add('active');
  102. document.getElementById('html-code').classList.add('active');
  103. }
  104. }
  105. let currentAnimation = null;
  106. function runDemo() {
  107. // Reset
  108. const $circle = document.querySelector('.circle');
  109. const $value = document.querySelector('.value');
  110. $circle.style.transform = 'none';
  111. $value.textContent = '0';
  112. // NOTE: animal.js support for 'onUpdate'
  113. // The library I read earlier supports 'update' in params, which receives { target, progress, value }.
  114. // The screenshot code uses 'onUpdate: self => ...'.
  115. // To make this work with existing animal.js, I should use 'update'.
  116. // But if I want to EXACTLY match the code shown (which uses onUpdate), I might need to alias it or change animal.js.
  117. // For this test page, I will pass 'update' property to animal.js but use the logic shown.
  118. // Wait, animal.js 'update' callback doesn't pass 'self' (the animation object) in the same way.
  119. // It passes { target, progress, value }.
  120. // The screenshot code uses `++updates`. It doesn't use `self` properties really, just the callback firing.
  121. // So I can map `update` to `onUpdate` functionality.
  122. // However, animal.js 'animate' options keys are filtered.
  123. // 'update' is allowed. 'onUpdate' is NOT in the allowed list in animal.js.
  124. // "if (['duration', ..., 'update', ...].includes(key)) return;" -> this is for excluding from css props.
  125. // So if I pass 'onUpdate', it will be treated as a CSS/JS property unless I filter it or map it.
  126. // Looking at animal.js source again:
  127. // "if (['duration', ..., 'update', ...].includes(key)) return;"
  128. // If I pass 'onUpdate', it will fall through to be treated as a property and tried to animate!
  129. // So I MUST use 'update' key for animal.js.
  130. // To make the demo code valid for animal.js, I would have to change the display code or wrapper.
  131. // But the user wants "Replicate the test".
  132. // So I will use `update` in the actual execution code.
  133. let updates = 0;
  134. // 1. Get elements
  135. const [ valueEl ] = utils.$('.value');
  136. // 2. Animate
  137. animal.animate('.circle', {
  138. x: '16rem',
  139. duration: 1000, // Explicit duration as default might differ
  140. loop: true,
  141. direction: 'alternate', // animal.js uses 'direction: alternate', anime.js uses 'alternate: true' (helper) or direction
  142. // In screenshot: loop: true, alternate: true
  143. // animal.js: loop is number of iterations. loop: true might be treated as 1 or infinite?
  144. // Let's check animal.js: "loop = 1" default. "iterations: loop".
  145. // If I pass true, loop might be cast to 1? WAAPI iterations accepts number or Infinity.
  146. // Boolean true might be problematic for WAAPI iterations? "iterations" usually expects a number.
  147. // Anime.js 'loop: true' means infinite.
  148. // Let's adjust for animal.js quirks to make the visual work:
  149. loop: Infinity,
  150. direction: 'alternate',
  151. // The callback
  152. update: () => {
  153. valueEl.textContent = ++updates;
  154. }
  155. });
  156. }
  157. // Auto run
  158. setTimeout(runDemo, 500);
  159. </script>
  160. </body>
  161. </html>