diff --git a/src/geo/stream-buffer.js b/src/geo/stream-buffer.js index 8c95f61e..fd63826e 100644 --- a/src/geo/stream-buffer.js +++ b/src/geo/stream-buffer.js @@ -1,43 +1,38 @@ -function d3_geo_streamBuffer() { - this._buffer = []; - this._point = d3_geo_pathCircle(4.5); -} +function d3_geo_streamBuffer(buffer, pointRadius) { + var stream = { + point: point, -d3_geo_streamBuffer.prototype = { - point: d3_geo_streamBufferPoint, + // While inside a line, override point to moveTo then lineTo. + lineStart: function() { stream.point = pointLineStart; }, + lineEnd: lineEnd, - // While inside a line, override point to moveTo then lineTo. - lineStart: function() { this.point = d3_geo_streamBufferPointLineStart; }, - lineEnd: d3_geo_streamBufferLineEnd, + // While inside a polygon, override lineEnd to closePath. + polygonStart: function() { stream.lineEnd = lineEndPolygon; }, + polygonEnd: function() { stream.lineEnd = lineEnd; } + }; - // While inside a polygon, override lineEnd to closePath. - polygonStart: function() { this.lineEnd = d3_geo_streamBufferLineEndPolygon; }, - polygonEnd: function() { this.lineEnd = d3_geo_streamBufferLineEnd; }, - - toString: function() { - var s = this._buffer.join(""); - this._buffer = []; - return s; + function point(x, y) { + buffer.push("M", x, ",", y, pointRadius); } -}; -function d3_geo_streamBufferPoint(x, y) { - this._buffer.push("M", x, ",", y, this._point); -} + function pointLineStart(x, y) { + buffer.push("M", x, ",", y); + stream.point = pointLine; + } -function d3_geo_streamBufferPointLineStart(x, y) { - this._buffer.push("M", x, ",", y); - this.point = d3_geo_streamBufferPointLine; -} + function pointLine(x, y) { + buffer.push("L", x, ",", y); + } -function d3_geo_streamBufferPointLine(x, y) { - this._buffer.push("L", x, ",", y); -} + function lineEnd() { + stream.point = point; + } -function d3_geo_streamBufferLineEnd() { - this.point = d3_geo_streamBufferPoint; -} + function lineEndPolygon() { + buffer.push("Z"); + } -function d3_geo_streamBufferLineEndPolygon() { - this._buffer.push("Z"); + pointRadius = d3_geo_pathCircle(4.5); + + return stream; } diff --git a/src/geo/stream-context.js b/src/geo/stream-context.js index 5b6aeb5a..472c446d 100644 --- a/src/geo/stream-context.js +++ b/src/geo/stream-context.js @@ -1,37 +1,37 @@ -function d3_geo_streamContext() { - this._pointRadius = 4.5; -} - -d3_geo_streamContext.prototype = { - point: d3_geo_streamContextPoint, - - // While inside a line, override point to moveTo then lineTo. - lineStart: function() { this.point = d3_geo_streamContextPointLineStart; }, - lineEnd: d3_geo_streamContextLineEnd, - - // While inside a polygon, override lineEnd to closePath. - polygonStart: function() { this.lineEnd = d3_geo_streamContextLineEndPolygon; }, - polygonEnd: function() { this.lineEnd = d3_geo_streamContextLineEnd; } -}; - -function d3_geo_streamContextPoint(x, y) { - this._context.moveTo(x, y); - this._context.arc(x, y, this._pointRadius, 0, 2 * π); -} - -function d3_geo_streamContextPointLineStart(x, y) { - this._context.moveTo(x, y); - this.point = d3_geo_streamContextPointLine; -} - -function d3_geo_streamContextPointLine(x, y) { - this._context.lineTo(x, y); -} - -function d3_geo_streamContextLineEnd() { - this.point = d3_geo_streamContextPoint; -} - -function d3_geo_streamContextLineEndPolygon() { - this._context.closePath(); +function d3_geo_streamContext(context, pointRadius) { + var stream = { + point: point, + + // While inside a line, override point to moveTo then lineTo. + lineStart: function() { stream.point = pointLineStart; }, + lineEnd: lineEnd, + + // While inside a polygon, override lineEnd to closePath. + polygonStart: function() { stream.lineEnd = lineEndPolygon; }, + polygonEnd: function() { stream.lineEnd = lineEnd; } + }; + + function point(x, y) { + context.moveTo(x, y); + context.arc(x, y, pointRadius, 0, 2 * π); + } + + function pointLineStart(x, y) { + context.moveTo(x, y); + stream.point = pointLine; + } + + function pointLine(x, y) { + context.lineTo(x, y); + } + + function lineEnd() { + stream.point = point; + } + + function lineEndPolygon() { + context.closePath(); + } + + return stream; } diff --git a/src/geo/stream-radians.js b/src/geo/stream-radians.js new file mode 100644 index 00000000..b7681140 --- /dev/null +++ b/src/geo/stream-radians.js @@ -0,0 +1,5 @@ +function d3_geo_streamRadians(stream) { + return d3_geo_streamTransform(stream, function(x, y) { + stream.point(x * d3_radians, y * d3_radians); + }); +} diff --git a/src/geo/stream-rotate.js b/src/geo/stream-rotate.js new file mode 100644 index 00000000..a8a19f1e --- /dev/null +++ b/src/geo/stream-rotate.js @@ -0,0 +1,6 @@ +function d3_geo_streamRotate(rotate, stream) { + return d3_geo_streamTransform(stream, function(x, y) { + x = rotate(x, y); + stream.point(x[0], x[1]); + }); +} diff --git a/src/geo/stream-transform.js b/src/geo/stream-transform.js new file mode 100644 index 00000000..ee10c349 --- /dev/null +++ b/src/geo/stream-transform.js @@ -0,0 +1,10 @@ +function d3_geo_streamTransform(stream, point) { + return { + point: point, + sphere: stream.sphere, + lineStart: stream.lineStart, + lineEnd: stream.lineEnd, + polygonStart: stream.polygonStart, + polygonEnd: stream.polygonEnd + }; +}