-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
155 lines (112 loc) · 3.74 KB
/
script.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
const config = {
MAX_WIDTH : 200,
MAX_HEIGHT : 200,
preview : true,
}
const fInput = document.querySelector('input[type=file]');
let preview = undefined;
class PreviewMode{
constructor(){
this.previewNode = document.createElement('div');
this.previewNode.setAttribute('id', 'preview')
this.emptyPreview();
const controlsNode = document.querySelector('.controls')
document.body.insertBefore(this.previewNode, controlsNode);
}
addElement(canvas, file, dataURL){
const elemNode = document.createElement('div');
elemNode.setAttribute('class', 'element');
elemNode.appendChild(canvas);
this.displayImage(elemNode, dataURL);
elemNode.title = file.name;
this.previewNode.appendChild(elemNode);
}
displayImage(node, dataURL){
const a = document.createElement('a');
a.setAttribute('href', dataURL);
const img = document.createElement('img');
img.setAttribute('src', dataURL);
a.appendChild(img);
node.appendChild(a);
}
emptyPreview(){
this.previewNode.innerHTML = `
<span>Не выбрано изображений</span>
`;
}
cleanPreview(){
this.previewNode.innerHTML = '';
}
remove(){
this.previewNode.remove();
}
}
function squeezeImages(e, cb){
const b64Images = [];
if (config.preview) preview.cleanPreview();
Array.from(e.target.files).forEach(computeImage);
cb(b64Images);
function computeImage(file){
squeeze(file, config.MAX_WIDTH, config.MAX_HEIGHT)
}
function squeeze(file, maxWidth, maxHeight){
const image = new Image();
image.onload = ()=>{
const size = computeImageSize(image, maxWidth, maxHeight);
const canvas = document.createElement('canvas');
canvas.width = size.width;
canvas.height = size.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(image, 0, 0, size.width, size.height);
const dataURL = canvas.toDataURL(file.type);
const b64 = dataURL.split(',').slice(-1)[0];
b64Images.push(b64);
if (config.preview) preview.addElement(canvas, file, dataURL);
}
let dataURL = URL.createObjectURL(file);
image.src=dataURL;
}
function computeImageSize(image, maxWidth, maxHeight){
const aspectRatio = image.height / image.width;
const preHeight = Math.min(image.height, maxHeight)
const preWidth = preHeight / aspectRatio;
const height = (preWidth <= maxWidth) ? preHeight : maxWidth * aspectRatio;
const width = preWidth <= maxWidth ? preWidth : maxWidth;
return { width, height };
}
}
fInput.addEventListener('change', e=>{squeezeImages(e, processImages)});
function processImages(data){
console.log(data)
}
const previewToggleNode = document.querySelector('input[type=checkbox][name=preview]')
const maxHeightNode = document.querySelector('input[name=maxHeight]')
const maxWidthNode = document.querySelector('input[name=maxWidth]')
initControls();
previewToggleNode.addEventListener('change', updatePreview);
updatePreview();
function updatePreview(){
config.preview = previewToggleNode.checked;
fInput.value = '';
if (previewToggleNode.checked){
preview = new PreviewMode();
} else {
preview.remove();
preview = undefined;
}
}
maxWidthNode.addEventListener('change', updateMaxWidth)
maxWidthNode.addEventListener('input', updateMaxWidth)
maxHeightNode.addEventListener('change', updateMaxHeight)
maxHeightNode.addEventListener('input', updateMaxHeight)
function updateMaxHeight(){
config.MAX_HEIGHT = maxHeightNode.value;
}
function updateMaxWidth(){
config.MAX_WIDTH = maxWidthNode.value;
}
function initControls(){
maxHeightNode.value = config.MAX_HEIGHT;
maxWidthNode.value = config.MAX_WIDTH;
previewToggleNode.checked = config.preview;
}