-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.js
255 lines (230 loc) · 7.89 KB
/
application.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
window.onload = function() {
const secret = [
[1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,0,0,0,1,1,1,1,1],
[1,1,1,0,0,1,1,1,0,0,1,1,1],
[1,1,0,1,1,1,1,1,1,1,0,1,1],
[1,1,0,1,1,0,1,0,1,1,0,1,1],
[1,0,1,1,1,0,1,0,1,1,1,0,1],
[1,0,1,1,1,1,1,1,1,1,1,0,1],
[1,0,1,1,0,1,1,1,0,1,1,0,1],
[1,1,0,1,1,0,0,0,1,1,0,1,1],
[1,1,0,1,1,1,1,1,1,1,0,1,1],
[1,1,1,0,0,1,1,1,0,0,1,1,1],
[1,1,1,1,1,0,0,0,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1],
];
var random_mask = generateRandomMask();
var masked_secred = generateMaskedSecret(secret, random_mask);
const secret_image_ctx = document.getElementById('original_image').getContext('2d');
secret_image_ctx.scale(10, 10);
const random_mask_image_ctx = document.getElementById('random_mask').getContext('2d');
random_mask_image_ctx.scale(10, 10);
const masked_secret_image_ctx = document.getElementById('masked_secret').getContext('2d');
masked_secret_image_ctx.scale(10, 10);
drawImage(secret, secret_image_ctx);
drawImage(random_mask, random_mask_image_ctx);
drawImage(masked_secred, masked_secret_image_ctx);
const playgound_canvas = document.getElementById('playground');
const playground_ctx = playgound_canvas.getContext('2d');
var x1 = 100;
var y1 = 100;
var x2 = 0;
var y2 = 0;
drawPlayground(random_mask, masked_secred, x1, y1, x2, y2, playgound_canvas, playground_ctx);
playgound_canvas.addEventListener("mousemove", (event) => {
x2 = event.offsetX - 65;
y2 = event.offsetY - 65;
drawPlayground(random_mask, masked_secred, x1, y1, x2, y2, playgound_canvas, playground_ctx);
});
window.addEventListener('keydown', (event) => {
if (event.key == 'b') {
random_mask = secret;
masked_secred = generateSolidColorMask(0);
} else if (event.key == 'w') {
random_mask = secret;
masked_secred = generateSolidColorMask(1);
} else if (event.key == 'c') {
random_mask = secret;
masked_secred = generateCheckerboardMask();
} else {
random_mask = generateRandomMask();
masked_secred = generateMaskedSecret(secret, random_mask);
}
drawImage(random_mask, random_mask_image_ctx);
drawImage(masked_secred, masked_secret_image_ctx);
drawPlayground(random_mask, masked_secred, x1, y1, x2, y2, playgound_canvas, playground_ctx);
});
}
function getFillStyle(val) {
return val == 1 ? 'white' : 'black';
}
function drawPlayground(a, b, a_x, a_y, b_x, b_y, canvas, ctx) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
const ret = [];
var w1 = (b_x + 10000 - a_x)%10;
if (w1 == 0) {
w1 = 10;
}
var h1 = (b_y + 10000 - a_y)%10;
if (h1 == 0) {
h1 = 10;
}
var w2 = (a_x + 10000 - b_x)%10;
if (w2 == 0) {
w2 = 10;
}
var h2 = (a_y + 10000 - b_y)%10;
if (h2 == 0) {
h2 = 10;
}
ctx.strokeRect(a_x, a_y, 130, 130);
ctx.strokeRect(b_x, b_y, 130, 130);
for(var i = 0; i < 13; i++) {
for(var j = 0; j < 13; j++) {
const x1 = a_x + 10*i;
const y1 = a_y + 10*j;
const x2 = b_x + 10*i;
const y2 = b_y + 10*j;
const top_left = {
x: Math.floor((x1 - b_x)/10.0),
y: Math.floor((y1 - b_y)/10.0),
};
const top_right = {
x: top_left.x + 1,
y: top_left.y,
};
const bottom_left = {
x: top_left.x,
y: top_left.y + 1,
};
const bottom_right = {
x: top_left.x + 1,
y: top_left.y + 1,
};
if (inBounds(top_left)) {
ctx.fillStyle = getFillStyle(a[j][i] ^ b[top_left.y][top_left.x]);
} else {
ctx.fillStyle = getFillStyle(a[j][i]);
}
ctx.fillRect(x1, y1, w1, h1);
if (inBounds(top_right)) {
ctx.fillStyle = getFillStyle(a[j][i] ^ b[top_right.y][top_right.x]);
} else {
ctx.fillStyle = getFillStyle(a[j][i]);
}
ctx.fillRect(x1 + w1, y1, 10 - w1, h1);
if (inBounds(bottom_left)) {
ctx.fillStyle = getFillStyle(a[j][i] ^ b[bottom_left.y][bottom_left.x]);
} else {
ctx.fillStyle = getFillStyle(a[j][i]);
}
ctx.fillRect(x1, y1 + h1, w1, 10 - h1);
if (inBounds(bottom_right)) {
ctx.fillStyle = getFillStyle(a[j][i] ^ b[bottom_right.y][bottom_right.x]);
} else {
ctx.fillStyle = getFillStyle(a[j][i]);
}
ctx.fillRect(x1 + w1, y1 + h1, 10 - w1, 10 - h1);
const top_left_2 = {
x: Math.floor((x2 - a_x)/10.0),
y: Math.floor((y2 - a_y)/10.0),
};
const top_right_2 = {
x: top_left_2.x + 1,
y: top_left_2.y,
};
const bottom_left_2 = {
x: top_left_2.x,
y: top_left_2.y + 1,
};
const bottom_right_2 = {
x: top_left_2.x + 1,
y: top_left_2.y + 1,
};
if (inBounds(top_left_2)) {
// Already drawn
// ctx.fillStyle = getFillStyle(a[j][i] ^ b[top_left_2.y][top_left_2.x]);
} else {
ctx.fillStyle = getFillStyle(b[j][i]);
ctx.fillRect(x2, y2, w2, h2);
}
if (inBounds(top_right_2)) {
// Already drawn
// ctx.fillStyle = getFillStyle(a[j][i] ^ b[top_right_2.y][top_right_2.x]);
} else {
ctx.fillStyle = getFillStyle(b[j][i]);
ctx.fillRect(x2 + w2, y2, 10 - w2, h2);
}
if (inBounds(bottom_left_2)) {
// Already drawn
// ctx.fillStyle = getFillStyle(a[j][i] ^ b[bottom_left_2.y][bottom_left_2.x]);
} else {
ctx.fillStyle = getFillStyle(b[j][i]);
ctx.fillRect(x2, y2 + h2, w2, 10 - h2);
}
if (inBounds(bottom_right_2)) {
// Already drawn
// ctx.fillStyle = getFillStyle(a[j][i] ^ b[bottom_right_2.y][bottom_right_2.x]);
} else {
ctx.fillStyle = getFillStyle(b[j][i]);
ctx.fillRect(x2 + w2, y2 + h2, 10 - w2, 10 - h2);
}
}
}
}
function inBounds(pt) {
if (pt.x < 0 || pt.y < 0 || pt.x >= 13 || pt.y >= 13) {
return false;
}
return true;
}
function drawImage(data, ctx) {
for(var i = 0; i < 13; i++) {
for(var j = 0; j < 13; j++) {
ctx.fillStyle = getFillStyle(data[j][i]);
ctx.fillRect(i, j, 1, 1);
}
}
}
function generateRandomMask() {
const ret = [];
for(var i = 0; i < 13; i++) {
ret.push([]);
for(var j = 0; j < 13; j++) {
ret[i].push(Math.random() > 0.5 ? 1 : 0);
}
}
return ret;
}
function generateSolidColorMask(color) {
const ret = [];
for(var i = 0; i < 13; i++) {
ret.push([]);
for(var j = 0; j < 13; j++) {
ret[i].push(color);
}
}
return ret;
}
function generateCheckerboardMask() {
const ret = [];
for(var i = 0; i < 13; i++) {
ret.push([]);
for(var j = 0; j < 13; j++) {
const color = (Math.floor(i / 7) % 2) ^ (Math.floor(j / 7) % 2);
ret[i].push(color);
}
}
return ret;
}
function generateMaskedSecret(secret, random_mask) {
const ret = [];
for(var i = 0; i < 13; i++) {
ret.push([]);
for(var j = 0; j < 13; j++) {
ret[i].push(secret[i][j] ^ random_mask[i][j]);
}
}
return ret;
}