sandbox/background-move/src/components/viewport.tsx

67 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-09-08 16:42:33 +00:00
import react, { useCallback, useState } from 'react';
2022-09-07 21:19:42 +00:00
import _ from 'lodash';
2022-09-09 18:22:20 +00:00
import MouseHandler from './mouse-handler';
import Layer from './layer';
2022-09-07 21:19:42 +00:00
import '../theme/viewport.css';
2022-09-09 18:22:20 +00:00
import SingleTouchHandler from './single-touch-handler';
import DoubleTouchHandler from './double-touch-handler';
export interface Point {
x: number;
y: number;
}
2022-09-07 21:19:42 +00:00
const Viewport: react.FC<{}> = (props: {}) => {
2022-09-08 13:00:36 +00:00
console.log(`--- Rendering viewport, props: ${JSON.stringify(props)} ---`);
const initialShitf: Point = { x: 0, y: 0 };
const [shift, setShift] = useState(initialShitf);
2022-09-07 21:19:42 +00:00
const [zoom, setZoom] = useState(1);
2022-09-07 21:19:42 +00:00
const genericHandler = (event: any) => {
console.log('Log - Event: ' + event.type);
return;
2022-09-07 21:19:42 +00:00
};
2022-09-08 13:35:39 +00:00
// TODO: implement resize event
2022-09-08 13:00:36 +00:00
// TODO: check boundaries
const updateShift = (shiftDelta: Point) => {
setShift({ x: shift.x + shiftDelta.x, y: shift.y + shiftDelta.y });
2022-09-07 21:19:42 +00:00
};
const updateZoom = (zoomFactor: number, center: Point) => {
const newZoom = zoom * zoomFactor;
setZoom(newZoom);
setShift({
x: shift.x + center.x / zoom - center.x / newZoom,
y: shift.y + center.y / zoom - center.y / newZoom,
});
// setShift({
// x: shift.x + 1440 / 2 / zoom - window.innerWidth / 2 / newZoom,
// y: shift.y + 2904 / 2 / zoom - window.innerHeight / 2 / newZoom,
// });
};
2022-09-07 21:19:42 +00:00
return (
<div className='viewport'>
2022-09-09 18:22:20 +00:00
<MouseHandler updateShift={updateShift}>
<SingleTouchHandler updateShift={updateShift}>
<DoubleTouchHandler updateZoom={updateZoom}>
<Layer shift={shift} zoom={zoom}>
<img src='/assets/background.jpg' alt='' />
</Layer>
2022-09-09 18:22:20 +00:00
</DoubleTouchHandler>
</SingleTouchHandler>
</MouseHandler>
2022-09-07 21:19:42 +00:00
</div>
);
};
export default Viewport;