I am writing a cross platform web application that displays information using SVG. I need to have a multi-touch interface to pan and zoom the SVG graphic. From my research, it appears that Hammer.js is my best approach, and I am using the jQuery library version.
I am able to pan and zoom the image using the viewBox property of the SVG. The problem I'm having is using the center point information during the pinch zoom (vs. scaling from the top left corner). That center point, wherever it is in my image, should remain in the center of the pinch. Clearly, the solution is to modify the first two parts of the viewBox, but I'm having trouble determining the correct values. The center point (event.gesture.center) appears to be in browser page coordinates, so I need to figure out how to reliably scale this and then calculate the viewBox x and y coordinates. I've looked for examples, but couldn't find anything that quite related.
Is using the viewBox attribute the best way to scale SVG images? Are there better alternatives? Does anyone have sample code of zooming an SVG image from the pinch center?
Also, I'm noticing that even though I'm binding the event handling to the SVG element, the pinch gesture seems to work from anywhere on the page. It isn't the default browser handling of the pinch because it is only zooming the SVG image using my code. Here's the code I'm using for binding the pinch event:
$(window.demoData.svgElement).hammer().on("pinch", function (event) {
event.preventDefault();
...
});
Thanks for any help on these two issues.