fit.js 910 B

12345678910111213141516171819202122232425262728293031
  1. import {default as geoStream} from "../stream";
  2. import boundsStream from "../path/bounds";
  3. export function fitExtent(projection, extent, object) {
  4. var w = extent[1][0] - extent[0][0],
  5. h = extent[1][1] - extent[0][1],
  6. clip = projection.clipExtent && projection.clipExtent();
  7. projection
  8. .scale(150)
  9. .translate([0, 0]);
  10. if (clip != null) projection.clipExtent(null);
  11. geoStream(object, projection.stream(boundsStream));
  12. var b = boundsStream.result(),
  13. k = Math.min(w / (b[1][0] - b[0][0]), h / (b[1][1] - b[0][1])),
  14. x = +extent[0][0] + (w - k * (b[1][0] + b[0][0])) / 2,
  15. y = +extent[0][1] + (h - k * (b[1][1] + b[0][1])) / 2;
  16. if (clip != null) projection.clipExtent(clip);
  17. return projection
  18. .scale(k * 150)
  19. .translate([x, y]);
  20. }
  21. export function fitSize(projection, size, object) {
  22. return fitExtent(projection, [[0, 0], size], object);
  23. }