-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
53 lines (50 loc) · 2.06 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
var foregroundImage = null;
var backgroundImage = null;
// initial global variable with uploaded image then display
function upload(type) {
if (type == 'foreground') {
var imgcanvas = document.getElementById("foregroundCanvas");
var fileinput = document.getElementById("foregroundFile");
foregroundImage = new SimpleImage(fileinput);
foregroundImage.drawTo(imgcanvas);
}
else {
var imgcanvas = document.getElementById("backgroundCanvas");
var fileinput = document.getElementById("backgroundFile");
backgroundImage = new SimpleImage(fileinput);
backgroundImage.drawTo(imgcanvas);
}
}
// solve using global var images
function solve() {
// if either image not uploaded alert error
if (foregroundImage == null || backgroundImage == null) {
alert("Please upload both images. If it still doesn't work then give it a minute to let the picutes load.");
}
// check every pixel and replace if green else dont
else {
var output = new SimpleImage(foregroundImage.getWidth(), foregroundImage.getHeight());
for (var pixel of foregroundImage.values()) {
if (pixel.getGreen() > (pixel.getRed() + pixel.getBlue())) {
var x = pixel.getX();
var y = pixel.getY();
var backgroundPixel = backgroundImage.getPixel(x, y);
output.setPixel(x, y, backgroundPixel);
}
else {
output.setPixel(pixel.getX(), pixel.getY(), pixel);
}
}
imgcanvas = document.getElementById("solutionCanvas");
output.drawTo(imgcanvas);
}
}
// initial global variable with demo images then display
function demo() {
var imgcanvasForeground = document.getElementById("foregroundCanvas");
var imgcanvasBackground = document.getElementById("backgroundCanvas");
foregroundImage = new SimpleImage("test-fg.png");
backgroundImage = new SimpleImage("test-bg.png");
foregroundImage.drawTo(imgcanvasForeground);
backgroundImage.drawTo(imgcanvasBackground);
}