這個範例完全不用 JavaScript、不用 GIF、也不用 canvas,只用 SVG 的 <animate> 與 <animateTransform>,就讓一個小人跑了起來——手腳擺動、身體上下起伏、手上的道具還能切換。全部程式碼就是一份 HTML 檔。
效果查看:https:///keleyi/phtml/html5/27.htm
整個 SVG 由下列幾個部分組合而成,每個部分都是一個 <path> 加上內含的 <animate>:
<!DOCTYPE html>
<html>
<head>
<title>純 html5 實現的人跑步動畫</title>
</head>
<body>
<svg version="1.1" xmlns="https://www.w3.org/2000/svg" xmlns:xlink="https://www.w3.org/1999/xlink"
x="0px" y="0px" style="max-height: 800px; min-height: 100vh" viewbox="0 0 400 400"
xml:space="preserve">
<defs>
<lineargradient id="linearGradient5090">
<stop style="stop-color: #b38443; stop-opacity: 1;" offset="0" id="stop5092" />
<stop id="stop5098" offset="0.5" style="stop-color: #694d27; stop-opacity: 1;" />
<stop style="stop-color: #4f3a1e; stop-opacity: 1;" offset="1" id="stop5094" />
</lineargradient>
<lineargradient id="linearGradient5072">
<stop style="stop-color: #888888; stop-opacity: 1;" offset="0" id="stop5074" />
<stop id="stop5086" offset="0.2385" style="stop-color: #707070; stop-opacity: 1;" />
<stop id="stop5080" offset="0.3664" style="stop-color: #ababab; stop-opacity: 1;" />
<stop style="stop-color: #9d9d9d; stop-opacity: 1;" offset="0.8549" id="stop5082" />
<stop style="stop-color: #cbcbcb; stop-opacity: 1;" offset="1" id="stop5076" />
</lineargradient>
</defs>
<!-- 手臂、腿、頭、軀幹等 path,各自內含 animate -->
<g id="props">
<animatetransform attributename="transform" attributetype="XML" type="translate"
dur="1s" repeatcount="indefinite" fill="freeze" values="
290.9,99.33; 295.3,133.8; 266.6,227.7; 228.9,246.7;
182.1,237.9; 155.7,225.9; 152.9,218.2; 143.7,188;
156,188.7; 182.4,207.7; 207.4,203.1; 258.8,185.5;
275.4,141.5; 293,106.6; 290.9,99.33" />
<g id="rotated_prop">
<animatetransform attributename="transform" attributetype="XML" type="rotate" dur="1s"
repeatcount="indefinite" fill="freeze" values="-120; 50; 90; 50; -120" />
<!-- 道具本體:這裡示範了斧頭與武士刀兩種,預設隱藏 -->
</g>
</g>
<line fill="none" stroke="black" stroke-dasharray="50 10" x1="350" y1="363.8" x2="50"
y2="363.8">
<animate attributename="stroke-dashoffset" dur=".48s" repeatcount="indefinite" from="0"
to="-300" />
</line>
<g style="cursor: pointer" id="BtnKatana">
<rect style="fill: #848ea6" width="75" height="20" x="300" y="5" />
<text style="font-size: .8em; fill: #000000; font-family: Sans" x="320" y="17">Katana</text>
</g>
<g style="cursor: pointer" id="BtnAxe">
<rect y="30" x="300" height="20" width="75" style="fill: #85a684" />
<text style="font-size: .8em; fill: #000000; font-family: Sans" x="320" y="42">Axe</text>
</g>
<g style="cursor: pointer" id="BtnNone">
<rect style="fill: #bd966d" y="55" x="300" height="20" width="75" />
<text style="font-size: .8em; fill: #000000; font-family: Sans" x="320" y="69">None</text>
</g>
</svg>
</body>
</html>
一、頻率不是全部一致。四肢用 dur="1s",而軀幹與頭部用 dur=".5s"。真實跑步時,手腳擺一個完整週期,身體會上下起伏兩次(左右腳各一次),這個 2:1 的頻率關係是讓動畫免費獲得「重量感」的關鍵。
二、地面要動,人不一定要真的前進。研究者用一條 stroke-dasharray="50 10" 的虛線,動畫改變 stroke-dashoffset 從 0 到 -300,看起來就像地面不斷往後捲動。這是製作跑步循環動畫最省力的做法——主體留在原地,靠背景移動製造前進錯覺。
props 群組裡有斧頭與武士刀兩個子群組,各自用 <animate> 控制 visibility:
<animate attributename="visibility" begin="BtnKatana.click" fill="freeze" to="visible" dur="0s" />
<animate attributename="visibility" begin="BtnAxe.click;BtnNone.click" fill="freeze" to="hidden" dur="0s" />
begin 屬性直接寫上「元素 id + .click」,就能讓 SVG 元素自己接收點擊事件並切換顯示狀態,一行 JavaScript 都不用寫。點 Axe 或 None 時才隱藏武士刀,這個「或」的語法用分號串接多個觸發條件即可。
由於各個 <path> 的 values 動畫關鍵幀資料量龐大(每個部位都有十幾組完整的路徑座標),完整內容請於上方預覽頁的原始碼中取得。實務上這類動畫建議用 Inkscape 或 Adobe Illustrator 繪製後,再用 SMIL 編輯器或 GSAP 的 MorphSVG 外掛產生關鍵幀,手寫座標幾乎不可行。
小提醒:SMIL 動畫在 Chrome 中曾一度宣布移除(後來保留),因此在需要長期維護的專案中,可考慮改用 CSS keyframes 搭配 transform,或直接用 JavaScript 動畫函式庫實作,相容性會更可控。