test_morph_path.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 (path d) - 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: 300px;
  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: 340px; overflow: visible; }
  55. .ref {
  56. fill: none;
  57. stroke: rgba(255,255,255,0.10);
  58. stroke-width: 2;
  59. stroke-dasharray: 6 6;
  60. opacity: 0.75;
  61. }
  62. .ref.to { stroke: rgba(255,159,67,0.32); }
  63. .ref.from { stroke: rgba(255,75,75,0.28); }
  64. .path {
  65. fill: rgba(255,159,67,0.12);
  66. stroke: rgba(255,159,67,0.62);
  67. stroke-width: 2.2;
  68. filter: drop-shadow(0 0 22px rgba(255,159,67,0.10));
  69. }
  70. .label {
  71. fill: rgba(255,255,255,0.22);
  72. font-weight: 800;
  73. font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  74. font-size: 12px;
  75. letter-spacing: 0.8px;
  76. }
  77. .meter {
  78. display: inline-flex;
  79. gap: 10px;
  80. align-items: center;
  81. padding: 8px 10px;
  82. border-radius: 12px;
  83. border: 1px solid #2a2a2a;
  84. background: rgba(255,255,255,0.02);
  85. color: #cfcfcf;
  86. font-size: 12px;
  87. font-weight: 800;
  88. }
  89. .dot {
  90. width: 10px;
  91. height: 10px;
  92. border-radius: 50%;
  93. background: #444;
  94. }
  95. .dot.on {
  96. background: var(--accent-color);
  97. box-shadow: 0 0 0 5px rgba(255,159,67,0.14);
  98. }
  99. .tools {
  100. display: flex;
  101. flex-wrap: wrap;
  102. gap: 10px;
  103. margin-top: 10px;
  104. }
  105. .mini-btn {
  106. background: rgba(255,255,255,0.02);
  107. border: 1px solid #2a2a2a;
  108. color: #cfcfcf;
  109. padding: 6px 10px;
  110. border-radius: 999px;
  111. font-size: 12px;
  112. font-weight: 800;
  113. cursor: pointer;
  114. }
  115. .mini-btn:hover {
  116. border-color: var(--orange);
  117. color: var(--orange);
  118. }
  119. </style>
  120. </head>
  121. <body>
  122. <div class="container">
  123. <div class="page-top">
  124. <div class="crumb">ANIMATION › SVG</div>
  125. <div class="since">SINCE 1.0.0</div>
  126. </div>
  127. <h1>变形动画:path 的 <code class="inline">d</code></h1>
  128. <p class="description">
  129. 这就是你想要的“形状变形”。在本库里,只要两段 <code class="inline">d</code> 字符串<strong>结构一致</strong>(命令序列相同,数字数量相同),
  130. 就能直接插值,做出平滑 morph。
  131. </p>
  132. <p class="hint">
  133. <strong>关键点:</strong>不要试图让“完全不相干”的两条 path 直接互变。先让它们有相同的命令结构(比如都用 4 段 cubic Bézier),再改数值。
  134. <br>下面 demo 会显示两条参考虚线(From/To),中间那条橙色实体才是正在 morph 的路径。
  135. </p>
  136. <div class="box-container">
  137. <div class="box-header">
  138. <div class="box-title">Morph (path d) code example</div>
  139. <div class="box-right">
  140. <button class="icon-btn" type="button" title="Copy" onclick="DocPage.copyActiveCode()" aria-label="Copy code">
  141. <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
  142. <rect x="9" y="9" width="13" height="13" rx="2"></rect>
  143. <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path>
  144. </svg>
  145. </button>
  146. <div class="tabs" role="tablist" aria-label="Code tabs">
  147. <div class="tab active" role="tab" aria-selected="true" tabindex="0" onclick="DocPage.switchTab('js')">JavaScript</div>
  148. <div class="tab" role="tab" aria-selected="false" tabindex="-1" onclick="DocPage.switchTab('html')">HTML</div>
  149. <div class="tab" role="tab" aria-selected="false" tabindex="-1" onclick="DocPage.switchTab('css')">CSS</div>
  150. </div>
  151. </div>
  152. </div>
  153. <div id="js-code" class="code-view active">
  154. <span class="kwd">const</span> FROM <span class="punc">=</span> <span class="str">'M160 60 C204 60 240 96 240 140 C240 184 204 220 160 220 C116 220 80 184 80 140 C80 96 116 60 160 60 Z'</span><span class="punc">;</span>
  155. <span class="kwd">const</span> TO <span class="punc">=</span> <span class="str">'M160 34 C252 62 260 112 224 140 C188 168 196 238 160 224 C124 210 132 168 96 140 C60 112 68 62 160 34 Z'</span><span class="punc">;</span>
  156. <span class="fun">xjs</span><span class="punc">(</span><span class="str">'#m'</span><span class="punc">)</span>.<span class="fun">animate</span><span class="punc">({</span>
  157. d<span class="punc">:</span> <span class="punc">[</span>FROM<span class="punc">,</span> TO<span class="punc">]</span><span class="punc">,</span>
  158. duration<span class="punc">:</span> <span class="num">1200</span><span class="punc">,</span>
  159. easing<span class="punc">:</span> <span class="str">'ease-in-out'</span><span class="punc">,</span>
  160. update<span class="punc">:</span> <span class="punc">({</span>progress<span class="punc">})</span> <span class="punc">=&gt;</span> console.<span class="fun">log</span>(progress)
  161. <span class="punc">}</span><span class="punc">)</span><span class="punc">;</span>
  162. </div>
  163. <div id="html-code" class="html-view">
  164. <span class="tag">&lt;path</span> <span class="attr">id</span>=<span class="val">"m"</span> <span class="attr">d</span>=<span class="val">"M160 40 C..."</span> <span class="tag">/&gt;</span>
  165. </div>
  166. <pre id="css-code" class="css-view">.path { fill: rgba(255,159,67,0.12); stroke: rgba(255,159,67,0.62); stroke-width: 2.2; }</pre>
  167. <div class="feature-desc">
  168. <strong>功能说明:</strong>
  169. <br>- 本库会把字符串里的数字提取出来逐个插值,然后把字符串“拼回去”写回属性。
  170. <br>- 适合:logo 变形、icon morph、液体/呼吸形状、SVG 插画过渡。
  171. </div>
  172. <div class="demo-visual">
  173. <div class="stage">
  174. <svg viewBox="0 0 320 260" aria-label="Morph path demo">
  175. <path id="refFrom" class="ref from"
  176. d="M160 60 C204 60 240 96 240 140 C240 184 204 220 160 220 C116 220 80 184 80 140 C80 96 116 60 160 60 Z"></path>
  177. <path id="refTo" class="ref to"
  178. d="M160 34 C252 62 260 112 224 140 C188 168 196 238 160 224 C124 210 132 168 96 140 C60 112 68 62 160 34 Z"></path>
  179. <path id="m" class="path"
  180. d="M160 60 C204 60 240 96 240 140 C240 184 204 220 160 220 C116 220 80 184 80 140 C80 96 116 60 160 60 Z"></path>
  181. <text class="label" x="160" y="252" text-anchor="middle">path d morph (watch orange shape)</text>
  182. </svg>
  183. </div>
  184. <div class="panel">
  185. <div>
  186. <div class="meter">
  187. <span id="runDot" class="dot"></span>
  188. <span id="pText">progress: 0%</span>
  189. </div>
  190. <div class="tools">
  191. <button class="mini-btn" type="button" onclick="setFrom()">SET FROM</button>
  192. <button class="mini-btn" type="button" onclick="setTo()">SET TO</button>
  193. <button class="mini-btn" type="button" onclick="logNow()">LOG d</button>
  194. </div>
  195. <div class="control">
  196. <label for="dur">duration</label>
  197. <input id="dur" type="range" min="200" max="3200" step="50" value="1200" />
  198. <output id="durOut">1200ms</output>
  199. </div>
  200. <div class="control">
  201. <label for="loop">loop</label>
  202. <input id="loop" type="range" min="1" max="6" step="1" value="2" />
  203. <output id="loopOut">2</output>
  204. </div>
  205. </div>
  206. <div style="display:flex; gap:10px; justify-content:flex-end;">
  207. <button class="play-btn secondary" onclick="pauseDemo()">PAUSE</button>
  208. <button class="play-btn secondary" onclick="resumeDemo()">RESUME</button>
  209. <button class="play-btn" onclick="runDemo()">REPLAY</button>
  210. </div>
  211. </div>
  212. </div>
  213. </div>
  214. <div class="doc-nav" aria-label="Previous and next navigation">
  215. <a href="#" onclick="goPrev(); return false;">
  216. <span>
  217. <span class="nav-label">Previous</span><br>
  218. <span id="prev-title" class="nav-title">—</span>
  219. </span>
  220. <span aria-hidden="true">←</span>
  221. </a>
  222. <div id="nav-center" class="nav-center">SVG</div>
  223. <a href="#" onclick="goNext(); return false;">
  224. <span>
  225. <span class="nav-label">Next</span><br>
  226. <span id="next-title" class="nav-title">—</span>
  227. </span>
  228. <span aria-hidden="true">→</span>
  229. </a>
  230. </div>
  231. </div>
  232. <div id="toast" class="toast" role="status" aria-live="polite"></div>
  233. <script src="../page_utils.js?v=1"></script>
  234. <script src="../../xjs.js?v=2"></script>
  235. <script>
  236. // Debug helper
  237. /* window.addEventListener('load', () => { ... }) */
  238. </script>
  239. <script>
  240. const CURRENT = 'svg/test_morph_path.html';
  241. let __ctls = [];
  242. let __lastD = '';
  243. const m = document.getElementById('m');
  244. const dur = document.getElementById('dur');
  245. const loop = document.getElementById('loop');
  246. const durOut = document.getElementById('durOut');
  247. const loopOut = document.getElementById('loopOut');
  248. const pText = document.getElementById('pText');
  249. const runDot = document.getElementById('runDot');
  250. const FROM = 'M160 60 C204 60 240 96 240 140 C240 184 204 220 160 220 C116 220 80 184 80 140 C80 96 116 60 160 60 Z';
  251. const TO = 'M160 34 C252 62 260 112 224 140 C188 168 196 238 160 224 C124 210 132 168 96 140 C60 112 68 62 160 34 Z';
  252. function read() {
  253. const d = Number(dur.value);
  254. const l = Number(loop.value);
  255. durOut.textContent = d + 'ms';
  256. loopOut.textContent = String(l);
  257. return { d, l };
  258. }
  259. function reset() {
  260. (__ctls || []).forEach(c => { try { c?.cancel?.(); } catch {} });
  261. __ctls = [];
  262. m.setAttribute('d', FROM);
  263. __lastD = m.getAttribute('d') || '';
  264. if (pText) pText.textContent = 'progress: 0%';
  265. if (runDot) runDot.classList.remove('on');
  266. }
  267. function bboxText() {
  268. try {
  269. const bb = m.getBBox();
  270. const w = Math.round(bb.width);
  271. const h = Math.round(bb.height);
  272. return `bbox:${w}×${h}`;
  273. } catch {
  274. return 'bbox:n/a';
  275. }
  276. }
  277. function setFrom() {
  278. reset();
  279. m.setAttribute('d', FROM);
  280. __lastD = m.getAttribute('d') || '';
  281. if (pText) pText.textContent = 'manual: FROM | ' + bboxText();
  282. }
  283. function setTo() {
  284. reset();
  285. m.setAttribute('d', TO);
  286. __lastD = m.getAttribute('d') || '';
  287. if (pText) pText.textContent = 'manual: TO | ' + bboxText();
  288. }
  289. function logNow() {
  290. const cur = m.getAttribute('d') || '';
  291. console.log('[morph:d]', { len: cur.length, head: cur.slice(0, 60), bbox: bboxText() });
  292. if (pText) pText.textContent = 'logged | ' + bboxText();
  293. }
  294. function runDemo() {
  295. reset();
  296. const { d, l } = read();
  297. if (runDot) runDot.classList.add('on');
  298. __ctls.push(xjs(m).animate({
  299. d: [FROM, TO],
  300. duration: d,
  301. easing: 'ease-in-out',
  302. direction: 'alternate',
  303. loop: l,
  304. update: ({ progress }) => {
  305. const pct = Math.round(progress * 100);
  306. const cur = m.getAttribute('d') || '';
  307. const changed = cur && cur !== __lastD;
  308. if (changed) __lastD = cur;
  309. if (pText) {
  310. const head = cur ? cur.slice(0, 18).replace(/\s+/g, ' ') : '';
  311. pText.textContent = 'progress: ' + pct + '% | d: ' + (changed ? 'changing' : 'stuck') + ' | ' + bboxText() + ' | ' + head + (cur ? '…' : '');
  312. }
  313. },
  314. complete: () => {
  315. if (runDot) runDot.classList.remove('on');
  316. }
  317. }));
  318. }
  319. function pauseDemo() { (__ctls || []).forEach(c => { try { c?.pause?.(); } catch {} }); }
  320. function resumeDemo() { (__ctls || []).forEach(c => { try { c?.play?.(); } catch {} }); }
  321. dur.addEventListener('input', read);
  322. loop.addEventListener('input', read);
  323. function goPrev() { window.parent?.docNavigatePrev?.(CURRENT); }
  324. function goNext() { window.parent?.docNavigateNext?.(CURRENT); }
  325. DocPage.enableTabKeyboardNav();
  326. DocPage.syncMiddleActive(CURRENT);
  327. DocPage.syncPrevNext(CURRENT);
  328. read();
  329. setTimeout(runDemo, 320);
  330. </script>
  331. </body>
  332. </html>