Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
244 changes: 106 additions & 138 deletions src/client/javascript/whiteboard.js
Original file line number Diff line number Diff line change
@@ -1,138 +1,106 @@
const Client = {};
Client.socket = io.connect();

Client.askNewPlayer = function(){
Client.socket.emit('isRoomAvailable');
}();

// recived messages

Client.socket.on('awaitRoom', function(data){
console.log('player 1')
})

Client.socket.on('startRoom', function(data){
Client.socket.roomId = data.roomId;
console.log("roomId set to ", data.roomId)
console.log('player 2')
Client.socket.emit('joiningRoom', data)
})

Client.socket.on('joiningRoom', function(data){
console.log('player 2 is joining')
Client.socket.roomId = data.roomId;
console.log("roomId", data.roomId)
})

Client.socket.on('app',function(data){
const objData = JSON.parse(data);
if (objData.newEvent.type = "draw") {
const {color, lineStyle, plots} = objData.newEvent.data;
console.log(color)
drawOnCanvas(color, lineStyle, plots);
}
});



(function() {
/* set up Canvas */

const canvas = document.getElementById('drawCanvas');
const ctx = canvas.getContext('2d');
let color = document.querySelector('#colorSwatch :checked').getAttribute('data-color');
let lineStyle = document.querySelector('#lineStyle :checked').getAttribute('data-type');
canvas.width = Math.min(document.documentElement.clientWidth, window.innerWidth || 300);
canvas.height = Math.min(document.documentElement.clientHeight, window.innerHeight || 300);

ctx.strokeStyle = color;
ctx.lineWidth = lineStyle;
ctx.lineCap = ctx.lineJoin = 'round';

/* Mouse and touch events */

document.getElementById('colorSwatch').addEventListener('click', function() {
color = document.querySelector('#colorSwatch :checked').getAttribute('data-color');
}, false);

//let lineStyle = document.querySelector('#lineStyle :checked').getAttribute('data-type');

document.getElementById('lineStyle').addEventListener('click', function() {
lineStyle = document.querySelector('#lineStyle :checked').getAttribute('data-type');
}, false);

const isTouchSupported = 'ontouchstart' in window;
const isPointerSupported = navigator.pointerEnabled;
const isMSPointerSupported = navigator.msPointerEnabled;

const downEvent = isTouchSupported ? 'touchstart' : (isPointerSupported ? 'pointerdown' : (isMSPointerSupported ? 'MSPointerDown' : 'mousedown'));
const moveEvent = isTouchSupported ? 'touchmove' : (isPointerSupported ? 'pointermove' : (isMSPointerSupported ? 'MSPointerMove' : 'mousemove'));
const upEvent = isTouchSupported ? 'touchend' : (isPointerSupported ? 'pointerup' : (isMSPointerSupported ? 'MSPointerUp' : 'mouseup'));

canvas.addEventListener(downEvent, startDraw, false);
canvas.addEventListener(moveEvent, draw, false);
canvas.addEventListener(upEvent, endDraw, false);

/* Socket.io */



/* Draw on canvas */

function drawOnCanvas(color, lineStyle, plots) {
ctx.strokeStyle = color;
ctx.lineWidth = lineStyle;
ctx.beginPath();
ctx.moveTo(plots[0].x, plots[0].y);

for(var i=1; i<plots.length; i++) {
ctx.lineTo(plots[i].x, plots[i].y);
}
ctx.stroke();
}

function drawFromStream(message) {
if(!message || message.plots.length < 1) return;
drawOnCanvas(message.color, message.plots);
}

// Get Older and Past Drawings!
if(drawHistory) {
pubnub.history({
channel : channel,
count : 50,
callback : function(messages) {
pubnub.each( messages[0], drawFromStream );
}
});
}

var isActive = false;
var plots = [];

function draw(e) {
e.preventDefault(); // prevent continuous touch event process e.g. scrolling!
if(!isActive) return;

var x = isTouchSupported ? (e.targetTouches[0].pageX - canvas.offsetLeft) : (e.offsetX || e.layerX - canvas.offsetLeft);
var y = isTouchSupported ? (e.targetTouches[0].pageY - canvas.offsetTop) : (e.offsetY || e.layerY - canvas.offsetTop);

plots.push({x: (x << 0), y: (y << 0)}); // round numbers for touch screens

drawOnCanvas(color, lineStyle, plots);
}

function startDraw(e) {
e.preventDefault();
isActive = true;
}

function endDraw(e) {
e.preventDefault();
isActive = false;

Client.socket.emit('app', JSON.stringify( {newEvent: {type: "draw", data: {color: color, lineStyle: lineStyle, plots: plots}}} ));

plots = [];
}
})();
let isActive = false;
let plots = [];

