-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
71 lines (63 loc) · 1.95 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var imgBg, imgFg, canvasBg, canvasFg, ctxFg, ctxBg;
function uploadBg(){
canvasBg = document.getElementById("canBg");
canvasBg.width = 500;
canvasBg.height = 300;
var fileInput = document.getElementById("bginput");
//var filename = fileInput.value;
imgBg = new SimpleImage(fileInput);
imgBg.drawTo(canvasBg);
}
function uploadFg(){
canvasFg = document.getElementById("canFg");
canvasFg.width = 500;
canvasFg.height = 300;
var fileInput2 = document.getElementById("fginput");
imgFg = new SimpleImage(fileInput2);
imgFg.drawTo(canvasFg);
}
function clearCanvas(){
ctxFg = canvasFg.getContext("2d");
ctxBg = canvasBg.getContext("2d");
ctxFg.clearRect(0,0,canvasFg.width,canvasFg.height);
ctxBg.clearRect(0,0,canvasBg.width,canvasBg.height);
}
function clearAll(){
clearCanvas();
var fileInput = document.getElementById("bginput");
var fileInput2 = document.getElementById("fginput");
fileInput.value = null;
fileInput2.value = null;
}
function makeComposite(){
if(imgFg == null || !imgFg.complete()){
alert("Foreground Not Loaded.");
return;
}
if(imgBg == null || !imgBg.complete()){
alert("Background Not Loaded.");
return;
}
clearCanvas();
var slider = document.getElementById("slider");
var greenThreshold = slider.value;
var imgOutput = new SimpleImage(imgFg.getWidth(),imgFg.getHeight());
for(var pixel of imgFg.values()){
var x = pixel.getX();
var y = pixel.getY();
if(pixel.getGreen() > greenThreshold){
var bgPixel = imgBg.getPixel(x, y);
imgOutput.setPixel(x, y, bgPixel);
}else{
imgOutput.setPixel(x, y, pixel);
}
}
imgOutput.drawTo(canvasFg);
}
function downloadImg(){
var img = canvasFg.toDataURL("image/png",1.0).replace("image/png","image/octect-stream");
var link = document.createElement("a");
link.download = "composite.png";
link.href = img;
link.click();
}