diff --git a/paint_client.js b/paint_client.js index 9b2b1a9..76c9dd9 100644 --- a/paint_client.js +++ b/paint_client.js @@ -160,32 +160,48 @@ function clearCanvas() send_change.send(); } -function clickCell() +function clickCell(cell) { var x = Math.round(canvas_mouse_x/cell_size); var y = Math.round(canvas_mouse_y/cell_size); + var tableCell; + if(cell) { + x = parseInt(cell.x); + y = parseInt(cell.y); + } + if(lastCell = []) + { + lastCell = tableArray[x][y]; + } var c = pen_color; for(var i=0; i0 && (newCell[1]-lastCell[1])>0) - { - var new0 = Math.floor(lastCell[0]+(slope*i)); - var new1 = Math.floor(lastCell[1]+i); - } - //console.log(i+"[0]:"+new0); - else if ((newCell[0]-lastCell[0])<0 && (newCell[1]-lastCell[1])>0) - { - var new0 = Math.floor(lastCell[0]+(slope*i)); - var new1 = Math.floor(lastCell[1]+i); - } - else if((newCell[0]-lastCell[0])<0 && (newCell[1]-lastCell[1])<0) - { - var new0 = Math.floor(lastCell[0]+(slope*(-i))); - var new1 = Math.floor(lastCell[1]-i); - } - else if((newCell[0]-lastCell[0])>0 && (newCell[1]-lastCell[1])<0) + if(!isFinite(slope) || slope > 15 || slope < -15) { - var new0 = Math.floor(lastCell[0]+(slope*(-i))); - var new1 = Math.floor(lastCell[1]-i); - } - //console.log("[1]:"+new1); - try{ - var tableCell = tableArray[new0][new1]; - tableCell.style.backgroundColor = pen_color; - tableCell.lastUpdated = new Date().getTime(); - console.log(tableCell); - clickedCells.push(tableCell, pen_color); + if(xDiff > 0)//move down + { + new1 = lastCellY; + new0 = lastCellX + i; + } + else if (xDiff < 0)//move up + { + new1 = lastCellY; + new0 = lastCellX - i; + } } - catch(e) + else if (isFinite(slope) && !isNaN(slope)) { - console.log("Couldn't paint cell") - } + //console.log("x: "+xDiff+" y: "+yDiff); + if ((xDiff>0 && yDiff>0) || (xDiff<0 && yDiff>0))//down,right + { + new0 = Math.floor(lastCellX+(slope*i)); + new1 = Math.floor(lastCellY+i); + } + //console.log(i+"[0]:"+new0); + else if(((newCellX-lastCellX)<0 && (newCellY-lastCellY)<0)||((newCellX-lastCellX)>0 && (newCellY-lastCellY)<0))//up,left + { + new0 = Math.floor(lastCellX+(slope*(-i))); + new1 = Math.floor(lastCellY-i); + } + //console.log("[1]:"+new1); + /*try{ + var tableCell = tableArray[new0][new1]; + tableCell.style.backgroundColor = pen_color; + tableCell.lastUpdated = new Date().getTime(); + console.log(tableCell); + clickedCells.push(tableCell, pen_color); + } + catch(e) + { + console.log("Couldn't paint cell") + }*/ + + /*for(var i=0; i 0) { + var url = "change_cells?"; + var i; + for(i=0; i 0 && + changedCells.indexOf(tableCell) === -1 && + clickedCells.indexOf(tableCell) === -1) { + + changedCellCount++; + tableCell.style.backgroundColor = cellColor; + + } else { + // console.log("Warning: Cell ["+i+", "+j+"] has been updated on the client more recently than the server. Won't overwrite."); + } + } else { + console.log("Error: Response did not match table cell."); + } + } + } else { + console.log("Error: Response was not a multidimensional array."); + } + } + } else { + console.log("Error: Response was not an array."); + } + // console.log("DONE PARSING. Response changed "+changedCellCount+" cells ("+cellCount+" total)."); +} + +function colorsSame(color1, color2) { + var same = false; + if(color1 === color2) { + same = true; + } else { + if(color1.toString().indexOf("#") > -1 && color2.toString().indexOf("rgb") > -1) { + color1 = hexToRgb(color1); + } else if(color2.toString().indexOf("#") > -1 && color1.toString().indexOf("rgb") > -1) { + color2 = hexToRgb(color2); + } + if(color1 === color2) { + same = true; + } + } + return same; +} + +/* This function from http://stackoverflow.com/a/5624139/3673087 */ +function hexToRgb(hex) { + var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); + return result ? + "rgb(" + parseInt(result[1], 16) + ", " + + parseInt(result[2], 16) + ", " + + parseInt(result[3], 16) + ")" + : null; +} + +function smooth(newCell) +{ + //console.log("new:"+newCell+"old:"+lastCell); + var pen_radius = pen_size/2; + var right, down; + if (Math.abs(newCell[0]-lastCell[0]) <= pen_size && Math.abs(newCell[1]-lastCell[1]) <= pen_size) + { + return; + } + /*if (lastCell = []) + { + lastCell = newCell; + return; + }*/ + else + { + //console.log(lastCell) + if (clickDragFlag) + { + var slope = (newCell[0]-lastCell[0])/(newCell[1]-lastCell[1]); + if(!isFinite(slope) || slope > 15 || slope < -15) + { + for (i = 1; i < Math.abs(newCell[0]-lastCell[0]); i++) + { + if(newCell[0]-lastCell[0] > 0) + { + var new1 = lastCell[1]; + var new0 = lastCell[0] + i; + } + else if (newCell[0]-lastCell[0] < 0) + { + var new1 = lastCell[1]; + var new0 = lastCell[0] - i; + } + } + } + if(isFinite(slope) && !isNaN(slope)) + { + console.log("x: "+(newCell[0]-lastCell[0])+" y: "+(newCell[1]-lastCell[1])); + for (i = 1; i < Math.abs(newCell[1]-lastCell[1]); i++) + { + if ((newCell[0]-lastCell[0])>0 && (newCell[1]-lastCell[1])>0) + { + var new0 = Math.floor(lastCell[0]+(slope*i)); + var new1 = Math.floor(lastCell[1]+i); + } + //console.log(i+"[0]:"+new0); + else if ((newCell[0]-lastCell[0])<0 && (newCell[1]-lastCell[1])>0) + { + var new0 = Math.floor(lastCell[0]+(slope*i)); + var new1 = Math.floor(lastCell[1]+i); + } + else if((newCell[0]-lastCell[0])<0 && (newCell[1]-lastCell[1])<0) + { + var new0 = Math.floor(lastCell[0]+(slope*(-i))); + var new1 = Math.floor(lastCell[1]-i); + } + else if((newCell[0]-lastCell[0])>0 && (newCell[1]-lastCell[1])<0) + { + var new0 = Math.floor(lastCell[0]+(slope*(-i))); + var new1 = Math.floor(lastCell[1]-i); + } + //console.log("[1]:"+new1); + /*try{ + var tableCell = tableArray[new0][new1]; + tableCell.style.backgroundColor = pen_color; + tableCell.lastUpdated = new Date().getTime(); + console.log(tableCell); + clickedCells.push(tableCell, pen_color); + } + catch(e) + { + console.log("Couldn't paint cell") + }*/ + + /*for(var i=0; i 0) { + var url = "change_cells?"; + var i; + for(i=0; i 0 && + changedCells.indexOf(tableCell) === -1 && + clickedCells.indexOf(tableCell) === -1) { + + changedCellCount++; + tableCell.style.backgroundColor = cellColor; + + } else { + // console.log("Warning: Cell ["+i+", "+j+"] has been updated on the client more recently than the server. Won't overwrite."); + } + } else { + console.log("Error: Response did not match table cell."); + } + } + } else { + console.log("Error: Response was not a multidimensional array."); + } + } + } else { + console.log("Error: Response was not an array."); + } + // console.log("DONE PARSING. Response changed "+changedCellCount+" cells ("+cellCount+" total)."); +} + +function colorsSame(color1, color2) { + var same = false; + if(color1 === color2) { + same = true; + } else { + if(color1.toString().indexOf("#") > -1 && color2.toString().indexOf("rgb") > -1) { + color1 = hexToRgb(color1); + } else if(color2.toString().indexOf("#") > -1 && color1.toString().indexOf("rgb") > -1) { + color2 = hexToRgb(color2); + } + if(color1 === color2) { + same = true; + } + } + return same; +} + +/* This function from http://stackoverflow.com/a/5624139/3673087 */ +function hexToRgb(hex) { + var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); + return result ? + "rgb(" + parseInt(result[1], 16) + ", " + + parseInt(result[2], 16) + ", " + + parseInt(result[3], 16) + ")" + : null; +} + +function smooth(newCell) +{ + //console.log("new:"+newCell+"old:"+lastCell); + var pen_radius = pen_size/2; + var right, down; + if (Math.abs(newCell[0]-lastCell[0]) <= pen_size && Math.abs(newCell[1]-lastCell[1]) <= pen_size) + { + return; + } + /*if (lastCell = []) + { + lastCell = newCell; + return; + }*/ + else + { + //console.log(lastCell) + if (clickDragFlag) + { + var slope = (newCell[0]-lastCell[0])/(newCell[1]-lastCell[1]); + if(!isFinite(slope) || slope > 15 || slope < -15) + { + for (i = 1; i < Math.abs(newCell[0]-lastCell[0]); i++) + { + if(newCell[0]-lastCell[0] > 0) + { + var new1 = lastCell[1]; + var new0 = lastCell[0] + i; + } + else if (newCell[0]-lastCell[0] < 0) + { + var new1 = lastCell[1]; + var new0 = lastCell[0] - i; + } + } + } + if(isFinite(slope) && !isNaN(slope)) + { + console.log("x: "+(newCell[0]-lastCell[0])+" y: "+(newCell[1]-lastCell[1])); + for (i = 1; i < Math.abs(newCell[1]-lastCell[1]); i++) + { + if ((newCell[0]-lastCell[0])>0 && (newCell[1]-lastCell[1])>0) + { + var new0 = Math.floor(lastCell[0]+(slope*i)); + var new1 = Math.floor(lastCell[1]+i); + } + //console.log(i+"[0]:"+new0); + else if ((newCell[0]-lastCell[0])<0 && (newCell[1]-lastCell[1])>0) + { + var new0 = Math.floor(lastCell[0]+(slope*i)); + var new1 = Math.floor(lastCell[1]+i); + } + else if((newCell[0]-lastCell[0])<0 && (newCell[1]-lastCell[1])<0) + { + var new0 = Math.floor(lastCell[0]+(slope*(-i))); + var new1 = Math.floor(lastCell[1]-i); + } + else if((newCell[0]-lastCell[0])>0 && (newCell[1]-lastCell[1])<0) + { + var new0 = Math.floor(lastCell[0]+(slope*(-i))); + var new1 = Math.floor(lastCell[1]-i); + } + //console.log("[1]:"+new1); + /*try{ + var tableCell = tableArray[new0][new1]; + tableCell.style.backgroundColor = pen_color; + tableCell.lastUpdated = new Date().getTime(); + console.log(tableCell); + clickedCells.push(tableCell, pen_color); + } + catch(e) + { + console.log("Couldn't paint cell") + }*/ + + /*for(var i=0; i -1) + + if(filename.indexOf("get_changed_cells") > -1)//how does indexOf > -1 work? and why is this if, not else if? { sendChangedCells( req, res ); } @@ -75,6 +75,11 @@ function serverFn(req,res) filename = "./index.html"; serveFile(filename, req, res); } + else if(filename.indexOf("build_array") > -1) + { + //runs the full line builder from the smooth function serverside to avoid crashes. + buildArray(filename,req,res); + } else { serveFile(filename, req, res); @@ -138,7 +143,7 @@ function serveFile( filename, req, res ) fourZeroFour(filename, res); return; } - + var extension = "html"; try { extension = filename.split(/.\./)[1]; @@ -163,10 +168,33 @@ function sendChangedCells(req, res) res.end(JSON.stringify(canvas)); } +function buildArray(filename, req, res) +{ + var requests = filename.split("?")[1].split("&"); + var new0 = Math.floor(requests[0].split("=")[1]); + var new1 = Math.floor(requests[1].split("=")[1]); + var color = requests[2].split("=")[1]; + var pen_radius = requests[3].split("=")[1]; + for(var i=0; i