-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsprite.js
47 lines (41 loc) · 1.49 KB
/
sprite.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
import {RGBAImage} from './image';
function getImageData(img, padding) {
const canvas = window.document.createElement('canvas');
const context = canvas.getContext('2d');
if (!context) {
throw new Error('failed to create canvas 2d context');
}
canvas.width = img.width;
canvas.height = img.height;
context.drawImage(img, 0, 0, img.width, img.height);
return context.getImageData(0, 0, img.width, img.height);
}
export default function(baseUrl) {
const format = window.devicePixelRatio > 1 ? '@2x' : '';
let json, image;
const jsonRequest = fetch(`${baseUrl}${format}.json`)
.then(r => r.json())
.then(r => json = r)
const imageRequest = fetch(`${baseUrl}${format}.png`)
.then(r => r.blob())
.then(r => {
image = new Image();
image.src = URL.createObjectURL(r);
return new Promise((resolve, reject) => {
image.onload = () => resolve();
image.onerror = () => reject();
});
});
return Promise.all([jsonRequest, imageRequest])
.then(() => {
const imageData = getImageData(image);
const result = {};
for (const id in json) {
const {width, height, x, y, sdf, pixelRatio, stretchX, stretchY, content} = json[id];
const data = new RGBAImage({width, height});
RGBAImage.copy(imageData, data, {x, y}, {x: 0, y: 0}, {width, height});
result[id] = {data, pixelRatio, sdf, stretchX, stretchY, content};
}
return result;
});
}