Skip to content

Commit 759606b

Browse files
committed
add pan zoom in URL
1 parent b05f3a7 commit 759606b

File tree

2 files changed

+61
-5
lines changed

2 files changed

+61
-5
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ User friendly side menus collapsible and width adjustable with the mouse.
1010

1111
## Features
1212
- Render standard markdown .md with custom Astro components
13-
- Markdown images enhanced with Modal and panzoom function
14-
- URL parameters auto open Modal
15-
- URL parameters text focus move to, zoom, then animate glow text (supports xhtml SVG foreign objects)
13+
- Panzoom and Modal on Markdown images
14+
- URL params auto open Modal
15+
- URL params text focus : move to, zoom, then animate glow text (supports xhtml SVG foreign objects)
16+
- URL params updated with pan/zoom and restored from the URL
1617
- Markdown image directive for centering and image size defintion
1718
- Markdown tables become interactive with data tables with [DataTables](https://datatables.net/)
1819
- from Markdown table
@@ -86,7 +87,6 @@ Express js server in `server\server.js` can optionally be used to serve the gene
8687
- list of dependencies : map, highlight list on key hover
8788
- PanZoom
8889
- URL params, multiple hits counter
89-
- update pan zoom status in url on mouse up
9090
- watch and regenerate .structure on save for modified files only
9191
- check potential replacement of scrollspy with intersection Observer API
9292
- enhance intersection to cover a path of all visible sections from the page in the toc : start heading, stop heading

src/components/panzoom/lib_panzoommodal.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,28 @@ async function cloneAsset(center){
7171
return {is_svg,svg_img}
7272
}
7373

74+
function window_url_add_pan(x,y){
75+
// Convert to integers to remove fractions and ensure the format "&pan=x33_y48"
76+
const intX = Math.floor(x);
77+
const intY = Math.floor(y);
78+
console.log(`Pan finished at (${intX},${intY})`);
79+
80+
const currentUrl = new URL(window.location.href);
81+
currentUrl.searchParams.set('pan', `x${intX}_y${intY}`);
82+
window.history.pushState({}, "", currentUrl.toString());
83+
}
84+
85+
function window_url_add_zoom(zoom){
86+
// Round to two decimal places to ensure the format "&zoom=1.27"
87+
const roundedZoom = Math.round(zoom * 100) / 100;
88+
console.log(`Zoom done at (${roundedZoom})`);
89+
90+
const currentUrl = new URL(window.location.href);
91+
currentUrl.searchParams.set('zoom', roundedZoom.toString());
92+
window.history.pushState({}, "", currentUrl.toString());
93+
}
7494
function window_url_add_modal(center){
75-
const container = center.parentElement.parentElement.parentElement.parentElement
95+
const container = center.parentElement.parentElement.parentElement.parentElement
7696
let modal_name
7797
const data_name = container.getAttribute("data-name")
7898
if(data_name != "diagram.svg"){
@@ -100,12 +120,41 @@ function is_url_modal(center){
100120

101121
async function handle_url_modal(modal,is_svg,svg,pzref){
102122
const params = new URL(location.href).searchParams;
123+
124+
// Handling text focus if applicable
103125
const text = params.get('text')
104126
if(text){
105127
if(is_svg){
106128
await svg_text_focus(modal,svg,text,pzref)
107129
}
108130
}
131+
132+
// Handling pan parameter
133+
const pan = params.get('pan');
134+
if (pan) {
135+
const matches = pan.match(/x(-?\d+)_y(-?\d+)/i); // Adjusted regex to include negative numbers
136+
console.log(matches)
137+
if (matches) {
138+
const x = parseInt(matches[1], 10);
139+
const y = parseInt(matches[2], 10);
140+
setTimeout(()=>{pzref.smoothMoveTo(x, y)}, 400)
141+
console.log(`Moving to x: ${x}, y: ${y}`);
142+
}
143+
}
144+
145+
// Handling zoom parameter
146+
const zoom = params.get('zoom');
147+
if (zoom) {
148+
const scale = parseFloat(zoom);
149+
let delay = 400
150+
if(pan){
151+
delay = 0
152+
}
153+
const svg_cx = svg.getAttribute("width").replace(/px$/, '')/2
154+
const svg_cy = svg.getAttribute("height").replace(/px$/, '')/2
155+
setTimeout(()=>{pzref.smoothZoom(svg_cx, svg_cy, zoom)}, delay)
156+
console.log(`Zooming to scale: ${scale}`);
157+
}
109158
}
110159

111160
async function openModal(event){
@@ -120,6 +169,13 @@ async function openModal(event){
120169
}
121170
const { default: panzoom } = await import('panzoom');
122171
pzref = panzoom(svg_img,zoomOptions)
172+
pzref.on('panend', () => {
173+
const t = pzref.getTransform()
174+
window_url_add_pan(t.x,t.y)
175+
});
176+
pzref.on('zoom', function() {
177+
window_url_add_zoom(pzref.getTransform().scale)
178+
});
123179

124180
close.onclick = ()=>{
125181
//console.log("closed click")

0 commit comments

Comments
 (0)