Skip to content

Commit

Permalink
Automatic clipExtent determination for mercator.
Browse files Browse the repository at this point in the history
Since 3.1, the Mercator projection does not clamp y-values. For rendering
latitudes near the pole, the y-values can approach infinity. Rather than
clamping each point in the projection, d3.geo.mercator now sets the clipExtent
automatically (if not overridden) using the projection's scale and translate.
  • Loading branch information
mbostock committed Mar 22, 2013
1 parent bcf70c9 commit 91d35b4
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 13 deletions.
2 changes: 1 addition & 1 deletion component.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "d3",
"version": "3.1.2",
"version": "3.1.3",
"main": "./d3.js"
}
30 changes: 27 additions & 3 deletions d3.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
d3 = function() {
var d3 = {
version: "3.1.2"
version: "3.1.3"
};
if (!Date.now) Date.now = function() {
return +new Date();
Expand Down Expand Up @@ -3625,8 +3625,32 @@ d3 = function() {
d3_geo_mercator.invert = function(x, y) {
return [ x, 2 * Math.atan(Math.exp(y)) - π / 2 ];
};
function d3_geo_mercatorProjection(project) {
var m = d3_geo_projection(project), scale = m.scale, translate = m.translate, clipExtent = m.clipExtent, clipAuto;
m.scale = function() {
var v = scale.apply(m, arguments);
return v === m ? clipAuto ? m.clipExtent(null) : m : v;
};
m.translate = function() {
var v = translate.apply(m, arguments);
return v === m ? clipAuto ? m.clipExtent(null) : m : v;
};
m.clipExtent = function(_) {
var v = clipExtent.apply(m, arguments);
if (v === m) {
if (clipAuto = _ == null) {
var k = π * scale(), t = translate();
clipExtent([ [ t[0] - k, t[1] - k ], [ t[0] + k, t[1] + k ] ]);
}
} else if (clipAuto) {
v = null;
}
return v;
};
return m.clipExtent(null);
}
(d3.geo.mercator = function() {
return d3_geo_projection(d3_geo_mercator);
return d3_geo_mercatorProjection(d3_geo_mercator);
}).raw = d3_geo_mercator;
var d3_geo_orthographic = d3_geo_azimuthal(function() {
return 1;
Expand All @@ -3650,7 +3674,7 @@ d3 = function() {
return [ Math.atan2(d3_sinh(x), Math.cos(y)), d3_asin(Math.sin(y) / d3_cosh(x)) ];
};
(d3.geo.transverseMercator = function() {
return d3_geo_projection(d3_geo_transverseMercator);
return d3_geo_mercatorProjection(d3_geo_transverseMercator);
}).raw = d3_geo_transverseMercator;
d3.geom = {};
d3.svg = {};
Expand Down
10 changes: 5 additions & 5 deletions d3.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "d3",
"version": "3.1.2",
"version": "3.1.3",
"description": "A small, free JavaScript library for manipulating documents based on data.",
"keywords": [
"dom",
Expand Down
35 changes: 34 additions & 1 deletion src/geo/mercator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,39 @@ d3_geo_mercator.invert = function(x, y) {
return [x, 2 * Math.atan(Math.exp(y)) - π / 2];
};

function d3_geo_mercatorProjection(project) {
var m = d3_geo_projection(project),
scale = m.scale,
translate = m.translate,
clipExtent = m.clipExtent,
clipAuto;

m.scale = function() {
var v = scale.apply(m, arguments);
return v === m ? (clipAuto ? m.clipExtent(null) : m) : v;
};

m.translate = function() {
var v = translate.apply(m, arguments);
return v === m ? (clipAuto ? m.clipExtent(null) : m) : v;
};

m.clipExtent = function(_) {
var v = clipExtent.apply(m, arguments);
if (v === m) {
if (clipAuto = _ == null) {
var k = π * scale(), t = translate();
clipExtent([[t[0] - k, t[1] - k], [t[0] + k, t[1] + k]]);
}
} else if (clipAuto) {
v = null;
}
return v;
};

return m.clipExtent(null);
}

(d3.geo.mercator = function() {
return d3_geo_projection(d3_geo_mercator);
return d3_geo_mercatorProjection(d3_geo_mercator);
}).raw = d3_geo_mercator;
3 changes: 2 additions & 1 deletion src/geo/transverse-mercator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "../math/trigonometry";
import "geo";
import "mercator";
import "projection";

function d3_geo_transverseMercator(λ, φ) {
Expand All @@ -18,5 +19,5 @@ d3_geo_transverseMercator.invert = function(x, y) {
};

(d3.geo.transverseMercator = function() {
return d3_geo_projection(d3_geo_transverseMercator);
return d3_geo_mercatorProjection(d3_geo_transverseMercator);
}).raw = d3_geo_transverseMercator;
2 changes: 1 addition & 1 deletion src/start.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
d3 = (function(){
var d3 = {version: "3.1.2"}; // semver
var d3 = {version: "3.1.3"}; // semver

0 comments on commit 91d35b4

Please sign in to comment.