pixel-editor/js/tools/ZoomTool.js

85 lines
3.4 KiB
JavaScript
Raw Normal View History

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
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;
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
if (mode == 'out' && currFile.zoom > MIN_ZOOM_LEVEL) {
2021-11-01 14:31:09 +03:00
zoomed = true;
if (currFile.zoom > 2)
currFile.zoom -= Math.ceil(currFile.zoom / 10);
2021-11-01 14:31:09 +03:00
else
currFile.zoom -= Math.ceil(currFile.zoom * 2 / 10) / 2;
2021-11-01 14:31:09 +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
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
else if (mode == 'in' && currFile.zoom + Math.ceil(currFile.zoom/10) < window.innerHeight/4) {
2021-11-01 14:31:09 +03:00
zoomed = true;
if (currFile.zoom > 2)
currFile.zoom += Math.ceil(currFile.zoom/10);
2021-11-01 14:31:09 +03:00
else {
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 {
currFile.zoom += Math.ceil(currFile.zoom * 2 / 10) / 2;
2021-11-01 14:31:09 +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
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
currFile.layers[0].resize();
2021-11-01 14:31:09 +03:00
// adjust brush size
ToolManager.currentTool().updateCursor();
// Adjust pixel grid thickness
if (zoomed) {
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') {
currFile.pixelGrid.enablePixelGrid();
currFile.pixelGrid.repaintPixelGrid((currFile.zoom - prevZoom) * 0.6);
2021-11-01 14:31:09 +03:00
}
else {
currFile.pixelGrid.enablePixelGrid();
2021-11-01 14:31:09 +03:00
}
}
for (let i=0; i<currFile.layers.length; i++) {
currFile.layers[i].copyData(currFile.layers[0]);
2021-11-01 14:31:09 +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
}
}