I have an svg image of a map that i have to scale up to make it zoom in. Javascript has a function to scale up SVG images. However the svg scale function uses the upper left corner as center when zooming.
So to counter this the usual trick is to shift the svg 50% to the left and 50% up so that the center moves to the upper left corner. Then scale the image and again move the image back.
Edit: I think this article might say what i need to do but i do not know enought about matrices: http://www.cs.rit.edu/~icss571/clipTrans/2DTransBack.html#BACK3.0
Edit3: Here is some more information about the problem: http://commons.oreilly.com/wiki/index.php/SVG_Essentials/Transforming_the_Coordinate_System#svgess-CHP-5-FIG-9
The logic behind it looks like this.
var bbox = mapGroup.getBBox(); // the element of the svg image i want to scale up/down.
// finding center of element
var cx = bbox.x + (bbox.width/2); // x is the offset of the element horizontally
var cy = bbox.y + (bbox.height/2); // y is the offset of the element vertically
// Shift the image so that the middle is in the upper left corner taking into account the amout of scaling;
mapGroup.attr('transform', 'translate(-' +(cx-(scale*cx)) + ', -' + (cy-(scale*cy)) + ')');
// scale the image
mapScaleGroup.attr('transform', 'scale('+ scale +')');
// Shift the image back again
mapGroup.attr('transform', 'translate(' + (cx-(scale*cx)) + ', ' + (cy-(scale*cy)) + ')');
This Works perfectly when i only want to hit the center of the image, however i have added a drag functionality that causes the offset to change. This causes problems as you can see here: http://nho-municipality-map.divshot.io/ Zoom to see how it should be or drag the map and then zoom to see the mistake.
So I think the key to the problem lies ether in this part: cx = bbox.x + (bbox.width/2) or this part (cx-(scale*cx))
How do i take the custom offset into account?
Edit2: Here is all the information i get from the bbox.
cx-(scale*cx)bycxandcy-(scale*cy)bycythroughout (i.e., both before and after scaling), does the code do what you expect? – Andrew D. Hwang Jul 05 '15 at 15:22cxandcyrepresent? Are they coordinates of the center of the image with the origin $(0, 0)$ at upper left? That is, arebbox.xandbbox.yequal to $(0, 0)$ before the user does any dragging, and are they not $(0, 0)$ after the user drags the map? Or doesbboxrefer to an SVG element the user has clicked? (Or is their meaning something else...?) – Andrew D. Hwang Jul 05 '15 at 17:26