Doodle3D-Core/src/reducer/d2/wheelZoomReducer.js

28 lines
884 B
JavaScript
Raw Normal View History

2017-11-14 15:27:48 +01:00
import update from 'react-addons-update';
2021-05-20 04:16:53 +02:00
import { Matrix } from '@doodle3d/cal';
2017-11-14 15:27:48 +01:00
import constrainMatrix from './constrainMatrix.js';
import { MAX_ZOOM } from '../../constants/d2Constants.js';
export default function d2WheelZoomReducer(state, action) {
const { position, wheelDelta, screenMatrixContainer } = action;
2018-05-09 11:29:00 +02:00
const { d2: { canvasMatrix }, disableScroll } = state;
if (disableScroll) return state;
2017-11-14 15:27:48 +01:00
const targetScale = 1 + wheelDelta / -500;
if (canvasMatrix.sx === MAX_ZOOM && targetScale > 1) return state;
const rotateAround = position.applyMatrix(screenMatrixContainer.inverseMatrix());
const scaleMatrix = new Matrix().scaleAroundAbsolute(targetScale, targetScale, rotateAround);
const matrix = canvasMatrix.multiplyMatrix(scaleMatrix);
constrainMatrix(matrix);
return update(state, {
d2: {
canvasMatrix: { $set: matrix }
}
});
}