2020-12-31 15:05:51 +03:00
|
|
|
/** Changes the zoom level of the canvas
|
|
|
|
* @param {*} direction 'in' or 'out'
|
|
|
|
* @param {*} cursorLocation The position of the cursor when the user zoomed
|
|
|
|
*/
|
|
|
|
function changeZoom (direction, cursorLocation) {
|
|
|
|
// Computing current width and height
|
2021-07-24 13:37:34 +03:00
|
|
|
let oldWidth = canvasSize[0] * zoom;
|
|
|
|
let oldHeight = canvasSize[1] * zoom;
|
|
|
|
let newWidth, newHeight;
|
|
|
|
let prevZoom = zoom;
|
|
|
|
let zoomed = false;
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
//change zoom level
|
|
|
|
//if you want to zoom out, and the zoom isnt already at the smallest level
|
|
|
|
if (direction == 'out' && zoom > 1) {
|
2021-07-24 13:37:34 +03:00
|
|
|
zoomed = true;
|
2019-03-27 02:20:54 +03:00
|
|
|
zoom -= Math.ceil(zoom / 10);
|
|
|
|
newWidth = canvasSize[0] * zoom;
|
|
|
|
newHeight = canvasSize[1] * zoom;
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
//adjust canvas position
|
2020-12-31 15:05:51 +03:00
|
|
|
layers[0].setCanvasOffset(
|
|
|
|
layers[0].canvas.offsetLeft + (oldWidth - newWidth) * cursorLocation[0]/oldWidth,
|
|
|
|
layers[0].canvas.offsetTop + (oldHeight - newHeight) * cursorLocation[1]/oldWidth);
|
2019-03-27 02:20:54 +03:00
|
|
|
}
|
|
|
|
//if you want to zoom in
|
2021-07-24 13:37:34 +03:00
|
|
|
else if (direction == 'in' && zoom + Math.ceil(zoom/10) < window.innerHeight/4) {
|
|
|
|
zoomed = true;
|
2019-03-27 02:20:54 +03:00
|
|
|
zoom += Math.ceil(zoom/10);
|
|
|
|
newWidth = canvasSize[0] * zoom;
|
|
|
|
newHeight = canvasSize[1] * zoom;
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
//adjust canvas position
|
2020-12-31 15:05:51 +03:00
|
|
|
layers[0].setCanvasOffset(
|
|
|
|
layers[0].canvas.offsetLeft - Math.round((newWidth - oldWidth)*cursorLocation[0]/oldWidth),
|
|
|
|
layers[0].canvas.offsetTop - Math.round((newHeight - oldHeight)*cursorLocation[1]/oldHeight));
|
2019-03-27 02:20:54 +03:00
|
|
|
}
|
2020-04-15 03:01:31 +03:00
|
|
|
|
2019-03-27 02:20:54 +03:00
|
|
|
//resize canvas
|
2020-12-31 15:05:51 +03:00
|
|
|
layers[0].resize();
|
2019-03-27 02:20:54 +03:00
|
|
|
// adjust brush size
|
2020-04-15 03:01:31 +03:00
|
|
|
currentTool.updateCursor();
|
2021-07-24 13:37:34 +03:00
|
|
|
|
|
|
|
// Adjust pixel grid thickness
|
|
|
|
if (zoomed) {
|
|
|
|
if (zoom <= 7)
|
|
|
|
disablePixelGrid();
|
|
|
|
else if (zoom >= 20 && direction == 'in') {
|
|
|
|
enablePixelGrid();
|
|
|
|
repaintPixelGrid(zoom - prevZoom);
|
|
|
|
}
|
|
|
|
else if ((prevZoom >= 20 && direction == 'out')) {
|
|
|
|
enablePixelGrid();
|
|
|
|
repaintPixelGrid(zoom - prevZoom);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
enablePixelGrid();
|
|
|
|
}
|
|
|
|
}
|
2020-04-04 10:41:56 +03:00
|
|
|
}
|