Skip to content

Commit f1693c4

Browse files
authored
Create script.js
1 parent 7a56d3c commit f1693c4

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

script.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
let animation = null;
2+
3+
const lottieContainer = document.getElementById('lottie-preview');
4+
const fileInput = document.getElementById('file-input');
5+
const jsonEditor = document.getElementById('json-editor');
6+
const exportJsonBtn = document.getElementById('export-json');
7+
const exportTgsBtn = document.getElementById('export-tgs');
8+
const refreshBtn = document.getElementById('refresh');
9+
10+
function clearLottie() {
11+
if (animation) {
12+
try { animation.destroy(); } catch {}
13+
lottieContainer.innerHTML = '';
14+
animation = null;
15+
}
16+
}
17+
18+
function showError(msg) {
19+
alert(msg);
20+
console.error(msg);
21+
}
22+
23+
function renderLottie(jsonString) {
24+
clearLottie();
25+
let json;
26+
try {
27+
json = JSON.parse(jsonString);
28+
} catch (e) {
29+
showError('Invalid JSON: ' + e.message);
30+
return;
31+
}
32+
try {
33+
animation = lottie.loadAnimation({
34+
container: lottieContainer,
35+
renderer: 'svg',
36+
loop: true,
37+
autoplay: true,
38+
animationData: json,
39+
rendererSettings: {
40+
preserveAspectRatio: 'xMidYMid meet',
41+
clearCanvas: true,
42+
progressiveLoad: true,
43+
className: 'lottie-svg'
44+
}
45+
});
46+
animation.addEventListener('DOMLoaded', function () {
47+
console.log('Lottie animation loaded!');
48+
});
49+
animation.addEventListener('data_failed', function () {
50+
showError('Failed to render animation: Lottie data error');
51+
});
52+
animation.addEventListener('error', function (e) {
53+
showError('Lottie error: ' + e.message);
54+
});
55+
} catch (e) {
56+
showError('Failed to load animation: ' + e.message);
57+
}
58+
}
59+
60+
fileInput.addEventListener('change', function(e) {
61+
const file = e.target.files[0];
62+
if (!file) return;
63+
const reader = new FileReader();
64+
65+
if (file.name.endsWith('.tgs')) {
66+
reader.onload = () => {
67+
try {
68+
const compressed = new Uint8Array(reader.result);
69+
let decompressed;
70+
try {
71+
decompressed = pako.ungzip(compressed, { to: 'string' });
72+
} catch (decompErr) {
73+
try {
74+
const decoder = new TextDecoder("utf-8");
75+
decompressed = decoder.decode(pako.ungzip(compressed));
76+
} catch {
77+
throw decompErr;
78+
}
79+
}
80+
jsonEditor.value = decompressed;
81+
renderLottie(decompressed);
82+
} catch (err) {
83+
showError("Invalid TGS file: " + (err.message || err));
84+
}
85+
};
86+
reader.readAsArrayBuffer(file);
87+
} else {
88+
reader.onload = () => {
89+
jsonEditor.value = reader.result;
90+
renderLottie(reader.result);
91+
};
92+
reader.readAsText(file);
93+
}
94+
});
95+
96+
refreshBtn.addEventListener('click', () => {
97+
renderLottie(jsonEditor.value);
98+
});
99+
100+
// Export as JSON
101+
exportJsonBtn.addEventListener('click', () => {
102+
const data = jsonEditor.value;
103+
try {
104+
JSON.parse(data); // Validate before export
105+
const blob = new Blob([data], { type: "application/json" });
106+
const url = URL.createObjectURL(blob);
107+
const a = document.createElement('a');
108+
a.href = url;
109+
a.download = "animation.json";
110+
a.click();
111+
URL.revokeObjectURL(url);
112+
} catch (err) {
113+
showError('Cannot export: Invalid JSON');
114+
}
115+
});
116+
117+
// Export as TGS (gzip)
118+
exportTgsBtn.addEventListener('click', () => {
119+
try {
120+
const json = JSON.parse(jsonEditor.value);
121+
const stringData = JSON.stringify(json);
122+
const compressed = pako.gzip(stringData);
123+
const blob = new Blob([compressed], { type: "application/gzip" });
124+
const url = URL.createObjectURL(blob);
125+
const a = document.createElement('a');
126+
a.href = url;
127+
a.download = "animation.tgs";
128+
a.click();
129+
URL.revokeObjectURL(url);
130+
} catch (err) {
131+
showError('Cannot export: Invalid JSON for TGS');
132+
}
133+
});

0 commit comments

Comments
 (0)