-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsprite-1.html
83 lines (79 loc) · 2.5 KB
/
sprite-1.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>先预加载相关资源 然后再初始化动画</title>
<style type="text/css">
div{
width:280px;
height:280px;
}
</style>
</head>
<body>
<div id="animation"></div>
<script src="./lib/lottie-sprite.js"></script>
<script type="text/javascript">
window.onload = function(){
//动态加载json文件
function formatResponse(xhr){
if(xhr.response && typeof xhr.response === 'object') {
return xhr.response;
} else if(xhr.response && typeof xhr.response === 'string') {
return JSON.parse(xhr.response);
} else if(xhr.responseText) {
return JSON.parse(xhr.responseText);
}
}
function loadJSON(src, callback){
var response;
var xhr = new XMLHttpRequest();
xhr.open('GET', src, true);
xhr.send();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
response = formatResponse(xhr);
callback(response);
}
};
}
//动态加载图片
function loadImage(src,callback){
var img = new Image();
img.src = src;
img.onload = function(){
callback(img);
};
}
//预加载相关资源
var $animation = document.querySelector('#animation');
var spriteImage;
var animationData;
loadImage('./animation/build/sprite.png',function(img){
spriteImage = img;
initAnimation($animation,spriteImage,animationData);
});
loadJSON('./animation/data_sprite.json',function(data){
animationData = data;
initAnimation($animation,spriteImage,animationData);
});
//初始化动画
function initAnimation(container,spriteImage,animationData){
console.log('---');
console.log(spriteImage);
console.log(animationData);
if(spriteImage&&animationData){
lottie.loadAnimation({
container: container,
renderer: 'canvas',
loop: true,
autoplay: true,
animationData: animationData,
_spriteSrc:spriteImage,
});
}
}
}
</script>
</body>
</html>