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

Revision 29925, 15.2 KB (checked in by davidb, 4 years ago)

Some minor tidy up, after some debugging

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