test_svg_attributes.html 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 Attributes - 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: 240px;
  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: 280px; }
  55. .bg {
  56. fill: rgba(255,255,255,0.02);
  57. stroke: rgba(255,255,255,0.06);
  58. stroke-width: 1;
  59. }
  60. .ring { fill: none; stroke: rgba(255,255,255,0.14); stroke-width: 8; }
  61. .ring.orange { stroke: var(--accent-color); filter: drop-shadow(0 0 10px rgba(255,159,67,0.18)); }
  62. .ring.red { stroke: var(--highlight-color); filter: drop-shadow(0 0 10px rgba(255,75,75,0.16)); }
  63. .dot { fill: rgba(255,255,255,0.18); }
  64. </style>
  65. </head>
  66. <body>
  67. <div class="container">
  68. <div class="page-top">
  69. <div class="crumb">ANIMATION › SVG</div>
  70. <div class="since">SINCE 1.0.0</div>
  71. </div>
  72. <h1>SVG 属性动画(cx / r / …)</h1>
  73. <p class="description">
  74. SVG 很多“形状”都由属性控制(比如 <code class="inline">cx</code>、<code class="inline">r</code>、<code class="inline">strokeDashoffset</code>)。
  75. 这类属性不一定走 WAAPI,但 <code class="inline">xjs().animate()</code> 仍然能动画它们(会走 JS fallback)。
  76. </p>
  77. <p class="hint">
  78. <strong>直觉理解:</strong>“CSS 属性”是写到 style 的;而 SVG 的很多东西是写在 attribute 上的。<br>
  79. 在本库里:同一套写法,内部会决定用 WAAPI 还是 JS 去更新。
  80. </p>
  81. <div class="box-container">
  82. <div class="box-header">
  83. <div class="box-title">SVG attributes code example</div>
  84. <div class="box-right">
  85. <button class="icon-btn" type="button" title="Copy" onclick="DocPage.copyActiveCode()" aria-label="Copy code">
  86. <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
  87. <rect x="9" y="9" width="13" height="13" rx="2"></rect>
  88. <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path>
  89. </svg>
  90. </button>
  91. <div class="tabs" role="tablist" aria-label="Code tabs">
  92. <div class="tab active" role="tab" aria-selected="true" tabindex="0" onclick="DocPage.switchTab('js')">JavaScript</div>
  93. <div class="tab" role="tab" aria-selected="false" tabindex="-1" onclick="DocPage.switchTab('html')">HTML</div>
  94. <div class="tab" role="tab" aria-selected="false" tabindex="-1" onclick="DocPage.switchTab('css')">CSS</div>
  95. </div>
  96. </div>
  97. </div>
  98. <div id="js-code" class="code-view active">
  99. <span class="fun">xjs</span><span class="punc">(</span><span class="str">'#c1'</span><span class="punc">)</span>.<span class="fun">animate</span><span class="punc">({</span>
  100. cx<span class="punc">:</span> <span class="punc">[</span><span class="num">96</span><span class="punc">,</span> <span class="num">224</span><span class="punc">]</span><span class="punc">,</span>
  101. r<span class="punc">:</span> <span class="punc">[</span><span class="num">14</span><span class="punc">,</span> <span class="num">34</span><span class="punc">]</span><span class="punc">,</span>
  102. duration<span class="punc">:</span> <span class="num">900</span><span class="punc">,</span>
  103. direction<span class="punc">:</span> <span class="str">'alternate'</span><span class="punc">,</span>
  104. loop<span class="punc">:</span> <span class="num">2</span>
  105. <span class="punc">}</span><span class="punc">)</span><span class="punc">;</span>
  106. </div>
  107. <div id="html-code" class="html-view">
  108. <span class="tag">&lt;circle</span> <span class="attr">id</span>=<span class="val">"c1"</span> <span class="attr">cx</span>=<span class="val">"96"</span> <span class="attr">cy</span>=<span class="val">"90"</span> <span class="attr">r</span>=<span class="val">"14"</span> <span class="tag">/&gt;</span>
  109. </div>
  110. <pre id="css-code" class="css-view">.ring.orange { stroke: var(--accent-color); }
  111. .ring.red { stroke: var(--highlight-color); }</pre>
  112. <div class="feature-desc">
  113. <strong>功能说明:</strong>
  114. <br>- 对 SVG 元素来说,“不是 transform / opacity / filter 的属性”,通常会走 JS 更新属性值。
  115. <br>- 这让你能直接动画 <code class="inline">cx</code>、<code class="inline">r</code>、<code class="inline">strokeWidth</code> 等等。
  116. </div>
  117. <div class="demo-visual">
  118. <div class="stage">
  119. <svg viewBox="0 0 320 220" aria-label="SVG attributes demo">
  120. <rect class="bg" x="16" y="16" width="288" height="188" rx="14"></rect>
  121. <circle class="ring" cx="160" cy="110" r="70"></circle>
  122. <circle id="c1" class="ring orange" cx="96" cy="90" r="14"></circle>
  123. <circle id="c2" class="ring red" cx="224" cy="130" r="22"></circle>
  124. <circle class="dot" cx="160" cy="110" r="2"></circle>
  125. </svg>
  126. </div>
  127. <div class="panel">
  128. <div>
  129. <div class="control">
  130. <label for="amp">amplitude</label>
  131. <input id="amp" type="range" min="20" max="120" step="5" value="70" />
  132. <output id="ampOut">70</output>
  133. </div>
  134. <div class="control">
  135. <label for="dur">duration</label>
  136. <input id="dur" type="range" min="200" max="2400" step="50" value="900" />
  137. <output id="durOut">900ms</output>
  138. </div>
  139. </div>
  140. <div style="display:flex; gap:10px; justify-content:flex-end;">
  141. <button class="play-btn secondary" onclick="pauseDemo()">PAUSE</button>
  142. <button class="play-btn secondary" onclick="resumeDemo()">RESUME</button>
  143. <button class="play-btn" onclick="runDemo()">REPLAY</button>
  144. </div>
  145. </div>
  146. </div>
  147. </div>
  148. <div class="doc-nav" aria-label="Previous and next navigation">
  149. <a href="#" onclick="goPrev(); return false;">
  150. <span>
  151. <span class="nav-label">Previous</span><br>
  152. <span id="prev-title" class="nav-title">—</span>
  153. </span>
  154. <span aria-hidden="true">←</span>
  155. </a>
  156. <div id="nav-center" class="nav-center">SVG</div>
  157. <a href="#" onclick="goNext(); return false;">
  158. <span>
  159. <span class="nav-label">Next</span><br>
  160. <span id="next-title" class="nav-title">—</span>
  161. </span>
  162. <span aria-hidden="true">→</span>
  163. </a>
  164. </div>
  165. </div>
  166. <div id="toast" class="toast" role="status" aria-live="polite"></div>
  167. <script src="../page_utils.js"></script>
  168. <script src="../../xjs.js"></script>
  169. <script>
  170. const CURRENT = 'svg/test_svg_attributes.html';
  171. let __ctls = [];
  172. const c1 = document.getElementById('c1');
  173. const c2 = document.getElementById('c2');
  174. const amp = document.getElementById('amp');
  175. const dur = document.getElementById('dur');
  176. const ampOut = document.getElementById('ampOut');
  177. const durOut = document.getElementById('durOut');
  178. function read() {
  179. const a = Number(amp.value);
  180. const d = Number(dur.value);
  181. ampOut.textContent = String(a);
  182. durOut.textContent = d + 'ms';
  183. return { a, d };
  184. }
  185. function reset() {
  186. const prev = __ctls || [];
  187. prev.forEach(c => { try { c?.cancel?.(); } catch {} });
  188. __ctls = [];
  189. c1.setAttribute('cx', '96'); c1.setAttribute('cy', '90'); c1.setAttribute('r', '14');
  190. c2.setAttribute('cx', '224'); c2.setAttribute('cy', '130'); c2.setAttribute('r', '22');
  191. }
  192. function runDemo() {
  193. reset();
  194. const { a, d } = read();
  195. // Two circles cross & breathe (separate controls; library doesn't support per-target function values here)
  196. __ctls.push(xjs(c1).animate({
  197. cx: [96, 160 + a],
  198. cy: [90, 110 - a * 0.2],
  199. r: [14, 24],
  200. duration: d,
  201. easing: 'ease-in-out',
  202. direction: 'alternate',
  203. loop: 2
  204. }));
  205. __ctls.push(xjs(c2).animate({
  206. cx: [224, 160 - a],
  207. cy: [130, 110 + a * 0.2],
  208. r: [22, 14],
  209. duration: d,
  210. easing: 'ease-in-out',
  211. direction: 'alternate',
  212. loop: 2
  213. }));
  214. }
  215. function pauseDemo() { (__ctls || []).forEach(c => { try { c?.pause?.(); } catch {} }); }
  216. function resumeDemo() { (__ctls || []).forEach(c => { try { c?.play?.(); } catch {} }); }
  217. amp.addEventListener('input', read);
  218. dur.addEventListener('input', read);
  219. function goPrev() { window.parent?.docNavigatePrev?.(CURRENT); }
  220. function goNext() { window.parent?.docNavigateNext?.(CURRENT); }
  221. DocPage.enableTabKeyboardNav();
  222. DocPage.syncMiddleActive(CURRENT);
  223. DocPage.syncPrevNext(CURRENT);
  224. read();
  225. setTimeout(runDemo, 320);
  226. </script>
  227. </body>
  228. </html>