2021-11-01 14:31:09 +03:00
|
|
|
class ZoomTool extends Tool {
|
|
|
|
constructor (name, options) {
|
|
|
|
super(name, options, undefined);
|
|
|
|
}
|
|
|
|
onMouseWheel(mousePos, mode) {
|
|
|
|
super.onMouseWheel(mousePos, mode);
|
|
|
|
|
|
|
|
// Computing current width and height
|
2021-12-06 19:37:43 +03:00
|
|
|
let oldWidth = currFile.canvasSize[0] * currFile.zoom;
|
|
|
|
let oldHeight = currFile.canvasSize[1] * currFile.zoom;
|
2021-11-01 14:31:09 +03:00
|
|
|
let newWidth, newHeight;
|
2021-12-06 19:37:43 +03:00
|
|
|
let prevZoom = currFile.zoom;
|
2021-11-01 14:31:09 +03:00
|
|
|
let zoomed = false;
|
|
|
|
|
|
|
|
//change zoom level
|
|
|
|
//if you want to zoom out, and the zoom isnt already at the smallest level
|
2021-12-06 19:37:43 +03:00
|
|
|
if (mode == 'out' && currFile.zoom > MIN_ZOOM_LEVEL) {
|
2021-11-01 14:31:09 +03:00
|
|
|
zoomed = true;
|
2021-12-06 19:37:43 +03:00
|
|
|
if (currFile.zoom > 2)
|
|
|
|
currFile.zoom -= Math.ceil(currFile.zoom / 10);
|
2021-11-01 14:31:09 +03:00
|
|
|
else
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.zoom -= Math.ceil(currFile.zoom * 2 / 10) / 2;
|
2021-11-01 14:31:09 +03:00
|
|
|
|
2021-12-06 19:37:43 +03:00
|
|
|
newWidth = currFile.canvasSize[0] * currFile.zoom;
|
|
|
|
newHeight = currFile.canvasSize[1] * currFile.zoom;
|
2021-11-01 14:31:09 +03:00
|
|
|
|
|
|
|
//adjust canvas position
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.layers[0].setCanvasOffset(
|
|
|
|
currFile.layers[0].canvas.offsetLeft + (oldWidth - newWidth) * mousePos[0]/oldWidth,
|
|
|
|
currFile.layers[0].canvas.offsetTop + (oldHeight - newHeight) * mousePos[1]/oldHeight);
|
2021-11-01 14:31:09 +03:00
|
|
|
}
|
|
|
|
//if you want to zoom in
|
2021-12-06 19:37:43 +03:00
|
|
|
else if (mode == 'in' && currFile.zoom + Math.ceil(currFile.zoom/10) < window.innerHeight/4) {
|
2021-11-01 14:31:09 +03:00
|
|
|
zoomed = true;
|
2021-12-06 19:37:43 +03:00
|
|
|
if (currFile.zoom > 2)
|
|
|
|
currFile.zoom += Math.ceil(currFile.zoom/10);
|
2021-11-01 14:31:09 +03:00
|
|
|
else {
|
2021-12-06 19:37:43 +03:00
|
|
|
if (currFile.zoom + currFile.zoom/10 > 2) {
|
|
|
|
currFile.zoom += Math.ceil(currFile.zoom/10);
|
|
|
|
currFile.zoom = Math.ceil(currFile.zoom);
|
2021-11-01 14:31:09 +03:00
|
|
|
}
|
|
|
|
else {
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.zoom += Math.ceil(currFile.zoom * 2 / 10) / 2;
|
2021-11-01 14:31:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 19:37:43 +03:00
|
|
|
newWidth = currFile.canvasSize[0] * currFile.zoom;
|
|
|
|
newHeight = currFile.canvasSize[1] * currFile.zoom;
|
2021-11-01 14:31:09 +03:00
|
|
|
|
|
|
|
//adjust canvas position
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.layers[0].setCanvasOffset(
|
|
|
|
currFile.layers[0].canvas.offsetLeft - Math.round((newWidth - oldWidth)*mousePos[0]/oldWidth),
|
|
|
|
currFile.layers[0].canvas.offsetTop - Math.round((newHeight - oldHeight)*mousePos[1]/oldHeight));
|
2021-11-01 14:31:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//resize canvas
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.layers[0].resize();
|
2021-11-01 14:31:09 +03:00
|
|
|
// adjust brush size
|
|
|
|
ToolManager.currentTool().updateCursor();
|
|
|
|
|
|
|
|
// Adjust pixel grid thickness
|
|
|
|
if (zoomed) {
|
2021-12-06 19:37:43 +03:00
|
|
|
if (currFile.zoom <= 7)
|
|
|
|
currFile.pixelGrid.disablePixelGrid();
|
|
|
|
else if (currFile.zoom >= 20 && mode == 'in') {
|
|
|
|
currFile.pixelGrid.enablePixelGrid();
|
|
|
|
currFile.pixelGrid.repaintPixelGrid((currFile.zoom - prevZoom) * 0.6);
|
2021-11-01 14:31:09 +03:00
|
|
|
}
|
|
|
|
else if (prevZoom >= 20 && mode == 'out') {
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.pixelGrid.enablePixelGrid();
|
|
|
|
currFile.pixelGrid.repaintPixelGrid((currFile.zoom - prevZoom) * 0.6);
|
2021-11-01 14:31:09 +03:00
|
|
|
}
|
|
|
|
else {
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.pixelGrid.enablePixelGrid();
|
2021-11-01 14:31:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-23 19:16:23 +03:00
|
|
|
for (let i=0; i<currFile.layers.length; i++) {
|
2021-12-06 19:37:43 +03:00
|
|
|
currFile.layers[i].copyData(currFile.layers[0]);
|
2021-11-01 14:31:09 +03:00
|
|
|
}
|
2022-02-23 19:16:23 +03:00
|
|
|
for (let i=0; i<currFile.sublayers.length; i++) {
|
|
|
|
currFile.sublayers[i].copyData(currFile.layers[0]);
|
|
|
|
}
|
2021-11-01 14:31:09 +03:00
|
|
|
}
|
|
|
|
}
|