source: node_modules/d3-geo/src/projection/fit.js@ e4c61dd

Last change on this file since e4c61dd was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 1.6 KB
Line 
1import {default as geoStream} from "../stream.js";
2import boundsStream from "../path/bounds.js";
3
4function 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
14export 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
25export function fitSize(projection, size, object) {
26 return fitExtent(projection, [[0, 0], size], object);
27}
28
29export 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
39export 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}
Note: See TracBrowser for help on using the repository browser.