Debian Codegraph @

topojson/test/scale-test.js

raw
1 var tape = require("tape"),
2 scale = require("../lib/topojson/scale");
3
4 require("./inDelta");
5
6 tape("scale with a quantized topology scales to fit the width of the viewport", function(test) {
7 test.inDelta(scale({
8 type: "Topology",
9 bbox: [11, 13, 17, 21], // 6:8 = 3:4
10 transform: {scale: [1, 2], translate: [11, 13]} // [3,2] -> [14,17]
11 }, {
12 width: 200,
13 invert: false
14 }), {
15 type: "Topology",
16 bbox: [0, 0, 200, 266.666667], // 200:266.67 = 3:4
17 transform: {scale: [33.333333, 66.666667], translate: [0, 0]} // [3,2] -> [100,133.33333]
18 });
19 test.inDelta(scale({
20 type: "Topology",
21 bbox: [11, 17, 17, 19], // 6:2 = 3:1
22 transform: {scale: [1, 2], translate: [11, 13]} // [3,2.5] -> [14,18]
23 }, {
24 width: 200,
25 invert: false
26 }), {
27 type: "Topology",
28 bbox: [0, 0, 200, 66.666667], // 200:66.667 = 3:1
29 transform: {scale: [33.3333333, 66.6666667], translate: [0, -133.333333]} // [3,2.5] -> [100,33.333333]
30 });
31 test.inDelta(scale({
32 type: "Topology",
33 bbox: [11, 17, 17, 19], // 6:2 = 3:1
34 transform: {scale: [1, 2], translate: [11, 13]} // [3,2.5] -> [14,18]
35 }, {
36 width: 200,
37 margin: 20,
38 invert: false
39 }), {
40 type: "Topology",
41 bbox: [20, 20, 180, 73.3333333], // 120:40 = 3:1
42 transform: {scale: [26.666667, 53.333333], translate: [20, -86.6666667]} // [3,2.5] -> [80,40]
43 });
44 test.end();
45 });
46
47 tape("scale with a quantized topology scales to fit the height of the viewport", function(test) {
48 test.inDelta(scale({
49 type: "Topology",
50 bbox: [13, 11, 21, 17], // 8:6 = 4:3
51 transform: {scale: [2, 1], translate: [13, 11]} // [2,3] -> [17,14]
52 }, {
53 height: 200,
54 invert: false
55 }), {
56 type: "Topology",
57 bbox: [0, 0, 266.666667, 200], // 266:200.67 = 4:3
58 transform: {scale: [66.666667, 33.333333], translate: [0, 0]} // [2,3] -> [133.33333,100]
59 });
60 test.inDelta(scale({
61 type: "Topology",
62 bbox: [17, 11, 19, 17], // 2:6 = 1:3
63 transform: {scale: [2, 1], translate: [13, 11]} // [2.5,3] -> [18,14]
64 }, {
65 height: 200,
66 invert: false
67 }), {
68 type: "Topology",
69 bbox: [0, 0, 66.666667, 200], // 66.667:200 = 1:3
70 transform: {scale: [66.6666667, 33.3333333], translate: [-133.333333, 0]} // [2.5,3] -> [33.333333,100]
71 });
72 test.inDelta(scale({
73 type: "Topology",
74 bbox: [17, 11, 19, 17], // 2:6 = 1:3
75 transform: {scale: [2, 1], translate: [13, 11]} // [2.5,3] -> [18,14]
76 }, {
77 height: 200,
78 margin: 20,
79 invert: false
80 }), {
81 type: "Topology",
82 bbox: [20, 20, 73.3333333, 180], // 40:120 = 1:3
83 transform: {scale: [53.333333, 26.666667], translate: [-86.6666667, 20]} // [2.5,3] -> [40,80]
84 });
85 test.end();
86 });
87
88 tape("scale with a quantized topology scales to fit the smaller side of the viewport", function(test) {
89 test.inDelta(scale({
90 type: "Topology",
91 bbox: [11, 13, 17, 21], // 6:8 = 3:4
92 transform: {scale: [1, 2], translate: [11, 13]} // [3,2] -> [14,17]
93 }, {
94 width: 190,
95 height: 200,
96 invert: false
97 }), {
98 type: "Topology",
99 bbox: [20, 0, 170, 200], // 150:200 = 3:4
100 transform: {scale: [25, 50], translate: [20, 0]} // [3,2] -> [95,100]
101 });
102 test.inDelta(scale({
103 type: "Topology",
104 bbox: [13, 11, 21, 17], // 8:6 = 4:3
105 transform: {scale: [2, 1], translate: [13, 11]} // [2,3] -> [17,14]
106 }, {
107 height: 190,
108 width: 200,
109 invert: false
110 }), {
111 type: "Topology",
112 bbox: [0, 20, 200, 170], // 200:150 = 4:3
113 transform: {scale: [50, 25], translate: [0, 20]} // [2,3] -> [100,95]
114 });
115 test.end();
116 });
117
118 tape("scale with a quantized topology returns the expected result when the original transform origin is not [xmin,ymin]", function(test) {
119 test.inDelta(scale({
120 type: "Topology",
121 bbox: [11, 13, 17, 21], // 6:8 = 3:4
122 transform: {scale: [1, 2], translate: [10, 11]} // [4,3] -> [14,17]
123 }, {
124 width: 200,
125 invert: false
126 }), {
127 type: "Topology",
128 bbox: [0, 0, 200, 266.666667], // 200:266.67 = 3:4
129 transform: {scale: [33.333333, 66.666667], translate: [-33.333333, -66.666667]} // [4,3] -> [100,133.33333]
130 });
131 test.end();
132 });
133
134
135 tape("scale with a non-quantized topology scales to fit the width of the viewport", function(test) {
136 test.inDelta(scale({
137 type: "Topology",
138 bbox: [11, 13, 17, 21], // 6:8 = 3:4
139 arcs: [[[11, 13], [17, 21]]]
140 }, {
141 width: 200,
142 invert: false
143 }), {
144 type: "Topology",
145 bbox: [0, 0, 200, 266.666667], // 200:266.67 = 3:4
146 arcs: [[[0, 0], [200, 266.666667]]]
147 });
148 test.inDelta(scale({
149 type: "Topology",
150 bbox: [11, 17, 17, 19], // 6:2 = 3:1
151 arcs: [[[11, 17], [17, 19]]]
152 }, {
153 width: 200,
154 invert: false
155 }), {
156 type: "Topology",
157 bbox: [0, 0, 200, 66.666667], // 200:66.667 = 3:1
158 arcs: [[[0, 0], [200, 66.666667]]]
159 });
160 test.inDelta(scale({
161 type: "Topology",
162 bbox: [11, 17, 17, 19], // 6:2 = 3:1
163 arcs: [[[11, 17], [17, 19]]]
164 }, {
165 width: 160,
166 margin: 20,
167 invert: false
168 }), {
169 type: "Topology",
170 bbox: [20, 20, 140, 60], // 120:40 = 3:1
171 arcs: [[[20, 20], [140, 60]]]
172 });
173 test.end();
174 });
175
176 tape("scale with a non-quantized topology scales to fit the height of the viewport", function(test) {
177 test.inDelta(scale({
178 type: "Topology",
179 bbox: [11, 13, 17, 21], // 6:8 = 3:4
180 arcs: [[[11, 13], [17, 21]]]
181 }, {
182 height: 200,
183 invert: false
184 }), {
185 type: "Topology",
186 bbox: [0, 0, 150, 200], // 150:200 = 3:4
187 arcs: [[[0, 0], [150, 200]]]
188 });
189 test.inDelta(scale({
190 type: "Topology",
191 bbox: [11, 17, 17, 19], // 6:2 = 3:1
192 arcs: [[[11, 17], [17, 19]]]
193 }, {
194 height: 200,
195 invert: false
196 }), {
197 type: "Topology",
198 bbox: [0, 0, 600, 200], // 600:200 = 3:1
199 arcs: [[[0, 0], [600, 200]]]
200 });
201 test.inDelta(scale({
202 type: "Topology",
203 bbox: [11, 17, 17, 19], // 6:2 = 3:1
204 arcs: [[[11, 17], [17, 19]]]
205 }, {
206 height: 160,
207 margin: 20,
208 invert: false
209 }), {
210 type: "Topology",
211 bbox: [20, 20, 380, 140], // 360:120 = 3:1
212 arcs: [[[20, 20], [380, 140]]]
213 });
214 test.end();
215 });
216
217 tape("scale with a non-quantized topology scales to fit the smaller side of the viewport", function(test) {
218 test.inDelta(scale({
219 type: "Topology",
220 bbox: [11, 13, 17, 21], // 6:8 = 3:4
221 arcs: [[[11, 13], [17, 21]]]
222 }, {
223 width: 190,
224 height: 200,
225 invert: false
226 }), {
227 type: "Topology",
228 bbox: [20, 0, 170, 200], // 150:200 = 3:4
229 arcs: [[[20, 0], [170, 200]]]
230 });
231 test.inDelta(scale({
232 type: "Topology",
233 bbox: [13, 11, 21, 17], // 8:6 = 4:3
234 arcs: [[[13, 11], [21, 17]]]
235 }, {
236 height: 190,
237 width: 200,
238 invert: false
239 }), {
240 type: "Topology",
241 bbox: [0, 20, 200, 170], // 200:150 = 4:3
242 arcs: [[[0, 20], [200, 170]]]
243 });
244 test.end();
245 });
246
247 tape("scale with a non-quantized topology inverts by default", function(test) {
248 test.inDelta(scale({
249 type: "Topology",
250 bbox: [11, 13, 17, 21],
251 arcs: [[[11, 13], [17, 21]]]
252 }, {
253 width: 200
254 }), {
255 type: "Topology",
256 bbox: [0, 0, 200, 266.666667],
257 arcs: [[[0, 266.666667], [200, 0]]]
258 });
259 test.end();
260 });