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

Revision 29811, 14.8 KB (checked in by davidb, 5 years ago)

Additional coding so the colour palette choices now feed through to the svg-editor

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