root/other-projects/nz-flag-design/trunk/main-form/choose-canvas.html @ 29906

Revision 29906, 15.0 KB (checked in by davidb, 4 years ago)

Changes resulting from writing the TPDL submission

  • Property svn:executable set to *
Line 
1<!DOCTYPE html> 
2<html id="story">
3  <head>
4    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
5
6    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
7    <meta http-equiv="Pragma" content="no-cache" />
8    <meta http-equiv="Expires" content="0" />
9
10<!--
11    <meta name="viewport" content="width=device-width, initial-scale=1"/>
12-->
13    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
14    <meta http-equiv="X-UA-Compatible" content="IE=Edge, chrome=1"/>
15    <meta name="apple-mobile-web-app-capable" content="yes"/>
16   
17    <script src="css/source-sans-pro.js"></script>
18    <link href="css/styles.css" rel="stylesheet"/>
19    <link href="css/storystyle.css" rel="stylesheet"/>
20   
21    <link rel="stylesheet"  href="lib-slider/css/jquery.mobile-1.3.0.css"/>
22    <link rel="stylesheet" href="lib-slider/css/jqm-demos.css"/>
23    <link rel="stylesheet" href="lib-slider/css/swipe-page.css"/>
24    <link rel="shortcut icon" href="lib-slider/nzflag-icon64-padded.png"/>
25
26    <!-- jQuery -->
27    <script src="lib/jquery-1.11.1.min.js"></script>
28    <script src="lib/jquery.cookie.js"></script>
29
30    <!-- page swipe -->
31    <script src="lib-slider/js/jquery.mobile.demos.js"></script>
32    <script src="lib-slider/js/jquery.mobile-1.3.0.js"></script>
33    <script src="lib-slider/js/swipe-page.js"></script>
34
35    <!-- accordion bars -->
36    <link href="css/liteaccordion.css" rel="stylesheet" />
37    <script src="js/jquery.easing.1.3.js"></script>
38    <script src="js/liteaccordion-with-resize.jquery.js"></script>
39
40    <!-- canvg library -->
41    <script type="text/javascript" src="lib/canvg/rgbcolor.js"></script>
42    <script type="text/javascript" src="lib/canvg/StackBlur.js"></script>
43    <script type="text/javascript" src="lib/canvg/canvg.js"></script>
44     
45    <!-- spectrum for colour palette -->
46    <!--  For documentation see https://bgrins.github.io/spectrum -->
47    <script src='bgrins-spectrum/spectrum.js'></script>
48    <link rel='stylesheet' href='bgrins-spectrum/spectrum.css' />
49
50    <script src="../similarity-2d/js-lib/Colour.js"></script>
51    <script src="../similarity-2d/flag-processing.js"></script>
52
53    <style>
54      a[href^="http://"]:after,
55      a[href^="https://"]:after {
56        content: url(http://upload.wikimedia.org/wikipedia/commons/6/64/Icon_External_Link.png);
57        margin: 0 0 0 5px;
58      }
59    </style>
60
61    <script type="text/javascript">
62        // Flag settings - used to initialise the svg canvas to the user's preferences
63
64        var colourPotSwatch = [ "#00247d", "#cc142b", "#ffffff", "#29afaf", "#008000", "#800080", "#ffff15", "#ff6600" ]
65        var numColourPots = colourPotSwatch.length;
66
67
68        var flagCanvasSettings = { width: 800, height: 400,
69                               backgroundColor: "#000066",
70                               colourPots: colourPotSwatch.slice(0,3)
71                             };
72    var isoCountryCode = null;
73
74
75
76// Variable for the currently selected flag ratio
77var currentRatio = null;
78
79var aspectRatios = null;
80var aspectMaxRatio = 0;
81
82function calcMaxRatio(aspect_ratio_data)
83{
84    // work out the maximum ratio     
85    var max_ratio = 0;
86
87    $.each(aspect_ratio_data, function(key, ratio_table) {
88        var ratio_array = key.split(":");
89        var ratio_x = ratio_array[0];
90        var ratio_y = ratio_array[1];
91        var ratio = ratio_y / ratio_x;
92   
93        if (ratio > max_ratio) {
94            max_ratio = ratio;
95        }
96    });
97
98    return max_ratio;
99}
100
101function calcCompleteWidth(aspect_ratio_data,scale)
102{
103    // work out the maximum ratio     
104    var max_width = 0;
105
106    $.each(aspect_ratio_data, function(key, ratio_table) {
107        var ratio_array = key.split(":");
108        var ratio_x = ratio_array[0];
109        var ratio_y = ratio_array[1];
110        var ratio = ratio_y / ratio_x;
111   
112    max_width += (scale*ratio);
113    });
114
115    return max_width;
116}
117
118
119function scaledDivsFitInPage(aspect_ratio_data, scale_to_height, num_of_rows_allowed, la_x_dim)
120{
121    var divs_fit = true;
122
123    var row = 1;
124    var total_row_width = 0;
125    var total_height = 0;
126
127    $.each(aspect_ratio_data, function(key, ratio_table) {
128        var ratio_array = key.split(":");
129        var ratio_x = ratio_array[0];
130        var ratio_y = ratio_array[1];
131        var ratio = ratio_y / ratio_x;
132   
133    var scaled_div_width = scale_to_height*ratio;
134    if (total_row_width + scaled_div_width > la_x_dim) {
135        // this div doesn't fit on the current row
136        // => start a new row
137        total_row_width = scaled_div_width;
138        row++;
139
140        if (row>num_of_rows_allowed) {
141        // blow out!
142        divs_fit = false;
143        return false; // jquery.each equivalent to 'break'
144        }
145    }
146    else {
147        total_row_width += scaled_div_width;
148        row++;
149    }
150    });
151
152    return divs_fit;
153}
154
155
156function calcScaleToHeight(aspectRatios,la_x_dim,la_y_dim)
157{
158    // determine scale factor so divs max out (as in, filling in the page) based la_y_dim height,
159
160    var num_of_rows_allowed = 1;
161    var scale_to_height = la_y_dim; // optimized version of true calcuation, la_y_dim / num_of_rows_allowed;
162
163    //var div_width  = complete_width_100 * (div_height/100)
164   
165    var num_flag_bins = aspectRatios.length;
166   
167    while (!scaledDivsFitInPage(aspectRatios,scale_to_height,num_of_rows_allowed,la_x_dim)) {
168       
169    num_of_rows_allowed++;
170   
171    if (num_of_rows_allowed>num_flag_bins) {
172        // needing more rows than there are flag bins is an indication that things
173        // just don't fit, and so will need to use a scroll bar.
174        // Exiting at this point will naturally achieve this
175        break;
176    }
177
178    scale_to_height = la_y_dim/num_of_rows_allowed;
179   
180    // if get to here, then need
181   
182    }
183
184    console.log("**** num_of_rows = " + num_of_rows_allowed + ", scale to height = " + scale_to_height);
185    return scale_to_height;
186}
187
188
189$(function() {
190    $.getJSON( "../similarity-2d/flag-aspect-ratios-json.jsp", function(data) {
191
192        aspectRatios = data;
193   
194        //var scale_to_y_dim = 130;
195   
196        // express scale_to_y_dim to use relative to height of window
197   
198    var window_height = $(window).height();
199    var div_top = $('#aspect-ratio-div').offset().top + 20;
200
201        var la_y_dim = (div_top>0) ? window_height - div_top : window_height * 0.6;
202    var la_x_dim = $(window).width() * 0.7;
203
204        //var scale_to_y_dim = la_y_dim/4;
205   
206        //console.log("*** scale to y dim = " + la_y_dim);
207   
208        //alert("scale_to_y_dim = " + scale_to_y_dim);
209
210
211        aspectMaxRatio = calcMaxRatio(aspectRatios);
212    //var complete_width_100 = calcCompleteWidth(aspectRatios,100); // as if every aspect ratio bin is scaled to 100 pixels high
213
214   
215    scale_to_y_dim = calcScaleToHeight(aspectRatios,la_x_dim,la_y_dim);
216
217
218        var items = [];
219   
220   
221        $.each(data, function(key, ratio_table) {
222            var freq = ratio_table.freq;
223            var ratio_array = key.split(":");
224            var ratio_x = ratio_array[0];
225            var ratio_y = ratio_array[1];
226            var ratio = ratio_y / ratio_x;
227       
228            var ratioScale = aspectMaxRatio/ratio;
229       
230            var x_dim = Math.round(scale_to_y_dim * ratioScale);
231            var y_dim = scale_to_y_dim;
232       
233            var innertext = "<div style='font-size: 180%;'>"+freq + " countries, " + key + "</div>";
234           
235            var flags = [];
236       
237            var nz_prioritized_flags = ratio_table.flags.sort();
238       
239            // Promote NZ flag to start of array, but otherwise keep sorted
240            var p = nz_prioritized_flags.length-1;
241            var found_nz = false;
242       
243            while (p>0) {               
244                var curr;
245                var curr_title;
246       
247                if (!found_nz) {
248                    curr = ratio_table.flags[p];
249                    curr_title = curr.replace(/^.*\//,"").replace(/\.(.*?)$/,"");
250                }
251       
252                if (curr_title == "nz") {
253                    found_nz = true;
254
255                    // swap with adjacent cell
256                    var tmp_m1 = nz_prioritized_flags[p-1];
257                    nz_prioritized_flags[p-1] = curr;
258                    nz_prioritized_flags[p] = tmp_m1;
259                }
260                p--;
261            }
262       
263            $.each(nz_prioritized_flags, function(index, flag_filename) {               
264                var title = flag_filename.replace(/^.*\//,"").replace(/\.(.*?)$/,"");
265       
266                flags.push("<img src='../similarity-2d/"+flag_filename+"' id='aspect-ratio-"+ title +"' style='height: 70px; padding: 3px; ' title='" + title.toUpperCase() + "'/>");
267            });
268       
269            innertext += flags.join("");
270       
271            var ratio_id = "ratio-" + key.replace(":","_");
272            if (found_nz) {
273                currentRatio = ratio_id;
274            }
275       
276            var id = " id='" + ratio_id + "'";
277       
278            var style = "style='width:" + x_dim + "px; height:" + y_dim + "px;";
279            if (found_nz) {
280                style += "border-color:white; border-width:4px;"
281            }
282       
283            style += " overflow-y:scroll;";
284            style += "'";
285       
286            var onclick = " onclick='updateCanvasDimensions("+ratio_y + "," + ratio_x + ")'";
287       
288            items.push( "<div class='ratioDiv'" + id + style + onclick + ">"+ innertext + "</div>" );
289       
290        });
291   
292        $('#aspect-ratio-div').append("<div class='centredDiv' style='width: 100%'>"+items.join("\n")+"</div>");
293   
294   
295        // now change the flag tooltip labels into their country names
296        $.getJSON( "../similarity-2d/iso-3166-keyed-by-alpha-2-countrycodes.json", function(data) {
297        isoCountryCodes = data;
298
299            $.each( data, function( iso2_key, country_name ) {
300        var $img = $('#aspect-ratio-'+iso2_key.toLowerCase());
301        var title = $img.attr("title") + ": " + country_name;
302        $img.attr("title",title);
303            }
304          )
305        });
306   
307    });
308});
309
310
311
312$(window).resize(function() {
313    console.log("choose-canvas resize()");
314
315    //console.log("*** aspect ratio div position top = " + $('#aspect-ratio-div').position().top);
316   
317    var window_height = $(window).height();
318    var div_top = $('#aspect-ratio-div').offset().top + 20;
319
320    //var top = 0;
321   
322    var la_y_dim = window_height - div_top;
323    var la_x_dim = $(window).width() * 0.7;
324
325    var scale_to_y_dim = calcScaleToHeight(aspectRatios,la_x_dim,la_y_dim);
326
327    // (top==0) ? la_y_dim/4 : la_y_dim/2.8;
328   
329    //console.log("*** scale to y dim = " + la_y_dim);
330   
331    $.each(aspectRatios, function(key, ratio_table) {
332        var freq = ratio_table.freq;
333        var ratio_array = key.split(":");
334        var ratio_x = ratio_array[0];
335        var ratio_y = ratio_array[1];
336        var ratio = ratio_y / ratio_x;
337   
338        var ratioScale = aspectMaxRatio/ratio;
339   
340        var x_dim = Math.round(scale_to_y_dim * ratioScale);
341        var y_dim = scale_to_y_dim;
342
343        var ratio_id = "ratio-" + key.replace(":","_");
344   
345        //console.log("*** ratio_id = " + ratio_id + " xdim = " + x_dim + ", ydim = " + y_dim);
346   
347        $('#'+ratio_id).css("width",x_dim + "px");
348        $('#'+ratio_id).css("height",y_dim + "px");
349   
350   
351    });
352});
353
354
355
356function updateCanvasDimensions(height, width) {
357   
358    // Show feedback by highlighting the chosen square
359    document.getElementById(currentRatio).style.borderColor = 'black';
360                document.getElementById(currentRatio).style.borderWidth = '1px';
361    currentRatio = "ratio-" + width + "_" + height;
362    document.getElementById(currentRatio).style.borderColor = 'white';
363    document.getElementById(currentRatio).style.borderWidth = '4px';
364   
365    // Changing the value of sizeConstant will change the
366    //  size of the svg canvas without affecting the
367    //  width/height ratios
368   
369    var sizeConstant = 400;
370   
371    var h = sizeConstant;
372    var w = (sizeConstant / height) * width;               
373   
374    if (typeof flagCanvasSettings !== 'undefined') {
375        flagCanvasSettings.width = w;
376        flagCanvasSettings.height = h;
377    }                               
378}
379
380
381
382    </script>
383           
384    <title>Canvas Size</title>
385
386  </head>
387
388  <body>
389    <div data-role="page" id="choose-canvas-page"
390     class="demo-page"
391     data-dom-cache="true"
392     data-next="choose-palette">
393     
394      <div data-role="content">
395     
396    <div data-role="controlgroup" class="control" data-mini="true">         
397          <a href="#" class="next right-button res-fwd" style="right:1%;"></a>
398<!--
399      <a href="#" class="prev left-button gen-back" style="left:1%;"></a>
400-->
401        </div>
402
403    <a target="_parent" href="../index.html" class="back-button back-left"></a>   
404     
405    <div class="story-page">
406
407      <!-- put custom content here -->       
408      <span class="left story-icon idea" ></span>
409      <h2>Canvas Size</h2>         
410     
411        <div class="dialog" id="dialog" title="Flat Aspect Ratio Selection Panel" widthXX="800px">
412            <br><br>
413            <h2>Please choose from the following aspect ratios to select the width and height of your flag.</h2>
414        <style>
415          p { margin: 10px }
416        </style>
417        <p>
418          New Zealand's current aspect ration (2:1) is selected by default.  Click on one of the other
419          boxes to choose a different size.
420        </p>
421            <p>
422          Once chosen, swipe the page or click/press on the right arrow to go onto the next step.
423          You can swipe/click back to this page at any time to change your selection.
424        </p>
425
426      <div id="aspect-ratio-div">
427      </div>
428
429
430<!--
431            <div class="centredDiv">
432                <div class="ratioDiv flagHover" id="ratio23" onclick="updateCanvasDimensions(2, 3)" title="2:3"></div>
433                <div class="ratioDiv" style="border-width:2px;" id="ratio12" onclick="updateCanvasDimensions(1, 2)" title="1:2"></div>
434                <div class="ratioDiv" id="ratio35" onclick="updateCanvasDimensions(3, 5)" title="3:5"></div>
435                <div style="clear: both"></div>
436            </div>
437            <p class="optionline">2:3 1:2 3:5</p><br>
438
439            <div class="centredDiv">
440                <div class="ratioDiv" id="ratio1019" onclick="updateCanvasDimensions(10, 19)" title="10:19"></div>
441                <div class="ratioDiv" id="ratio58" onclick="updateCanvasDimensions(5, 8)" title="5:8"></div>
442                <div class="ratioDiv" id="ratio811" onclick="updateCanvasDimensions(8, 11)" title="8:11"></div>
443                <div style="clear: both"></div>
444            </div>           
445            <p class="optionline">10:19 5:8 8:11</p>
446-->
447        </div>
448       
449      <!-- end of putting custom content -->
450
451    </div> <!-- end story-page-->   
452   
453      </div><!-- /content -->
454
455<!-- make persistent?
456      <div data-role="footer" style="width:100%;">
457    COSI: Centre of Open Source Innovation @ The University of Waikato.
458      </div>
459-->
460     
461    </div><!-- /page -->
462     
463  </body>
464</html>
Note: See TracBrowser for help on using the browser.