Skip to content

Commit

Permalink
Fixes for non-root maps.
Browse files Browse the repository at this point in the history
If the map's container is not a root SVGSSVGElement, then we need to apply the
inverse transform to convert the mouse location to view coordinates. Also, set
the size of the child rect to be the actual size of the map, rather than 100% of
the window width and height.
  • Loading branch information
Mike Bostock committed Aug 9, 2010
1 parent a7471fb commit f17acbe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
33 changes: 17 additions & 16 deletions src/Map.js
Expand Up @@ -94,21 +94,8 @@ po.map = function() {
center.lat = Math.max(-l, Math.min(+l, center.lat));
}

/*
* In Firefox, SVG elements do not report their dimensions correctly. However,
* Firefox does correctly report the bounding box of the first child rect!
*/
function bounds() {
var svg = container.ownerSVGElement || container;
return (typeof svg.offsetWidth == "undefined"
? svg.firstChild
: svg).getBoundingClientRect();
}

// a place to capture mouse events if no tiles exist
var rect = po.svg("rect");
rect.setAttribute("width", "100%");
rect.setAttribute("height", "100%");
rect.setAttribute("visibility", "hidden");
rect.setAttribute("pointer-events", "all");

Expand All @@ -128,8 +115,10 @@ po.map = function() {
};

map.mouse = function(e) {
var x = e.clientX, y = e.clientY, b = bounds();
return {x: x - b.left, y: y - b.top};
var point = (container.ownerSVGElement || container).createSVGPoint();
point.x = e.clientX;
point.y = e.clientY;
return point.matrixTransform(container.getScreenCTM().inverse());
};

map.size = function(x) {
Expand All @@ -140,13 +129,25 @@ po.map = function() {

map.resize = function() {
if (!size) {
var b = bounds();
/*
* Firefox does not correctly report the dimensions of SVG elements.
* However, it does correctly report the size of the child rect!
*/
var e = container.ownerSVGElement || container;
if (e.offsetWidth == null) {
rect.setAttribute("width", "100%");
rect.setAttribute("height", "100%");
e = rect;
}
b = e.getBoundingClientRect();
sizeActual = {x: b.width, y: b.height};
resizer.add(map);
} else {
sizeActual = size;
resizer.remove(map);
}
rect.setAttribute("width", sizeActual.x);
rect.setAttribute("height", sizeActual.y);
sizeRadius = {x: sizeActual.x / 2, y: sizeActual.y / 2};
recenter();
event({type: "resize"});
Expand Down
2 changes: 1 addition & 1 deletion src/start.js
Expand Up @@ -2,6 +2,6 @@ if (!org) var org = {};
if (!org.polymaps) org.polymaps = {};
(function(po){

po.version = "1.4.3"; // semver.org
po.version = "1.4.4"; // semver.org

var zero = {x: 0, y: 0};

0 comments on commit f17acbe

Please sign in to comment.