/
2-2-lottie.html
91 lines (83 loc) · 3.06 KB
/
2-2-lottie.html
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>cdn-animation</title>
<script src="https://cdn.bootcdn.net/ajax/libs/lottie-web/5.7.8/lottie.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tween.js/16.7.0/Tween.js"></script>
</head>
<style>
html, body{
width: 100vw;
width: 100vh;
}
.bodymovin {
width: 200px;
height: 100px;
position: absolute;
transform: translate(-30%, 0);
top: 50px;
left: 0;
}
</style>
<body>
<div class="bodymovin"></div>
<script>
fetch('./bomb.json')
.then(data => {
console.log(data);
renderBomb(() => {
const animation = window.bodymovin.loadAnimation({
container: document.querySelector('.bodymovin'), // 要包含该动画的dom元素
renderer: 'svg', // 渲染方式,svg、canvas、html(轻量版仅svg渲染)
loop: false, // 是否循环播放
autoplay: false, // 是否自动播放
path: './bomb.json', // 动画json文件路径
});
animation.play();
animation.addEventListener('complete', () => {
animation.destroy()
})
})
})
function renderBomb(cb) {
var box = document.createElement('div');
box.style.setProperty('width', '20px');
box.style.setProperty('height', '20px');
box.style.setProperty('background-position', 'center');
box.style.setProperty('background-size', '100%');
box.style.setProperty('background-image', 'url(./bomb.png)');
box.style.setProperty('transform', 'translate(' + 300 + 'px, ' + 300 + 'px)');
document.body.appendChild(box);
function animate(time) {
requestAnimationFrame(animate);
TWEEN.update(time);
}
requestAnimationFrame(animate);
var H = 100;
var coords = { x: 300, y: 0 };
var tween = new TWEEN.Tween(coords)
.to({ x: 0, y: 360 }, 1000)
.easing(TWEEN.Easing.Linear.None)
.onUpdate(function () {
var c = H;
var a = 5 / (6 * H);
var b = -11 / 6;
var x = coords.x;
var y = a * x * x + b * x + c;
console.log(coords.y);
box.style.setProperty('transform', `translate(${x}px, ${y}px) rotate(${coords.y}deg)`);
// box.style.setProperty('transform', `rotate(${coords.y}deg)`);
})
.start()
.onComplete(function () {
box.style.setProperty('display', 'none');
tween.stop();
cb && cb()
})
}
</script>
</body>
</html>