test_svg_transforms.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 Transforms - 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: 260px;
  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; overflow: visible; }
  55. .grid-bg {
  56. stroke: rgba(255,255,255,0.06);
  57. stroke-width: 1;
  58. }
  59. .box {
  60. fill: rgba(255,159,67,0.15);
  61. stroke: rgba(255,159,67,0.45);
  62. stroke-width: 2;
  63. rx: 10;
  64. ry: 10;
  65. transform-box: fill-box;
  66. transform-origin: center;
  67. }
  68. .box.red {
  69. fill: rgba(255,75,75,0.12);
  70. stroke: rgba(255,75,75,0.45);
  71. }
  72. .box.gray {
  73. fill: rgba(255,255,255,0.06);
  74. stroke: rgba(255,255,255,0.16);
  75. }
  76. .label {
  77. fill: rgba(255,255,255,0.22);
  78. font-weight: 800;
  79. font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  80. font-size: 12px;
  81. letter-spacing: 0.8px;
  82. }
  83. </style>
  84. </head>
  85. <body>
  86. <div class="container">
  87. <div class="page-top">
  88. <div class="crumb">ANIMATION › SVG</div>
  89. <div class="since">SINCE 1.0.0</div>
  90. </div>
  91. <h1>SVG transform(x/y/rotate/scale)</h1>
  92. <p class="description">
  93. SVG 元素也能像普通 DOM 一样做 transform 动画:<code class="inline">x</code>/<code class="inline">y</code>、<code class="inline">rotate</code>、<code class="inline">scale</code>。
  94. 这类通常会走 WAAPI,性能也更稳。
  95. </p>
  96. <p class="hint">
  97. <strong>建议:</strong>如果你要做“动起来的图标/插画”,优先用 transform(位移/旋转/缩放),再配合 opacity / filter。
  98. </p>
  99. <div class="box-container">
  100. <div class="box-header">
  101. <div class="box-title">SVG transforms code example</div>
  102. <div class="box-right">
  103. <button class="icon-btn" type="button" title="Copy" onclick="DocPage.copyActiveCode()" aria-label="Copy code">
  104. <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
  105. <rect x="9" y="9" width="13" height="13" rx="2"></rect>
  106. <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path>
  107. </svg>
  108. </button>
  109. <div class="tabs" role="tablist" aria-label="Code tabs">
  110. <div class="tab active" role="tab" aria-selected="true" tabindex="0" onclick="DocPage.switchTab('js')">JavaScript</div>
  111. <div class="tab" role="tab" aria-selected="false" tabindex="-1" onclick="DocPage.switchTab('html')">HTML</div>
  112. <div class="tab" role="tab" aria-selected="false" tabindex="-1" onclick="DocPage.switchTab('css')">CSS</div>
  113. </div>
  114. </div>
  115. </div>
  116. <div id="js-code" class="code-view active">
  117. <span class="fun">xjs</span><span class="punc">(</span><span class="str">'#b1'</span><span class="punc">)</span>.<span class="fun">animate</span><span class="punc">({</span> x<span class="punc">:</span> <span class="str">'6rem'</span><span class="punc">,</span> rotate<span class="punc">:</span> <span class="str">'1turn'</span><span class="punc"> }</span><span class="punc">)</span><span class="punc">;</span>
  118. <span class="fun">xjs</span><span class="punc">(</span><span class="str">'#b2'</span><span class="punc">)</span>.<span class="fun">animate</span><span class="punc">({</span> y<span class="punc">:</span> <span class="str">'-3rem'</span><span class="punc">,</span> scale<span class="punc">:</span> <span class="punc">[</span><span class="num">1</span><span class="punc">,</span> <span class="num">1.35</span><span class="punc">]</span><span class="punc"> }</span><span class="punc">)</span><span class="punc">;</span>
  119. </div>
  120. <div id="html-code" class="html-view">
  121. <span class="tag">&lt;rect</span> <span class="attr">id</span>=<span class="val">"b1"</span> <span class="attr">x</span>=<span class="val">"78"</span> <span class="attr">y</span>=<span class="val">"92"</span> <span class="attr">width</span>=<span class="val">"44"</span> <span class="attr">height</span>=<span class="val">"44"</span> <span class="tag">/&gt;</span>
  122. </div>
  123. <pre id="css-code" class="css-view">.box { transform-box: fill-box; transform-origin: center; }</pre>
  124. <div class="feature-desc">
  125. <strong>功能说明:</strong>
  126. <br>- 对 SVG 做 transform 动画时,建议设置 <code class="inline">transform-box</code> 和 <code class="inline">transform-origin</code>,旋转更符合直觉。
  127. <br>- 你可以把多个图形组合成一个小“角色”,用一套参数让它动起来。
  128. </div>
  129. <div class="demo-visual">
  130. <div class="stage">
  131. <svg viewBox="0 0 320 240" aria-label="SVG transforms demo">
  132. <!-- background grid -->
  133. <g opacity="1">
  134. <path class="grid-bg" d="M16 40 H304"></path>
  135. <path class="grid-bg" d="M16 80 H304"></path>
  136. <path class="grid-bg" d="M16 120 H304"></path>
  137. <path class="grid-bg" d="M16 160 H304"></path>
  138. <path class="grid-bg" d="M16 200 H304"></path>
  139. <path class="grid-bg" d="M64 24 V216"></path>
  140. <path class="grid-bg" d="M112 24 V216"></path>
  141. <path class="grid-bg" d="M160 24 V216"></path>
  142. <path class="grid-bg" d="M208 24 V216"></path>
  143. <path class="grid-bg" d="M256 24 V216"></path>
  144. </g>
  145. <rect id="b1" class="box" x="78" y="92" width="44" height="44" rx="10" ry="10"></rect>
  146. <rect id="b2" class="box gray" x="138" y="92" width="44" height="44" rx="10" ry="10"></rect>
  147. <rect id="b3" class="box red" x="198" y="92" width="44" height="44" rx="10" ry="10"></rect>
  148. <text class="label" x="160" y="230" text-anchor="middle">x / y / rotate / scale</text>
  149. </svg>
  150. </div>
  151. <div class="panel">
  152. <div>
  153. <div class="control">
  154. <label for="dist">distance</label>
  155. <input id="dist" type="range" min="20" max="140" step="5" value="96" />
  156. <output id="distOut">96px</output>
  157. </div>
  158. <div class="control">
  159. <label for="dur">duration</label>
  160. <input id="dur" type="range" min="200" max="2400" step="50" value="900" />
  161. <output id="durOut">900ms</output>
  162. </div>
  163. </div>
  164. <div style="display:flex; gap:10px; justify-content:flex-end;">
  165. <button class="play-btn secondary" onclick="pauseDemo()">PAUSE</button>
  166. <button class="play-btn secondary" onclick="resumeDemo()">RESUME</button>
  167. <button class="play-btn" onclick="runDemo()">REPLAY</button>
  168. </div>
  169. </div>
  170. </div>
  171. </div>
  172. <div class="doc-nav" aria-label="Previous and next navigation">
  173. <a href="#" onclick="goPrev(); return false;">
  174. <span>
  175. <span class="nav-label">Previous</span><br>
  176. <span id="prev-title" class="nav-title">—</span>
  177. </span>
  178. <span aria-hidden="true">←</span>
  179. </a>
  180. <div id="nav-center" class="nav-center">SVG</div>
  181. <a href="#" onclick="goNext(); return false;">
  182. <span>
  183. <span class="nav-label">Next</span><br>
  184. <span id="next-title" class="nav-title">—</span>
  185. </span>
  186. <span aria-hidden="true">→</span>
  187. </a>
  188. </div>
  189. </div>
  190. <div id="toast" class="toast" role="status" aria-live="polite"></div>
  191. <script src="../page_utils.js"></script>
  192. <script src="../../xjs.js"></script>
  193. <script>
  194. const CURRENT = 'svg/test_svg_transforms.html';
  195. let __ctls = [];
  196. const b1 = document.getElementById('b1');
  197. const b2 = document.getElementById('b2');
  198. const b3 = document.getElementById('b3');
  199. const dist = document.getElementById('dist');
  200. const dur = document.getElementById('dur');
  201. const distOut = document.getElementById('distOut');
  202. const durOut = document.getElementById('durOut');
  203. function read() {
  204. const d = Number(dist.value);
  205. const t = Number(dur.value);
  206. distOut.textContent = d + 'px';
  207. durOut.textContent = t + 'ms';
  208. return { d, t };
  209. }
  210. function reset() {
  211. (__ctls || []).forEach(c => { try { c?.cancel?.(); } catch {} });
  212. __ctls = [];
  213. [b1, b2, b3].forEach(el => { if (el) el.style.transform = 'none'; });
  214. }
  215. function runDemo() {
  216. reset();
  217. const { d, t } = read();
  218. __ctls.push(xjs(b1).animate({ x: d, rotate: '1turn', duration: t, easing: 'ease-out' }));
  219. __ctls.push(xjs(b2).animate({ y: -Math.round(d * 0.55), scale: [1, 1.35], duration: t, easing: 'ease-in-out' }));
  220. __ctls.push(xjs(b3).animate({ x: -d, rotate: '-1turn', duration: t, easing: 'ease-out' }));
  221. }
  222. function pauseDemo() { (__ctls || []).forEach(c => { try { c?.pause?.(); } catch {} }); }
  223. function resumeDemo() { (__ctls || []).forEach(c => { try { c?.play?.(); } catch {} }); }
  224. dist.addEventListener('input', read);
  225. dur.addEventListener('input', read);
  226. function goPrev() { window.parent?.docNavigatePrev?.(CURRENT); }
  227. function goNext() { window.parent?.docNavigateNext?.(CURRENT); }
  228. DocPage.enableTabKeyboardNav();
  229. DocPage.syncMiddleActive(CURRENT);
  230. DocPage.syncPrevNext(CURRENT);
  231. read();
  232. setTimeout(runDemo, 320);
  233. </script>
  234. </body>
  235. </html>