const WhiteBoard = {
/* Draw on canvas */
drawOnCanvas: (color, lineStyle, plots) => {
ctx.strokeStyle = color;
ctx.lineWidth = lineStyle;
ctx.beginPath();
ctx.moveTo(plots[0].x, plots[0].y);

for(var i=1; i<plots.length; i++) {
ctx.lineTo(plots[i].x, plots[i].y);
}
ctx.stroke();
},

drawFromStream: (message) => {
if(!message || message.plots.length < 1) return;
WhiteBoard.drawOnCanvas(message.color, message.plots);
},

draw: (e) => {
e.preventDefault(); // prevent continuous touch event process e.g. scrolling!
if(!isActive) return;

var x = isTouchSupported ? (e.targetTouches[0].pageX - canvas.offsetLeft) : (e.offsetX || e.layerX - canvas.offsetLeft);
var y = isTouchSupported ? (e.targetTouches[0].pageY - canvas.offsetTop) : (e.offsetY || e.layerY - canvas.offsetTop);

plots.push({x: (x << 0), y: (y << 0)}); // round numbers for touch screens

WhiteBoard.drawOnCanvas(color, lineStyle, plots);
},

startDraw: (e) => {
e.preventDefault();
isActive = true;
},

endDraw: (e) => {
e.preventDefault();
isActive = false;

Client.socket.emit('app', JSON.stringify( {newEvent: {type: "draw", data: {color: color, lineStyle: lineStyle, plots: plots}}} ));

plots = [];
},
};

Object.freeze(WhiteBoard);

// *********************Events and Setup below *******************************
const Client = {};

Client.socket = io.connect({
autoConnect: false,
transports: ['websocket'],
});

Client.socket.on('app', (data) => {
const objData = JSON.parse(data);
if (objData.newEvent.type === 'draw') {
const {color, lineStyle, plots} = objData.newEvent.data;
console.log(`Whiteboard Color: ${color}`)
WhiteBoard.drawOnCanvas(color, lineStyle, plots);
}
});

// Need to determine how best to implement this function
// that is automatically executed.

/* set up Canvas */
const canvas = document.getElementById('drawCanvas');
const ctx = canvas.getContext('2d');
let color = document.querySelector('#colorSwatch :checked').getAttribute('data-color');
let lineStyle = document.querySelector('#lineStyle :checked').getAttribute('data-type');
canvas.width = Math.min(document.documentElement.clientWidth, window.innerWidth || 300);
canvas.height = Math.min(document.documentElement.clientHeight, window.innerHeight || 300);

ctx.strokeStyle = color;
ctx.lineWidth = lineStyle;
ctx.lineCap = ctx.lineJoin = 'round';

/* Mouse and touch events */
document.getElementById('colorSwatch').addEventListener('click', function() {
color = document.querySelector('#colorSwatch :checked').getAttribute('data-color');
}, false);

document.getElementById('lineStyle').addEventListener('click', function() {
lineStyle = document.querySelector('#lineStyle :checked').getAttribute('data-type');
}, false);

const isTouchSupported = 'ontouchstart' in window;
const isPointerSupported = navigator.pointerEnabled;
const isMSPointerSupported = navigator.msPointerEnabled;

