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
|
|
|
|
let pixelGridVisible = true;
|
2020-12-31 18:47:56 +03:00
|
|
|
// Saving the canvas containing the pixel grid
|
2020-09-26 12:51:18 +03:00
|
|
|
pixelGridCanvas = document.getElementById("pixel-grid");
|
|
|
|
|
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) {
|
|
|
|
console.log('toggling pixel grid', 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";
|
|
|
|
pixelGridCanvas.style.display = "inline-block";
|
|
|
|
}
|
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";
|
|
|
|
pixelGridCanvas.style.display = "none";
|
|
|
|
}
|
|
|
|
}
|
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() {
|
2020-09-26 13:32:31 +03:00
|
|
|
let context = pixelGridCanvas.getContext("2d");
|
|
|
|
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
|
2020-09-26 13:32:31 +03:00
|
|
|
pixelGridCanvas.width = originalSize[0] * lineDistance;
|
|
|
|
pixelGridCanvas.height = originalSize[1] * lineDistance;
|
|
|
|
|
|
|
|
// OPTIMIZABLE, could probably be a bit more elegant
|
|
|
|
// Draw horizontal lines
|
|
|
|
for (let i=0; i<pixelGridCanvas.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
|
|
|
|
for (let i=0; i<pixelGridCanvas.height / lineDistance; i++) {
|
|
|
|
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) {
|
|
|
|
pixelGridCanvas.style.display = 'none';
|
|
|
|
}
|
2020-09-26 12:51:18 +03:00
|
|
|
}
|