Skip to content

Commit 67421b6

Browse files
committed
testing zoom and pan
1 parent 57984a8 commit 67421b6

2 files changed

Lines changed: 52 additions & 6 deletions

File tree

devconnect-app/src/app/worlds-fair/venue-map/VenueMap2.tsx

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ const MapPane = (props: {
2828

2929
const element = selection ? elementLookup[selection] : null;
3030

31-
console.log(element, 'SELECTION');
32-
3331
return (
3432
<div
3533
className={cn(
@@ -56,7 +54,7 @@ export const VenueMap = () => {
5654

5755
const isHoveredOrSelected = hoveredElement || selectedElement;
5856

59-
const panzoomInstance = usePanzoom('venue-map');
57+
const { panzoomInstance, panAndZoomLevels } = usePanzoom('venue-map');
6058

6159
useEffect(() => {
6260
// Wait for next frame to ensure SVG is fully rendered
@@ -130,10 +128,46 @@ export const VenueMap = () => {
130128
id: string,
131129
event: React.MouseEvent<SVGElement>
132130
) => {
133-
console.log('SVG element clicked:', id);
131+
// console.log('SVG element clicked:', id);
134132
if (elementLookup[id]) {
135133
setSelectedElement(selectedElement === id ? null : id);
136-
console.log('Element position data:', elementLookup[id]);
134+
135+
// Center on and zoom to the selected element
136+
if (panzoomInstance) {
137+
const targetZoom = 2; // Fixed zoom level
138+
const element = elementLookup[id];
139+
140+
// Get container dimensions
141+
const container = document.getElementById('venue-map');
142+
if (container) {
143+
const containerRect = container.getBoundingClientRect();
144+
145+
// Calculate the pan position to center the element
146+
// We need to account for the current zoom level and calculate where to pan
147+
const centerX = containerRect.width / 2;
148+
const centerY = containerRect.height / 2;
149+
150+
// Calculate the offset needed to center the element
151+
// const panX = centerX - element.centerX * targetZoom;
152+
// const panY = centerY - element.centerY * targetZoom;
153+
154+
// First reset zoom to target level, then pan to center
155+
panzoomInstance.smoothZoomAbs(centerX, centerY, targetZoom);
156+
// panzoomInstance.smoothMoveTo(centerX, centerY);
157+
158+
// console.log(panzoomInstance, 'panzoom instance');
159+
console.log(element, 'element');
160+
console.log(panAndZoomLevels, 'pan and zoom levels');
161+
162+
// After zoom completes, pan to center the element
163+
// setTimeout(() => {
164+
// panzoomInstance.moveTo(panX, panY);
165+
// }, 300); // Adjust timing based on your zoom animation duration
166+
}
167+
}
168+
169+
// console.log(elementLookup[id], 'element lookup');
170+
// console.log('Element position data:', elementLookup[id]);
137171
}
138172
};
139173

devconnect-app/src/app/worlds-fair/venue-map/panzoom.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ export const PanzoomControls = (props: { pz: PanZoom | null }) => {
4848
};
4949

5050
export const usePanzoom = (elementId: string) => {
51+
const [panAndZoomLevels, setPanAndZoomLevels] = React.useState<{
52+
x: number;
53+
y: number;
54+
scale: number;
55+
}>({ x: 0, y: 0, scale: 1 });
5156
const [panzoomInstance, setPanzoomInstance] = React.useState<PanZoom | null>(
5257
null
5358
);
@@ -63,6 +68,7 @@ export const usePanzoom = (elementId: string) => {
6368
boundsPadding: 0.5,
6469
// maxZoom: 2.5,
6570
minZoom: 0.5,
71+
// transformOrigin: { x: 0.5, y: 0.5 },
6672
beforeWheel: function (e) {
6773
// allow wheel-zoom only if altKey is down. Otherwise - ignore
6874
var shouldIgnore = !e.altKey && !e.ctrlKey;
@@ -75,6 +81,12 @@ export const usePanzoom = (elementId: string) => {
7581
// },
7682
});
7783

84+
panzoomInstance.on('zoom', (e: any) => {
85+
const zoomLevels = e.getTransform();
86+
87+
setPanAndZoomLevels(zoomLevels);
88+
});
89+
7890
// Prevent double-click zoom by intercepting double-click events
7991
scene.addEventListener('dblclick', (e) => {
8092
e.preventDefault();
@@ -90,7 +102,7 @@ export const usePanzoom = (elementId: string) => {
90102
}
91103
}, [elementId]);
92104

93-
return panzoomInstance;
105+
return { panzoomInstance, panAndZoomLevels };
94106
};
95107

96108
// export const Venue = (props: Props) => {

0 commit comments

Comments
 (0)