技术拆解-炫酷动画

去哪里找动画?

https://daneden.github.io/animate.css/

http://www.htmleaf.com/

http://www.jq22.com/webinfo1

设计思路

时间轴

和时间相关的,都可以当做时间轴的表现形式,比如进度条时钟等。

关系

飞线。

技术拆解

线条延伸

常见的是地图上的飞线,以及两个物品之间的动画连线。

地图飞线

地图飞线实现方案有多种,大多都是通过贝塞尔曲线作图。

D3:http://d3.artzub.com/wbca/
高德+ECharts:https://blog.csdn.net/weixin_42236849/article/details/97786262
three.js的飞线:https://blog.csdn.net/guoweish/article/details/52989098
炫酷的效果(视频):https://mp.weixin.qq.com/s/AnOw11kovSYPr_MEogFeig
SVG绘制飞线:https://www.jianshu.com/p/69dd56c28248
Canvas画飞线(有性能测试和对比SVG):https://www.jianshu.com/p/38229570b308

物品连线

这里不是简单的连线,而是有一个逐渐画出来的延伸效果。

思路一:用div画一根很长的线,通过平移+遮挡来实现动态延伸。

思路二:用div画一根很长的线,通过修改其高宽值来实现动态延伸。

思路三:画一根定长的虚线,通过@keyframes设置其margin-left属性,来实现动效。

SVG画线: https://www.w3cways.com/1999.html

元素移动

平移

可以通过@keyframes设置margin-left来实现水平移动;纵向移动同理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Move</title>
<style>
@keyframes move {
100% {
margin-left: -1px;
}
}
.shape {
position: absolute;
left: 50%;
top: 40%;
width: 100px;
height: 100px;
margin-top: -5px;
margin-left: 300px;
background: rgb(219, 110, 110);
z-index: -1;
animation: move 1s infinite;
}
</style>
</head>
<body>
<div class="shape"></div>
</body>
</html>

拖拽

关键属性: draggable=”true”

https://segmentfault.com/a/1190000013606983

将元素设置为可拖拽,然后通过一系列的拖拽事件来实现效果。

如果是和Vue结合使用,那么:

在 Vue 项目中,被拖拽对象和可拖放区域可能放在不同组件之中,此时,关键数据的传递最好借助 Vuex数据总线实现。让数据而非 DOM 流转是 Vue 项目的基本思路。

这里有个基于Vue的拖拽文章: https://segmentfault.com/a/1190000014572113?utm_source=tag-newest

思路其实很简单,就是:

1、鼠标按下的时候,给元素绑定mousemove事件,在事件里面随着鼠标移动,设置元素的绝对位置。

2、鼠标抬起的时候,清空mousemove事件

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<html>

<head>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<style>
#app {
position: relative;
/*定位*/
top: 10px;
left: 10px;
width: 200px;
height: 200px;
background: #666;
/*设置一下背景*/
}
</style>

</head>

<body>
<main>
<div id="app" v-drag>
</main>
</body>
<script>
let app = new Vue({
el: '#app',
data: {},
methods: {},
directives: {
drag: {
// 指令的定义
bind: function (el) {
let odiv = el; //获取当前元素
oDiv.onmousedown = (e) => {
//算出鼠标相对元素的位置
let disX = e.clientX - odiv.offsetLeft;
let disY = e.clientY - odiv.offsetTop;

document.onmousemove = (e) => {
//用鼠标的位置减去鼠标相对元素的位置,得到元素的位置
let left = e.clientX - disX;
let top = e.clientY - disY;

//绑定元素位置到positionX和positionY上面
this.positionX = top;
this.positionY = left;

//移动当前元素
odiv.style.left = left + 'px';
odiv.style.top = top + 'px';
};
document.onmouseup = (e) => {
document.onmousemove = null;
document.onmouseup = null;
};
};
}
}
}
});
</script>
</html>

开源实现:

http://www.ptbird.cn/vue-draggable-dragging.html

拖拽也可以通过数据驱动的形式,当从A区域拖拽到B区域后,就直接删除A区域的数据,并将其添加到B区域即可,很方便。
结合computed就可以判断目前组件到底位于哪个区域。

形状

常用border-radius来实现。

圆形

三角形

心形

吸附

可以使用这个开源组件:

https://github.com/gorkys/vue-draggable-resizable-gorkys

这是基于 https://github.com/mauricius/vue-draggable-resizable 二次开发而来的

作者还翻译了一个演示的网站,很有意思。

旋转

融合

裂变

爆炸

JavaScript

可以用圆圈模拟爆炸后的碎片。触发爆炸后,通过js生成大小不一的N个圆圈,然后通过css使其以不同速率往外飞离中心,并设置颜色淡化。

另外,这个思路还可以做一个爆炸后,收起还原的效果,即爆炸开后,再恢复到原来的位置即可。

jQuery: http://www.jq22.com/yanshi6272

Three.js: http://www.htmleaf.com/Demo/201904015589.html

光影

粒子效果

水波图

纯CSS实现

https://www.imooc.com/article/67783

这个实现很巧妙,通过css的boder-radius设置水波,然后转动图形,就可以模拟出来了。

图片实现

可以做一张有较长波纹的图片,然后让图片不停循环平移,就可以实现水波图的效果。

进度条

如果只是想一次性的展示固定进度百分比,直接用css的@keyframes就可以了。

如果想控制进度百分比,则需要加入js来操控。一种方案是通过js修改页面元素的class。

另外还有获取页面css中对应的@keyframes,修改其内容的:

中文版: https://www.jianshu.com/p/b7b347c9783e

英文版: https://blogs.msdn.microsoft.com/msdn_answers/2013/11/04/part-i-using-javascript-to-set-keyframes-in-css-animations-windows-store-apps-ie/

http://www.htmleaf.com/Demo/201802124980.html

http://www.htmleaf.com/Demo/201909165793.html

不用Three.js能否实现炫酷3D动画?

案例搜集

16个创意Canvas动画:https://www.html5tricks.com/16-html5-canvas-animation.html

炫酷时钟:https://juejin.im/post/5cb53e93e51d456e55623b07

放大镜效果:https://motion.ant.design/exhibition/demo/linked-animate-cn

参考资料

2019JS动画库:https://baijiahao.baidu.com/s?id=1627781416301282257&wfr=spider&for=pc