-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
310 lines (254 loc) · 6.84 KB
/
client.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
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
// on load of page
var drawing = false;
var users = {};
var cursors = {};
var socket = io.connect();
var id;
var myname;
var tool = "pencil";
var currentColor;
var width = 1;
var brush = new Image();
brush.src = "brush2.png";
var touchSupported = Modernizr.touch;
var mouseDownEvent,
mouseMoveEvent,
mouseUpEvent;
if (touchSupported) {
mouseDownEvent = "touchstart";
mouseMoveEvent = "touchmove";
mouseUpEvent = "touchend";
} else {
mouseDownEvent = "mousedown";
mouseMoveEvent = "mousemove";
mouseUpEvent = "mouseup";
}
function ID () {
var S4 = function () {
return Math.floor(
Math.random() * 0x10000 /* 65536 */
).toString(16);
};
return ( S4() + S4() + S4() );
}
var Trig = {
distanceBetween2Points: function ( point1, point2 ) {
var dx = point2.x - point1.x;
var dy = point2.y - point1.y;
return Math.sqrt( Math.pow( dx, 2 ) + Math.pow( dy, 2 ) );
},
angleBetween2Points: function ( point1, point2 ) {
var dx = point2.x - point1.x;
var dy = point2.y - point1.y;
return Math.atan2( dx, dy );
}
}
socket.on('connect', function() {
id = ID();
myname = prompt("What's your handle?");
socket.emit('adduser', myname, id);
});
// listener, whenever the server emits 'updatechat', this updates the chat body
socket.on('updatechat', function (username, data, timestamp) {
if(timestamp === undefined){
timestamp = "";
}else{
timestamp = timestamp + " ";
}
$('#conversation').append(timestamp + '<b>'+username + ':</b> ' + data + '<br>');
$('#conversation').scrollTop(10000);
});
CanvasRenderingContext2D.prototype.clear =
CanvasRenderingContext2D.prototype.clear || function (preserveTransform) {
if (preserveTransform) {
this.save();
this.setTransform(1, 0, 0, 1, 0, 0);
}
this.clearRect(0, 0, this.canvas.width, this.canvas.height);
if (preserveTransform) {
this.restore();
}
};
$(function() {
var canvas = $('#paper');
var context = canvas[0].getContext('2d');
if(!('getContext' in document.createElement('canvas'))) {
alert("Your browser does not support canvas");
return false;
}
//canvas.bind( mouseDownEvent, onCanvasMouseDown() );
$('#data').focus();
// when the client clicks SEND
$('#datasend').click( function() {
var message = $('#data').val();
$('#data').val('');
// tell server to execute 'sendchat' and send along one parameter
socket.emit('sendchat', message);
$('#data').focus();
});
$('#pencil').click( function() {
tool = "pencil";
});
$('#crayon').click( function() {
tool = "crayon";
});
$('#brush').click( function() {
tool = "brush";
});
$('#clear').click( function() {
//context.clear();
socket.emit('clear');
});
$('#eraser').click( function() {
tool = "eraser";
});
$('#save').click( function() {
window.open(save(), '_blank', 'width=800,height=600')
});
$('#width').bind('keyup', function() {
width = $(this).val() // get the current value of the input field.
});
// when the client hits ENTER on their keyboard
$('#data').keypress(function(e) {
if(e.which == 13) {
$(this).blur();
$('#datasend').focus().click();
$('#data').focus();
}
});
var prev = {};
socket.on('moving', function (data) {
if(data.id !== undefined){
if(!cursors[data.id]){
cursors[data.id] = $('<div class="cursor">' + data.username + '</div>').appendTo('#cursors');
}
cursors[data.id].css({
'left': data.x,
'top': data.y,
'background-color': data.color
});
if(data.drawing && users[data.id]) {
drawLine(users[data.id].x, users[data.id].y, data.x, data.y, data.color, data.tool, data.width);
}
users[data.id] = data;
users[data.id].updated = $.now();
}
});
socket.on('clearcanvas', function() {
context.clear();
});
var lastEmit = $.now();
canvas.on(mouseDownEvent, function(e) {
e.preventDefault();
drawing = true;
var target;
if (touchSupported) {
target = e.originalEvent.touches[0]
}
else {
target = e;
}
prev.x = target.pageX;
prev.y = target.pageY;
});
$(document).bind(mouseUpEvent + ' mouseleave', function(){
drawing = false;
});
$(document).on(mouseMoveEvent, function(e){
var target;
if (touchSupported) {
target = e.originalEvent.touches[0]
}
else {
target = e;
}
if($.now() - lastEmit > 30){
socket.emit('mousemove', {
'x': target.pageX,
'y': target.pageY,
'drawing': drawing,
'id': id,
'username': myname,
'color': $('#color-selector').val(),
'tool': tool,
'width': width
});
lastEmit = $.now();
}
if(drawing){
//drawLine(prev.x, prev.y, e.pageX, e.pageY);
prev.x = target.pageX;
prev.y = target.pageY;
}
});
/*
onCanvasMouseDown = function () {
return function(event) {
mouseMoveHandler = onCanvasMouseMove()
mouseUpHandler = onCanvasMouseUp()
$(document).bind( mouseMoveEvent, mouseMoveHandler );
$(document).bind( mouseUpEvent, mouseUpHandler );
updateMousePosition( event );
renderFunction( event );
}
}
onCanvasMouseMove = function () {
return function(event) {
renderFunction( event );
event.preventDefault();
return false;
}
}
onCanvasMouseUp = function (event) {
return function(event) {
$(document).unbind( mouseMoveEvent, mouseMoveHandler );
$(document).unbind( mouseUpEvent, mouseUpHandler );
mouseMoveHandler = null;
mouseUpHandler = null;
}
}*/
function save(){
var dataString = canvas.get(0).toDataURL("image/png");
//var index = dataString.indexOf( "," )+1;
//dataString = dataString.substring( index );
return dataString;
}
function drawLine(from_x, from_y, to_x, to_y, color, tool, width) {
if(tool === "brush"){
var halfBrushW = brush.width/2;
var halfBrushH = brush.height/2;
var start = { x:from_x, y: from_y };
var end = { x: to_x, y: to_y };
var distance = parseInt( Trig.distanceBetween2Points( start, end ) );
var angle = Trig.angleBetween2Points( start, end );
var x,y;
context.globalCompositeOperation = "source-over";
for ( var z=0; (z<=distance || z==0); z++ )
{
x = start.x + (Math.sin(angle) * z) - halfBrushW;
y = start.y + (Math.cos(angle) * z) - halfBrushH;
context.drawImage(brush, x, y);
}
}else if(tool === "pencil"){
context.beginPath();
context.moveTo(from_x, from_y);
context.lineTo(to_x, to_y);
context.lineJoin = 'round';
context.lineCap = 'round';
context.lineWidth = width;
context.strokeStyle = color;
context.globalCompositeOperation = "source-over";
context.stroke();
}else if(tool === "eraser"){
context.beginPath();
context.moveTo(from_x, from_y);
context.lineTo(to_x, to_y);
context.lineJoin = 'round';
context.lineCap = 'round';
context.lineWidth = width;
context.fillStyle = "rgba(0,0,0,1)";
context.globalCompositeOperation = "destination-out";
context.stroke();
}
}
});