| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>SVG Morph (points) - Animal.js</title>
- <link rel="stylesheet" href="../demo.css">
- <style>
- .hint {
- color: #9a9a9a;
- font-size: 12px;
- line-height: 1.65;
- margin-top: -8px;
- margin-bottom: 16px;
- }
- .demo-visual {
- padding: 26px 30px;
- background: #151515;
- display: grid;
- grid-template-columns: 1fr 280px;
- gap: 16px;
- align-items: stretch;
- }
- .stage {
- background: rgba(0,0,0,0.25);
- border: 1px solid #262626;
- border-radius: 12px;
- padding: 14px;
- overflow: hidden;
- position: relative;
- min-height: 280px;
- display: grid;
- place-items: center;
- }
- .panel {
- background: rgba(0,0,0,0.18);
- border: 1px solid #262626;
- border-radius: 12px;
- padding: 14px;
- display: flex;
- flex-direction: column;
- gap: 12px;
- justify-content: space-between;
- }
- .control {
- display: grid;
- grid-template-columns: 90px 1fr 64px;
- gap: 10px;
- align-items: center;
- }
- .control label { color: #bdbdbd; font-size: 12px; font-weight: 800; }
- .control output { color: #e6e6e6; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; font-size: 12px; }
- input[type="range"] { width: 100%; }
- svg { width: 100%; height: 100%; max-height: 320px; }
- .shape {
- fill: rgba(255,159,67,0.14);
- stroke: rgba(255,159,67,0.55);
- stroke-width: 2;
- filter: drop-shadow(0 0 18px rgba(255,159,67,0.10));
- }
- .shape.red {
- fill: rgba(255,75,75,0.12);
- stroke: rgba(255,75,75,0.50);
- filter: drop-shadow(0 0 18px rgba(255,75,75,0.10));
- }
- .label {
- fill: rgba(255,255,255,0.22);
- font-weight: 800;
- font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
- font-size: 12px;
- letter-spacing: 0.8px;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="page-top">
- <div class="crumb">ANIMATION › SVG</div>
- <div class="since">SINCE 1.0.0</div>
- </div>
- <h1>变形动画:points(多边形)</h1>
- <p class="description">
- 这是最“稳”的 morph:只要两边的 <code class="inline">points</code> 有<strong>相同数量的数字</strong>,
- 就能直接做平滑过渡(本库会把字符串里的数字逐个插值)。
- </p>
- <p class="hint">
- <strong>规则:</strong>points 其实就是一串数字:<code class="inline">x1 y1 x2 y2 ...</code>。
- 你想从 A 变到 B,就确保 A/B 的点数量一致(比如都 12 个点)。
- </p>
- <div class="box-container">
- <div class="box-header">
- <div class="box-title">Morph (points) code example</div>
- <div class="box-right">
- <button class="icon-btn" type="button" title="Copy" onclick="DocPage.copyActiveCode()" aria-label="Copy code">
- <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
- <rect x="9" y="9" width="13" height="13" rx="2"></rect>
- <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path>
- </svg>
- </button>
- <div class="tabs" role="tablist" aria-label="Code tabs">
- <div class="tab active" role="tab" aria-selected="true" tabindex="0" onclick="DocPage.switchTab('js')">JavaScript</div>
- <div class="tab" role="tab" aria-selected="false" tabindex="-1" onclick="DocPage.switchTab('html')">HTML</div>
- <div class="tab" role="tab" aria-selected="false" tabindex="-1" onclick="DocPage.switchTab('css')">CSS</div>
- </div>
- </div>
- </div>
- <div id="js-code" class="code-view active">
- <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>
- <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>
- <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>
- points<span class="punc">:</span> <span class="punc">[</span>star<span class="punc">,</span> blob<span class="punc">]</span><span class="punc">,</span>
- duration<span class="punc">:</span> <span class="num">1200</span><span class="punc">,</span>
- easing<span class="punc">:</span> <span class="str">'ease-in-out'</span>
- <span class="punc">}</span><span class="punc">)</span><span class="punc">;</span>
- </div>
- <div id="html-code" class="html-view">
- <span class="tag"><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">/></span>
- </div>
- <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>
- <div class="feature-desc">
- <strong>功能说明:</strong>
- <br>- 这里的 <code class="inline">points</code> 是“数字模板字符串”。本库会识别其中的数字并逐个插值。
- <br>- 适合:图标变形、加载态、装饰动效(尤其是多边形/折线)。
- </div>
- <div class="demo-visual">
- <div class="stage">
- <svg viewBox="0 0 320 220" aria-label="Morph points demo">
- <polygon id="poly" class="shape"
- points="160 35 185 75 230 80 195 110 206 155 160 132 114 155 125 110 90 80 135 75"></polygon>
- <polygon id="poly2" class="shape red" opacity="0.65"
- points="160 50 205 60 235 95 220 130 190 165 160 175 130 165 100 130 85 95 115 60"></polygon>
- <text class="label" x="160" y="210" text-anchor="middle">points morph (same number tokens)</text>
- </svg>
- </div>
- <div class="panel">
- <div>
- <div class="control">
- <label for="dur">duration</label>
- <input id="dur" type="range" min="200" max="3200" step="50" value="1200" />
- <output id="durOut">1200ms</output>
- </div>
- <div class="control">
- <label for="loop">loop</label>
- <input id="loop" type="range" min="1" max="6" step="1" value="2" />
- <output id="loopOut">2</output>
- </div>
- </div>
- <div style="display:flex; gap:10px; justify-content:flex-end;">
- <button class="play-btn secondary" onclick="pauseDemo()">PAUSE</button>
- <button class="play-btn secondary" onclick="resumeDemo()">RESUME</button>
- <button class="play-btn" onclick="runDemo()">REPLAY</button>
- </div>
- </div>
- </div>
- </div>
- <div class="doc-nav" aria-label="Previous and next navigation">
- <a href="#" onclick="goPrev(); return false;">
- <span>
- <span class="nav-label">Previous</span><br>
- <span id="prev-title" class="nav-title">—</span>
- </span>
- <span aria-hidden="true">←</span>
- </a>
- <div id="nav-center" class="nav-center">SVG</div>
- <a href="#" onclick="goNext(); return false;">
- <span>
- <span class="nav-label">Next</span><br>
- <span id="next-title" class="nav-title">—</span>
- </span>
- <span aria-hidden="true">→</span>
- </a>
- </div>
- </div>
- <div id="toast" class="toast" role="status" aria-live="polite"></div>
- <script src="../page_utils.js?v=1"></script>
- <script src="../../xjs.js?v=1"></script>
- <script>
- const CURRENT = 'svg/test_morph_points.html';
- let __ctls = [];
- const poly = document.getElementById('poly');
- const poly2 = document.getElementById('poly2');
- const dur = document.getElementById('dur');
- const loop = document.getElementById('loop');
- const durOut = document.getElementById('durOut');
- const loopOut = document.getElementById('loopOut');
- const STAR = '160 35 185 75 230 80 195 110 206 155 160 132 114 155 125 110 90 80 135 75';
- const BLOB = '160 50 205 60 235 95 220 130 190 165 160 175 130 165 100 130 85 95 115 60';
- function read() {
- const d = Number(dur.value);
- const l = Number(loop.value);
- durOut.textContent = d + 'ms';
- loopOut.textContent = String(l);
- return { d, l };
- }
- function reset() {
- (__ctls || []).forEach(c => { try { c?.cancel?.(); } catch {} });
- __ctls = [];
- poly.setAttribute('points', STAR);
- poly2.setAttribute('points', BLOB);
- }
- function runDemo() {
- reset();
- const { d, l } = read();
- __ctls.push(xjs(poly).animate({
- points: [STAR, BLOB],
- duration: d,
- easing: 'ease-in-out',
- direction: 'alternate',
- loop: l
- }));
- __ctls.push(xjs(poly2).animate({
- points: [BLOB, STAR],
- duration: d,
- easing: 'ease-in-out',
- direction: 'alternate',
- loop: l
- }));
- }
- function pauseDemo() { (__ctls || []).forEach(c => { try { c?.pause?.(); } catch {} }); }
- function resumeDemo() { (__ctls || []).forEach(c => { try { c?.play?.(); } catch {} }); }
- dur.addEventListener('input', read);
- loop.addEventListener('input', read);
- function goPrev() { window.parent?.docNavigatePrev?.(CURRENT); }
- function goNext() { window.parent?.docNavigateNext?.(CURRENT); }
- DocPage.enableTabKeyboardNav();
- DocPage.syncMiddleActive(CURRENT);
- DocPage.syncPrevNext(CURRENT);
- read();
- setTimeout(runDemo, 320);
- </script>
- </body>
- </html>
|