-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
146 lines (128 loc) · 3.91 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
const filePicker = document.getElementById('file-picker');
const editorCanvas = document.getElementById('canvas-editor');
const resultCanvas = document.getElementById('canvas-result');
const canvasContainer = document.getElementsByClassName('canvas-container')[0];
const convertButton = document.getElementById('convert-button');
const editorContext = editorCanvas.getContext('2d');
const resultContext = resultCanvas.getContext('2d');
const colorThief = new ColorThief();
const container = document.querySelector('.container');
const ImagesCount = 420;
const ImagesFolderCount = './app_images_minecraft_16';
const pixle_size = 16;
const IconPixles = [];
const loadIcons = (index = 1) => {
if (index > ImagesCount) {
//show page content when finished loading
container.style.visibility = 'visible';
return;
}
let imageName = '' + index + '.png';
let imagePath = ImagesFolderCount + '/' + imageName;
let img = document.createElement('img');
img.src = imagePath;
img.onload = (ev) => {
editorCanvas.width = img.width;
editorCanvas.height = img.height;
editorContext.drawImage(img, 0, 0);
let imageData = editorContext.getImageData(
0,
0,
editorCanvas.width,
editorCanvas.height
);
IconPixles.push({
image: imageData,
rgb: colorThief.getColor(img),
});
loadIcons(index + 1);
};
};
loadIcons();
const downloadImage = () => {
let dataURL = resultCanvas.toDataURL('image/png');
let link = document.createElement('a');
link.href = dataURL;
link.download = 'image.png';
link.click();
};
const process = (img) => {
resultCanvas.classList.add('blurred');
canvasContainer.style.visibility = 'visible';
canvasContainer.style.position = 'static';
editorCanvas.width = img.width;
editorCanvas.height = img.height;
editorContext.drawImage(img, 0, 0);
resultCanvas.width = img.width;
resultCanvas.height = img.height;
resultContext.drawImage(img, 0, 0);
convertButton.addEventListener('click', (e) => {
let imageData = editorContext.getImageData(
0,
0,
editorCanvas.width,
editorCanvas.height
);
resultCanvas.classList.remove('blurred');
resultCanvas.width = img.width * pixle_size;
resultCanvas.height = img.height * pixle_size;
navigator.serviceWorker.controller.postMessage({
type: 'process-image',
imageData: imageData,
IconPixles: IconPixles,
pixle_size: pixle_size,
});
});
};
const registerServiceWorker = async () => {
if ('serviceWorker' in navigator) {
try {
const registration = await navigator.serviceWorker.register(
'serviceWorker.js',
{
scope: '/',
}
);
if (registration.installing) {
console.log('Service worker installing');
} else if (registration.waiting) {
console.log('Service worker installed');
} else if (registration.active) {
console.log('Service worker active');
}
} catch (error) {
console.error(`Registration failed with ${error}`);
}
}
};
registerServiceWorker();
filePicker.addEventListener('change', (e) => {
let file = e.target.files[0];
let reader = new FileReader();
reader.onload = function (event) {
let img = new Image();
img.onload = function () {
process(img);
};
img.src = event.target.result;
};
reader.readAsDataURL(file);
});
navigator.serviceWorker.addEventListener('message', (event) => {
if (event.data && event.data.type === 'process-image') {
let width = event.data.width;
let height = event.data.height;
let row = event.data.row;
let resultImageData = new ImageData(
new Uint8ClampedArray(event.data.resultImageDataArray),
width * pixle_size,
pixle_size
);
console.log(resultImageData);
resultContext.putImageData(resultImageData, 0, row * pixle_size);
if (row === height - 1)
setTimeout(() => {
downloadImage();
}, 200);
}
});