-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (124 loc) · 3.59 KB
/
script.js
File metadata and controls
133 lines (124 loc) · 3.59 KB
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
let animation = null;
const lottieContainer = document.getElementById('lottie-preview');
const fileInput = document.getElementById('file-input');
const jsonEditor = document.getElementById('json-editor');
const exportJsonBtn = document.getElementById('export-json');
const exportTgsBtn = document.getElementById('export-tgs');
const refreshBtn = document.getElementById('refresh');
function clearLottie() {
if (animation) {
try { animation.destroy(); } catch {}
lottieContainer.innerHTML = '';
animation = null;
}
}
function showError(msg) {
alert(msg);
console.error(msg);
}
function renderLottie(jsonString) {
clearLottie();
let json;
try {
json = JSON.parse(jsonString);
} catch (e) {
showError('Invalid JSON: ' + e.message);
return;
}
try {
animation = lottie.loadAnimation({
container: lottieContainer,
renderer: 'svg',
loop: true,
autoplay: true,
animationData: json,
rendererSettings: {
preserveAspectRatio: 'xMidYMid meet',
clearCanvas: true,
progressiveLoad: true,
className: 'lottie-svg'
}
});
animation.addEventListener('DOMLoaded', function () {
console.log('Lottie animation loaded!');
});
animation.addEventListener('data_failed', function () {
showError('Failed to render animation: Lottie data error');
});
animation.addEventListener('error', function (e) {
showError('Lottie error: ' + e.message);
});
} catch (e) {
showError('Failed to load animation: ' + e.message);
}
}
fileInput.addEventListener('change', function(e) {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
if (file.name.endsWith('.tgs')) {
reader.onload = () => {
try {
const compressed = new Uint8Array(reader.result);
let decompressed;
try {
decompressed = pako.ungzip(compressed, { to: 'string' });
} catch (decompErr) {
try {
const decoder = new TextDecoder("utf-8");
decompressed = decoder.decode(pako.ungzip(compressed));
} catch {
throw decompErr;
}
}
jsonEditor.value = decompressed;
renderLottie(decompressed);
} catch (err) {
showError("Invalid TGS file: " + (err.message || err));
}
};
reader.readAsArrayBuffer(file);
} else {
reader.onload = () => {
jsonEditor.value = reader.result;
renderLottie(reader.result);
};
reader.readAsText(file);
}
});
refreshBtn.addEventListener('click', () => {
renderLottie(jsonEditor.value);
});
// Export as JSON
exportJsonBtn.addEventListener('click', () => {
const data = jsonEditor.value;
try {
JSON.parse(data); // Validate before export
const blob = new Blob([data], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = "animation.json";
a.click();
URL.revokeObjectURL(url);
} catch (err) {
showError('Cannot export: Invalid JSON');
}
});
// Export as TGS (gzip)
exportTgsBtn.addEventListener('click', () => {
try {
const json = JSON.parse(jsonEditor.value);
const stringData = JSON.stringify(json);
const compressed = pako.gzip(stringData);
const blob = new Blob([compressed], { type: "application/gzip" });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = "animation.tgs";
a.click();
URL.revokeObjectURL(url);
} catch (err) {
showError('Cannot export: Invalid JSON for TGS');
}
});