-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
132 lines (110 loc) · 3.56 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
import addImageToCanvas from "./modules/addImageToCanvas.js"
import dotifyCanvas from "./modules/dotifyCanvas.js";
import getAverageColorOfPixels from "./modules/getAverageColor.js";
import exportCanvas from "./modules/exportCanvas.js";
import readImageFromInput from "./modules/readImageFromInput.js";
const outputCanvas = document.getElementById("screen");
const mainContext = outputCanvas.getContext("2d");
const inputButton_button = document.getElementById("file");
const cache = document.createElement("canvas");
const cacheContext = cache.getContext("2d");
let color, gui, dropbox, w, h;
let systemCache = new Map();
let pixels = [];
const imgTypes =[
"image/png",
"image/jpeg",
"image/bmp",
];
let system = {
fileName: "",
imageScale: 1,
downScaling: 1,
imgType: "image/png",
grid:true,
exportCanvas:()=>exportCanvas(cache,this)
};
function buildGUI() {
gui = new dat.GUI({
name: 'GUI'
});
let controls = gui
.addFolder('File Options');
controls.open();
controls.add(system,"fileName")
.onChange((e) => system.fileName = stripExtensions(e));
controls.add(system, "imgType", imgTypes)
.onChange((e) => system.imgType = e);
let downScaling = gui.addFolder('Scaling')
downScaling.open();
downScaling.add(system, 'imageScale', .1, 1, .1)
.onFinishChange(update)
downScaling.add(system, 'downScaling', 1, 10, 1)
.onFinishChange(update);
gui
.add(system, "exportCanvas")
.name("Export");
}
function update() {
let key = `${system.fileName}${system.downScaling}${system.imageScale}`;
let img;
if(systemCache.has(key)){
img = systemCache.get(key);
addImageToCanvas(img, outputCanvas, system.imageScale,pixels);
}else{
img = dotifyCanvas(cacheContext, system.downScaling, cacheContext.getImageData(0, 0, cache.width, cache.height),pixels);
systemCache.set(key, img);
addImageToCanvas(img, outputCanvas, system.imageScale);
}
}
async function init(f) {
try {
system = {
fileName: "",
imageScale: 1,
downScaling: 1,
imgType: "image/png",
exportCanvas:()=>exportCanvas(cache,system)
}
let img = await readImageFromInput(f,system);
cache.width = img.width;
cache.height = img.height;
if (!gui) {
outputCanvas.classList.remove("visually-hidden");
document.getElementById("label").classList.add("visually-hidden");
document.getElementById("title").style.opacity = 0;
buildGUI();
}else{
gui.destroy();
buildGUI();
}
addImageToCanvas(img, cache);
update();
color = getAverageColorOfPixels(pixels);
document.body.style.backgroundColor = color;
} catch (err) {
console.log(err);
}
}
inputButton_button.addEventListener("change",e=>init(e.target))
function setUpDropbox() {
dropbox = document.getElementById("dropbox");
dropbox.addEventListener("dragenter", dragenter, false);
dropbox.addEventListener("dragover", dragover, false);
dropbox.addEventListener("drop", drop, false);
function dragenter(e) {
e.stopPropagation();
e.preventDefault();
}
function dragover(e) {
e.stopPropagation();
e.preventDefault();
}
function drop(e) {
e.stopPropagation();
e.preventDefault();
const dt = e.dataTransfer;
init(dt);
}
}
window.addEventListener("DOMContentLoaded",setUpDropbox)