-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
195 lines (163 loc) · 5.11 KB
/
index.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
var rlottieHandler;
function setup() {
var head = document.head;
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "rlottie-wasm.js";
head.appendChild(script);
script.onload = _ => {
Module.onRuntimeInitialized = _ => {
rlottieHandler = new RLottieHandler(1);
addListener();
window.requestAnimationFrame(updater);
}
}
}
setup();
function updater() {
rlottieHandler.rafId = window.requestAnimationFrame(updater);
rlottieHandler.render();
}
function play() {
document.getElementById("playButton").innerHTML = "<em class='fas fa-pause'></em>";
rlottieHandler.play();
}
function playDisable() {
document.getElementById("playButton").innerHTML = "<em class='fas fa-pause' style='color:#979797'></em>";
rlottieHandler.play();
}
function pause() {
document.getElementById("playButton").innerHTML = "<em class='fas fa-play'></em>";
rlottieHandler.pause();
}
function pauseDisable() {
document.getElementById("playButton").innerHTML = "<em class='fas fa-play' style='color:#979797'></em>";
rlottieHandler.pause();
}
function getRModule(canvasId) {
return rlottieHandler.rlottieModule[canvasId];
}
function windowResizeDone() {
rlottieHandler.relayoutCanvas();
if (rlottieHandler.wasPlaying) {
rlottieHandler.wasPlaying = false;
play();
}
else rlottieHandler.update();
}
function windowResize() {
if (rlottieHandler.playing) {
rlottieHandler.wasPlaying = true;
pause();
}
clearTimeout(rlottieHandler.resizeId);
rlottieHandler.resizeId = setTimeout(windowResizeDone, 150);
}
function buttonClicked() {
if (rlottieHandler.playing) pause();
else play();
}
function onSliderDrag(value) {
rlottieHandler.seek(value);
}
function addListener() {
window.addEventListener("dragover", handleDragOver, false);
window.addEventListener("drop", handleFileSelect, false);
window.addEventListener("resize", windowResize);
}
function addImportListener() {
var input = document.getElementById("fileSelector");
input.addEventListener("change", fileSelectionChanged);
}
function fileSelectionChanged() {
var input = document.getElementById("fileSelector");
var contentName = document.getElementById("contentName");
contentName.innerText = input.files[0].name;
contentName.title = input.files[0].name;
handleFiles(input.files);
}
function handleFiles(files) {
for (let i = 0, f; f = files[i]; i++) {
if (f.type.includes("json")) {
var read = new FileReader();
read.readAsText(f);
read.onloadend = function () {
rlottieHandler.reload(read.result);
}
break;
}
}
}
function getLottieFromUrl(input) {
var url = input.trim();
if (url == "" || !(url.startsWith("http://") || url.startsWith("https://"))) {
alert("Please enter correct URL that starts with 'http://' or 'https://'");
return;
}
var xhr = new XMLHttpRequest();
xhr.onload = function () {
if (xhr.status == 200) {
var data = xhr.responseText;
try {
JSON.parse(data);
} catch (error) {
throw new Error("The URL you entered is not in JSON format");
}
rlottieHandler.reload(data);
} else {
throw new Error("Your request failed. Please type in another URL.");
}
};
xhr.open("GET", url);
xhr.send(null);
}
function handleDragOver(event) {
event.stopPropagation();
event.preventDefault();
event.dataTransfer.dropEffect = "copy";
}
function handleFileSelect(event) {
event.stopPropagation();
event.preventDefault();
var contentName = document.getElementById('contentName')
contentName.innerText = event.dataTransfer.files[0].name
contentName.title = event.dataTransfer.files[0].name
handleFiles(event.dataTransfer.files);
}
function setLayerColor(keypath, r, g, b) {
rlottieHandler.rlottieModule[0].lottieHandle.setFillColor(keypath, r, g, b);
rlottieHandler.rlottieModule[0].lottieHandle.setStrokeColor(keypath, r, g, b);
}
function setLayerOpacity(keypath, opacity) {
rlottieHandler.rlottieModule[0].lottieHandle.setFillOpacity(keypath, opacity);
rlottieHandler.rlottieModule[0].lottieHandle.setStrokeOpacity(keypath, opacity);
}
function setStrokeWidth(keypath, width) {
rlottieHandler.rlottieModule[0].lottieHandle.setStrokeWidth(keypath, width);
}
function setPosition(keypath, x, y) {
rlottieHandler.rlottieModule[0].lottieHandle.setPosition(keypath, x, y);
}
function setScale(keypath, width, height) {
rlottieHandler.rlottieModule[0].lottieHandle.setScale(keypath, width, height);
}
function setRotation(keypath, degree) {
rlottieHandler.rlottieModule[0].lottieHandle.setRotation(keypath, degree);
}
function onResizeSliderDrag(value) {
var width = document.getElementById("player").clientWidth;
var height = document.getElementById("player").clientHeight;
width = width / 4 * 3;
height = height / 4 * 3;
var size = width < height ? width : height;
size = size * (value / 100);
if (size < 20)
size = 20;
rlottieHandler.rlottieModule.forEach(rm => {
rm.canvas.width = size;
rm.canvas.height = size;
rm.canvas.style.width = size + "px";
rm.canvas.style.height = size + "px";
});
rlottieHandler.update();
}