|
|
@ -1,6 +1,6 @@ |
|
|
|
import react, { useCallback, useState } from 'react'; |
|
|
|
|
|
|
|
import _ from 'lodash'; |
|
|
|
import _, { constant } from 'lodash'; |
|
|
|
|
|
|
|
import MouseHandler from './mouse-handler'; |
|
|
|
import Layer from './layer'; |
|
|
@ -14,48 +14,82 @@ export interface Point { |
|
|
|
y: number; |
|
|
|
} |
|
|
|
|
|
|
|
export interface Translation { |
|
|
|
translate: Point; |
|
|
|
} |
|
|
|
|
|
|
|
export interface Scale { |
|
|
|
scale: { |
|
|
|
center: Point; |
|
|
|
factor: number; |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
export type Transformation = Translation | Scale; |
|
|
|
|
|
|
|
// const transform1: Transformation = { translate: { x: 0, y: 1 } };
|
|
|
|
// const transform2: Transformation = {
|
|
|
|
// scale: { center: { x: 10, y: 20 }, factor: 2 },
|
|
|
|
// };
|
|
|
|
|
|
|
|
interface ViewportProps { |
|
|
|
children: any; |
|
|
|
} |
|
|
|
|
|
|
|
export interface ViewportState { |
|
|
|
scale: number; |
|
|
|
translation: Point; |
|
|
|
} |
|
|
|
|
|
|
|
const Viewport: react.FC<ViewportProps> = (props: ViewportProps) => { |
|
|
|
//console.log(`--- Rendering viewport, props: ${JSON.stringify(props)} ---`);
|
|
|
|
|
|
|
|
const initialShitf: Point = { x: 0, y: 0 }; |
|
|
|
|
|
|
|
const [shift, setShift] = useState(initialShitf); |
|
|
|
const initialState: ViewportState = { scale: 1, translation: { x: 0, y: 0 } }; |
|
|
|
|
|
|
|
const [zoom, setZoom] = useState(1); |
|
|
|
const [state, setState] = useState(initialState); |
|
|
|
|
|
|
|
const genericHandler = (event: any) => { |
|
|
|
console.log('Log - Event: ' + event.type); |
|
|
|
return; |
|
|
|
}; |
|
|
|
|
|
|
|
// TODO: implement resize event
|
|
|
|
// TODO: check boundaries
|
|
|
|
|
|
|
|
const updateShift = (shiftDelta: Point) => { |
|
|
|
setShift({ x: shift.x + shiftDelta.x, y: shift.y + shiftDelta.y }); |
|
|
|
}; |
|
|
|
|
|
|
|
const updateZoom = (zoomFactor: number, center: Point) => { |
|
|
|
const newZoom = zoom * zoomFactor; |
|
|
|
updateShift({ |
|
|
|
x: (shift.x - center.x) * (zoomFactor - 1), |
|
|
|
y: (shift.y - center.y) * (zoomFactor - 1), |
|
|
|
}); |
|
|
|
setZoom(newZoom); |
|
|
|
const applyTransformations = (transformations: Transformation[]) => { |
|
|
|
const newState = transformations.reduce( |
|
|
|
(previousState: ViewportState, transformation): ViewportState => { |
|
|
|
if ('scale' in transformation) { |
|
|
|
return { |
|
|
|
scale: previousState.scale * transformation.scale.factor, |
|
|
|
translation: { |
|
|
|
x: |
|
|
|
previousState.translation.x + |
|
|
|
(previousState.translation.x - transformation.scale.center.x) * |
|
|
|
(transformation.scale.factor - 1), |
|
|
|
y: |
|
|
|
previousState.translation.y + |
|
|
|
(previousState.translation.y - transformation.scale.center.y) * |
|
|
|
(transformation.scale.factor - 1), |
|
|
|
}, |
|
|
|
}; |
|
|
|
} |
|
|
|
return { |
|
|
|
scale: previousState.scale, |
|
|
|
translation: { |
|
|
|
x: previousState.translation.x + transformation.translate.x, |
|
|
|
y: previousState.translation.y + transformation.translate.y, |
|
|
|
}, |
|
|
|
}; |
|
|
|
}, |
|
|
|
state |
|
|
|
); |
|
|
|
setState(newState); |
|
|
|
}; |
|
|
|
|
|
|
|
return ( |
|
|
|
<div className='viewport'> |
|
|
|
<MouseHandler updateShift={updateShift} updateZoom={updateZoom}> |
|
|
|
<SingleTouchHandler updateShift={updateShift}> |
|
|
|
<DoubleTouchHandler updateZoom={updateZoom} updateShift={updateShift}> |
|
|
|
<Layer shift={shift} zoom={zoom}> |
|
|
|
{props.children} |
|
|
|
</Layer> |
|
|
|
<MouseHandler applyTransformations={applyTransformations}> |
|
|
|
<SingleTouchHandler applyTransformations={applyTransformations}> |
|
|
|
<DoubleTouchHandler applyTransformations={applyTransformations}> |
|
|
|
<Layer viewportState={state}>{props.children}</Layer> |
|
|
|
</DoubleTouchHandler> |
|
|
|
</SingleTouchHandler> |
|
|
|
</MouseHandler> |
|
|
|