bumbu / svg-pan-zoom

JavaScript library that enables panning and zooming of an SVG in an HTML document, with mouse events or custom JavaScript hooks
https://github.com/ariutta/svg-pan-zoom#demos
BSD 2-Clause "Simplified" License
1.74k stars 390 forks source link

Is it possible to zoom on a path by ID? #359

Open CristianEstiber opened 4 years ago

CristianEstiber commented 4 years ago

For example you can zoom on coords like panZoom.zoomAtPoint(2, {x: 50, y: 50}), is there a way to tell a path ID and zoom there?

EDIT: also I'm trying to zoom at point sending coordinates but if I moved or zoomed my svg before it doesn't go to the desires coords

EDIT: here is my question on stackoverflow https://stackoverflow.com/questions/59579575/how-to-get-x-y-coordinates-from-path-element-inside-svg

brasofilo commented 3 years ago

OP received a nice answer at SO. Here's the demo: https://jsfiddle.net/04Lg9ruj/

And I adapted their code to this to get the points coordinates:

//Find your root SVG element
var Mapa = document.querySelector('#mapa-mundi');
var SVGDoc = Mapa.getSVGDocument(); 
var svg = SVGDoc.documentElement; 

//Create an SVGPoint for future math
var pt = svg.createSVGPoint();

//Get point in global SVG space
function cursorPoint(evt){
    pt.x = evt.clientX; pt.y = evt.clientY;
    return pt.matrixTransform(svg.getScreenCTM().inverse());
}

svg.addEventListener('click',function(evt){
    var loc = cursorPoint(evt);
    alert(loc.x+" "+loc.y);
},false);
wassfila commented 3 years ago

@CristianEstiber I answered your question on stack overflow, here also a link for the demo and a gif for those just curious to see the end result https://www.homesmartmesh.com/docs/microcontrollers/nrf52/thread_sensortag/?svg=nrf52-sensor-tag&text=VEML6030 Deep-SVG-Link

wassfila commented 3 years ago

@bumbu, if you'd like to take this upstream I'd help to contribute to this nice library, apparently such zoom on id, text with transitions and effect are demanded features. The code is available here https://github.com/WebSVG/hugo-book/blob/7a7d44ea0f7f91a51c15bcb8e8efd294ef29f42f/static/js/setup-svg-pan-zoom.js#L70

bumbu commented 3 years ago

Hey @wassfila Thanks for sharing the examples. It is always great to see that people find this library useful and adapt it to their needs. Right now there's no active development happening to the library, but I'm happy to review and accept well tested pull requests. Looking at your code, probably the best would be to:

For the new API we could add something like focusOn which would take a DOM node or id as first argument, and some options as second. For options the only thing that I could think right now (that is also generic) is how much should we zoom on the desired element. For example if the viewport has 1000px width, and we want to focus on a rectangle that has 100px width, but make it so that it only takes 50% of the viewport, we could specify it as zoomOn('#my-element-id', {scale: 0.5}).

Let me know what are your thoughts, if you think we need a more flexible/strict API, or any other ideas you have. Or just feel free to open a PR and we can continue the discussion there :)

wassfila commented 3 years ago

Thank you @bumbu for the feedback, I understand you, I also have many concepts developed for fun but then we have to move on. I'm a bit intimidated by the toolchain, I managed to build it to fix a bug var passiveOption = {passive: true}; but I'm not that confident, also self learning web and got more used to the modules approach. It's not only about throwing a function but also testing it and documenting it. Your library might be minimal but well packed and clear so maybe those corner cases can stay for those seeking advanced dev and can handle it themselves, if you collect references of all those who used your library that might leverage a lot of reuse between those projects. My environment of choice is hugo markdown and short codes.

If to give a though about the API (just for those it might inspire), I see three options :

https://www.homesmartmesh.com/docs/microcontrollers/nrf52/thread_sensortag/?svg=nrf52-sensor-tag&x=0.5&y=0.5&z=3.4#schematics