<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SVG路径描边动画</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #121212;
color: #fff;
flex-direction: column;
}
.container {
text-align: center;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1 {
margin-bottom: 30px;
color: #4CAF50;
}
.svg-container {
width: 100%;
max-width: 600px;
margin: 0 auto 40px;
}
svg {
width: 100%;
height: auto;
}
path {
stroke: #4CAF50;
stroke-width: 3;
fill: none;
stroke-dasharray: 1000;
stroke-dashoffset: 1000;
animation: draw 5s ease-in-out forwards;
}
@keyframes draw {
to {
stroke-dashoffset: 0;
}
}
.controls {
margin: 30px 0;
display: flex;
justify-content: center;
gap: 15px;
}
button {
padding: 10px 20px;
background: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
transition: all 0.3s;
}
button:hover {
background: #45a049;
transform: translateY(-2px);
}
.description {
margin: 20px 0;
line-height: 1.6;
color: #ccc;
}
.footer {
margin-top: 50px;
color: #666;
font-size: 0.9rem;
}
.footer a {
color: #4CAF50;
text-decoration: none;
}
.footer a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<h1>SVG路径描边动画</h1>
<p class="description">这是一个使用SVG和CSS创建的路径描边动画效果。动画模拟了手绘线条的过程。</p>
<div class="svg-container">
<svg viewBox="0 0 500 300" xmlns="http://www.w3.org/2000/svg">
<!-- 心形路径 -->
<path id="heart-path" d="M250,100 C200,50 150,50 100,100 C50,150 50,200 100,250 C150,300 200,300 250,250 C300,300 350,300 400,250 C450,200 450,150 400,100 C350,50 300,50 250,100 Z" />
</svg>
</div>
<div class="controls">
<button id="replay-btn">重播动画</button>
<button id="change-path">更换路径</button>
</div>
<div class="footer">
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const replayBtn = document.getElementById('replay-btn');
const changePathBtn = document.getElementById('change-path');
const pathElement = document.getElementById('heart-path');
const svgContainer = document.querySelector('.svg-container');
// 不同路径数据
const paths = [
{
name: "心形",
d: "M250,100 C200,50 150,50 100,100 C50,150 50,200 100,250 C150,300 200,300 250,250 C300,300 350,300 400,250 C450,200 450,150 400,100 C350,50 300,50 250,100 Z"
},
{
name: "星星",
d: "M250,50 L300,200 L450,200 L330,290 L380,440 L250,350 L120,440 L170,290 L50,200 L200,200 Z"
},
{
name: "螺旋",
d: "M250,150 Q200,150 200,200 Q200,250 250,250 Q300,250 300,200 Q300,150 250,150 Q200,150 200,100 Q200,50 250,50 Q300,50 300,100 Q300,150 250,150 Q200,150 200,200 Q200,250 250,250 Q300,250 300,200 Q300,150 250,150"
},
{
name: "波浪",
d: "M50,150 C100,50 150,250 200,150 C250,50 300,250 350,150 C400,50 450,250 450,150"
},
{
name: "复杂曲线",
d: "M50,250 C100,50 200,50 250,150 C300,250 400,250 450,150 C500,50 550,50 550,150"
}
];
let currentPathIndex = 0;
// 重播动画
function replayAnimation() {
pathElement.style.animation = 'none';
void pathElement.offsetWidth; // 触发重绘
pathElement.style.animation = 'draw 5s ease-in-out forwards';
}
// 更换路径
function changePath() {
currentPathIndex = (currentPathIndex + 1) % paths.length;
const newPath = paths[currentPathIndex];
// 创建新的SVG
const newSVG = document.createElementNS("http://www.w3.org/2000/svg", "svg");
newSVG.setAttribute("viewBox", "0 0 500 300");
// 创建新路径
const newPathElement = document.createElementNS("http://www.w3.org/2000/svg", "path");
newPathElement.setAttribute("d", newPath.d);
newPathElement.setAttribute("stroke", "#4CAF50");
newPathElement.setAttribute("stroke-width", "3");
newPathElement.setAttribute("fill", "none");
newPathElement.setAttribute("stroke-dasharray", "1000");
newPathElement.setAttribute("stroke-dashoffset", "1000");
newPathElement.style.animation = "draw 5s ease-in-out forwards";
newPathElement.id = "heart-path";
newSVG.appendChild(newPathElement);
// 替换SVG
svgContainer.innerHTML = '';
svgContainer.appendChild(newSVG);
// 更新路径引用
pathElement = document.getElementById('heart-path');
// 更新标题
document.querySelector('h1').textContent = `SVG路径描边动画 - ${newPath.name}`;
}
// 事件监听
replayBtn.addEventListener('click', replayAnimation);
changePathBtn.addEventListener('click', changePath);
// 初始动画
replayAnimation();
});
</script>
</body>
</html>
1320

被折叠的 条评论
为什么被折叠?



