-
Notifications
You must be signed in to change notification settings - Fork 21
/
index_mobile.html
433 lines (361 loc) · 12.7 KB
/
index_mobile.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>whiteboard for mobile device</title>
<script src="https://code.jquery.com/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript">
// Variables for referencing the canvas and 2dcanvas context
jQuery(function(){
var canvas, canvastmp, ctx, ctxtmp;
// Variables to keep track of the mouse position and left-button status
var mouseX,mouseY,mouseDown=0;
// Variables to keep track of the touch position
var touchX,touchY;
// Keep track of the old/last position when drawing a line
// We set it to -1 at the start to indicate that we don't have a good value for it yet
var lastX,lastY=-1;
var url = window.location.hostname;
var socket;
var stanza = '';
var username = '';
if (location.href.indexOf('#')!= -1){
stanza = location.href.substring(location.href.indexOf('#')+1);
}
if(!username)
{
username = prompt("Hey there, insert your nick name, please", "");
}
username = username.substr(0,20);
// A flag for drawing activity
var clients = {};
var cursors = {};
// funzione richiesta di nick name
socket = io.connect(url);
if (socket) {
var id = Math.round(jQuery.now()*Math.random());
if (username =='') {username = id }
//url.substring(url.indexOf('#')+1);
if (stanza.length > 2) {
socket.emit('setuproom',{
'room': stanza,
'id': id,
'usernamerem' : username
});
} else {
socket.emit('setuproom',{
'room': 'public',
'id': id,
'usernamerem' : username
});
}
}
socket.on('setuproomserKO', function (data) {
stanza = data.room;
//document.getElementById('audiocall').disabled = false;
// document.getElementById('videocall').disabled = false;
console.log(data.inforoom);
});
socket.on('setuproomser', function (data) {
stanza = data.room;
console.log('registrato nella stanza ' + stanza);
//jQuery('<div class="testochatser"><span>FROM SERVER:</span> '+ data.inforoom + data.listautenti +'</div>').appendTo('#testichat');
//document.getElementById('frecce').style.backgroundColor ='#ffff00';
});
socket.on('camperaltriser', function (data) {
var camdaclient = new Image();
camdaclient.src = data.camperaltridati;
camdaclient.onload = function() {
// imgdaclient.src = data.fileperaltri;
ctx.drawImage(camdaclient,positionxcam,positionycam,320,240);
}
});
// Draws a line between the specified position on the supplied canvas name
// Parameters are: A canvas context, the x position, the y position, the size of the dot
function drawLine(ctx,x,y,size) {
// If lastX is not set, set lastX and lastY to the current position
if (lastX==-1) {
lastX=x;
lastY=y;
}
// Let's use black by setting RGB values to 0, and 255 alpha (completely opaque)
r=0; g=255; b=0; a=255;
// Select a fill style
ctx.strokeStyle = "rgba("+r+","+g+","+b+","+(a/255)+")";
// Set the line "cap" style to round, so lines at different angles can join into each other
ctx.lineCap = "round";
//ctx.lineJoin = "round";
// Draw a filled line
ctx.beginPath();
// First, move to the old (previous) position
ctx.moveTo(lastX,lastY);
// Now draw a line to the current touch/pointer position
ctx.lineTo(x,y);
// Set the line thickness and draw the line
ctx.lineWidth = size;
ctx.stroke();
ctx.closePath();
// Update the last position to reference the current position
lastX=x;
lastY=y;
}
function changetime (ilvalore) {
var temposel = ilvalore;
clearInterval(idtempo);
if (temposel!=="") {
idtempo = setInterval(function() {
takepicture();
},temposel);
}}
var idtempo;
var positionxcam = 3;
var positionycam = 30;
function takepicture(e) {
// ctx.drawImage(video, positionx, positiony,320,240);
ctx.drawImage(video,positionxcam,positionycam,320,240);
//var imgdata = ctx.getImageData(positionxcam,positionycam, 320, 240);
ctxtmp.drawImage(video,0,0,320,240);
var datacam = canvastmp.toDataURL('image/png');
// paper1 e ctx1 servono per prelevare solo i dati della webcam e inviarli al server per gli altri
socket.emit('camperaltri',{
'id': id,
'positionx': positionxcam,
'positiony': positionycam,
'camperaltridati': datacam,
'room' : stanza
});
}
// Clear the canvas context using the canvas width and height
function clearCanvas(canvas,ctx) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
// Keep track of the mouse button being pressed and draw a dot at current location
function sketchpad_mouseDown() {
mouseDown=1;
drawLine(ctx,mouseX,mouseY,12);
}
// Keep track of the mouse button being released
function sketchpad_mouseUp() {
mouseDown=0;
// Reset lastX and lastY to -1 to indicate that they are now invalid, since we have lifted the "pen"
lastX=-1;
lastY=-1;
}
// Keep track of the mouse position and draw a dot if mouse button is currently pressed
function sketchpad_mouseMove(e) {
// Update the mouse co-ordinates when moved
getMousePos(e);
// Draw a dot if the mouse button is currently being pressed
if (mouseDown==1) {
drawLine(ctx,mouseX,mouseY,12);
}
}
// Get the current mouse position relative to the top-left of the canvas
function getMousePos(e) {
if (!e)
var e = event;
if (e.offsetX) {
mouseX = e.offsetX;
mouseY = e.offsetY;
}
else if (e.layerX) {
mouseX = e.layerX;
mouseY = e.layerY;
}
}
// Draw something when a touch start is detected
function sketchpad_touchStart() {
// Update the touch co-ordinates
getTouchPos();
drawLine(ctx,touchX,touchY,12);
// Prevents an additional mousedown event being triggered
event.preventDefault();
}
function sketchpad_touchEnd() {
// Reset lastX and lastY to -1 to indicate that they are now invalid, since we have lifted the "pen"
lastX=-1;
lastY=-1;
}
// Draw something and prevent the default scrolling when touch movement is detected
function sketchpad_touchMove(e) {
// Update the touch co-ordinates
getTouchPos(e);
// During a touchmove event, unlike a mousemove event, we don't need to check if the touch is engaged, since there will always be contact with the screen by definition.
drawLine(ctx,touchX,touchY,12);
// Prevent a scrolling action as a result of this touchmove triggering.
event.preventDefault();
}
// Get the touch position relative to the top-left of the canvas
// When we get the raw values of pageX and pageY below, they take into account the scrolling on the page
// but not the position relative to our target div. We'll adjust them using "target.offsetLeft" and
// "target.offsetTop" to get the correct values in relation to the top left of the canvas.
function getTouchPos(e) {
if (!e)
var e = event;
if(e.touches) {
if (e.touches.length == 1) { // Only deal with one finger
var touch = e.touches[0]; // Get the information for finger #1
touchX=touch.pageX-touch.target.offsetLeft;
touchY=touch.pageY-touch.target.offsetTop;
}
}
}
// Set-up the canvas and add our event handlers after the page has loaded
function init() {
(function() {
var streaming = false,
video = document.getElementById('video'),
// paper1 = document.getElementById('paper1'),
startbutton = document.getElementById('catturacam'),
width = 320,
height = 240;
navigator.getMedia = ( navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
navigator.getMedia(
{
video: true,
audio: false
},
function(stream) {
if (navigator.mozGetUserMedia) {
video.mozSrcObject = stream;
} else {
var vendorURL = window.URL || window.webkitURL;
video.src = vendorURL ? vendorURL.createObjectURL(stream) : stream;
}
video.play();
},
function(err) {
console.log("An error occured! " + err);
});
video.addEventListener('canplay', function(ev){
if (!streaming) {
height = video.videoHeight / (video.videoWidth/width);
video.setAttribute('width', 320);
video.setAttribute('height', 240);
// video.setAttribute('maxFrameRate',5);
// canvas.setAttribute('width', width);
// canvas.setAttribute('height', height);
streaming = true;
}
}, false);
startbutton.addEventListener('click', function(ev){
takepicture();
ev.preventDefault();
}, false);
/*
document.getElementById('autocamabi').addEventListener('change', function(ev){
if (document.getElementById('autocamabi').checked) {
document.getElementById('tempocam').disabled = true;
idtempo = setInterval(function() {
takepicture();
},document.getElementById('tempocam').value);
}else{
clearInterval(idtempo);
document.getElementById('tempocam').disabled = false;
}
}, false);
*/
})
(); /// end function init() *************************************************
canvas = document.getElementById('sketchpad');
canvastmp = document.getElementById('canvastmp');
canvas.width = window.innerWidth;
canvas.width = window.innerHeight;
// If the browser supports the canvas tag, get the 2d drawing context for this canvas
if (canvas.getContext)
ctx = canvas.getContext('2d');
if (canvastmp.getContext)
ctxtmp = canvastmp.getContext('2d');
// Check that we have a valid context to draw on/with before adding event handlers
if (ctx) {
// React to mouse events on the canvas, and mouseup on the entire document
canvas.addEventListener('mousedown', sketchpad_mouseDown, false);
canvas.addEventListener('mousemove', sketchpad_mouseMove, false);
window.addEventListener('mouseup', sketchpad_mouseUp, false);
// React to touch events on the canvas
canvas.addEventListener('touchstart', sketchpad_touchStart, false);
canvas.addEventListener('touchend', sketchpad_touchEnd, false);
canvas.addEventListener('touchmove', sketchpad_touchMove, false);
window.addEventListener('resize', resizecanvas, false);
window.addEventListener('orientationchange', resizecanvas, false);
resizecanvas();
}
}
function resizecanvas (){
var imgdata = ctx.getImageData(0,0, canvas.width, canvas.height);
canvas.width =window.innerWidth -2;
canvas.height = window.innerHeight -46;
ctx.putImageData(imgdata,0,0);
}
jQuery('#timecam').change(function() {changetime(this.value)});
jQuery('#clearbutton').click(function() {clearCanvas(canvas,ctx)});
init();
});
</script>
<link rel="stylesheet" href="assets/css/bootstrap.min.css" />
<style>
/* Some CSS styling */
#sketchpadapp {
/* Prevent nearby text being highlighted when accidentally dragging mouse outside confines of the canvas */
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
#sketchpad {
border:1px red solid;
position:absolute; /* Necessary for correct mouse co-ords in Firefox */
overflow:hidden;
bottom:0;
right:0;
}
#video {
display:none;
position:absolute;
top:0px;
left:20px;
width:320px;
height:240px;
}
select.form-control {
display:inline;
width:auto;
}
</style>
</head>
<body>
<div class="container">
<video id="video" style="width:320px;height:240px;" autoplay></video>
<canvas id="canvastmp" style="display:none;" width="320" height="240"></canvas>
<canvas id="sketchpad"></canvas>
<div class="row">
<div class="col-md-12 col-lg-12" id="bottoni">
<div id="sketchpadapp">
<button id="catturacam" class="btn btn-primary">Get photo</button>
<input class="btn btn-primary" type="button" value="Clear board" id="clearbutton">
<select id="timecam" name="timecam" class= "form-control">
<option value="" selected="selected">Choice time</option>
<option value="1000">1</option>
<option value="2000">2</option>
<option value="4000">4</option>
<option value="8000">8</option>
<option value="20000">20</option>
<option value="60000">60</option>
<option value="120000">120</option>
<option value="300000">300</option>
<option value="600000">600</option>
</select>
</div>
</div>
</div>
</div>
</body>
</html>