-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
320 lines (267 loc) · 7.6 KB
/
app.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
'use strict';
/**
* Dependencies
*/
var UPLOAD_URL = require('./config.json').upload_url;
var exif = require('exif-js');
function App(el) {
this.el = el;
this.els = {};
this.render();
this.setState('empty');
}
App.prototype = {
render: function() {
this.el.innerHTML = this.template();
// grab refs
this.els = {
preview: this.el.querySelector('.preview'),
input: this.el.querySelector('.file-input'),
upload: this.el.querySelector('.button-upload'),
cancel: this.el.querySelector('.button-cancel')
};
// events
this.els.input.addEventListener('change', this.onFileChanged.bind(this));
this.els.cancel.addEventListener('click', this.clear.bind(this));
this.els.upload.addEventListener('click', this.upload.bind(this));
},
template: function() {
return '<div class="container">' +
'<div class="preview"></div>' +
'<div class="canvas-overlay">' +
'<label class="button button-file-input">Add Photo' +
'<small>to Magnet photo wall</small>' +
'<input type="file" class="file-input" hidden/>' +
'</label>' +
'<button class="button button-upload">Upload</button>' +
'<div class="bottom">' +
'<button class="button button-cancel">Cancel</button>' +
'</div>' +
'<h2 class="scaling">Resizing image …</h2>' +
'<h2 class="uploading">Uploading …</h2>' +
'<h2 class="confirmation">Uploaded</h2>' +
'</div>' +
'</div>';
},
onFileChanged: function(e) {
var file = e.target.files[0];
if (!file) return;
this.renderImage(file);
},
renderImage: function(file) {
this.setState('scaling');
this.image = new ScaledImage(file, {
width: 600,
height: 600
});
this.image.process(function() {
this.setState('uploadable');
this.els.preview.appendChild(this.image.canvas);
}.bind(this));
},
upload: function() {
if (!(this.image && this.image.complete)) return;
var self = this;
this.setState('uploading');
upload(this.image.toBlob(), this.image.name, function(err) {
if (err) throw err;
self.setState('uploaded');
setTimeout(function() {
self.clear();
}, 1200);
});
},
clear: function() {
this.els.preview.innerHTML = '';
this.els.input.value = '';
this.image = null;
this.setState('empty');
},
setState: function(value) {
this.state = value;
this.el.setAttribute('state', value);
}
};
function ScaledImage(file, options) {
this.canvas = document.createElement('canvas');
this.canvas.width = options.width;
this.canvas.height = options.height;
this.name = file.name;
this.file = file;
this.image = new Image();
}
ScaledImage.prototype = {
load: function(done) {
this.image.src = URL.createObjectURL(this.file);
this.image.onload = function() {
this.naturalWidth = this.image.naturalWidth;
this.naturalHeight = this.image.naturalHeight;
done();
}.bind(this);
},
process: function(done) {
var self = this;
this.load(function() {
self.getOrientation(function(err, orientation) {
self.orientation = orientation;
var canvas = self.downsample();
URL.revokeObjectURL(self.image.src);
canvas = self.adjustAngle(canvas);
self.draw(canvas);
self.complete = true;
done(null);
});
});
},
getOrientation: function(done) {
exif.getData(this.image, function() {
done(null, exif.getTag(this, 'Orientation'));
});
},
downsample: function() {
var size = {
width: this.naturalWidth,
height: this.naturalHeight
};
var scaled = fill(this.canvas, size);
return resizeImage(this.image, size, scaled);
},
adjustAngle: function(canvas) {
return applyOrientation(canvas, this.orientation);
},
draw: function(image) {
var ctx = this.canvas.getContext('2d');
ctx.translate(this.canvas.width / 2, this.canvas.height / 2);
ctx.drawImage(image, -image.width / 2, -image.height / 2);
},
toBlob: function() {
var type = this.file.type;
var uri = this.canvas.toDataURL(type)
return dataUriToBlob(uri, type);
}
};
function fill(parent, child) {
var sw = parent.width / child.width;
var sh = parent.height / child.height;
// select the largest scale to fill image
// completely within the container
var scale = Math.max(sw, sh);
var w = child.width * scale;
var h = child.height * scale;
return {
width: w,
height: h
};
}
function resizeImage(image, from, to) {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var steps = 2;
var pxPerStep = (from.width - to.width) / steps;
var w = from.width;
var h = from.height;
for (var i = 0; i < steps; i++) {
var w2 = w - pxPerStep;
var scale = w2 / w;
var h2 = h * scale;
canvas.width = w2;
canvas.height = h2;
// ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(
image,
0,
0,
w2,
h2
);
w = w2;
h = h2;
}
return canvas;
}
/**
* Applys transforms to rotate/flip
* image so that it appears upright.
*
* Spec: http://jpegclub.org/exif_orientation.html
*
* @param {HTMLCanvasElement} canvas
* @param {Number} orientation
* @return {HTMLCanvasElement}
*/
function applyOrientation(canvas, orientation) {
switch (orientation) {
case 1: return canvas;
case 2: return flipHorizontal(canvas);
case 3: return rotate(canvas, 180);
case 4: return flipVertical(canvas);
case 5: return flipHorizontal(rotate(canvas, 90));
case 6: return rotate(canvas, 90);
case 7: return flipVertical(rotate(canvas, 90));
case 8: return rotate(canvas, 270);
case undefined: return canvas;
default: throw Error('unknown orientation value: ' + orientation);
}
}
function flipHorizontal(image) {
return flip(image, 'horizontal');
}
function flipVertical(image) {
return flip(image, 'vertical');
}
function flip(image, direction) {
var canvas = document.createElement('canvas');
var vertical = direction == 'vertical';
var ctx = canvas.getContext('2d');
canvas.width = image.width;
canvas.height = image.height;
ctx.translate(
!vertical ? canvas.width : 0,
vertical ? canvas.height : 0
);
ctx.scale(
vertical ? 1 : -1,
vertical ? -1 : 1
);
ctx.drawImage(image, 0, 0);
return canvas;
}
function rotate(canvas, deg) {
var rotated = document.createElement('canvas');
rotated.ctx = rotated.getContext('2d');
var rotatedSize = getRotatedSize(canvas, deg);
rotated.width = rotatedSize.width;
rotated.height = rotatedSize.height;
rotated.ctx.translate(rotated.width / 2, rotated.height / 2);
rotated.ctx.rotate(deg * Math.PI / 180);
rotated.ctx.drawImage(canvas, -canvas.width / 2, -canvas.height / 2);
return rotated;
}
function getRotatedSize(size, deg) {
switch (deg) {
case 90:
case 270: return { width: size.height, height: size.width }
case 180:
case 0: return size;
}
}
function upload(blob, name, callback) {
var formData = new FormData();
var xhr = new XMLHttpRequest();
formData.append('image', blob, name);
xhr.open('POST', UPLOAD_URL, true);
xhr.send(formData);
xhr.onreadystatechange = function() {
if (xhr.readyState !== XMLHttpRequest.DONE) return;
callback(null, xhr.responseText);
};
}
function dataUriToBlob(string, type) {
var binary = atob(string.split(',')[1]);
var array = [];
for (var i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
return new Blob([new Uint8Array(array)], { type: type });
}
new App(document.querySelector('.app'));