2021-11-12 01:13:15 +03:00
|
|
|
class Checkerboard extends Layer {
|
|
|
|
// Checkerboard color 1
|
|
|
|
firstCheckerBoardColor = 'rgba(179, 173, 182, 1)';
|
|
|
|
// Checkerboard color 2
|
|
|
|
secondCheckerBoardColor = 'rgba(204, 200, 206, 1)';
|
|
|
|
// Square size for the checkerboard
|
|
|
|
checkerBoardSquareSize = 16;
|
|
|
|
|
|
|
|
// Setting current colour (each square has a different colour
|
|
|
|
currentColor = undefined;
|
|
|
|
// Saving number of squares filled until now
|
|
|
|
nSquaresFilled = 0;
|
|
|
|
|
|
|
|
constructor(width, height, canvas, menuEntry) {
|
|
|
|
super(width, height, document.getElementById('checkerboard'), menuEntry);
|
|
|
|
this.initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
initialize() {
|
|
|
|
super.initialize();
|
|
|
|
console.log("Square size: " + this.checkerBoardSquareSize);
|
|
|
|
this.currentColor = this.firstCheckerBoardColor;
|
|
|
|
this.fillCheckerboard();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Fills the checkerboard canvas with squares with alternating colours
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
fillCheckerboard() {
|
2021-12-06 19:37:43 +03:00
|
|
|
this.context.clearRect(0, 0, currFile.canvasSize[0], currFile.canvasSize[1]);
|
2021-11-12 01:13:15 +03:00
|
|
|
|
|
|
|
// Cycling through the canvas (using it as a matrix)
|
2021-12-06 19:37:43 +03:00
|
|
|
for (let i=0; i<currFile.canvasSize[0] / this.checkerBoardSquareSize; i++) {
|
2021-11-12 01:13:15 +03:00
|
|
|
this.nSquaresFilled = 0;
|
|
|
|
|
2021-12-06 19:37:43 +03:00
|
|
|
for (let j=0; j<currFile.canvasSize[1] / this.checkerBoardSquareSize; j++) {
|
2021-11-12 01:13:15 +03:00
|
|
|
let rectX;
|
|
|
|
let rectY;
|
|
|
|
|
|
|
|
// Managing the not perfect squares (the ones at the sides if the canvas' sizes are not powers of checkerBoardSquareSize
|
2021-12-06 19:37:43 +03:00
|
|
|
if (i * this.checkerBoardSquareSize < currFile.canvasSize[0]) {
|
2021-11-12 01:13:15 +03:00
|
|
|
rectX = i * this.checkerBoardSquareSize;
|
|
|
|
}
|
|
|
|
else {
|
2021-12-06 19:37:43 +03:00
|
|
|
rectX = currFile.canvasSize[0];
|
2021-11-12 01:13:15 +03:00
|
|
|
}
|
|
|
|
|
2021-12-06 19:37:43 +03:00
|
|
|
if (j * this.checkerBoardSquareSize < currFile.canvasSize[1]) {
|
2021-11-12 01:13:15 +03:00
|
|
|
rectY = j * this.checkerBoardSquareSize;
|
|
|
|
}
|
|
|
|
else {
|
2021-12-06 19:37:43 +03:00
|
|
|
rectY = currFile.canvasSize[1];
|
2021-11-12 01:13:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Selecting the colour
|
|
|
|
this.context.fillStyle = this.currentColor;
|
|
|
|
this.context.fillRect(rectX, rectY, this.checkerBoardSquareSize, this.checkerBoardSquareSize);
|
|
|
|
|
|
|
|
// Changing colour
|
|
|
|
this.changeCheckerboardColor();
|
|
|
|
this.nSquaresFilled++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the number of filled squares was even, I change colour for the next column
|
|
|
|
if ((this.nSquaresFilled % 2) == 0) {
|
|
|
|
this.changeCheckerboardColor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Simply switches the checkerboard colour
|
|
|
|
changeCheckerboardColor() {
|
|
|
|
if (this.currentColor == this.firstCheckerBoardColor) {
|
|
|
|
this.currentColor = this.secondCheckerBoardColor;
|
|
|
|
} else if (this.currentColor == this.secondCheckerBoardColor) {
|
|
|
|
this.currentColor = this.firstCheckerBoardColor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|