Skip to content

Instantly share code, notes, and snippets.

@mbostock
Created December 19, 2012 01:12
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/4333610 to your computer and use it in GitHub Desktop.
Save mbostock/4333610 to your computer and use it in GitHub Desktop.
Finite State Stream
function d3_geo_pathContext(context) {
var pointRadius = 4.5,
state = 0;
// state bit 0: not-first point?
// state bit 1: inside line?
// state bit 2: inside polygon?
function point(x, y) {
if (state) {
if (state & 1) {
context.lineTo(x, y);
} else {
context.moveTo(x, y);
state |= 1;
}
} else {
context.moveTo(x, y);
context.arc(x, y, pointRadius, 0, 2 * π);
}
}
return {
point: point,
lineStart: function() { state |= 2; },
lineEnd: function() { state &= ~3; if (state) context.closePath(); },
polygonStart: function() { state |= 4; },
polygonEnd: function() { state &= ~4; },
pointRadius: function(_) { pointRadius = _; return this; },
result: d3_noop
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment