Fixed bug in checkerboard creation.

Signed-off-by: npalomba <nicola.palomba@studenti.galileilivorno.gov.it>
This commit is contained in:
npalomba 2019-03-31 15:10:51 +02:00
parent cf4f17d891
commit f229c14d91
2 changed files with 34 additions and 13 deletions

View File

@ -1,4 +1,5 @@
var currentColor = firstCheckerBoardColor; var currentColor = firstCheckerBoardColor;
var nSquaresFilled = 0;
/* TODO add check for canvas dimentions (right now negative values can be inserted and a canvas will be generated, it is just /* TODO add check for canvas dimentions (right now negative values can be inserted and a canvas will be generated, it is just
necessary to add a conversion from negative to positive values. necessary to add a conversion from negative to positive values.
@ -6,23 +7,44 @@ var currentColor = firstCheckerBoardColor;
function fillCheckerboard() { function fillCheckerboard() {
for (var i=0; i<canvasSize[0] / checkerBoardSquareSize; i++) { for (var i=0; i<canvasSize[0] / checkerBoardSquareSize; i++) {
for (var j=0; j<canvasSize[1] / checkerBoardSquareSize; j++) { nSquaresFilled = 0;
context.fillStyle = currentColor;
context.fillRect(i * checkerBoardSquareSize, j * checkerBoardSquareSize, checkerBoardSquareSize, checkerBoardSquareSize);
changeCheckerboardColor(false); for (var j=0; j<canvasSize[1] / checkerBoardSquareSize; j++) {
var rectX;
var rectY;
if (i * checkerBoardSquareSize < canvasSize[0]) {
rectX = i * checkerBoardSquareSize;
}
else {
rectX = canvasSize[0];
}
if (j * checkerBoardSquareSize < canvasSize[1]) {
rectY = j * checkerBoardSquareSize;
}
else {
rectY = canvasSize[1];
}
context.fillStyle = currentColor;
context.fillRect(rectX, rectY, checkerBoardSquareSize, checkerBoardSquareSize);
changeCheckerboardColor();
nSquaresFilled++;
} }
changeCheckerboardColor(false); if ((nSquaresFilled % 2) == 0) {
changeCheckerboardColor();
}
} }
} }
function changeCheckerboardColor(rowEndReached) { function changeCheckerboardColor(isVertical) {
if (!rowEndReached) { if (currentColor == firstCheckerBoardColor) {
if (currentColor == firstCheckerBoardColor) { currentColor = secondCheckerBoardColor;
currentColor = secondCheckerBoardColor; } else if (currentColor == secondCheckerBoardColor) {
} else if (currentColor == secondCheckerBoardColor) { currentColor = firstCheckerBoardColor;
currentColor = firstCheckerBoardColor;
}
} }
} }

View File

@ -8,7 +8,6 @@ function line(x0,y0,x1,y1) {
var err = dx-dy; var err = dx-dy;
while (true) { while (true) {
console.log("drawing line");
//set pixel //set pixel
if (currentTool == 'pencil') { if (currentTool == 'pencil') {
context.fillRect(x0-Math.floor(brushSize/2), y0-Math.floor(brushSize/2), brushSize, brushSize); context.fillRect(x0-Math.floor(brushSize/2), y0-Math.floor(brushSize/2), brushSize, brushSize);