| 1 | import {default as geoStream} from "../stream.js";
|
|---|
| 2 | import boundsStream from "../path/bounds.js";
|
|---|
| 3 |
|
|---|
| 4 | function fit(projection, fitBounds, object) {
|
|---|
| 5 | var clip = projection.clipExtent && projection.clipExtent();
|
|---|
| 6 | projection.scale(150).translate([0, 0]);
|
|---|
| 7 | if (clip != null) projection.clipExtent(null);
|
|---|
| 8 | geoStream(object, projection.stream(boundsStream));
|
|---|
| 9 | fitBounds(boundsStream.result());
|
|---|
| 10 | if (clip != null) projection.clipExtent(clip);
|
|---|
| 11 | return projection;
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | export function fitExtent(projection, extent, object) {
|
|---|
| 15 | return fit(projection, function(b) {
|
|---|
| 16 | var w = extent[1][0] - extent[0][0],
|
|---|
| 17 | h = extent[1][1] - extent[0][1],
|
|---|
| 18 | k = Math.min(w / (b[1][0] - b[0][0]), h / (b[1][1] - b[0][1])),
|
|---|
| 19 | x = +extent[0][0] + (w - k * (b[1][0] + b[0][0])) / 2,
|
|---|
| 20 | y = +extent[0][1] + (h - k * (b[1][1] + b[0][1])) / 2;
|
|---|
| 21 | projection.scale(150 * k).translate([x, y]);
|
|---|
| 22 | }, object);
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | export function fitSize(projection, size, object) {
|
|---|
| 26 | return fitExtent(projection, [[0, 0], size], object);
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | export function fitWidth(projection, width, object) {
|
|---|
| 30 | return fit(projection, function(b) {
|
|---|
| 31 | var w = +width,
|
|---|
| 32 | k = w / (b[1][0] - b[0][0]),
|
|---|
| 33 | x = (w - k * (b[1][0] + b[0][0])) / 2,
|
|---|
| 34 | y = -k * b[0][1];
|
|---|
| 35 | projection.scale(150 * k).translate([x, y]);
|
|---|
| 36 | }, object);
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | export function fitHeight(projection, height, object) {
|
|---|
| 40 | return fit(projection, function(b) {
|
|---|
| 41 | var h = +height,
|
|---|
| 42 | k = h / (b[1][1] - b[0][1]),
|
|---|
| 43 | x = -k * b[0][0],
|
|---|
| 44 | y = (h - k * (b[1][1] + b[0][1])) / 2;
|
|---|
| 45 | projection.scale(150 * k).translate([x, y]);
|
|---|
| 46 | }, object);
|
|---|
| 47 | }
|
|---|