-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileHandler.js
48 lines (45 loc) · 1.42 KB
/
FileHandler.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
class FileHandler {
reader = new FileReader();
resize(imagePath, callback) {
const originalImage = new Image();
originalImage.src = imagePath;
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
originalImage.addEventListener('load', () => {
const originalWidth = originalImage.naturalWidth;
const originalHeight = originalImage.naturalHeight;
const ratio = originalWidth / originalHeight;
if (ratio != 1) {
document.getElementById('error').style.setProperty('display', '');
return;
}
canvas.width = 48;
canvas.height = 48;
ctx.drawImage(originalImage, 0, 0, canvas.width, canvas.height);
callback(document.querySelector('canvas').toDataURL('image/png', 0.3));
});
}
getImage(callback) {
const file = document.querySelector('input[type=file]').files[0];
this.reader.addEventListener('load', (res) => {
const image = res.target.result;
this.resize(image, callback);
});
if (typeof file == 'object') {
this.reader.readAsDataURL(file);
} else {
callback(null);
}
}
getText(callback) {
const file = document.querySelector('input[type=file]').files[0];
this.reader.addEventListener('load', (res) => {
callback(res.target.result);
});
if (typeof file == 'object') {
this.reader.readAsText(file);
} else {
callback(null);
}
}
}