| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>SVG Attributes - 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: 240px;
- 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: 280px; }
- .bg {
- fill: rgba(255,255,255,0.02);
- stroke: rgba(255,255,255,0.06);
- stroke-width: 1;
- }
- .ring { fill: none; stroke: rgba(255,255,255,0.14); stroke-width: 8; }
- .ring.orange { stroke: var(--accent-color); filter: drop-shadow(0 0 10px rgba(255,159,67,0.18)); }
- .ring.red { stroke: var(--highlight-color); filter: drop-shadow(0 0 10px rgba(255,75,75,0.16)); }
- .dot { fill: rgba(255,255,255,0.18); }
- </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>SVG 属性动画(cx / r / …)</h1>
- <p class="description">
- SVG 很多“形状”都由属性控制(比如 <code class="inline">cx</code>、<code class="inline">r</code>、<code class="inline">strokeDashoffset</code>)。
- 这类属性不一定走 WAAPI,但 <code class="inline">xjs().animate()</code> 仍然能动画它们(会走 JS fallback)。
- </p>
- <p class="hint">
- <strong>直觉理解:</strong>“CSS 属性”是写到 style 的;而 SVG 的很多东西是写在 attribute 上的。<br>
- 在本库里:同一套写法,内部会决定用 WAAPI 还是 JS 去更新。
- </p>
- <div class="box-container">
- <div class="box-header">
- <div class="box-title">SVG attributes 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="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>
- 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>
- 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>
- duration<span class="punc">:</span> <span class="num">900</span><span class="punc">,</span>
- direction<span class="punc">:</span> <span class="str">'alternate'</span><span class="punc">,</span>
- loop<span class="punc">:</span> <span class="num">2</span>
- <span class="punc">}</span><span class="punc">)</span><span class="punc">;</span>
- </div>
- <div id="html-code" class="html-view">
- <span class="tag"><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">/></span>
- </div>
- <pre id="css-code" class="css-view">.ring.orange { stroke: var(--accent-color); }
- .ring.red { stroke: var(--highlight-color); }</pre>
- <div class="feature-desc">
- <strong>功能说明:</strong>
- <br>- 对 SVG 元素来说,“不是 transform / opacity / filter 的属性”,通常会走 JS 更新属性值。
- <br>- 这让你能直接动画 <code class="inline">cx</code>、<code class="inline">r</code>、<code class="inline">strokeWidth</code> 等等。
- </div>
- <div class="demo-visual">
- <div class="stage">
- <svg viewBox="0 0 320 220" aria-label="SVG attributes demo">
- <rect class="bg" x="16" y="16" width="288" height="188" rx="14"></rect>
- <circle class="ring" cx="160" cy="110" r="70"></circle>
- <circle id="c1" class="ring orange" cx="96" cy="90" r="14"></circle>
- <circle id="c2" class="ring red" cx="224" cy="130" r="22"></circle>
- <circle class="dot" cx="160" cy="110" r="2"></circle>
- </svg>
- </div>
- <div class="panel">
- <div>
- <div class="control">
- <label for="amp">amplitude</label>
- <input id="amp" type="range" min="20" max="120" step="5" value="70" />
- <output id="ampOut">70</output>
- </div>
- <div class="control">
- <label for="dur">duration</label>
- <input id="dur" type="range" min="200" max="2400" step="50" value="900" />
- <output id="durOut">900ms</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"></script>
- <script src="../../xjs.js"></script>
- <script>
- const CURRENT = 'svg/test_svg_attributes.html';
- let __ctls = [];
- const c1 = document.getElementById('c1');
- const c2 = document.getElementById('c2');
- const amp = document.getElementById('amp');
- const dur = document.getElementById('dur');
- const ampOut = document.getElementById('ampOut');
- const durOut = document.getElementById('durOut');
- function read() {
- const a = Number(amp.value);
- const d = Number(dur.value);
- ampOut.textContent = String(a);
- durOut.textContent = d + 'ms';
- return { a, d };
- }
- function reset() {
- const prev = __ctls || [];
- prev.forEach(c => { try { c?.cancel?.(); } catch {} });
- __ctls = [];
- c1.setAttribute('cx', '96'); c1.setAttribute('cy', '90'); c1.setAttribute('r', '14');
- c2.setAttribute('cx', '224'); c2.setAttribute('cy', '130'); c2.setAttribute('r', '22');
- }
- function runDemo() {
- reset();
- const { a, d } = read();
- // Two circles cross & breathe (separate controls; library doesn't support per-target function values here)
- __ctls.push(xjs(c1).animate({
- cx: [96, 160 + a],
- cy: [90, 110 - a * 0.2],
- r: [14, 24],
- duration: d,
- easing: 'ease-in-out',
- direction: 'alternate',
- loop: 2
- }));
- __ctls.push(xjs(c2).animate({
- cx: [224, 160 - a],
- cy: [130, 110 + a * 0.2],
- r: [22, 14],
- duration: d,
- easing: 'ease-in-out',
- direction: 'alternate',
- loop: 2
- }));
- }
- function pauseDemo() { (__ctls || []).forEach(c => { try { c?.pause?.(); } catch {} }); }
- function resumeDemo() { (__ctls || []).forEach(c => { try { c?.play?.(); } catch {} }); }
- amp.addEventListener('input', read);
- dur.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>
|