|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +(function() { |
| 4 | + |
| 5 | + var socket = io(); |
| 6 | + var canvas = document.getElementsByClassName('whiteboard')[0]; |
| 7 | + var colors = document.getElementsByClassName('color'); |
| 8 | + var context = canvas.getContext('2d'); |
| 9 | + |
| 10 | + var current = { |
| 11 | + color: 'black' |
| 12 | + }; |
| 13 | + var drawing = false; |
| 14 | + |
| 15 | + canvas.addEventListener('mousedown', onMouseDown, false); |
| 16 | + canvas.addEventListener('mouseup', onMouseUp, false); |
| 17 | + canvas.addEventListener('mouseout', onMouseUp, false); |
| 18 | + canvas.addEventListener('mousemove', throttle(onMouseMove, 10), false); |
| 19 | + |
| 20 | + for (var i = 0; i < colors.length; i++){ |
| 21 | + colors[i].addEventListener('click', onColorUpdate, false); |
| 22 | + } |
| 23 | + |
| 24 | + socket.on('drawing', onDrawingEvent); |
| 25 | + |
| 26 | + window.addEventListener('resize', onResize, false); |
| 27 | + onResize(); |
| 28 | + |
| 29 | + |
| 30 | + function drawLine(x0, y0, x1, y1, color, emit){ |
| 31 | + context.beginPath(); |
| 32 | + context.moveTo(x0, y0); |
| 33 | + context.lineTo(x1, y1); |
| 34 | + context.strokeStyle = color; |
| 35 | + context.lineWidth = 2; |
| 36 | + context.stroke(); |
| 37 | + context.closePath(); |
| 38 | + |
| 39 | + if (!emit) { return; } |
| 40 | + var w = canvas.width; |
| 41 | + var h = canvas.height; |
| 42 | + |
| 43 | + socket.emit('drawing', { |
| 44 | + x0: x0 / w, |
| 45 | + y0: y0 / h, |
| 46 | + x1: x1 / w, |
| 47 | + y1: y1 / h, |
| 48 | + color: color |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + function onMouseDown(e){ |
| 53 | + drawing = true; |
| 54 | + current.x = e.clientX; |
| 55 | + current.y = e.clientY; |
| 56 | + } |
| 57 | + |
| 58 | + function onMouseUp(e){ |
| 59 | + if (!drawing) { return; } |
| 60 | + drawing = false; |
| 61 | + drawLine(current.x, current.y, e.clientX, e.clientY, current.color, true); |
| 62 | + } |
| 63 | + |
| 64 | + function onMouseMove(e){ |
| 65 | + if (!drawing) { return; } |
| 66 | + drawLine(current.x, current.y, e.clientX, e.clientY, current.color, true); |
| 67 | + current.x = e.clientX; |
| 68 | + current.y = e.clientY; |
| 69 | + } |
| 70 | + |
| 71 | + function onColorUpdate(e){ |
| 72 | + current.color = e.target.className.split(' ')[1]; |
| 73 | + } |
| 74 | + |
| 75 | + // limit the number of events per second |
| 76 | + function throttle(callback, delay) { |
| 77 | + var previousCall = new Date().getTime(); |
| 78 | + return function() { |
| 79 | + var time = new Date().getTime(); |
| 80 | + |
| 81 | + if ((time - previousCall) >= delay) { |
| 82 | + previousCall = time; |
| 83 | + callback.apply(null, arguments); |
| 84 | + } |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + function onDrawingEvent(data){ |
| 89 | + var w = canvas.width; |
| 90 | + var h = canvas.height; |
| 91 | + drawLine(data.x0 * w, data.y0 * h, data.x1 * w, data.y1 * h, data.color); |
| 92 | + } |
| 93 | + |
| 94 | + // make the canvas fill its parent |
| 95 | + function onResize() { |
| 96 | + canvas.width = window.innerWidth; |
| 97 | + canvas.height = window.innerHeight; |
| 98 | + } |
| 99 | + |
| 100 | +})(); |
0 commit comments