import clipRectangle from "../clip/rectangle.js"; import identity from "../identity.js"; import {transformer} from "../transform.js"; import {fitExtent, fitSize, fitWidth, fitHeight} from "./fit.js"; function scaleTranslate(kx, ky, tx, ty) { return kx === 1 && ky === 1 && tx === 0 && ty === 0 ? identity : transformer({ point: function(x, y) { this.stream.point(x * kx + tx, y * ky + ty); } }); } export default function() { var k = 1, tx = 0, ty = 0, sx = 1, sy = 1, transform = identity, // scale, translate and reflect x0 = null, y0, x1, y1, // clip extent postclip = identity, cache, cacheStream, projection; function reset() { cache = cacheStream = null; return projection; } return projection = { stream: function(stream) { return cache && cacheStream === stream ? cache : cache = transform(postclip(cacheStream = stream)); }, postclip: function(_) { return arguments.length ? (postclip = _, x0 = y0 = x1 = y1 = null, reset()) : postclip; }, clipExtent: function(_) { return arguments.length ? (postclip = _ == null ? (x0 = y0 = x1 = y1 = null, identity) : clipRectangle(x0 = +_[0][0], y0 = +_[0][1], x1 = +_[1][0], y1 = +_[1][1]), reset()) : x0 == null ? null : [[x0, y0], [x1, y1]]; }, scale: function(_) { return arguments.length ? (transform = scaleTranslate((k = +_) * sx, k * sy, tx, ty), reset()) : k; }, translate: function(_) { return arguments.length ? (transform = scaleTranslate(k * sx, k * sy, tx = +_[0], ty = +_[1]), reset()) : [tx, ty]; }, reflectX: function(_) { return arguments.length ? (transform = scaleTranslate(k * (sx = _ ? -1 : 1), k * sy, tx, ty), reset()) : sx < 0; }, reflectY: function(_) { return arguments.length ? (transform = scaleTranslate(k * sx, k * (sy = _ ? -1 : 1), tx, ty), reset()) : sy < 0; }, fitExtent: function(extent, object) { return fitExtent(projection, extent, object); }, fitSize: function(size, object) { return fitSize(projection, size, object); }, fitWidth: function(width, object) { return fitWidth(projection, width, object); }, fitHeight: function(height, object) { return fitHeight(projection, height, object); } }; }