1 | // Variable for the currently selected flag ratio
|
---|
2 | var currentRatio = null;
|
---|
3 |
|
---|
4 | var aspectRatios = null;
|
---|
5 | var aspectMaxRatio = 0;
|
---|
6 |
|
---|
7 | function calcMaxRatio(aspect_ratio_data)
|
---|
8 | {
|
---|
9 | // work out the maximum ratio
|
---|
10 | var max_ratio = 0;
|
---|
11 |
|
---|
12 | $.each(aspect_ratio_data, function(key, ratio_table) {
|
---|
13 | var ratio_array = key.split(":");
|
---|
14 | var ratio_x = ratio_array[0];
|
---|
15 | var ratio_y = ratio_array[1];
|
---|
16 | var ratio = ratio_y / ratio_x;
|
---|
17 |
|
---|
18 | if (ratio > max_ratio) {
|
---|
19 | max_ratio = ratio;
|
---|
20 | }
|
---|
21 | });
|
---|
22 |
|
---|
23 | return max_ratio;
|
---|
24 | }
|
---|
25 |
|
---|
26 | function calcCompleteWidth(aspect_ratio_data,scale)
|
---|
27 | {
|
---|
28 | // work out the maximum ratio
|
---|
29 | var max_width = 0;
|
---|
30 |
|
---|
31 | $.each(aspect_ratio_data, function(key, ratio_table) {
|
---|
32 | var ratio_array = key.split(":");
|
---|
33 | var ratio_x = ratio_array[0];
|
---|
34 | var ratio_y = ratio_array[1];
|
---|
35 | var ratio = ratio_y / ratio_x;
|
---|
36 |
|
---|
37 | max_width += (scale*ratio);
|
---|
38 | });
|
---|
39 |
|
---|
40 | return max_width;
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | function scaledDivsFitInPage(aspect_ratio_data, scale_to_height, num_of_rows_allowed, la_x_dim)
|
---|
45 | {
|
---|
46 | var divs_fit = true;
|
---|
47 |
|
---|
48 | var row = 1;
|
---|
49 | var total_row_width = 0;
|
---|
50 | var total_height = 0;
|
---|
51 |
|
---|
52 | $.each(aspect_ratio_data, function(key, ratio_table) {
|
---|
53 | var ratio_array = key.split(":");
|
---|
54 | var ratio_x = ratio_array[0];
|
---|
55 | var ratio_y = ratio_array[1];
|
---|
56 | var ratio = ratio_y / ratio_x;
|
---|
57 |
|
---|
58 | var scaled_div_width = scale_to_height*ratio;
|
---|
59 | if (total_row_width + scaled_div_width > la_x_dim) {
|
---|
60 | // this div doesn't fit on the current row
|
---|
61 | // => start a new row
|
---|
62 | total_row_width = scaled_div_width;
|
---|
63 | row++;
|
---|
64 |
|
---|
65 | if (row>num_of_rows_allowed) {
|
---|
66 | // blow out!
|
---|
67 | divs_fit = false;
|
---|
68 | return false; // jquery.each equivalent to 'break'
|
---|
69 | }
|
---|
70 | }
|
---|
71 | else {
|
---|
72 | total_row_width += scaled_div_width;
|
---|
73 | row++;
|
---|
74 | }
|
---|
75 | });
|
---|
76 |
|
---|
77 | return divs_fit;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | function calcScaleToHeight(aspectRatios,la_x_dim,la_y_dim)
|
---|
82 | {
|
---|
83 | // determine scale factor so divs max out (as in, filling in the page) based la_y_dim height,
|
---|
84 |
|
---|
85 | var num_of_rows_allowed = 1;
|
---|
86 | var scale_to_height = la_y_dim; // optimized version of true calcuation, la_y_dim / num_of_rows_allowed;
|
---|
87 |
|
---|
88 | //var div_width = complete_width_100 * (div_height/100)
|
---|
89 |
|
---|
90 | var num_flag_bins = aspectRatios.length;
|
---|
91 |
|
---|
92 | while (!scaledDivsFitInPage(aspectRatios,scale_to_height,num_of_rows_allowed,la_x_dim)) {
|
---|
93 |
|
---|
94 | num_of_rows_allowed++;
|
---|
95 |
|
---|
96 | if (num_of_rows_allowed>num_flag_bins) {
|
---|
97 | // needing more rows than there are flag bins is an indication that things
|
---|
98 | // just don't fit, and so will need to use a scroll bar.
|
---|
99 | // Exiting at this point will naturally achieve this
|
---|
100 | break;
|
---|
101 | }
|
---|
102 |
|
---|
103 | scale_to_height = la_y_dim/num_of_rows_allowed;
|
---|
104 |
|
---|
105 | // if get to here, then need
|
---|
106 |
|
---|
107 | }
|
---|
108 |
|
---|
109 | console.log("**** num_of_rows = " + num_of_rows_allowed + ", scale to height = " + scale_to_height);
|
---|
110 | return scale_to_height;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | $(function() {
|
---|
115 | $.getJSON( "../similarity-2d/flag-aspect-ratios-json.jsp", function(data) {
|
---|
116 |
|
---|
117 | aspectRatios = data;
|
---|
118 |
|
---|
119 | //var scale_to_y_dim = 130;
|
---|
120 |
|
---|
121 | // express scale_to_y_dim to use relative to height of window
|
---|
122 |
|
---|
123 | var window_height = $(window).height();
|
---|
124 | var div_top = $('#aspect-ratio-div').offset().top + 20;
|
---|
125 |
|
---|
126 | var la_y_dim = (div_top>0) ? window_height - div_top : window_height * 0.6;
|
---|
127 | var la_x_dim = $(window).width() * 0.7;
|
---|
128 |
|
---|
129 | //var scale_to_y_dim = la_y_dim/4;
|
---|
130 |
|
---|
131 | //console.log("*** scale to y dim = " + la_y_dim);
|
---|
132 |
|
---|
133 | //alert("scale_to_y_dim = " + scale_to_y_dim);
|
---|
134 |
|
---|
135 |
|
---|
136 | aspectMaxRatio = calcMaxRatio(aspectRatios);
|
---|
137 | //var complete_width_100 = calcCompleteWidth(aspectRatios,100); // as if every aspect ratio bin is scaled to 100 pixels high
|
---|
138 |
|
---|
139 |
|
---|
140 | scale_to_y_dim = calcScaleToHeight(aspectRatios,la_x_dim,la_y_dim);
|
---|
141 |
|
---|
142 |
|
---|
143 | var items = [];
|
---|
144 |
|
---|
145 |
|
---|
146 | $.each(data, function(key, ratio_table) {
|
---|
147 | var freq = ratio_table.freq;
|
---|
148 | var ratio_array = key.split(":");
|
---|
149 | var ratio_x = ratio_array[0];
|
---|
150 | var ratio_y = ratio_array[1];
|
---|
151 | var ratio = ratio_y / ratio_x;
|
---|
152 |
|
---|
153 | var ratioScale = aspectMaxRatio/ratio;
|
---|
154 |
|
---|
155 | var x_dim = Math.round(scale_to_y_dim * ratioScale);
|
---|
156 | var y_dim = scale_to_y_dim;
|
---|
157 |
|
---|
158 | var innertext = "<div style='font-size: 180%;'>"+freq + " countries, " + key + "</div>";
|
---|
159 |
|
---|
160 | var flags = [];
|
---|
161 |
|
---|
162 | var nz_prioritized_flags = ratio_table.flags.sort();
|
---|
163 |
|
---|
164 | // Promote NZ flag to start of array, but otherwise keep sorted
|
---|
165 | var p = nz_prioritized_flags.length-1;
|
---|
166 | var found_nz = false;
|
---|
167 |
|
---|
168 | while (p>0) {
|
---|
169 | var curr;
|
---|
170 | var curr_title;
|
---|
171 |
|
---|
172 | if (!found_nz) {
|
---|
173 | curr = ratio_table.flags[p];
|
---|
174 | curr_title = curr.replace(/^.*\//,"").replace(/\.(.*?)$/,"");
|
---|
175 | }
|
---|
176 |
|
---|
177 | if (curr_title == "nz") {
|
---|
178 | found_nz = true;
|
---|
179 |
|
---|
180 | // swap with adjacent cell
|
---|
181 | var tmp_m1 = nz_prioritized_flags[p-1];
|
---|
182 | nz_prioritized_flags[p-1] = curr;
|
---|
183 | nz_prioritized_flags[p] = tmp_m1;
|
---|
184 | }
|
---|
185 | p--;
|
---|
186 | }
|
---|
187 |
|
---|
188 | $.each(nz_prioritized_flags, function(index, flag_filename) {
|
---|
189 | var title = flag_filename.replace(/^.*\//,"").replace(/\.(.*?)$/,"");
|
---|
190 |
|
---|
191 | flags.push("<img src='../similarity-2d/"+flag_filename+"' id='aspect-ratio-"+ title +"' style='height: 70px; padding: 3px; ' title='" + title.toUpperCase() + "'/>");
|
---|
192 | });
|
---|
193 |
|
---|
194 | innertext += flags.join("");
|
---|
195 |
|
---|
196 | var ratio_id = "ratio-" + key.replace(":","_");
|
---|
197 | if (found_nz) {
|
---|
198 | currentRatio = ratio_id;
|
---|
199 | }
|
---|
200 |
|
---|
201 | var id = " id='" + ratio_id + "'";
|
---|
202 |
|
---|
203 | var style = "style='width:" + x_dim + "px; height:" + y_dim + "px;";
|
---|
204 | if (found_nz) {
|
---|
205 | style += "border-color:white; border-width:4px;"
|
---|
206 | }
|
---|
207 |
|
---|
208 | style += " overflow-y:scroll;";
|
---|
209 | style += "'";
|
---|
210 |
|
---|
211 | var onclick = " onclick='updateCanvasDimensions("+ratio_y + "," + ratio_x + ")'";
|
---|
212 |
|
---|
213 | items.push( "<div class='ratioDiv'" + id + style + onclick + ">"+ innertext + "</div>" );
|
---|
214 |
|
---|
215 | });
|
---|
216 |
|
---|
217 | $('#aspect-ratio-div').append("<div class='centredDiv' style='width: 100%'>"+items.join("\n")+"</div>");
|
---|
218 |
|
---|
219 |
|
---|
220 | // now change the flag tooltip labels into their country names
|
---|
221 | $.getJSON( "../similarity-2d/iso-3166-keyed-by-alpha-2-countrycodes.json", function(data) {
|
---|
222 |
|
---|
223 | $.each( data, function( iso2_key, country_name ) {
|
---|
224 | var $img = $('#aspect-ratio-'+iso2_key.toLowerCase());
|
---|
225 | var title = $img.attr("title") + ": " + country_name;
|
---|
226 | $img.attr("title",title);
|
---|
227 | }
|
---|
228 | )
|
---|
229 | });
|
---|
230 |
|
---|
231 | });
|
---|
232 | });
|
---|
233 |
|
---|
234 |
|
---|
235 |
|
---|
236 | $(window).resize(function() {
|
---|
237 | console.log("choose-canvas resize()");
|
---|
238 |
|
---|
239 | //console.log("*** aspect ratio div position top = " + $('#aspect-ratio-div').position().top);
|
---|
240 |
|
---|
241 | var window_height = $(window).height();
|
---|
242 | var div_top = $('#aspect-ratio-div').offset().top + 20;
|
---|
243 |
|
---|
244 | //var top = 0;
|
---|
245 |
|
---|
246 | var la_y_dim = window_height - div_top;
|
---|
247 | var la_x_dim = $(window).width() * 0.7;
|
---|
248 |
|
---|
249 | var scale_to_y_dim = calcScaleToHeight(aspectRatios,la_x_dim,la_y_dim);
|
---|
250 |
|
---|
251 | // (top==0) ? la_y_dim/4 : la_y_dim/2.8;
|
---|
252 |
|
---|
253 | //console.log("*** scale to y dim = " + la_y_dim);
|
---|
254 |
|
---|
255 | $.each(aspectRatios, function(key, ratio_table) {
|
---|
256 | var freq = ratio_table.freq;
|
---|
257 | var ratio_array = key.split(":");
|
---|
258 | var ratio_x = ratio_array[0];
|
---|
259 | var ratio_y = ratio_array[1];
|
---|
260 | var ratio = ratio_y / ratio_x;
|
---|
261 |
|
---|
262 | var ratioScale = aspectMaxRatio/ratio;
|
---|
263 |
|
---|
264 | var x_dim = Math.round(scale_to_y_dim * ratioScale);
|
---|
265 | var y_dim = scale_to_y_dim;
|
---|
266 |
|
---|
267 | var ratio_id = "ratio-" + key.replace(":","_");
|
---|
268 |
|
---|
269 | //console.log("*** ratio_id = " + ratio_id + " xdim = " + x_dim + ", ydim = " + y_dim);
|
---|
270 |
|
---|
271 | $('#'+ratio_id).css("width",x_dim + "px");
|
---|
272 | $('#'+ratio_id).css("height",y_dim + "px");
|
---|
273 |
|
---|
274 |
|
---|
275 | });
|
---|
276 | });
|
---|
277 |
|
---|
278 |
|
---|
279 |
|
---|
280 | function updateCanvasDimensions(height, width) {
|
---|
281 |
|
---|
282 | // Show feedback by highlighting the chosen square
|
---|
283 | document.getElementById(currentRatio).style.borderColor = 'black';
|
---|
284 | document.getElementById(currentRatio).style.borderWidth = '1px';
|
---|
285 | currentRatio = "ratio-" + width + "_" + height;
|
---|
286 | document.getElementById(currentRatio).style.borderColor = 'white';
|
---|
287 | document.getElementById(currentRatio).style.borderWidth = '4px';
|
---|
288 |
|
---|
289 | // Changing the value of sizeConstant will change the
|
---|
290 | // size of the svg canvas without affecting the
|
---|
291 | // width/height ratios
|
---|
292 |
|
---|
293 | var sizeConstant = 400;
|
---|
294 |
|
---|
295 | var h = sizeConstant;
|
---|
296 | var w = (sizeConstant / height) * width;
|
---|
297 |
|
---|
298 | if (typeof flagCanvasSettings !== 'undefined') {
|
---|
299 | flagCanvasSettings.width = w;
|
---|
300 | flagCanvasSettings.height = h;
|
---|
301 | }
|
---|
302 | }
|
---|
303 |
|
---|