-
Notifications
You must be signed in to change notification settings - Fork 111
/
main.js
187 lines (148 loc) · 4.12 KB
/
main.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
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
class RadioInput {
constructor(name, onChange) {
this.inputs = document.querySelectorAll(`input[name=${name}]`);
for (let input of this.inputs) {
input.addEventListener('change', onChange);
}
}
get value() {
for (let input of this.inputs) {
if (input.checked) {
return input.value;
}
}
}
}
class Input {
constructor(id, onChange) {
this.input = document.getElementById(id);
this.input.addEventListener('change', onChange);
this.valueAttrib = this.input.type === 'checkbox' ? 'checked' : 'value';
}
get value() {
return this.input[this.valueAttrib];
}
}
class CubeFace {
constructor(faceName) {
this.faceName = faceName;
this.anchor = document.createElement('a');
this.anchor.style.position='absolute';
this.anchor.title = faceName;
this.img = document.createElement('img');
this.img.style.filter = 'blur(4px)';
this.anchor.appendChild(this.img);
}
setPreview(url, x, y) {
this.img.src = url;
this.anchor.style.left = `${x}px`;
this.anchor.style.top = `${y}px`;
}
setDownload(url, fileExtension) {
this.anchor.href = url;
this.anchor.download = `${this.faceName}.${fileExtension}`;
this.img.style.filter = '';
}
}
function removeChildren(node) {
while (node.firstChild) {
node.removeChild(node.firstChild);
}
}
const mimeType = {
'jpg': 'image/jpeg',
'png': 'image/png'
};
function getDataURL(imgData, extension) {
canvas.width = imgData.width;
canvas.height = imgData.height;
ctx.putImageData(imgData, 0, 0);
return new Promise(resolve => {
canvas.toBlob(blob => resolve(URL.createObjectURL(blob)), mimeType[extension], 0.92);
});
}
const dom = {
imageInput: document.getElementById('imageInput'),
faces: document.getElementById('faces'),
generating: document.getElementById('generating')
};
dom.imageInput.addEventListener('change', loadImage);
const settings = {
cubeRotation: new Input('cubeRotation', loadImage),
interpolation: new RadioInput('interpolation', loadImage),
format: new RadioInput('format', loadImage),
};
const facePositions = {
pz: {x: 1, y: 1},
nz: {x: 3, y: 1},
px: {x: 2, y: 1},
nx: {x: 0, y: 1},
py: {x: 1, y: 0},
ny: {x: 1, y: 2}
};
function loadImage() {
const file = dom.imageInput.files[0];
if (!file) {
return;
}
const img = new Image();
img.src = URL.createObjectURL(file);
img.addEventListener('load', () => {
const {width, height} = img;
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0);
const data = ctx.getImageData(0, 0, width, height);
processImage(data);
});
}
let finished = 0;
let workers = [];
function processImage(data) {
removeChildren(dom.faces);
dom.generating.style.visibility = 'visible';
for (let worker of workers) {
worker.terminate();
}
for (let [faceName, position] of Object.entries(facePositions)) {
renderFace(data, faceName, position);
}
}
function renderFace(data, faceName, position) {
const face = new CubeFace(faceName);
dom.faces.appendChild(face.anchor);
const options = {
data: data,
face: faceName,
rotation: Math.PI * settings.cubeRotation.value / 180,
interpolation: settings.interpolation.value,
};
const worker = new Worker('convert.js');
const setDownload = ({data: imageData}) => {
const extension = settings.format.value;
getDataURL(imageData, extension)
.then(url => face.setDownload(url, extension));
finished++;
if (finished === 6) {
dom.generating.style.visibility = 'hidden';
finished = 0;
workers = [];
}
};
const setPreview = ({data: imageData}) => {
const x = imageData.width * position.x;
const y = imageData.height * position.y;
getDataURL(imageData, 'jpg')
.then(url => face.setPreview(url, x, y));
worker.onmessage = setDownload;
worker.postMessage(options);
};
worker.onmessage = setPreview;
worker.postMessage(Object.assign({}, options, {
maxWidth: 200,
interpolation: 'linear',
}));
workers.push(worker);
}