-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
149 lines (125 loc) · 4.04 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
'use strict';
const ditherWorker = new Worker('worker.js');
const fullscreenElement = [
'fullscreenElement',
'webkitFullscreenElement',
'mozFullScreenElement',
'msFullscreenElement'
].find(prop => prop in document);
const exitFullscreen = [
'exitFullscreen',
'webkitExitFullscreen',
'mozCancelFullScreen',
'msExitFullscreen'
].find(prop => prop in document);
const requestFullscreen = [
'requestFullscreen',
'webkitRequestFullscreen',
'mozRequestFullScreen',
'msRequestFullscreen'
].find(prop => prop in Element.prototype);
const fullscreenerror = [
'fullscreenerror',
'webkitfullscreenerror',
'mozfullscreenerror',
'msfullscreenerror'
].find(eventName => 'on' + eventName in document);
window.addEventListener('load', function() {
const img = new Image();
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
const form = document.getElementById('dither');
const uploadLbl = document.getElementById('uploadLbl');
const uploader = document.getElementById('uploader');
const formButtons = document.getElementById('formButtons');
const statusBox = document.getElementById('processing');
const hint = document.getElementById('hint');
const output = document.getElementById('output');
const downloadBtn = document.getElementById('download');
img.addEventListener('load', () => draw(ctx, img));
form.addEventListener('submit', (event) => {
event.preventDefault();
const reader = new FileReader();
const file = uploader.files[0];
if(file == null || !(/image\/.+/).test(file.type) && file.type) {
return;
}
const name = file.name || 'ditheredImage';
downloadBtn.dataset.download = name.replace(/\.[^.]+$/, '');
reader.addEventListener('load', () => {
if(img.src != reader.result) {
img.src = reader.result;
} else {
draw(ctx, img);
}
});
reader.readAsDataURL(uploader.files[0]);
// update UI
statusBox.classList.remove('hidden');
hint.classList.add('hidden');
output.classList.add('hidden');
formButtons.setAttribute('disabled', true);
});
form.addEventListener('reset', () => {
hint.classList.remove('hidden');
output.classList.add('hidden');
uploadLbl.textContent = 'Upload';
});
uploader.addEventListener('change', () => {
const file = uploader.files[0];
let name;
if(file == null) {
name = 'Upload';
} else {
name = file.name || 'Uploaded';
}
uploadLbl.textContent = name.length > 20 ? name.slice(0, 19) + '…' : name;
});
canvas.addEventListener('click', () => {
if(document[fullscreenElement]) {
document[exitFullscreen]();
canvas.title = 'Open preview';
} else {
document.getElementById('fullscreen')[requestFullscreen]();
canvas.title = 'Close preview';
}
});
document.addEventListener(fullscreenerror, () => {
canvas.title = 'Open preview';
});
downloadBtn.addEventListener('click', () => {
canvas.toBlob((blob) => {
const url = window.URL.createObjectURL(blob);
const aTag = document.createElement('a');
aTag.href = canvas.toDataURL();
aTag.download = downloadBtn.dataset.download;
aTag.style.display = 'none';
document.body.append(aTag);
aTag.click();
aTag.remove();
setTimeout(() => window.URL.revokeObjectURL(url), 0);
});
});
ditherWorker.addEventListener('message', (event) => {
const imageData = event.data;
drawCanvas(ctx, imageData, 'putImageData');
// update UI
statusBox.classList.add('hidden');
output.classList.remove('hidden');
formButtons.removeAttribute('disabled');
});
});
function draw(ctx, img) {
drawCanvas(ctx, img, 'drawImage');
const imageData = ctx.getImageData(0, 0, img.width, img.height);
ditherWorker.postMessage({
imageData,
ditherId: document.getElementById('bitdepth').value
}, [imageData.data.buffer]);
}
function drawCanvas(ctx, img, methodName) {
const { canvas } = ctx;
canvas.width = img.width;
canvas.height = img.height;
ctx[methodName](img, 0, 0);
}