From dfc4531bfae224d6ed33ff577f24befdafa107bd Mon Sep 17 00:00:00 2001 From: evlist Date: Thu, 13 Oct 2022 14:39:03 +0200 Subject: [PATCH] Moving stuff around. --- svgmap/src/components/map/Map.css | 6 +++ svgmap/src/components/map/Map.tsx | 26 ++++++++++- svgmap/src/components/map/MouseHandler.tsx | 50 ++++++++++------------ svgmap/src/components/map/Viewport.tsx | 47 +++----------------- 4 files changed, 58 insertions(+), 71 deletions(-) diff --git a/svgmap/src/components/map/Map.css b/svgmap/src/components/map/Map.css index 34812f7..d961862 100644 --- a/svgmap/src/components/map/Map.css +++ b/svgmap/src/components/map/Map.css @@ -1,3 +1,9 @@ .map { overflow: hidden; } + +.handler { + position: fixed; + height: 100%; + width: 100%; +} diff --git a/svgmap/src/components/map/Map.tsx b/svgmap/src/components/map/Map.tsx index ba68acf..fea65e2 100644 --- a/svgmap/src/components/map/Map.tsx +++ b/svgmap/src/components/map/Map.tsx @@ -1,4 +1,4 @@ -import { IonContent, IonApp } from '@ionic/react'; +import { IonContent } from '@ionic/react'; import react, { useState } from 'react'; import './Map.css'; @@ -13,13 +13,35 @@ interface MapProperties { const Map: react.FC = (props: MapProperties) => { const boardSize = Math.max(props.width, props.height) * 2; + const [shift, setShift] = useState({ x: 0, y: 0 }); + const [zoom, setZoom] = useState(1); + + const addShift = (deltaShift: { x: number; y: number }) => { + setShift({ x: shift.x + deltaShift.x, y: shift.y + deltaShift.y }); + }; + + const addZoom = (zoomFactor: number, center: { x: number; y: number }) => { + setShift({ + x: (shift.x - center.x) * (zoomFactor - 1), + y: (shift.y - center.y) * (zoomFactor - 1), + }); + setZoom(zoom * zoomFactor); + }; + return (
- + +
); diff --git a/svgmap/src/components/map/MouseHandler.tsx b/svgmap/src/components/map/MouseHandler.tsx index 32ab746..e5b210e 100644 --- a/svgmap/src/components/map/MouseHandler.tsx +++ b/svgmap/src/components/map/MouseHandler.tsx @@ -1,10 +1,17 @@ import react, { useState } from 'react'; -import Viewport from './Viewport'; +import './Map.css'; + +interface Point { + x: number; + y: number; +} interface MouseHandlerProperties { boardSize: number; - shift: { x: number; y: number }; + shift: Point; + addShift: (shift: Point) => void; zoom: number; + addZoom: (zoomFactor: number, center: Point) => void; } const MouseHandler: react.FC = ( @@ -18,12 +25,12 @@ const MouseHandler: react.FC = ( const [mouseState, setMouseState] = useState(initialMouseState); - const [shift, setShift] = useState(props.shift); - const [zoom, setZoom] = useState(props.zoom); - console.log('MouseHandler, mouseState: ' + JSON.stringify(mouseState)); console.log( - 'MouseHandler, shift: ' + JSON.stringify(shift) + ', zoom:' + zoom + 'MouseHandler, shift: ' + + JSON.stringify(props.shift) + + ', zoom:' + + props.zoom ); const genericHandler = (event: any) => { @@ -69,9 +76,9 @@ const MouseHandler: react.FC = ( y: event.pageY - mouseState.starting.y, })}` ); - setShift({ - x: shift.x + (event.pageX - mouseState.starting.x), - y: shift.y + (event.pageY - mouseState.starting.y), + props.addShift({ + x: event.pageX - mouseState.starting.x, + y: event.pageY - mouseState.starting.y, }); setMouseState({ down: true, @@ -84,17 +91,9 @@ const MouseHandler: react.FC = ( } }; - const doScale = (x: number, y: number, factor: number) => { - setShift({ - x: shift.x + (shift.x - x) * (factor - 1), - y: shift.y + (shift.y - y) * (factor - 1), - }); - setZoom(zoom * factor); - }; - const doubleClickHandler = (event: any) => { genericHandler(event); - doScale(event.pageX, event.pageY, Math.SQRT2); + props.addZoom(Math.SQRT2, { x: event.pageX, y: event.pageY }); }; const initialWheelState = { @@ -109,11 +108,10 @@ const MouseHandler: react.FC = ( event.deltaMode === WheelEvent.DOM_DELTA_PIXEL && Date.now() - wheelState.timestamp > 100 ) { - doScale( - event.pageX, - event.pageY, - event.deltaY < 0 ? Math.SQRT2 : Math.SQRT1_2 - ); + props.addZoom(event.deltaY < 0 ? Math.SQRT2 : Math.SQRT1_2, { + x: event.pageX, + y: event.pageY, + }); setWheelState({ timestamp: Date.now(), @@ -122,16 +120,14 @@ const MouseHandler: react.FC = ( }; return ( -
- -
+ > ); }; diff --git a/svgmap/src/components/map/Viewport.tsx b/svgmap/src/components/map/Viewport.tsx index 9310aa4..a8806e0 100644 --- a/svgmap/src/components/map/Viewport.tsx +++ b/svgmap/src/components/map/Viewport.tsx @@ -1,4 +1,5 @@ import react from 'react'; +import TiledLayer from './TiledLayer'; interface ViewportProperties { boardSize: number; @@ -6,55 +7,17 @@ interface ViewportProperties { zoom: number; } -/** - * - * Let's call: - * - x and y the SVG coordinates - * - X and Y the screen coordinates (pixels on screen) - * - * X0 = (x + shift.x * zoom) * zoom - * or - * x = X0 * zoom - shift.x - * id for Y - * - * To add a new shift of S screen pixels, we need to apply a zoom of S/zoom - * - * How can we zoom so that X and x stay constant ? - * - * Knowing X0, x0, zoom0, zoom1 and shift.x0, - * - * - * X0 = (x0 + shift.x0 *zoom0) * zoom0 - * X1 = (x1 + shift.x1 *zoom1) * zoom1 - * X0 = X1 (=X) - * x0 = x1 (=x) - * => - * (x + shift.x1*zoom1) * zoom1 = (x + shift.x0 * zoom0) * zoom0 - * => - * (x + shift.x1*zoom1) = (x + shift.x0*zoom0) * zoom0 / zoom1 - * shift.x1 = ((x + shift.x0) * zoom0 / zoom1 - x) / zoom1 - * - * x = 333 - * 282 => -25,5 - * - */ - -/** - * - * viewBox={`${-props.shift.x} ${-props.shift.y} ${ - props.boardSize / props.zoom - } ${props.boardSize / props.zoom}`} - - * - */ - const Viewport: react.FC = (props: ViewportProperties) => { return ( + + + + );