-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.js
More file actions
413 lines (360 loc) · 11.9 KB
/
client.js
File metadata and controls
413 lines (360 loc) · 11.9 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
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
/*
* pubsub-scribble client
*
* @license MIT License
* @author Thomas Heidrich, Adrian Kummerländer
* @copyright Copyright (c) 2012 Thomas Heidrich and other authors
*/
/**
* Handles communication with NodeJS hub.
* @param String url The URL of the hub to communicate with.
*/
var PubSubCommunication = function(url){
if(typeof(io) != 'object'){
throw "no socket.io";
}
/**
* connection establishment during construction
*/
var socket = io.connect(
url
,{
'connect timeout': 5000,
'reconnect': true,
'reconnection delay': 500,
'max reconnection attempts': 8,
// 'try multiple transports': true,
'transports': [
// 'xhr-polling',
'websocket',
// 'jsonp-polling',
]
}
);
/**
* Emit data to hub.
* @param Map dto The data transfer object to emit.
*/
this.emitMove = function(dto){
socket.emit('move', dto);
};
/**
* Adds an event handler to the communication system.
* @param string event The event the handler shall wait for.
* @param function handler The function which shall be executed in case
* the event gets fired.
*/
this.addEventHandler = function(event, handler){
socket.on(event, handler);
};
};
/**
* Handles exports of canvas content.
* @param canvasID The ID of the canvas element to export.
*/
var CanvasExport = function(canvasID){
var canvas = document.getElementById(canvasID);
var exportNumber = 1;
/**
* opens a new window and imports the the content of the
* canvas as a png image
*/
this.doPNGExport = function(){
var exportwindow = window.open('', 'export' + exportNumber);
exportwindow.document.writeln(
'<html><head>'
+'<title>PNG Export '
+exportNumber
+' - Scribble - gnuheidix.de</title>'
+'</head><body>'
+'<img '
+'src="'+canvas.toDataURL('image/png')+'"'
+'alt="Kritzelexport"'
+'/>'
+'</body></html>'
);
exportwindow.document.close();
++exportNumber;
};
};
/**
* This class takes care of browser dependent pointer position calculation and
* builds the DataTransferObject which will be sent to the hub for publishing.
*/
var InputController = function(colorPickerID, toolPickerID){
var color = document.getElementById(colorPickerID);
var tool = document.getElementById(toolPickerID);
var oldMouseX = 0;
var oldMouseY = 0;
/**
* Calculates the current pointer position browser dependently.
*/
var getMousePos = function(evt){
if (evt.offsetX){
// Webkit (!iOS)
return {
x: evt.offsetX
, y: evt.offsetY
};
}
else if (evt.layerX){
// Firefox
return {
x: evt.layerX
, y: evt.layerY
};
}
else {
// iOS
return {
x: evt.pageX - evt.target.offsetLeft
, y: evt.pageY - evt.target.offsetTop
};
}
};
/**
* Returns a DTO which represents the current input state. This DTO might
* be published through the hub.
*/
this.getDTO = function(evt){
var mousePos = getMousePos(evt);
return {
x: mousePos.x
, y: mousePos.y
, c: color.value
, oldX: oldMouseX
, oldY: oldMouseY
, t: tool.value
};
};
/**
* Updates the previous mouse position in order to enable the controller
* to make the DTO hold an information where the pointer was last time.
*/
this.updateOldPos = function(evt){
var mousePos = getMousePos(evt);
oldMouseX = mousePos.x;
oldMouseY = mousePos.y;
};
};
/**
* This class is a prototype for canvas elements in the UI.
*/
var Canvas = function(canvasID){
var canvas = document.getElementById(canvasID);
/**
* Sets the size of the canvas.
*/
this.initSize = function(width, height){
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
};
/**
* Drops the content of the canvas.
*/
this.clear = function(){
if(canvas.getContext){
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
}
};
this.addEventHandler = function(event, handler){
canvas.addEventListener(event, handler);
};
this.removeEventHandler = function(event, handler){
canvas.removeEventListener(event, handler);
};
};
/**
* This class represents a helper layer for displaying misc stuff over
* the actual drawing canvas.
*/
var OverlayCanvas = function(canvasID){
var canvas = document.getElementById(canvasID);
this.drawCrosshair = function(mouseX, mouseY){
if(canvas.getContext){
var ctx = canvas.getContext('2d');
ctx.beginPath();
this.clear();
ctx.lineWidth = 2;
ctx.strokeStyle = 'silver';
ctx.moveTo(mouseX, 0);
ctx.lineTo(mouseX, canvas.height);
ctx.moveTo(0, mouseY);
ctx.lineTo(canvas.width, mouseY);
ctx.stroke();
ctx.closePath();
}
};
};
/**
* This class represents the canvas used for drawing.
*/
var DrawCanvas = function(canvasID){
var canvas = document.getElementById(canvasID);
/**
* Converts a hex color into its rgba representation.
* @param string color The color to convert as string. e.g. 11aa33
* @param double alpha The transparency value the rgba sould have.
* @return string The rgba representation. e.g. rgba(1,23,22,0.1)
*/
var hexToRGBA = function(color, alpha){
r = parseInt(color.substring(0,2), 16);
g = parseInt(color.substring(2,4), 16);
b = parseInt(color.substring(4,6), 16);
return ('rgba(' + r + ', ' + g + ', ' + b + ', ' + alpha + ')');
};
this.drawLine = function(pos, local){
if(canvas.getContext){
var ctx = canvas.getContext('2d');
ctx.beginPath();
if(local){
ctx.strokeStyle = hexToRGBA(pos.c, 0.1);
}else{
ctx.strokeStyle = '#' + pos.c;
}
switch(pos.t){
case '0':
ctx.lineWidth = 1;
break;
default:
case '1':
ctx.lineWidth = 6;
break;
case '2':
ctx.lineWidth = 12;
break;
}
ctx.lineCap = 'round';
ctx.moveTo(pos.oldX, pos.oldY);
ctx.lineTo(pos.x, pos.y);
ctx.stroke();
ctx.closePath();
}
};
};
/**
* This function wires up the HTML UI with all necessary event handlers.
*/
var pubSubUI = function(canvasID){
/**
* The canvas we are going to scribble on it.
*/
DrawCanvas.prototype = new Canvas(canvasID);
var drawCanvas = new DrawCanvas(canvasID);
/**
* Setup export button
*/
var exportController = new CanvasExport(canvasID);
var exportButton = document.getElementById('pngexport');
exportButton.addEventListener('click', exportController.doPNGExport);
/**
* Setup communication
*/
var commController;
try{
commController = new PubSubCommunication('http://scribble.gnuheidix.de:8080');
}catch(e){
alert('Der Scribble-Server ist zur Zeit nicht erreichbar.');
return;
}
var connStatus = document.getElementById('status');
commController.addEventHandler('connect', function(){
connStatus.innerHTML = '<span style="color:#00FF00;">verbunden</span>';
});
commController.addEventHandler('disconnect', function(){
connStatus.innerHTML = '<span style="color:#FF0000;">getrennt</span>';
});
commController.addEventHandler('reconnecting', function(){
connStatus.innerHTML = '<span style="color:#FFFF00;">verbinde...</span>';
});
commController.addEventHandler('reconnect_failed', function(){
connStatus.innerHTML = '<span style="color:#FF0000;">wiederverbinden fehlgeschlagen</span>';
});
commController.addEventHandler('moved', function(pos){
drawCanvas.drawLine(pos, false);
});
/**
* Setup overlay and crosshair checkbox
*/
OverlayCanvas.prototype = new Canvas('helperlayer');
var overlay = new OverlayCanvas('helperlayer');
var crosshair = document.getElementById('fadenkreuz');
crosshair.addEventListener('click', overlay.clear);
/**
* Setup input controller
*/
var inputController = new InputController('farbe', 'werkzeug');
// interactive UI elements
var picker = document.getElementById('picker');
// convenience functions
var initUI = function(headerID, footerID){
var header = document.getElementById(headerID);
var footer = document.getElementById(footerID);
var windowW = window.innerWidth;
var windowH = window.innerHeight;
var headerH = header.offsetHeight;
var footerH = footer.offsetHeight;
var calcWidth = windowW - 10;
var calcHeight = windowH - footerH - headerH - 50;
overlay.initSize(calcWidth, calcHeight);
drawCanvas.initSize(calcWidth, calcHeight);
};
// UI event handler
var handleMouseMove = function(evt){
var curPos = inputController.getDTO(evt);
if(crosshair.checked){
overlay.drawCrosshair(curPos.x, curPos.y);
}
drawCanvas.drawLine(curPos, true);
commController.emitMove(curPos);
inputController.updateOldPos(evt);
};
var handleTouchMove = function(evt){
evt.preventDefault();
if(evt.changedTouches.length == 1){ // disable multitouch
handleMouseMove(evt.changedTouches[0]);
}
};
var handleMouseDown = function(evt){
// prevent webkit from showing the cursor I
evt.preventDefault();
// make color picker disappear
picker.blur();
overlay.addEventHandler('mousemove', handleMouseMove);
inputController.updateOldPos(evt);
var curPos = inputController.getDTO(evt);
drawCanvas.drawLine(curPos, true);
commController.emitMove(curPos);
};
var handleTouchStart = function(evt){
// make color picker disappear
picker.blur();
if(evt.changedTouches.length == 1){ // disable multitouch
overlay.addEventHandler('touchmove', handleTouchMove);
inputController.updateOldPos(evt.changedTouches[0]);
var curPos = inputController.getDTO(evt.changedTouches[0]);
drawCanvas.drawLine(curPos, true);
commController.emitMove(curPos);
}
};
var handleMouseUp = function(){
overlay.removeEventHandler('mousemove', handleMouseMove);
};
var handleTouchEnd = function(){
overlay.removeEventHandler('touchmove', handleTouchMove);
};
// setup of UI events
if(window.Touch){
overlay.addEventHandler('touchstart', handleTouchStart);
overlay.addEventHandler('touchend', handleTouchEnd);
}
overlay.addEventHandler('mousedown', handleMouseDown);
overlay.addEventHandler('mouseup', handleMouseUp);
window.onresize = function(){
initUI('oben', 'unten');
};
// trigger initialization
window.onresize();
};
// fire up UI
pubSubUI('zeichenbrett');