Skip to content

Instantly share code, notes, and snippets.

@mbostock
Created September 29, 2012 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbostock/3804779 to your computer and use it in GitHub Desktop.
Save mbostock/3804779 to your computer and use it in GitHub Desktop.
Projection Contexts
projection.line = function(coordinates, context) {
if (!(n = coordinates.length)) return;
context = radiansAndRotateContext(projectAndTransformContext(context));
var i = 0,
n = coordinates.length,
p = coordinates[0];
context.moveTo(p[0], p[1]);
while (++i < n) context.lineTo((p = coordinates[i])[0], p[1]);
};
function radiansAndRotateContext(context) {
return {
moveTo: function() {
var p = arguments;
radians(p);
rotate(p);
context.moveTo(p[0], p[1]);
},
lineTo: function() {
var p = arguments;
radians(p);
rotate(p);
context.lineTo(p[0], p[1]);
},
closePath: function() {
context.closePath();
}
};
}
function intersectAntemeridian(context) {
var λ0, φ0, sλ0;
return {
moveTo: function(λ, φ) {
sλ0 = λ > 0 ? π : -π;
context.moveTo(λ0 = λ, φ0 = φ);
},
lineTo: function(λ, φ) {
var = λ > 0 ? π : -π;
if (sλ0 !== && Math.abs(λ - λ0) >= π) {
context.lineTo(sλ0, φ0 = d3_geo_projectionIntersectAntemeridian(λ0, φ0, λ, φ));
context.moveTo(sλ0 = , φ0);
}
context.lineTo(λ0 = λ, φ0 = φ);
},
closePath: function() {
context.closePath();
}
};
}
function projectAndTransformContext(context) {
return {
moveTo: function() {
var p = arguments;
project(p);
transform(p);
context.moveTo(p[0], p[1]);
},
lineTo: function() {
var p = arguments;
project(p);
transform(p);
context.lineTo(p[0], p[1]);
},
closePath: function() {
context.closePath();
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment