Skip to content

Commit

Permalink
smooth works slowly in slow fill and not at all in normal client
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyhofford committed May 13, 2015
1 parent eef5071 commit 5d4d8b0
Show file tree
Hide file tree
Showing 4 changed files with 939 additions and 63 deletions.
156 changes: 99 additions & 57 deletions paint_client.js
Expand Up @@ -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; i<pen_size/2; i++) {
for(var j=0; j<pen_size/2; j++) {
var cellIndex = x + (pen_size/4 - i);
var rowIndex = y + (pen_size/4 - j);
try {
var tableCell = tableArray[rowIndex][cellIndex];
tableCell = tableArray[rowIndex][cellIndex];
//console.log(tableCell);
tableCell.style.backgroundColor = c;
tableCell.lastUpdated = new Date().getTime();
if(clickedCells.indexOf(tableCell) === -1)
{
clickedCells.push(tableCell); //,pen color?
smooth([rowIndex,cellIndex]);
lastCell = [rowIndex,cellIndex];
}

} catch(e) {
console.log("Error: Couldn't paint cell ["+rowIndex+", "+cellIndex+"].");
console.log("Error: Couldn't paint cell ["+rowIndex+", "+cellIndex+"]:"+e);
}
}
}
console.log(x+" "+y);
var cellToSmooth = tableArray[x][y];
console.log(celltoSmooth);
var smoothCells = smooth(cellToSmooth);
console.log(smoothCells);
lastCell = tableCell;
/*for(var k=0; k<smoothCells.length; k++) {
clickCell(smoothCells[k]);
}*/
}

function changeClickedCells()
Expand Down Expand Up @@ -233,7 +249,7 @@ function pollColors()
function colorsListener()
{
this.endTime = new Date().getTime();
// console.log("Request load time: "+(this.endTime-this.startTime)+". Parsing response...");
console.log("Request load time: "+(this.endTime-this.startTime)+". Parsing response...");
var canvas = JSON.parse(this.responseText);
var cellCount = 0;
var changedCellCount = 0;
Expand Down Expand Up @@ -304,8 +320,19 @@ 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_radius && Math.abs(newCell[1]-lastCell[1]) <= pen_radius)
//var right, down
var newCellX = newCell.x;
var newCellY = newCell.y;
var lastCellX = lastCell.x;
var lastCellY = lastCell.y;
var xDiff = newCellX-lastCellX;
var xDiffAbs = Math.abs(xDiff);
var yDiff = (newCellY-lastCellY);
var yDiffAbs = Math.abs(newCellY-lastCellY);

var cellsToAdd = [];

if (xDiffAbs <= pen_size && yDiffAbs <= pen_size)
{
return;
}
Expand All @@ -316,63 +343,78 @@ function smooth(newCell)
}*/
else
{
console.log(lastCell)
if (clickDownFlag && clickDragFlag)
//console.log(lastCell)
if (clickDragFlag)
{
var slope = (newCell[0]-lastCell[0])/(newCell[1]-lastCell[1]);
console.log("slope"+slope);
for (i = 1; i < Math.abs(newCell[1]-lastCell[1]); i++)
var new1, new0;
var slope = (newCellX-lastCellX)/(newCellY-lastCellY);
for (i = 1; i < xDiffAbs; i+=cell_size)
{
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)
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<pen_radius; i++) {
for(var j=0; j<pen_radius; j++) {
var cellIndex = new1 + (pen_size/4 - i);
var rowIndex = new0 + (pen_size/4 - j);
try {
var tableCell = tableArray[rowIndex][cellIndex];
tableCell.style.backgroundColor = pen_color;
tableCell.lastUpdated = new Date().getTime();
clickedCells.push(tableCell);
/*for(var i=0; i<pen_radius; i++) {
for(var j=0; j<pen_radius; j++) {
var cellIndex = new1 + (pen_size/4 - i);
var rowIndex = new0 + (pen_size/4 - j);
try {
var tableCell = tableArray[rowIndex][cellIndex];
tableCell.style.backgroundColor = pen_color;
tableCell.lastUpdated = new Date().getTime();
clickedCells.push(tableCell);
} catch(e) {
console.log("Error: Couldn't paint cell ["+rowIndex+", "+cellIndex+"].");
} catch(e) {
console.log("Error: Couldn't paint cell ["+rowIndex+", "+cellIndex+"].");
}
}
}
}*/
}*/
}
cellsToAdd.push(tableArray[new0][new1]);
}
console.log("0:"+new0+" 1:"+new1);
var url = "build_array?zero="+new0+"&one="+new1+"&color="+pen_color+"&size="+pen_radius;
var build_array = new XMLHttpRequest;
build_array.onload = colorsListener;
build_array.open("get", url);
build_array.send();
}
}
return cellsToAdd;
}

0 comments on commit 5d4d8b0

Please sign in to comment.