dyomedea/src/store/slippy.ts

42 lines
1.2 KiB
TypeScript

import { createSlice } from '@reduxjs/toolkit';
import { Point } from '../components/slippy/slippy';
export interface SlippyState {
scale: number;
translation: Point;
}
const initialSlippyState: SlippyState = {
scale: 1,
translation: { x: 0, y: 0 },
};
const slippySlice = createSlice({
name: 'slippy',
initialState: initialSlippyState,
reducers: {
scale(state, action) {
console.log(`redux scale: ${JSON.stringify(action.payload)}`);
state.scale = state.scale * action.payload.factor;
state.translation.x =
state.translation.x +
(state.translation.x - action.payload.center.x) *
(action.payload.factor - 1);
state.translation.y =
state.translation.y +
(state.translation.y - action.payload.center.y) *
(action.payload.factor - 1);
},
translate(state, action) {
console.log(`redux translate: action=${JSON.stringify(action)}`);
state.translation.x = state.translation.x + action.payload.x;
state.translation.y = state.translation.y + action.payload.y;
},
},
});
export const slippyActions = slippySlice.actions;
export default slippySlice.reducer;