-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreenscreen.html
151 lines (124 loc) · 3.99 KB
/
greenscreen.html
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
147
148
149
150
151
<!DOCTYPE html>
<html>
<head>
<title>Hello Greenscreen!</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="UTF-8">
<meta name="description" content="Your Fiendly Picture Editor">
<meta name="keywords" content="Picture Editor">
<meta name="author" content="Pakbung Pimnara">
<style>
* {
font-family: monospace;
color: Black;
}
body{
font-size: 15px;
}
#fg, #bg {
border: 1px solid black;
width: 500px;
height: auto;
overflow: hidden;
}
</style>
</head>
<body>
<h1>Your personal Greenscreen editor</h1>
<p>Be careful on the size of foreground and background pictures, they should be *the same size*</p>
<canvas id="fg"></canvas>
<canvas id="bg"></canvas>
<br><br>
<p>
Select a foreground with Greenscreen:
<input type="file" id="fgfile" multiple="false" accept="image/*"> <input type="button" value="Upload foreground" onclick="drawFg()">
</p>
<p>
Select a background:
<input type="file" id="bgfile" multiple="false" accept="image/*"> <input type="button" value="Upload background" onclick="drawBg()">
</p>
<p>
<input type="button" value="Edit your picture" onclick="editPic()">
</p>
<script>
// reader
var reader
var img
// canvas
var canFg;
var ctxFg;
var canBg;
var ctxBg;
// image
var fgImg;
var bgImg;
function drawFg() {
fgImg = document.getElementById("fgfile").files[0];
if (!fgImg) {
return; // Handle case where no file is selected
}
reader = new FileReader();
reader.onload = function(event) {
img = new Image();
img.onload = function() {
canFg = document.getElementById("fg");
canFg.width = img.naturalWidth;
canFg.height = img.naturalHeight;
ctxFg = canFg.getContext("2d");
ctxFg.drawImage(img, 0, 0);
};
img.src = event.target.result;
};
reader.readAsDataURL(fgImg);
}
function drawBg() {
bgImg = document.getElementById("bgfile").files[0];
if (!bgImg) {
return; // Handle case where no file is selected
}
reader = new FileReader();
reader.onload = function(event) {
img = new Image();
img.onload = function() {
canBg = document.getElementById("bg");
canBg.width = img.naturalWidth;
canBg.height = img.naturalHeight;
ctxBg = canBg.getContext("2d");
ctxBg.drawImage(img, 0, 0);
};
img.src = event.target.result;
};
reader.readAsDataURL(bgImg);
}
function doEdit(){
var res= checkUpload();
if (res == true){
editPic();
}
}
function checkUpload() {
if ((fgImg.complete() || fgImg != null) || (bgImg.complete() || bgImg != null)){
return true;
}
return false;
}
function editPic() {
var imgDataFg = ctxFg.getImageData(0, 0, canFg.width, canFg.height);
var imgDataBg = ctxBg.getImageData(0, 0, canBg.width, canBg.height);
// invert colors
for (let i = 0; i < imgDataFg.data.length; i += 4) {
var red = imgDataFg.data[i];
var green = imgDataFg.data[i+1];
var blue = imgDataFg.data[i+2];
if (green > red + blue){
imgDataFg.data[i] = imgDataBg.data[i];
imgDataFg.data[i+1] = imgDataBg.data[i+1];
imgDataFg.data[i+2] = imgDataBg.data[i+2];
}
}
ctxFg.putImageData(imgDataFg, 0, 0);
ctxBg.clearRect(0, 0, canBg.width, canBg.height); // and clear canBg
};
</script>
</body>
</html>