test_motion_path.html 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 Motion Path - Animal.js</title>
  7. <link rel="stylesheet" href="../demo.css">
  8. <style>
  9. .demo-visual {
  10. padding: 26px 30px;
  11. background: #151515;
  12. display: grid;
  13. grid-template-columns: 1fr 280px;
  14. gap: 16px;
  15. }
  16. .stage {
  17. background: rgba(0,0,0,0.25);
  18. border: 1px solid #262626;
  19. border-radius: 12px;
  20. padding: 14px;
  21. overflow: hidden;
  22. min-height: 220px;
  23. display: grid;
  24. place-items: center;
  25. }
  26. .panel {
  27. background: rgba(0,0,0,0.18);
  28. border: 1px solid #262626;
  29. border-radius: 12px;
  30. padding: 14px;
  31. display: flex;
  32. flex-direction: column;
  33. gap: 12px;
  34. }
  35. svg { width: 100%; height: 100%; max-height: 260px; }
  36. .path {
  37. fill: none;
  38. stroke: rgba(255,255,255,0.12);
  39. stroke-width: 2.2;
  40. stroke-linecap: round;
  41. stroke-linejoin: round;
  42. }
  43. .runner {
  44. fill: var(--accent-color);
  45. filter: drop-shadow(0 0 8px rgba(255,159,67,0.25));
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <div class="container">
  51. <div class="page-top">
  52. <div class="crumb">ANIMATION › SVG</div>
  53. <div class="since">SINCE 1.0.0</div>
  54. </div>
  55. <h1>沿路径运动(Motion Path)</h1>
  56. <p class="description">让一个小点沿着 path 跑:像小飞机巡航、像精灵带路、像 UI 的“引导线”。</p>
  57. <div class="box-container">
  58. <div class="demo-visual">
  59. <div class="stage">
  60. <svg viewBox="0 0 320 180">
  61. <path id="track" class="path" d="M18,142 C62,14 142,12 176,90 C204,154 262,166 300,42" />
  62. <g id="runner" transform="translate(18 142)">
  63. <circle class="runner" cx="0" cy="0" r="10"></circle>
  64. </g>
  65. </svg>
  66. </div>
  67. <div class="panel">
  68. <button class="play-btn" onclick="runDemo()">REPLAY</button>
  69. </div>
  70. </div>
  71. </div>
  72. <script src="../page_utils.js"></script>
  73. <script src="../../xjs.js"></script>
  74. <script>
  75. const path = document.getElementById('track');
  76. const runner = document.getElementById('runner');
  77. function runDemo() {
  78. const len = path.getTotalLength();
  79. xjs({ p: 0 }).animate({
  80. p: [0, 1],
  81. duration: 1400,
  82. easing: 'ease-in-out',
  83. update: ({ progress }) => {
  84. const pt = path.getPointAtLength(len * progress);
  85. runner.setAttribute('transform', `translate(${pt.x} ${pt.y})`);
  86. }
  87. });
  88. }
  89. setTimeout(runDemo, 320);
  90. </script>
  91. </div>
  92. </body>
  93. </html>