-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsTutorial.js
More file actions
211 lines (170 loc) · 5.25 KB
/
Copy pathjsTutorial.js
File metadata and controls
211 lines (170 loc) · 5.25 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
var image1;
var image2;
var og1;
var og2;
function upload1() {
var fileInput = document.getElementById('fginput');
var imgCanvas = document.getElementById("d1");
image1 = new SimpleImage(fileInput);
og1 = new SimpleImage(fileInput);
image1.drawTo(imgCanvas);
}
function upload2() {
var imgCanvas2 = document.getElementById('d2');
var fileInput = document.getElementById('bginput');
image2 = new SimpleImage(fileInput);
og2 = new SimpleImage(fileInput);
image2.drawTo(imgCanvas2);
}
function clearCanvas() {
var imgCanvas1 = document.getElementById("d1");
var ctx1 = imgCanvas1.getContext("2d");
ctx1.clearRect(0,0,imgCanvas1.width,imgCanvas1.height);
imgCanvas1.width = window.innerWidth/4;
imgCanvas1.height = window.innerHeight/2;
var imgCanvas2 = document.getElementById("d2");
var ctx2 = imgCanvas2.getContext("2d");
ctx2.clearRect(0,0,imgCanvas2.width,imgCanvas2.height);
imgCanvas2.width = (window.innerWidth)/4;
imgCanvas2.height = (window.innerHeight)/2;
}
function greenscreen() {
var imgCanvas = document.getElementById('d1');
var outputImg = new SimpleImage(image1.getWidth(),image1.getHeight());
for (var pixel of image1.values()){
var x = pixel.getX();
var y = pixel.getY();
if (pixel.getGreen() > pixel.getRed()+pixel.getBlue()){
var bgPixel = image2.getPixel(x,y);
outputImg.setPixel(x,y,bgPixel);
}
else {
outputImg.setPixel(x,y,pixel);
}
}
clearCanvas();
outputImg.drawTo(imgCanvas);
}
function gray1() {
for (var pixel of image1.values()){
var avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue())/3;
pixel.setRed(avg);
pixel.setGreen(avg);
pixel.setBlue(avg);
}
var imgCanvas = document.getElementById("d1");
image1.drawTo(imgCanvas);
}
function gray2() {
for (var pixel of image2.values()){
var avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue())/3;
pixel.setRed(avg);
pixel.setGreen(avg);
pixel.setBlue(avg);
}
var imgCanvas = document.getElementById("d2");
image2.drawTo(imgCanvas);
}
function red1() {
for (var pixel of image1.values()){
pixel.setRed(255);
}
var imgCanvas = document.getElementById("d1");
image1.drawTo(imgCanvas);
}
function red2() {
for (var pixel of image2.values()){
pixel.setRed(255);
}
var imgCanvas = document.getElementById("d2");
image2.drawTo(imgCanvas);
}
function blur1() {
function ensureInImage (coordinate, size) {
// coordinate cannot be negative
if (coordinate < 0) {
return 0;
}
// coordinate must be in range [0 .. size-1]
if (coordinate >= size) {
return size - 1;
}
return coordinate;
}
function getPixelNearby (image1, x, y, diameter) {
var dx = Math.random() * diameter - diameter / 2;
var dy = Math.random() * diameter - diameter / 2;
var nx = ensureInImage(x + dx, image1.getWidth());
var ny = ensureInImage(y + dy, image1.getHeight());
return image1.getPixel(nx, ny);
}
for (var pixel of image1.values()) {
var x = pixel.getX();
var y = pixel.getY();
if (Math.random() > 0.5) {
var other = getPixelNearby(image1, x, y, 10);
image1.setPixel(x, y, other);
}
else {
image1.setPixel(x, y, pixel);
}
}
var imgCanvas = document.getElementById("d1");
image1.drawTo(imgCanvas);
}
function blur2() {
function ensureInImage (coordinate, size) {
// coordinate cannot be negative
if (coordinate < 0) {
return 0;
}
// coordinate must be in range [0 .. size-1]
if (coordinate >= size) {
return size - 1;
}
return coordinate;
}
function getPixelNearby (image2, x, y, diameter) {
var dx = Math.random() * diameter - diameter / 2;
var dy = Math.random() * diameter - diameter / 2;
var nx = ensureInImage(x + dx, image2.getWidth());
var ny = ensureInImage(y + dy, image2.getHeight());
return image2.getPixel(nx, ny);
}
for (var pixel of image2.values()) {
var x = pixel.getX();
var y = pixel.getY();
if (Math.random() > 0.5) {
var other = getPixelNearby(image2, x, y, 10);
image2.setPixel(x, y, other);
}
else {
image2.setPixel(x, y, pixel);
}
}
var imgCanvas = document.getElementById("d2");
image2.drawTo(imgCanvas);
}
function reset() {
var imgCanvas1 = document.getElementById("d1");
og1.drawTo(imgCanvas1);
var imgCanvas2 = document.getElementById("d2");
og2.drawTo(imgCanvas2);
}
function rainbow1() {
var imgCanvas = document.getElementById('d1');
for (var pixel of image1.values()){
var x = pixel.getX();
var y = pixel.getY();
if (x <= (image1.getHeight()/3)){
pixel.setRed(255);
}
else if (x <= (image1.getHeight()/3)*2){
pixel.setGreen(255);
}
else{
pixel.setBlue(255);
}
}
image1.drawTo(imgCanvas);
}