source: other-projects/nz-flag-design/trunk/main-form/iterative-design.html@ 29809

Last change on this file since 29809 was 29809, checked in by davidb, 9 years ago

Better computing info displayed

  • Property svn:executable set to *
File size: 14.6 KB
Line 
1<!DOCTYPE html>
2<html id="story">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
5 <meta name="viewport" content="width=device-width, initial-scale=1"/>
6
7 <!-- jQuery -->
8 <script src="lib/jquery-1.11.1.min.js"></script>
9
10 <script src="../similarity-2d/js-lib/Colour.js"></script>
11 <script src="../similarity-2d/flag-processing.js"></script>
12
13 <title>Iterative Design</title>
14 </head>
15
16 <body>
17 <div data-role="page" id="iterative-design-page"
18 class="demo-page"
19 data-dom-cache="true"
20 data-prev="choose-palette"
21 data-next="enter-name">
22
23 <div data-role="content">
24 <div data-role="controlgroup" class="control" data-mini="true">
25 <a href="#" class="next right-button test-fwd" style="right:1%;"></a>
26 <a href="#" class="prev left-button idea-back" style="left:1%;"></a>
27 </div>
28
29 <div class="story-page">
30
31 <!-- put custom content here -->
32 <span class="left story-icon research" ></span>
33 <h2 id="iterative-design-header">Iterative Design</h2>
34
35 <div style="padding-top: 10px;">
36 <div id="side-by-side">
37 <ol>
38
39 <li>
40 <h2><span>2D Design</span></h2>
41 <div id="design-2d-div" >
42 <iframe style="width: 100%; height: 100%"
43 id="design-2d-iframe" ></iframe>
44 </div>
45 </li>
46
47 <li>
48 <h2><span title="Click to Open/Close">Flag Similarity</span></h2>
49
50 <div id="similarity-2d-div">
51 <iframe src="../similarity-2d/display-flags.jsp"
52 style="width: 100%; height: 100%"
53 id="similarity-2d-iframe" ></iframe>
54 </div>
55 </li>
56 <li>
57 <h2><span title="Click to Open/Close">Render 3D</span></h2>
58 <div id="render-3d-div">
59 <iframe src="../render-3d/flag.html"
60 style="width: 100%; height: 100%; overflow: hidden;"
61 id="render-3d-iframe" ></iframe>
62 </div>
63 </li>
64
65 </ol>
66 </div>
67 </div>
68
69 <!-- end of putting custom content -->
70
71 </div> <!-- end story-page-->
72 </div><!-- /content -->
73
74 <script>
75 var activatedAccordion = false;
76 var activatedSVGEditorFrame = false;
77
78 function savePNGAsFile(imgType, quality) {
79 var design_frame = document.getElementById('design-2d-iframe');
80 var svgCanvas = design_frame.contentWindow.svgCanvas;
81 var svgedit = design_frame.contentWindow.svgedit;
82
83 var mimeType = 'image/' + imgType.toLowerCase();
84 var svg_str = svgCanvas.svgCanvasToString();
85
86 var type = imgType || 'PNG';
87 if (!$('#export_canvas').length) {
88 $('<canvas>', {id: 'export_canvas'}).hide().appendTo('body');
89 }
90 var c = $('#export_canvas')[0];
91 c.width = svgCanvas.contentW;
92 c.height = svgCanvas.contentH;
93
94 canvg(c, svg_str, {renderCallback: function() {
95 var dataURLType = (type === 'ICO' ? 'BMP' : type).toLowerCase();
96 var datauri = quality ? c.toDataURL('image/' + dataURLType, quality) : c.toDataURL('image/' + dataURLType);
97
98 // Look up jsession id; if not present fall back to use a timestamp
99 var jsession_id = $.cookie('JSESSIONID') || new Date().getTime();
100 //var jsession_id = $.cookie('JSESSIONID') + "-" + new Date().getTime(); // quick hack for summer scholarship presentation day
101
102 console.log("jsession id = " + jsession_id);
103 var imageFilename = "flag-" + jsession_id + ".png";
104
105 $.ajax({
106 type: "POST",
107 url: "../design-2d/image-data-to-file.jsp",
108 data: { imageData: datauri, imageFilename: imageFilename },
109 success: function() {
110 console.log("Exported PNG data to file ");
111 var render_frame = document.getElementById('render-3d-iframe');
112 // trigger a reload in the 3D Flag Renderer
113 render_frame.contentWindow.createFlag(imageFilename);
114 }
115 });
116 }});
117 }
118
119 function exportPNG() {
120 var design_frame = document.getElementById('design-2d-iframe')
121 var str = design_frame.contentWindow.svgEditor.uiStrings.notification.loadingImage;
122
123 var exportWindow = window.open(
124 'data:text/html;charset=utf-8,' + encodeURIComponent('<title>' + str + '</title><h1>' + str + '</h1>'),
125 'svg-edit-exportWindow'
126 );
127
128 design_frame.contentWindow.svgCanvas.rasterExport("PNG", null, exportWindow.name);
129 }
130
131 function reconfigureSvgCanvas(settings) {
132 console.log("*** entered reconfigureSvgCanvas");
133 console.log("*** settings: background=" + settings.backgroundColor);
134 console.log("*** settings: w x h = " + settings.width + " x " + settings.height);
135
136 var design_frame = document.getElementById('design-2d-iframe');
137 var svgCanvas = design_frame.contentWindow.svgCanvas;
138 var svgedit = design_frame.contentWindow.svgedit;
139
140 // Set the new canvas size
141 svgCanvas.setResolution(settings.width, settings.height);
142
143 var widthVar = settings.width;
144 var heightVar = settings.height;
145
146 var canvasWidth = svgCanvas.getResolution().w;
147 var canvasHeight = svgCanvas.getResolution().h;
148
149 var userBackground = settings.backgroundColor;
150
151 // decide if this is the first time we're looking to set the background colour
152 // or not. If it is, then use svgCanvas API to create a suitable layer
153
154 var backRectSvg = svgCanvas.getElem('backgroundRectangle');
155
156 if (backRectSvg == null) {
157 svgCanvas.createLayer("background");
158
159 svgCanvas.addSvgElementFromJson({
160 "element": "rect",
161 "attr": {
162 "id": "backgroundRectangle",
163 "width": canvasWidth,
164 "height": canvasHeight,
165 "x": "0",
166 "y": "0",
167 "fill": userBackground,
168 "opacity": "1.0",
169 "stroke-opacity": "0",
170 "stroke-linecap": "null",
171 "stroke-linejoin": "null",
172 "stroke-dasharray": "null",
173 "stroke-width": "5",
174 "stroke": "#000000"
175 },
176 "curStyles": true
177 });
178 svgCanvas.setCurrentLayerPosition(0);
179
180 // Refresh the canvas layers
181 svgCanvas.identifyLayers();
182 }
183 else {
184 console.log("Background to flag already exists -- changing colour to: " + userBackground);
185 backRectSvg.setAttributeNS(null,"fill",userBackground);
186 // find out how to change width and height directly also?
187
188 console.log("Setting background width and height to: " + widthVar + " and " + heightVar);
189
190 backRectSvg.setAttributeNS(null,"width",canvasWidth);
191 backRectSvg.setAttributeNS(null,"height",canvasHeight);
192
193 //svgCanvas.setResolution(backgroundWidth, backgroundHeight);
194 }
195 // Centres the canvas (somewhat)
196 //svgCanvas.updateCanvas(100, 100);
197
198 }
199
200
201 function rankFlags() {
202 console.log("rankFlags()");
203
204 // Get a message up on the screen to the similarity calculation is on its way
205 var $iframe = $('#similarity-2d-iframe').contents();
206
207 var $flagArea = $iframe.find('#flagArea');
208 var $progressArea = $iframe.find('#progressArea');
209
210 $progressArea.find('span:first').text("Comparing new flag with existing flags from around the world");
211 $progressArea.slideDown();
212 var $progressBar = $iframe.find('#progressBar');
213
214
215 setTimeout(function() {
216 // Compute colour histogram for user drawn flag
217 var imageCanvas = document.getElementById('export_canvas');
218 var context = imageCanvas.getContext('2d');
219 var imageData = context.getImageData(0,0, imageCanvas.width, imageCanvas.height);
220
221 var outputArray = createHSLHistogramFromCanvasData(imageData,imageCanvas.width,imageCanvas.height,16);
222
223 // Now compare this colour histogram with the stored ones, updating the progress bar as we go
224
225
226 calcSimilarityValues(outputArray, $flagArea,$progressArea,$progressBar);
227
228 // And finally reorder them based on this
229 sortFlagsBySimilarityValue($iframe)
230
231 $progressArea.slideUp();
232 }, 500);
233 }
234
235 function activateAccordion() {
236 console.log("activateAccordion()");
237
238 var la_x_dim = $(window).width() * 0.70;
239 var la_y_dim = $(window).height() * 0.67;
240
241 $('#side-by-side').show(10,
242 function() {
243 $('#side-by-side')
244 .liteAccordion({ theme : 'stitch', // 'light',
245 rounded : true,
246 //responsive : true,
247 containerWidth : la_x_dim,
248 containerHeight : la_y_dim,
249 onTriggerSlide : function() {
250 console.log("onTriggerSlide: this = " + this);
251 console.log("onTriggerSlide: id = " + this[0].id);
252 if (this[0].id == "similarity-2d-div") {
253 console.log("Triggering flag similarity comparison");
254 savePNGAsFile("PNG",null);
255
256 setTimeout(rankFlags, 1000);
257
258 }
259 if (this[0].id == "render-3d-div") {
260 console.log("Triggering SVG-Editor exportPNG()");
261 savePNGAsFile("PNG",null);
262 }
263 }
264 });
265 });
266 }
267
268 function activateSVGEditor() {
269 console.log("activateSVGEditor()");
270 $('#design-2d-iframe').attr("src","svg-edit-trunk/editor/svg-editor.html?noStorageOnLoad=1");
271 $('#design-2d-iframe').load(function() {
272 console.log("*** loaded sgv iframe: " + this);
273
274 var design_frame = document.getElementById('design-2d-iframe');
275 var svgCanvas = design_frame.contentWindow.svgCanvas;
276
277 console.log("*** svgCanvas should now be initialised: " + svgCanvas);
278 //don't load the previous drawing!
279 reconfigureSvgCanvas(flagCanvasSettings);
280 });
281 }
282
283 $(document).on("pageload",function(event,data){
284 console.log( "Iterative Design Page: jquerymobile pageload()");
285 if (!activatedAccordion) {
286 activateAccordion();
287 activatedAccordion = true;
288 }
289 });
290
291 $(window).resize(function() {
292 var la_x_dim = $(window).width() * 0.70;
293 var la_y_dim = $(window).height() * 0.67;
294
295 if ($('#side-by-side').liteAccordion) {
296 $('#side-by-side')
297 .liteAccordion('resize', {
298 containerWidth : la_x_dim,
299 containerHeight : la_y_dim
300 });
301 }
302 });
303
304 $(document).on("pagebeforeshow",function(event,data) {
305 var active_page = $.mobile.activePage.attr("id");
306 console.log("Iterative-Design fielding Event: pagebeforeshow() for page " + active_page);
307
308 if (active_page == "iterative-design-page") {
309 if (!activatedSVGEditorFrame) {
310 activateSVGEditor();
311 activatedSVGEditorFrame = true;
312 }
313 else {
314 reconfigureSvgCanvas(flagCanvasSettings);
315 }
316 }
317 });
318 </script>
319
320 </div><!-- /page -->
321 </body>
322</html>
Note: See TracBrowser for help on using the repository browser.