-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
57 lines (51 loc) · 1.35 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
var fgImage = null;
var bgImage = null;
var fgCanvas;
var bgCanvas;
function loadForegroundImage() {
var file = document.getElementById("fgfile");
fgImage = new SimpleImage(file);
fgCanvas = document.getElementById("fgcan");
fgImage.drawTo(fgCanvas);
}
function loadBackgroundImage() {
var file = document.getElementById("bgfile");
bgImage = new SimpleImage(file);
bgCanvas = document.getElementById("bgcan");
bgImage.drawTo(bgCanvas);
}
function createComposite() {
var output = new SimpleImage(fgImage.getWidth(),fgImage.getHeight());
var greenThreshold = 240;
for (var pixel of fgImage.values()) {
var x = pixel.getX();
var y = pixel.getY();
if (pixel.getGreen() > greenThreshold) {
var bgPixel = bgImage.getPixel(x,y);
output.setPixel(x,y,bgPixel);
}
else {
output.setPixel(x,y,pixel);
}
}
return output;
}
function doGreenScreen() {
if (fgImage == null || ! fgImage.complete()) {
alert("Foreground image not loaded");
}
if (bgImage == null || ! bgImage.complete()) {
alert("Background image not loaded");
}
clearCanvas();
var finalImage = createComposite();
finalImage.drawTo(fgCanvas);
}
function clearCanvas() {
doClear(fgCanvas);
doClear(bgCanvas);
}
function doClear(canvas) {
var context = canvas.getContext("2d");
context.clearRect(0,0,canvas.width,canvas.height);
}