-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlottie2Sprite.js
237 lines (206 loc) · 7.4 KB
/
lottie2Sprite.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
var FileSaver = require('file-saver');
var JSZip = require("jszip");
let cancelled = false;
let zipContent = null;
const logContainer = document.querySelector("#logs");
const player = document.querySelector("lottie-player");
const btnLoad = document.querySelector("#load");
const btnConvert = document.querySelector("#convert");
const btnConvertPng = document.querySelector("#convertPng");
const btnConvertSprite = document.querySelector("#convertSprite");
const inputLottieUrl = document.querySelector("#lottieUrl");
const spriteName = document.querySelector("#spriteName");
const imageWidth = document.querySelector("#imgW");
const imageHeight = document.querySelector("#imgH");
const log = message => {
const msgElem = document.createElement("p");
msgElem.innerText = message;
logContainer.appendChild(msgElem);
logContainer.scrollTop = logContainer.scrollHeight;
};
const loadAsImage = markup => {
const img = new Image();
return new Promise((res, rej) => {
img.onload = e => res(img);
img.onerror = rej;
img.src = "data:image/svg+xml;charset=utf-8," + encodeURIComponent(markup);
});
};
const convert = async url => {
cancelled = false;
log(`Loading image`);
// Get lottie size
const lottieDetails = await getLottieDetails(url);
log(
`Lottie as width = ${lottieDetails.width}, height = ${
lottieDetails.height
}, frames = ${lottieDetails.frames}`
);
log(`Set sprite dimensions...`);
// Load into player
player.addEventListener("ready", async () => {
player.stop();
player.setLooping(false);
zipContent = null;
const zip = new JSZip();
const photoZip = zip.folder("photos");
for (let f = 1; f <= lottieDetails.frames; f++) {
if (cancelled) break;
log(`Rendering frame #${f}...`);
player.seek(f);
// await new Promise(resolve => setTimeout(() => resolve(), 1000));
const svgMarkup = await player.snapshot(false);
try {
const svgAsImg = await loadAsImage(svgMarkup);
photoZip.file(`${spriteName.value}_${f}.svg`, svgMarkup,{base64: false});
const xOffset = lottieDetails.width * (f - 1);
ctx.drawImage(svgAsImg, xOffset, 0);
} catch (e) {}
}
zip.generateAsync({type:"blob"}).then(function(content) {
// see FileSaver.js
FileSaver.saveAs(content, "sprites_svg.zip");
});
log(`Done!`);
player.removeEventListener("ready", ()=> {})
});
log(`Startng animation...`);
player.load(url);
};
const getLottieDetails = async url => {
let json = "";
if (url.indexOf("{") == 0) {
json = JSON.parse(url);
} else {
const result = await fetch(url);
json = await result.json();
}
console.log("getLottieDetails", {
height: json.h,
width: json.w,
frames: json.op - json.ip
})
return {
height: json.h,
width: json.w,
frames: json.op - json.ip
};
};
const convertSpiteSheet = async url => {
cancelled = false;
const zip = new JSZip();
log(`Loading ${url}`);
// Get lottie size
const lottieDetails = await getLottieDetails(url);
log(
`Lottie as width = ${lottieDetails.width}, height = ${
lottieDetails.height
}, frames = ${lottieDetails.frames}`
);
const canvas2 = document.createElement('canvas'); // Not shown on page
const ctx = canvas2.getContext("2d");
canvas2.height = imageHeight.value;
canvas2.width = imageWidth.value * lottieDetails.frames;
log(`Set sprite dimensions...`);
// Load into player
player.addEventListener("ready", async () => {
player.stop();
player.setLooping(false);
for (let f = 1; f <= lottieDetails.frames; f++) {
if (cancelled) break;
log(`Rendering frame #${f}...`);
player.seek(f);
const svgMarkup = await player.snapshot(false);
try {
const svgAsImg = await loadAsImage(svgMarkup);
const xOffset = imageWidth.value * (f - 1);
ctx.drawImage(svgAsImg, xOffset, 0, imageWidth.value, imageHeight.value);
} catch (e) {
console.log(e)
}
}
log(`Done!`);
const savable = new Image();
savable.src = canvas2.toDataURL("image/png");
zip.file(`${spriteName.value}_Sprite.png`, savable.src.substr(savable.src.indexOf(',')+1),{base64: true});
zip.generateAsync({type:"blob"}).then(function(content) {
// see FileSaver.js
FileSaver.saveAs(content, "spritesSheet.zip");
});
player.removeEventListener("ready", ()=> {})
});
log(`Startng animation...`);
player.load(url);
};
const convertPNG = async url => {
cancelled = false;
log(`Loading image`);
// Get lottie size
const lottieDetails = await getLottieDetails(url);
log(
`Lottie as width = ${lottieDetails.width}, height = ${
lottieDetails.height
}, frames = ${lottieDetails.frames}`
);
log(`Set sprite dimensions...`);
// Load into player
player.addEventListener("ready", async () => {
player.stop();
player.setLooping(false);
zipContent = null;
const zip = new JSZip();
const photoZip = zip.folder("photos");
for (let f = 1; f <= lottieDetails.frames; f++) {
if (cancelled) break;
log(`Rendering frame #${f}...`);
player.seek(f);
// await new Promise(resolve => setTimeout(() => resolve(), 1000));
const svgMarkup = await player.snapshot(false);
try {
const svgAsImg = await loadAsImage(svgMarkup);
const canTemp = document.createElement('canvas'); // Not shown on page
canTemp.width = imageWidth.value;
canTemp.height = imageHeight.value;
const ctxTemp = canTemp.getContext('2d');
ctxTemp.drawImage(svgAsImg, 0, 0, canTemp.width, canTemp.height);
const savable = new Image();
savable.src = canTemp.toDataURL("image/png");
photoZip.file(`${spriteName.value}_${f}.png`, savable.src.substr(savable.src.indexOf(',')+1),{base64: true});
} catch (e) {
console.log(e);
}
}
zip.generateAsync({type:"blob"}).then(function(content) {
// see FileSaver.js
FileSaver.saveAs(content, "sprites_png.zip");
});
log(`Done!`);
player.removeEventListener("ready", ()=> {})
});
log(`Startng animation...`);
player.load(url);
};
if (btnLoad) {
btnLoad.addEventListener("click", async () => {
player.load(inputLottieUrl.value);
const lottieDetails = await getLottieDetails(inputLottieUrl.value);
imageWidth.value = lottieDetails.width;
imageHeight.value = lottieDetails.height;
cancelled = false;
});
}
if (btnConvert) {
btnConvert.addEventListener("click", () => {
convert(inputLottieUrl.value);
});
}
if (btnConvertPng) {
btnConvertPng.addEventListener("click", () => {
convertPNG(inputLottieUrl.value);
});
}
if (btnConvertSprite) {
btnConvertSprite.addEventListener("click", () => {
convertSpiteSheet(inputLottieUrl.value);
});
}