SVG 闪烁动画
闪烁动画效果的核心在于控制元素颜色的变化,掌握好时间的控制,让颜色的变化有引人注目的显示,这样在静态的页面中,自然会吸引住用户的眼球。
CSS实现闪烁动画
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
span{
animation: shan 0.5s infinite alternate;
}
@keyframes shan {
to{
color: transparent;
}
}
</head>
<body>
<div>
<span>Hello,你好啊</span>
</div>
</body>
</html>
- 为了使颜色过渡更平滑,animation-direction的属性设置为 alternate 或者 alternate-reverse(默认为 normal)
- 作用是会让动画增加一个循环周期
- 还需要把 animation-duration (动画时长)减半,才能达到预期效果
SVG实现闪烁动画
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<svg width="960px" height="500px" class="left-top border" style="background-color: #0e2439;">
<path fill="none" style="stroke: #2de8e8;stroke-width: 3px;" d="m50,150 0,-40 30,-30 150,0 20,20 150,0">
<animate
attributeName="stroke"
values="#2de8e8;#235fa7;#2de8e8"
dur="0.5s"
begin="0s"
repeatCount="indefinite"
/>
</path>
<polygon fill="#2de8e8" style="stroke: #235fa7;stroke-width: 3px;" points="250,80 360,80 370,90 260,90">
<animate
attributeName="fill"
values="#235fa7;#2de8e8;#235fa7"
dur="1s"
begin="0s"
repeatCount="indefinite"
/>
<animate
attributeName="stroke"
values="#235fa7;#2de8e8;#235fa7"
dur="1s"
begin="0s"
repeatCount="indefinite"
/>
</polygon>
<path fill="none" style="stroke: #2de8e8;stroke-width: 3px;" d="m350,60 50,20 170,0 30,30 0,40">
<animate
attributeName="stroke"
values="#2de8e8;#235fa7;#2de8e8"
dur="0.5s"
begin="0.5s"
repeatCount="indefinite"
/>
</path>
</svg>
</body>
</html>
GSAP 实现闪烁动画
多条折线同时闪烁,每条折线都有自己的动画设定,所以在 GSAP 中我们不能使用 timeline,动画不是线性的,而是并行的。
整个图的svg:
<svg width="960px" height="500px" class="left-top border" style="background-color: #0e2439;">
<path fill="none" style="stroke: #2de8e8;stroke-width: 3px;" d="m50,150 0,-40 30,-30 150,0 20,20 150,0"></path>
<polygon fill="#2de8e8" style="stroke: #2de8e8;stroke-width: 3px;" points="250,80 360,80 370,90 260,90"></polygon>
<path fill="none" style="stroke: #2de8e8;stroke-width: 3px;" d="m350,60 50,20 170,0 30,30 0,40"></path>
<ellipse rx="5" ry="8" cx="50" cy="170" fill="#2de8e8" />
<line x1="50" x2="50" y1="170" y2="300" stroke-width="3px" stroke="#2de8e8"></line>
<ellipse rx="13" ry="5" cx="130" cy="120" fill="#2de8e8" />
<ellipse rx="13" ry="5" cx="500" cy="120" fill="#2de8e8" />
<line x1="120" x2="500" y1="120" y2="120" stroke-width="1px" stroke="#2de8e8"></line>
<path fill="none" style="stroke: #2de8e8;stroke-width: 3px;" d="m50,320 0,40 30,30 150,0 20,-20 150,0"></path>
<polygon fill="#2de8e8" style="stroke: #2de8e8;stroke-width: 3px;" points="260,380 380,380 370,390 250,390"></polygon>
<path fill="none" style="stroke: #2de8e8;stroke-width: 3px;" d="m370,400 50,-20 150,0 30,-30 0,-40"></path>
<ellipse rx="5" ry="8" cx="600" cy="170" fill="#2de8e8" />
<line x1="600" x2="600" y1="170" y2="300" stroke-width="3px" stroke="#2de8e8"></line>
<ellipse rx="13" ry="5" cx="130" cy="350" fill="#2de8e8" />
<ellipse rx="13" ry="5" cx="500" cy="350" fill="#2de8e8" />
<line x1="120" x2="500" y1="350" y2="350" stroke-width="1px" stroke="#2de8e8"></line>
</svg>
做动画
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<svg width="960px" height="500px" class="left-top border" style="background-color: #0e2439;">
<path id="left-top" fill="none" style="stroke: #235fa7;stroke-width: 3px;" d="m50,150 0,-40 30,-30 150,0 20,20 150,0"></path>
<polygon id="top-polygon" fill="#2de8e8" style="stroke: #2de8e8;stroke-width: 3px;" points="250,80 360,80 370,90 260,90"></polygon>
<path id="right-top" fill="none" style="stroke: #235fa7;stroke-width: 3px;" d="m350,60 50,20 170,0 30,30 0,40"></path>
<ellipse id="left-ellipse" rx="5" ry="8" cx="50" cy="170" fill="#235fa7" />
<line id="left-line" x1="50" x2="50" y1="170" y2="300" stroke-width="3px" stroke="#235fa7"></line>
<!-- 中心顶部线条-->
<ellipse id="top-left-ellipse" rx="13" ry="5" cx="130" cy="120" fill="#235fa7" />
<ellipse id="top-right-ellipse" rx="13" ry="5" cx="500" cy="120" fill="#235fa7" />
<line id="top-line" x1="120" x2="500" y1="120" y2="120" stroke-width="1px" stroke="#235fa7"></line>
<!-- 右边中间的椭圆点和直线-->
<ellipse id="left-ellipse" rx="5" ry="8" cx="600" cy="170" fill="#235fa7" />
<line id="left-line" x1="600" x2="600" y1="170" y2="300" stroke-width="3px" stroke="#235fa7"></line>
<!--底部的线条-->
<path id="left-top" fill="none" style="stroke: #235fa7;stroke-width: 3px;" d="m50,320 0,40 30,30 150,0 20,-20 150,0"></path>
<polygon id="top-polygon" fill="#2de8e8" style="stroke: #2de8e8;stroke-width: 3px;" points="260,380 380,380 370,390 250,390"></polygon>
<path id="right-top" fill="none" style="stroke: #235fa7;stroke-width: 3px;" d="m370,400 50,-20 150,0 30,-30 0,-40"></path>
<ellipse id="top-left-ellipse" rx="13" ry="5" cx="130" cy="350" fill="#235fa7" />
<ellipse id="top-right-ellipse" rx="13" ry="5" cx="500" cy="350" fill="#235fa7" />
<line id="top-line" x1="120" x2="500" y1="350" y2="350" stroke-width="1px" stroke="#235fa7"></line>
</svg>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.11.1/gsap.min.js"></script>
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/16327/DrawSVGPlugin3.min.js"></script>
<script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/16327/MorphSVGPlugin3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.2.6/MotionPathPlugin.min.js"></script>
<script>
gsap.to("#left-top",{
stroke: "#2de8e8",
repeat: -1,
yoyo: true,
duration: 0.5,
repeatDelay: 0
})
gsap.to("#top-polygon",{
fill:"#235fa7",
stroke: "#235fa7",
repeat: -1,
yoyo: true,
duration: 0.5,
repeatDelay: 0
})
gsap.to("#right-top",{
stroke: "#2de8e8",
repeat: -1,
yoyo: true,
duration: 0.25,
repeatDelay: 0.1
})
gsap.to("#left-ellipse",{
fill: "#2de8e8",
stroke: "#2de8e8",
repeat: -1,
yoyo: true,
duration: 0.3,
repeatDelay: 0
})
gsap.to("#left-line",{
fill: "#2de8e8",
stroke: "#2de8e8",
repeat: -1,
yoyo: true,
duration: 0.3,
repeatDelay: 0
})
gsap.to("#top-left-ellipse",{
fill: "#2de8e8",
stroke: "#2de8e8",
repeat: -1,
yoyo: true,
duration: 0.3,
repeatDelay: 0
})
gsap.to("#top-right-ellipse",{
fill: "#2de8e8",
stroke: "#2de8e8",
repeat: -1,
yoyo: true,
duration: 0.3,
repeatDelay: 0
})
gsap.to("#top-line",{
fill: "#2de8e8",
stroke: "#2de8e8",
repeat: -1,
yoyo: true,
duration: 0.3,
repeatDelay: 0
})
</script>
</html>
SVG图案动画
在 Web 页面开发中使用 SVG 生成背景
SVG Pattern
- 在 SVG 中声明一个 pattern 元素
- pattern 中要声明我们想要绘制的图形
- 创建一个新的图形,使用 SVG 中的 fill 属性,填充图案
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 30px;
}
</style>
</head>
<body>
<div id="app">
<svg width="360" height="240">
<defs>
<pattern id="rect" patternUnits="userSpaceOnUse" width="60" height="60">
<rect width="30" height="30" fill="#4af" x="0" y="0"></rect>
</pattern>
</defs>
<rect width="60" height="60" stroke="#a4f" stroke-width="2" fill="none" />
<rect id="canvas" width="360" height="240" stroke="#aaa" fill="url(#rect)" />
</svg>
</div>
</body>
</html>