test_morph_points.html 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>SVG Morph (points) - Animal.js</title>
  7. <link rel="stylesheet" href="../demo.css">
  8. <style>
  9. .hint {
  10. color: #9a9a9a;
  11. font-size: 12px;
  12. line-height: 1.65;
  13. margin-top: -8px;
  14. margin-bottom: 16px;
  15. }
  16. .demo-visual {
  17. padding: 26px 30px;
  18. background: #151515;
  19. display: grid;
  20. grid-template-columns: 1fr 280px;
  21. gap: 16px;
  22. align-items: stretch;
  23. }
  24. .stage {
  25. background: rgba(0,0,0,0.25);
  26. border: 1px solid #262626;
  27. border-radius: 12px;
  28. padding: 14px;
  29. overflow: hidden;
  30. position: relative;
  31. min-height: 280px;
  32. display: grid;
  33. place-items: center;
  34. }
  35. .panel {
  36. background: rgba(0,0,0,0.18);
  37. border: 1px solid #262626;
  38. border-radius: 12px;
  39. padding: 14px;
  40. display: flex;
  41. flex-direction: column;
  42. gap: 12px;
  43. justify-content: space-between;
  44. }
  45. .control {
  46. display: grid;
  47. grid-template-columns: 90px 1fr 64px;
  48. gap: 10px;
  49. align-items: center;
  50. }
  51. .control label { color: #bdbdbd; font-size: 12px; font-weight: 800; }
  52. .control output { color: #e6e6e6; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 12px; }
  53. input[type="range"] { width: 100%; }
  54. svg { width: 100%; height: 100%; max-height: 320px; }
  55. .shape {
  56. fill: rgba(255,159,67,0.14);
  57. stroke: rgba(255,159,67,0.55);
  58. stroke-width: 2;
  59. filter: drop-shadow(0 0 18px rgba(255,159,67,0.10));
  60. }
  61. .shape.red {
  62. fill: rgba(255,75,75,0.12);
  63. stroke: rgba(255,75,75,0.50);
  64. filter: drop-shadow(0 0 18px rgba(255,75,75,0.10));
  65. }
  66. .label {
  67. fill: rgba(255,255,255,0.22);
  68. font-weight: 800;
  69. font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  70. font-size: 12px;
  71. letter-spacing: 0.8px;
  72. }
  73. </style>
  74. </head>
  75. <body>
  76. <div class="container">
  77. <div class="page-top">
  78. <div class="crumb">ANIMATION › SVG</div>
  79. <div class="since">SINCE 1.0.0</div>
  80. </div>
  81. <h1>变形动画:points(多边形)</h1>
  82. <p class="description">
  83. 这是最“稳”的 morph:只要两边的 <code class="inline">points</code> 有<strong>相同数量的数字</strong>,
  84. 就能直接做平滑过渡(本库会把字符串里的数字逐个插值)。
  85. </p>
  86. <p class="hint">
  87. <strong>规则:</strong>points 其实就是一串数字:<code class="inline">x1 y1 x2 y2 ...</code>。
  88. 你想从 A 变到 B,就确保 A/B 的点数量一致(比如都 12 个点)。
  89. </p>
  90. <div class="box-container">
  91. <div class="box-header">
  92. <div class="box-title">Morph (points) code example</div>
  93. <div class="box-right">
  94. <button class="icon-btn" type="button" title="Copy" onclick="DocPage.copyActiveCode()" aria-label="Copy code">
  95. <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
  96. <rect x="9" y="9" width="13" height="13" rx="2"></rect>
  97. <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path>
  98. </svg>
  99. </button>
  100. <div class="tabs" role="tablist" aria-label="Code tabs">
  101. <div class="tab active" role="tab" aria-selected="true" tabindex="0" onclick="DocPage.switchTab('js')">JavaScript</div>
  102. <div class="tab" role="tab" aria-selected="false" tabindex="-1" onclick="DocPage.switchTab('html')">HTML</div>
  103. <div class="tab" role="tab" aria-selected="false" tabindex="-1" onclick="DocPage.switchTab('css')">CSS</div>
  104. </div>
  105. </div>
  106. </div>
  107. <div id="js-code" class="code-view active">
  108. <span class="kwd">const</span> star <span class="punc">=</span> <span class="str">'160 35 185 75 230 80 195 110 206 155 160 132 114 155 125 110 90 80 135 75'</span><span class="punc">;</span>
  109. <span class="kwd">const</span> blob <span class="punc">=</span> <span class="str">'160 50 205 60 235 95 220 130 190 165 160 175 130 165 100 130 85 95 115 60'</span><span class="punc">;</span>
  110. <span class="fun">xjs</span><span class="punc">(</span><span class="str">'#poly'</span><span class="punc">)</span>.<span class="fun">animate</span><span class="punc">({</span>
  111. points<span class="punc">:</span> <span class="punc">[</span>star<span class="punc">,</span> blob<span class="punc">]</span><span class="punc">,</span>
  112. duration<span class="punc">:</span> <span class="num">1200</span><span class="punc">,</span>
  113. easing<span class="punc">:</span> <span class="str">'ease-in-out'</span>
  114. <span class="punc">}</span><span class="punc">)</span><span class="punc">;</span>
  115. </div>
  116. <div id="html-code" class="html-view">
  117. <span class="tag">&lt;polygon</span> <span class="attr">id</span>=<span class="val">"poly"</span> <span class="attr">points</span>=<span class="val">"160 35 ..."</span> <span class="tag">/&gt;</span>
  118. </div>
  119. <pre id="css-code" class="css-view">.shape { fill: rgba(255,159,67,0.14); stroke: rgba(255,159,67,0.55); stroke-width: 2; }</pre>
  120. <div class="feature-desc">
  121. <strong>功能说明:</strong>
  122. <br>- 这里的 <code class="inline">points</code> 是“数字模板字符串”。本库会识别其中的数字并逐个插值。
  123. <br>- 适合:图标变形、加载态、装饰动效(尤其是多边形/折线)。
  124. </div>
  125. <div class="demo-visual">
  126. <div class="stage">
  127. <svg viewBox="0 0 320 220" aria-label="Morph points demo">
  128. <polygon id="poly" class="shape"
  129. points="160 35 185 75 230 80 195 110 206 155 160 132 114 155 125 110 90 80 135 75"></polygon>
  130. <polygon id="poly2" class="shape red" opacity="0.65"
  131. points="160 50 205 60 235 95 220 130 190 165 160 175 130 165 100 130 85 95 115 60"></polygon>
  132. <text class="label" x="160" y="210" text-anchor="middle">points morph (same number tokens)</text>
  133. </svg>
  134. </div>
  135. <div class="panel">
  136. <div>
  137. <div class="control">
  138. <label for="dur">duration</label>
  139. <input id="dur" type="range" min="200" max="3200" step="50" value="1200" />
  140. <output id="durOut">1200ms</output>
  141. </div>
  142. <div class="control">
  143. <label for="loop">loop</label>
  144. <input id="loop" type="range" min="1" max="6" step="1" value="2" />
  145. <output id="loopOut">2</output>
  146. </div>
  147. </div>
  148. <div style="display:flex; gap:10px; justify-content:flex-end;">
  149. <button class="play-btn secondary" onclick="pauseDemo()">PAUSE</button>
  150. <button class="play-btn secondary" onclick="resumeDemo()">RESUME</button>
  151. <button class="play-btn" onclick="runDemo()">REPLAY</button>
  152. </div>
  153. </div>
  154. </div>
  155. </div>
  156. <div class="doc-nav" aria-label="Previous and next navigation">
  157. <a href="#" onclick="goPrev(); return false;">
  158. <span>
  159. <span class="nav-label">Previous</span><br>
  160. <span id="prev-title" class="nav-title">—</span>
  161. </span>
  162. <span aria-hidden="true">←</span>
  163. </a>
  164. <div id="nav-center" class="nav-center">SVG</div>
  165. <a href="#" onclick="goNext(); return false;">
  166. <span>
  167. <span class="nav-label">Next</span><br>
  168. <span id="next-title" class="nav-title">—</span>
  169. </span>
  170. <span aria-hidden="true">→</span>
  171. </a>
  172. </div>
  173. </div>
  174. <div id="toast" class="toast" role="status" aria-live="polite"></div>
  175. <script src="../page_utils.js?v=1"></script>
  176. <script src="../../xjs.js?v=1"></script>
  177. <script>
  178. const CURRENT = 'svg/test_morph_points.html';
  179. let __ctls = [];
  180. const poly = document.getElementById('poly');
  181. const poly2 = document.getElementById('poly2');
  182. const dur = document.getElementById('dur');
  183. const loop = document.getElementById('loop');
  184. const durOut = document.getElementById('durOut');
  185. const loopOut = document.getElementById('loopOut');
  186. const STAR = '160 35 185 75 230 80 195 110 206 155 160 132 114 155 125 110 90 80 135 75';
  187. const BLOB = '160 50 205 60 235 95 220 130 190 165 160 175 130 165 100 130 85 95 115 60';
  188. function read() {
  189. const d = Number(dur.value);
  190. const l = Number(loop.value);
  191. durOut.textContent = d + 'ms';
  192. loopOut.textContent = String(l);
  193. return { d, l };
  194. }
  195. function reset() {
  196. (__ctls || []).forEach(c => { try { c?.cancel?.(); } catch {} });
  197. __ctls = [];
  198. poly.setAttribute('points', STAR);
  199. poly2.setAttribute('points', BLOB);
  200. }
  201. function runDemo() {
  202. reset();
  203. const { d, l } = read();
  204. __ctls.push(xjs(poly).animate({
  205. points: [STAR, BLOB],
  206. duration: d,
  207. easing: 'ease-in-out',
  208. direction: 'alternate',
  209. loop: l
  210. }));
  211. __ctls.push(xjs(poly2).animate({
  212. points: [BLOB, STAR],
  213. duration: d,
  214. easing: 'ease-in-out',
  215. direction: 'alternate',
  216. loop: l
  217. }));
  218. }
  219. function pauseDemo() { (__ctls || []).forEach(c => { try { c?.pause?.(); } catch {} }); }
  220. function resumeDemo() { (__ctls || []).forEach(c => { try { c?.play?.(); } catch {} }); }
  221. dur.addEventListener('input', read);
  222. loop.addEventListener('input', read);
  223. function goPrev() { window.parent?.docNavigatePrev?.(CURRENT); }
  224. function goNext() { window.parent?.docNavigateNext?.(CURRENT); }
  225. DocPage.enableTabKeyboardNav();
  226. DocPage.syncMiddleActive(CURRENT);
  227. DocPage.syncPrevNext(CURRENT);
  228. read();
  229. setTimeout(runDemo, 320);
  230. </script>
  231. </body>
  232. </html>