-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplelottieplayer.html
163 lines (157 loc) · 6.52 KB
/
simplelottieplayer.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Application to demonstrate the use of the Simple Lottie Player">
<meta name="author" content="Carlos A.">
<title>Simple Lottie Files Player</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/lottie-web/5.10.2/lottie_svg.js" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="js/simplelottieplayer.js"></script>
<style>
html, body {
height: 100%;
}
#dropzone {
width: 50%;
height: 50%;
min-height: 300px;
min-width: 300px;
border-style: dashed !important;
border-width: 5px !important;
position: relative;
}
#dropzone input[type=file] {
opacity: 0;
position: absolute; top:0; left:0; bottom: 0; right: 0; width: 100%; height: 100%
}
#simple-lottie-player {
height: 200px;
width: 200px;
}
</style>
</head>
<body class="text-center text-bg-dark h-100 d-flex flex-column">
<div class="header w-100">
<div class="mx-3 d-flex px-3 pt-3">
<h1 class="mt-auto">Simple LottieFile Player</h1>
<div class="ms-auto my-auto">
<a class="btn btn-outline-light" href="https://github.com/dealfonso/simplelottieplayer" target="_blank"><i class="bi bi-github pe-2"></i>view in github</a>
</div>
</div>
</div>
<div class="m-auto w-100 d-flex flex-column p-5">
<div class="m-auto">
<div class="text-center w-100">
<p>an application to preview <a href="https://lottiefiles.com" target="_blank">LottieFiles</a> animations</p>
<div id="dropzone" class="rounded-5 border border-white m-auto w-100 d-flex">
<div class="m-auto">
<h2>drop your animation</h2>
<h4>or press here to browse</h4>
</div>
<input type="file" >
</div>
<div id="simple-lottie-player" class="rounded-2 border border-white m-auto"></div>
<a href="#" id="download-button" class="download-btn btn btn-primary d-none">download</a>
</div>
</div>
</div>
<simplelottie class="rounded-2 border border-white m-auto" overlay-buttons fullsize-button="true" autoplay loop max-height="300" url="https://assets6.lottiefiles.com/packages/lf20_fj8rlma5.json"></simplelottie>
<footer class="text-white-50">
<p>© Carlos A. - <a href="https://github.com/dealfonso" target="_blank">https://github.com/dealfonso</a></p>
</footer>
</body>
<script>
let lottiePlayer = null;
const State = {
empty: 0,
animation: 1
}
let state = State.empty;
function updateState(newState) {
state = newState;
const dropzone = document.getElementById("dropzone");
const lottiePlayer = document.getElementById("simple-lottie-player");
switch (state) {
case State.animation:
dropzone.style.setProperty("display", "none", "important");
lottiePlayer.style.setProperty("display", "block", "important");
break;
default:
dropzone.style.setProperty("display", "block", "important");
lottiePlayer.style.setProperty("display", "none", "important");
break;
}
}
if (document.addEventListener !== undefined) {
function processFile(file) {
if (file.type === undefined) {
lottiePlayer.load(JSON.parse(JSON.stringify(file))).catch((e) => {
throw(`invalid animation ${e}`);
}).then(() => {
updateState(State.animation)
});
} else {
if (file.type != "application/json") {
throw("invalid content-type");
}
let fileReader = new FileReader();
fileReader.onload = async (event) => {
// Retrieve the animation
let loadedAnimation = null;
try {
loadedAnimation = JSON.parse(event.target.result);
} catch (e) {
throw ("invalid json file")
}
lottiePlayer.load(JSON.parse(JSON.stringify(loadedAnimation))).catch((e) => {
throw(`invalid animation ${e}`);
}).then(() => {
updateState(State.animation)
})
}
fileReader.readAsText(file);
}
}
lottiePlayer = new SimpleLottiePlayer("#simple-lottie-player", {
overlayButtons: true,
fullsizeButton: true,
maxHeight: 300,
});
document.addEventListener("dragover", event => event.preventDefault());
document.addEventListener("drop", event => {
event.preventDefault();
if (event.dataTransfer.files && event.dataTransfer.files.length > 0) {
processFile(event.dataTransfer.files[0]);
}
});
document.addEventListener("paste", (event) => {
let pasteContent = (event.clipboardData || window.clipboardData);
if ((pasteContent.files) && (pasteContent.files.length > 0)) {
processFile(pasteContent.files[0]);
} else {
let pasteText = pasteContent.getData("text");
if (pasteText != "") {
let url = null;
try {
url = new URL(pasteText);
fetch(url).then((content) => {
content.json().then((json) => processFile(json));
})
} catch (e) {
console.error(e);
return;
}
}
}
});
const fileSelector = document.querySelector('#dropzone input[type=file]');
fileSelector.addEventListener('change', (event) => {
const fileList = event.target.files;
processFile(fileList[0]);
});
updateState(State.empty);
}
</script>
</html>