const downEvent = isTouchSupported ? 'touchstart' : (isPointerSupported ? 'pointerdown' : (isMSPointerSupported ? 'MSPointerDown' : 'mousedown'));
const moveEvent = isTouchSupported ? 'touchmove' : (isPointerSupported ? 'pointermove' : (isMSPointerSupported ? 'MSPointerMove' : 'mousemove'));
const upEvent = isTouchSupported ? 'touchend' : (isPointerSupported ? 'pointerup' : (isMSPointerSupported ? 'MSPointerUp' : 'mouseup'));

canvas.addEventListener(downEvent, WhiteBoard.startDraw, false);
canvas.addEventListener(moveEvent, WhiteBoard.draw, false);
canvas.addEventListener(upEvent, WhiteBoard.endDraw, false);

// **************Events and setup above **********************************

80 changes: 40 additions & 40 deletions src/client/whiteboard.html
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="client/css/style.css">
<script src="/socket.io/socket.io.js"></script>
</head>

<body>
<header>
<div>
<h1>Whiteboard</h1>
</div>
</header>

<section id="main">
<canvas id="drawCanvas" width="300" height="300">Canvas is not supported on this browser!</canvas>

<section id="colorSwatch">
<input type="radio" name="color" id="color01" data-color="gold" checked><label for="color01"></label>
<input type="radio" name="color" id="color02" data-color="darkorange"> <label for="color02"></label>
<input type="radio" name="color" id="color03" data-color="navy"> <label for="color03"></label>
<input type="radio" name="color" id="color04" data-color="yellowgreen"> <label for="color04"></label>
<input type="radio" name="color" id="color05" data-color="firebrick"> <label for="color05"></label>
<input type="radio" name="color" id="color06" data-color="powderblue"> <label for="color06"></label>
</section>

<section id="lineStyle">
<input type="radio" name="lineStyle" id="style01" data-type="1"> <label for="style01"></label>
<input type="radio" name="lineStyle" id="style02" data-type="2"> <label for="style02"></label>
<input type="radio" name="lineStyle" id="style03" data-type="3"> <label for="style03"></label>
<input type="radio" name="lineStyle" id="style04" data-type="4"> <label for="style04"></label>
<input type="radio" name="lineStyle" id="style05" data-type="5" checked> <label for="style05"></label>
</section>
</section>
<script src="client/js/whiteboard.js"></script>
</body>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="client/css/whiteboard.css">
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<header>
<div>
<h1>Whiteboard</h1>
</div>
</header>
<section id="main">
<canvas id="drawCanvas" width="300" height="300">Canvas is not supported on this browser!</canvas>
<section id="colorSwatch">
<input type="radio" name="color" id="color01" data-color="gold" checked><label for="color01"></label>
<input type="radio" name="color" id="color02" data-color="darkorange"> <label for="color02"></label>
<input type="radio" name="color" id="color03" data-color="navy"> <label for="color03"></label>
<input type="radio" name="color" id="color04" data-color="yellowgreen"> <label for="color04"></label>
<input type="radio" name="color" id="color05" data-color="firebrick"> <label for="color05"></label>
<input type="radio" name="color" id="color06" data-color="powderblue"> <label for="color06"></label>
</section>
<section id="lineStyle">
<input type="radio" name="lineStyle" id="style01" data-type="1"> <label for="style01"></label>
<input type="radio" name="lineStyle" id="style02" data-type="2"> <label for="style02"></label>
<input type="radio" name="lineStyle" id="style03" data-type="3"> <label for="style03"></label>
<input type="radio" name="lineStyle" id="style04" data-type="4"> <label for="style04"></label>
<input type="radio" name="lineStyle" id="style05" data-type="5" checked> <label for="style05"></label>
</section>
</section>
<script src="client/javascript/whiteboard.js"></script>
</body>
</html>
4 changes: 2 additions & 2 deletions src/server/user/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ const UserController = {
if (connectionArray[i].username === message.target) {
connectionArray[i].socket.emit(message.type, msgString);

Logger(`Emitting message to clientID: ${connectionArray[i].clientID}.`);
Logger(`Message (type / message): ${message.type} / ${msgString}`);
Logger.log(`Emitting message to clientID: ${connectionArray[i].clientID}.`);
Logger.log(`Message (type / message): ${message.type} / ${msgString}`);

break;
}
Expand Down