forked from maracatronics/ARena
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
302 lines (245 loc) · 7.79 KB
/
index.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>ARena : a projection mapping tool for line follower robots arena construction</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
body {
margin: 0px;
padding: 0px;
font-family: "Verdana", Verdana, sans-serif;
}
video {
position:absolute;
visibility:hidden;
}
button {
border:none;
display:block;
padding:1.5em 3em;
margin:1% auto 0;
color:#fff;
background-color:#9f361b;
}
button:active { background-color:#e44d26; }
</style>
<script src="perspective-transform.js"></script>
<script>
var state = 0;
var srcCorners = [0, 0, 0, 0, 0, 0, 0, 0];
var dstCorners = [0, 0, 0, 0, 0, 0, 0, 0];
var pointCorners = [0, 0, 0, 0, 0, 0, 0, 0];
var dragCorner = [false,false,false,false];
var index = 0;
var img;
greyscale=false;
window.addEventListener('DOMContentLoaded', function() {
var isStreaming = false,
video = document.getElementById('v'),
canva = document.getElementById('canvas'),
grey = document.getElementById('grey');
con = canva.getContext('2d');
w = 1280*1.2,
h = 720*1.2,
pointCorners[0]=w/8,
pointCorners[1]=h/8,
pointCorners[2]=w*7/8,
pointCorners[3]=h/8,
pointCorners[4]=w*7/8,
pointCorners[5]=h*7/8,
pointCorners[6]=w/8,
pointCorners[7]=h*7/8;
canva.setAttribute('width', w);
canva.setAttribute('height', h);
srcCorners[0] = 0;
srcCorners[1] = 0;
srcCorners[2] = 1280*1.2;
srcCorners[3] = 0;
srcCorners[4] = 1280*1.2;
srcCorners[5] = 720*1.2;
srcCorners[6] = 0;
srcCorners[7] = 720*1.2;
video.play();
function getMousePos(canva, evt) {
var rect = canva.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
canva.addEventListener('mousedown', function(evt) {
var mousePos = getMousePos(canva, evt);
var calcDist = 0;
for(i=0; i<4; i++){
var dx = mousePos.x-pointCorners[2*i];
var dy = mousePos.y-pointCorners[2*i+1];
calcDist = dx*dx + dy*dy;
dist = Math.sqrt(calcDist);
if(dist<20){
dragCorner[i] = true;
}
}
video.play();
}, false);
canva.addEventListener('mouseup', function(evt) {
for(i=0; i<4; i++){
dragCorner[i] = false;
}
video.play();
}, false);
canva.addEventListener('mousemove', function(evt) {
var mousePos = getMousePos(canvas, evt);
for(i=0; i<4; i++){
if (dragCorner[i]) {
pointCorners[2*i] = mousePos.x;
pointCorners[2*i+1] = mousePos.y;
}
}
video.play();
}, false);
// Wait for the video to start to play
video.addEventListener('play', function() {
// Every 33 milliseconds copy the video image to the canvas
setInterval(function() {
//if (v.paused || v.ended) return;
if(state == 0) {
con.fillStyle = '#000000';
con.fillRect(0, 0, w, h);
con.fillStyle = '#FF0000';
drawCircle(con,pointCorners[0], pointCorners[1],20);
dstCorners[0] = pointCorners[0];
dstCorners[1] = pointCorners[1];
con.fillStyle = '#00FF00';
drawCircle(con,pointCorners[2],pointCorners[3],20);
dstCorners[2] = pointCorners[2];
dstCorners[3] = pointCorners[3];
con.fillStyle = '#00FFFF';
drawCircle(con,pointCorners[4],pointCorners[5],20);
dstCorners[4] = pointCorners[4];
dstCorners[5] = pointCorners[5];
con.fillStyle = '#FFFF00';
drawCircle(con,pointCorners[6],pointCorners[7],20);
dstCorners[6] = pointCorners[6];
dstCorners[7] = pointCorners[7];
}else if(state===4){
var perspT1 = PerspT(dstCorners, srcCorners);
// var originalData = con.getImageData(0, 0, w, h);
// var oData = originalData.data;
// // var imageData = context2.getImageData(0, 0, 1280, 720);
// // var data = imageData.data;
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
canvas.width = w;
canvas.height = h;
context.drawImage(img, 0, 0, w, h);
var originalData = context.getImageData(0, 0, w, h);
var oData = originalData.data;
con.fillStyle = '#000000';
con.fillRect(0, 0, w, h);
var imageData = con.getImageData(0, 0, w, h);
var data = imageData.data;
var counter = 0;
for( var i = 0; i < h; i++) {
for(var j = 0; j < w; j++) {
var srcPt = [j, i];
var dstPt2 = perspT1.transform(srcPt[0], srcPt[1]);
dstPt2[0] = Math.round(dstPt2[0]);
dstPt2[1] = Math.round(dstPt2[1]);
if(dstPt2[0]>= 0 && dstPt2[0]<w && dstPt2[1]>=0 && dstPt2[1]<h){
data[(i*w + j)*4 + 0] = oData[(dstPt2[1]*w + dstPt2[0])*4 + 0];
data[(i*w + j)*4 + 1] = oData[(dstPt2[1]*w + dstPt2[0])*4 + 1];
data[(i*w + j)*4 + 2] = oData[(dstPt2[1]*w + dstPt2[0])*4 + 2];
data[(i*w + j)*4 + 3] = oData[(dstPt2[1]*w + dstPt2[0])*4 + 3];
counter++;
}
}
}
alert(counter + "!");
con.putImageData(imageData, 0, 0);
if (greyscale) goingGrey();
state = 5;
}
}, 33);
}, false);
var goingGrey = function() {
var imageData = con.getImageData(0, 0, w, h);
var data = imageData.data;
for (var i = 0; i < data.length; i += 4) {
var bright = 0.34 * data[i] + 0.5 * data[i + 1] + 0.16 * data[i + 2];
data[i] = bright;
data[i + 1] = bright;
data[i + 2] = bright;
}
con.putImageData(imageData, 0, 0);
}
// When the grey button is clicked, toggle the greyness indicator
grey.addEventListener('click', function() { greyscale = !greyscale; }, false);
});
window.addEventListener('keydown',this.check,false);
function check(e) {
var elem = document.getElementById("canvas");
if(e.keyCode == 80 || e.keyCode == 83) {
if (elem.requestFullscreen) {
elem.requestFullscreen();
video.play();
} else if (elem.mozRequestFullScreen) {
elem.mozRequestFullScreen();
video.play();
} else if (elem.webkitRequestFullscreen) {
elem.webkitRequestFullscreen();
video.play();
}
elem.style.width = window.innerWidth + "px";
elem.style.height = window.innerHeight + "px";
if(e.keyCode == 80 || e.keyCode == 83) state = 0;
} else if(e.keyCode == 81) { // q
index = 0;
state = 0;
} else if(e.keyCode == 82) { // r
state = 4;
} else {
elem.style.width = 1280*1.2 + "px";;
elem.style.height = 720*1.2 + "px";;
}
}
function drawCircle(c, x, y, r) {
c.beginPath();
c.arc(x, y, r, 0, 2 * Math.PI, false);
c.fill();
c.beginPath();
c.fillStyle = '#000000';
c.arc(x, y, r/4, 0, 2 * Math.PI, false);
c.fill();
}
function storeGuess(event) {
var rect = document.getElementById("canvas");
var x = event.offsetX - rect.offsetLeft;
var y = event.offsetY - rect.offsetTop;
srcCorners[index++] = x;
srcCorners[index++] = y;
}
function loadImg(event) {
var input = event.target;
var reader = new FileReader();
reader.onload = function() {
var dataURL = reader.result;
img = new Image();
img.src = dataURL;
img.onload=function(){
state = 4;
}
}
reader.readAsDataURL(input.files[0]);
}
</script>
</head>
<body>
<video id='v'></video>
<canvas id='canvas'></canvas>
<div id="coordinates"></div>
<br>Select image for projection:<br>
<input type='file' accept='image/*' onchange='loadImg(event)'><br>
<br><br>
</body>
</html>