2021-07-22 20:26:40 +03:00
|
|
|
// REFACTOR: inherit from Layer, override init method (call super as well)
|
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
// Start colour of the pixel grid (can be changed in the preferences)
|
2021-07-07 03:42:11 +03:00
|
|
|
let pixelGridColor = "#000000";
|
2020-12-31 18:47:56 +03:00
|
|
|
// Distance between one line and another in HTML pixels
|
2020-09-26 13:32:31 +03:00
|
|
|
let lineDistance = 12;
|
2021-07-07 03:51:20 +03:00
|
|
|
// The grid is visible by default
|
2021-07-22 19:02:19 +03:00
|
|
|
let pixelGridVisible = false;
|
2020-09-26 12:51:18 +03:00
|
|
|
|
2021-07-22 19:02:19 +03:00
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
/** Shows or hides the pixel grid depening on its current visibility
|
|
|
|
* (triggered by the show pixel grid button in the top menu)
|
|
|
|
*
|
|
|
|
*/
|
2021-07-07 03:51:20 +03:00
|
|
|
function togglePixelGrid(newState) {
|
2020-12-31 18:47:56 +03:00
|
|
|
// Getting the button because I have to change its text
|
2020-09-26 13:40:03 +03:00
|
|
|
let button = document.getElementById("toggle-pixelgrid-button");
|
|
|
|
|
2021-07-07 03:51:20 +03:00
|
|
|
//Set the state based on the passed newState variable, otherwise just toggle it
|
|
|
|
if (newState == 'on') pixelGridVisible = true;
|
|
|
|
else if (newState == 'off') pixelGridVisible = false;
|
|
|
|
else pixelGridVisible = !pixelGridVisible;
|
2020-09-26 13:40:03 +03:00
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
// If it was visible, I hide it
|
2020-09-26 13:40:03 +03:00
|
|
|
if (pixelGridVisible) {
|
|
|
|
button.innerHTML = "Hide pixel grid";
|
2021-07-22 19:57:44 +03:00
|
|
|
pixelGrid.canvas.style.display = "inline-block";
|
2020-09-26 13:40:03 +03:00
|
|
|
}
|
2020-12-31 18:47:56 +03:00
|
|
|
// Otherwise, I show it
|
2020-09-26 13:40:03 +03:00
|
|
|
else {
|
|
|
|
button.innerHTML = "Show pixel grid";
|
2021-07-22 19:57:44 +03:00
|
|
|
pixelGrid.canvas.style.display = "none";
|
2020-09-26 13:40:03 +03:00
|
|
|
}
|
|
|
|
}
|
2020-09-26 13:32:31 +03:00
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
/** Fills the pixelGridCanvas with the pixelgrid
|
|
|
|
*
|
|
|
|
*/
|
2020-09-26 12:51:18 +03:00
|
|
|
function fillPixelGrid() {
|
2021-07-22 19:57:44 +03:00
|
|
|
let context = pixelGrid.context;
|
2020-09-26 13:32:31 +03:00
|
|
|
let originalSize = layers[0].canvasSize;
|
|
|
|
|
2020-12-31 18:47:56 +03:00
|
|
|
// The pixelGridCanvas is lineDistance times bigger so that the lines don't take 1 canvas pixel
|
|
|
|
// (which would cover the whole canvas with the pixelGridColour), but they take 1/lineDistance canvas pixels
|
2021-07-22 19:57:44 +03:00
|
|
|
pixelGrid.canvas.width = originalSize[0] * lineDistance;
|
|
|
|
pixelGrid.canvas.height = originalSize[1] * lineDistance;
|
2020-09-26 13:32:31 +03:00
|
|
|
|
|
|
|
// OPTIMIZABLE, could probably be a bit more elegant
|
|
|
|
// Draw horizontal lines
|
2021-07-22 19:57:44 +03:00
|
|
|
for (let i=0; i<pixelGrid.canvas.width / lineDistance; i++) {
|
2020-09-29 20:10:50 +03:00
|
|
|
context.strokeStyle = settings.pixelGridColour;
|
2020-09-26 13:32:31 +03:00
|
|
|
|
|
|
|
context.beginPath();
|
|
|
|
context.moveTo(i * lineDistance + 0.5, 0);
|
|
|
|
context.lineTo(i * lineDistance + 0.5, originalSize[1] * lineDistance);
|
|
|
|
context.stroke();
|
|
|
|
context.closePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw vertical lines
|
2021-07-22 19:57:44 +03:00
|
|
|
for (let i=0; i<pixelGrid.canvas.height / lineDistance; i++) {
|
2020-09-26 13:32:31 +03:00
|
|
|
context.beginPath();
|
|
|
|
context.moveTo(0, i * lineDistance + 0.5);
|
|
|
|
context.lineTo(originalSize[0] * lineDistance, i * lineDistance + 0.5);
|
|
|
|
context.stroke();
|
|
|
|
context.closePath();
|
|
|
|
}
|
2020-09-26 13:40:03 +03:00
|
|
|
|
|
|
|
if (!pixelGridVisible) {
|
2021-07-22 19:57:44 +03:00
|
|
|
pixelGrid.canvas.style.display = 'none';
|
2020-09-26 13:40:03 +03:00
|
|
|
}
|
2020-09-26 12:51:18 +03:00
|
|
|
}
|