1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is KaiRo.at Mandelbrot, XULRunner version.
16 * The Initial Developer of the Original Code is
17 * Robert Kaiser <kairo@kairo.at>.
18 * Portions created by the Initial Developer are Copyright (C) 2008-2011
19 * the Initial Developer. All Rights Reserved.
22 * Robert Kaiser <kairo@kairo.at>
23 * prefiks (patch for some speedups)
24 * Boris Zbarsky <bzbarsky@mit.edu> (use imageData for canvas interaction)
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 Components.utils.import("resource://gre/modules/Services.jsm");
42 var gColorPalette = [];
45 var gCurrentImageData;
49 gMbrotBundle = document.getElementById("mbrotBundle");
50 document.getElementById("statusLabel").value = gMbrotBundle.getString("statusEmpty");
52 let img = document.getElementById("mbrotImage");
53 img.addEventListener("mouseup", imgEvHandler, false);
54 img.addEventListener("mousedown", imgEvHandler, false);
55 img.addEventListener("mousemove", imgEvHandler, false);
56 img.addEventListener("touchstart", imgEvHandler, false);
57 img.addEventListener("touchend", imgEvHandler, false);
58 img.addEventListener("touchcancel", imgEvHandler, false);
59 img.addEventListener("touchleave", imgEvHandler, false);
60 img.addEventListener("touchmove", imgEvHandler, false);
62 Services.obs.notifyObservers(window, "mandelbrot-loaded", null);
65 function getAdjustPref(prefname) {
72 value = Services.prefs.getIntPref("mandelbrot." + prefname);
75 if ((value < 10) || (value > 5000)) {
77 Services.prefs.setIntPref("mandelbrot." + prefname, value);
80 case "last_image.Cr_*":
84 Cr_min = parseFloat(Services.prefs.getCharPref("mandelbrot.last_image.Cr_min"));
85 Cr_max = parseFloat(Services.prefs.getCharPref("mandelbrot.last_image.Cr_max"));
88 if ((Cr_min < -3) || (Cr_min > 2) ||
89 (Cr_max < -3) || (Cr_max > 2) || (Cr_min >= Cr_max)) {
90 Cr_min = -2.0; Cr_max = 1.0;
92 Services.prefs.setCharPref("mandelbrot.last_image.Cr_min", Cr_min);
93 Services.prefs.setCharPref("mandelbrot.last_image.Cr_max", Cr_max);
94 return {Cr_min: Cr_min, Cr_max: Cr_max};
95 case "last_image.Ci_*":
99 Ci_min = parseFloat(Services.prefs.getCharPref("mandelbrot.last_image.Ci_min"));
100 Ci_max = parseFloat(Services.prefs.getCharPref("mandelbrot.last_image.Ci_max"));
103 if ((Ci_min < -2.5) || (Ci_min > 2.5) ||
104 (Ci_max < -2.5) || (Ci_max > 2.5) || (Ci_min >= Ci_max)) {
105 Ci_min = -1.5; Ci_max = 1.5;
107 Services.prefs.setCharPref("mandelbrot.last_image.Ci_min", Ci_min);
108 Services.prefs.setCharPref("mandelbrot.last_image.Ci_max", Ci_max);
109 return {Ci_min: Ci_min, Ci_max: Ci_max};
110 case "iteration_max":
113 value = Services.prefs.getIntPref("mandelbrot." + prefname);
118 if (value < 10 || value > 10000) {
123 case "use_algorithm":
126 value = Services.prefs.getCharPref("mandelbrot." + prefname);
132 case "color_palette":
135 value = Services.prefs.getCharPref("mandelbrot." + prefname);
141 case "syncProportions":
144 value = Services.prefs.getBoolPref("mandelbrot." + prefname);
147 Services.prefs.setBoolPref("mandelbrot." + prefname, value);
155 function adjustCoordsAndDraw(aC_min, aC_max) {
156 let iWidth = getAdjustPref("image.width");
157 let iHeight = getAdjustPref("image.height");
159 // correct coordinates
164 if ((aC_min.r > 2) || (aC_max.r < -2) || (aC_min.r >= aC_max.r)) {
165 aC_min.r = -2.0; aC_max.r = 1.0;
171 if ((aC_min.i > 2) || (aC_max.i < -2) || (aC_min.i >= aC_max.i)) {
172 aC_min.i = -1.3; aC_max.i = 1.3;
175 let CWidth = aC_max.r - aC_min.r;
176 let CHeight = aC_max.i - aC_min.i;
177 let C_mid = new complex(aC_min.r + CWidth / 2, aC_min.i + CHeight / 2);
179 let CRatio = Math.max(CWidth / iWidth, CHeight / iHeight);
181 Services.prefs.setCharPref("mandelbrot.last_image.Cr_min", C_mid.r - iWidth * CRatio / 2);
182 Services.prefs.setCharPref("mandelbrot.last_image.Cr_max", C_mid.r + iWidth * CRatio / 2);
183 Services.prefs.setCharPref("mandelbrot.last_image.Ci_min", C_mid.i - iHeight * CRatio / 2);
184 Services.prefs.setCharPref("mandelbrot.last_image.Ci_max", C_mid.i + iHeight * CRatio / 2);
189 function drawImage() {
190 let canvas = document.getElementById("mbrotImage");
191 let context = canvas.getContext("2d");
193 document.getElementById("drawButton").hidden = true;
195 document.getElementById("statusLabel").value = gMbrotBundle.getString("statusDrawing");
197 if (gCurrentImageData) {
198 gLastImageData = gCurrentImageData;
201 let Cr_vals = getAdjustPref("last_image.Cr_*");
202 let Cr_min = Cr_vals.Cr_min;
203 let Cr_max = Cr_vals.Cr_max;
205 let Ci_vals = getAdjustPref("last_image.Ci_*");
206 let Ci_min = Ci_vals.Ci_min;
207 let Ci_max = Ci_vals.Ci_max;
209 let iterMax = getAdjustPref("iteration_max");
210 let algorithm = getAdjustPref("use_algorithm");
212 let currentPalette = getAdjustPref("color_palette");
213 if (!gColorPalette || !gColorPalette.length)
214 gColorPalette = getColorPalette(currentPalette);
216 let iWidth = getAdjustPref("image.width");
217 let iHeight = getAdjustPref("image.height");
219 gCurrentImageData = {
220 C_min: new complex(Cr_min, Ci_min),
221 C_max: new complex(Cr_max, Ci_max),
227 canvas.width = iWidth;
228 canvas.height = iHeight;
230 context.fillStyle = "rgba(255, 255, 255, 127)";
231 context.fillRect(0, 0, canvas.width, canvas.height);
233 gStartTime = new Date();
235 drawLine(0, [Cr_min, Cr_max, Ci_min, Ci_max],
236 canvas, context, iterMax, algorithm);
239 function drawLine(line, dimensions, canvas, context, iterMax, algorithm) {
240 let Cr_min = dimensions[0];
241 let Cr_max = dimensions[1];
242 let Cr_scale = Cr_max - Cr_min;
244 let Ci_min = dimensions[2];
245 let Ci_max = dimensions[3];
246 let Ci_scale = Ci_max - Ci_min;
248 let lines = Math.min(canvas.height - line, 8);
249 let imageData = context.createImageData(canvas.width, lines);
250 let pixels = imageData.data;
252 for (var img_y = line; img_y < canvas.height && img_y < line+8; img_y++)
253 for (let img_x = 0; img_x < canvas.width; img_x++) {
254 let C = new complex(Cr_min + (img_x / canvas.width) * Cr_scale,
255 Ci_min + (img_y / canvas.height) * Ci_scale);
256 let colors = drawPoint(context, img_x, img_y, C, iterMax, algorithm);
257 pixels[idx++] = colors[0];
258 pixels[idx++] = colors[1];
259 pixels[idx++] = colors[2];
260 pixels[idx++] = colors[3];
262 context.putImageData(imageData, 0, line);
264 if (img_y < canvas.height)
265 setTimeout(drawLine, 0, img_y, dimensions, canvas, context, iterMax, algorithm);
271 let endTime = new Date();
272 let timeUsed = (endTime.getTime() - gStartTime.getTime()) / 1000;
273 document.getElementById("statusLabel").value =
274 gMbrotBundle.getFormattedString("statusTime", [timeUsed.toFixed(3)]);
278 function complex(aReal, aImag) {
282 complex.prototype = {
284 return new complex(this.r * this.r - this.i * this.i,
285 2 * this.r * this.i);
288 return Math.sqrt(this.r * this.r + this.i * this.i);
290 add: function(aComplex) {
291 return new complex(this.r + aComplex.r, this.i + aComplex.i);
295 function mandelbrotValueOO (aC, aIterMax) {
296 // this would be nice code in general but it looks like JS objects are too heavy for normal use.
297 let Z = new complex(0.0, 0.0);
298 for (var iter = 0; iter < aIterMax; iter++) {
299 Z = Z.square().add(aC);
300 if (Z.r * Z.r + Z.i * Z.i > 256) { break; }
305 function mandelbrotValueNumeric (aC, aIterMax) {
306 // optimized numeric code for fast calculation
307 let Cr = aC.r, Ci = aC.i;
308 let Zr = 0.0, Zi = 0.0;
309 let Zr2 = Zr * Zr, Zi2 = Zi * Zi;
310 for (var iter = 0; iter < aIterMax; iter++) {
311 Zi = 2 * Zr * Zi + Ci;
314 Zr2 = Zr * Zr; Zi2 = Zi * Zi;
315 if (Zr2 + Zi2 > 256) { break; }
320 function getColor(aIterValue, aIterMax) {
321 let standardizedValue = Math.round(aIterValue * 1024 / aIterMax);
322 if (gColorPalette && gColorPalette.length)
323 return gColorPalette[standardizedValue];
325 // fallback to simple b/w if for some reason we don't have a palette
326 if (aIterValue == aIterMax)
327 return [0, 0, 0, 255];
329 return [255, 255, 255, 255];
332 function getColorPalette(palName) {
336 for (let i = 0; i < 1024; i++) {
337 palette[i] = [255, 255, 255, 255];
339 palette[1024] = [0, 0, 0, 255];
343 for (let i = 0; i < 32; i++) {
344 let cc1 = Math.floor(i * 127 / 31);
345 let cc2 = 170 - Math.floor(i * 43 / 31);
346 palette[i] = [cc1, cc2, cc1, 255];
349 for (let i = 0; i < 51; i++) {
350 let cc = Math.floor(i * 170 / 50);
351 palette[32 + i] = [cc, 0, (170-cc), 255];
354 for (let i = 0; i < 101; i++) {
355 let cc = Math.floor(i * 200 / 100);
356 palette[83 + i] = [255, cc, 0, 255];
359 for (let i = 0; i < 201; i++) {
360 let cc1 = 255 - Math.floor(i * 85 / 200);
361 let cc2 = 200 - Math.floor(i * 30 / 200);
362 let cc3 = Math.floor(i * 170 / 200);
363 palette[184 + i] = [cc1, cc2, cc3, 255];
365 for (let i = 0; i < 301; i++) {
366 let cc1 = 170 - Math.floor(i * 43 / 300);
367 let cc2 = 170 + Math.floor(i * 85 / 300);
368 palette[385 + i] = [cc1, cc1, cc2, 255];
370 for (let i = 0; i < 338; i++) {
371 let cc = 127 + Math.floor(i * 128 / 337);
372 palette[686 + i] = [cc, cc, 255, 255];
374 palette[1024] = [0, 0, 0, 255];
376 case 'rainbow-linear1':
377 for (let i = 0; i < 256; i++) {
378 palette[i] = [i, 0, 0, 255];
379 palette[256 + i] = [255, i, 0, 255];
380 palette[512 + i] = [255 - i, 255, i, 255];
381 palette[768 + i] = [i, 255-i, 255, 255];
383 palette[1024] = [0, 0, 0, 255];
385 case 'rainbow-squared1':
386 for (let i = 0; i < 34; i++) {
387 let cc = Math.floor(i * 255 / 33);
388 palette[i] = [cc, 0, 0, 255];
390 for (let i = 0; i < 137; i++) {
391 let cc = Math.floor(i * 255 / 136);
392 palette[34 + i] = [255, cc, 0, 255];
394 for (let i = 0; i < 307; i++) {
395 let cc = Math.floor(i * 255 / 306);
396 palette[171 + i] = [255 - cc, 255, cc, 255];
398 for (let i = 0; i < 546; i++) {
399 let cc = Math.floor(i * 255 / 545);
400 palette[478 + i] = [cc, 255 - cc, 255, 255];
402 palette[1024] = [0, 0, 0, 255];
404 case 'rainbow-linear2':
405 for (let i = 0; i < 205; i++) {
406 let cc = Math.floor(i * 255 / 204);
407 palette[i] = [255, cc, 0, 255];
408 palette[204 + i] = [255 - cc, 255, 0, 255];
409 palette[409 + i] = [0, 255, cc, 255];
410 palette[614 + i] = [0, 255 - cc, 255, 255];
411 palette[819 + i] = [cc, 0, 255, 255];
413 palette[1024] = [0, 0, 0, 255];
415 case 'rainbow-squared2':
416 for (let i = 0; i < 19; i++) {
417 let cc = Math.floor(i * 255 / 18);
418 palette[i] = [255, cc, 0, 255];
420 for (let i = 0; i < 74; i++) {
421 let cc = Math.floor(i * 255 / 73);
422 palette[19 + i] = [255 - cc, 255, 0, 255];
424 for (let i = 0; i < 168; i++) {
425 let cc = Math.floor(i * 255 / 167);
426 palette[93 + i] = [0, 255, cc, 255];
428 for (let i = 0; i < 298; i++) {
429 let cc = Math.floor(i * 255 / 297);
430 palette[261 + i] = [0, 255 - cc, 255, 255];
432 for (let i = 0; i < 465; i++) {
433 let cc = Math.floor(i * 255 / 464);
434 palette[559 + i] = [cc, 0, 255, 255];
436 palette[1024] = [0, 0, 0, 255];
440 'Standard-Palette (QB-Colors)
442 xx = CInt(i * 500 / 1024 + 2)
443 If xx <= 15 Then clr = xx
444 If xx > 15 Then clr = CInt(Sqr((xx - 15 + 1) * 15 ^ 2 / 485))
445 If xx >= 500 Then clr = 0
446 palette(i) = QBColor(clr)
452 function drawPoint(context, img_x, img_y, C, iterMax, algorithm) {
456 itVal = mandelbrotValueOO(C, iterMax);
460 itVal = mandelbrotValueNumeric(C, iterMax);
463 return getColor(itVal, iterMax);
466 /***** pure UI functions *****/
473 handleEvent: function(aEvent) {
474 let canvas = document.getElementById("mbrotImage");
475 let context = canvas.getContext("2d");
476 let touchEvent = aEvent.type.indexOf('touch') != -1;
478 // Bail out if this is neither a touch nor left-click.
479 if (!touchEvent && aEvent.button != 0)
482 // Bail out if the started touch can't be found.
483 if (touchEvent && zoomstart &&
484 !aEvent.changedTouches.identifiedTouch(zoomTouchID))
487 let coordObj = touchEvent ?
488 aEvent.changedTouches.identifiedTouch(zoomTouchID) :
491 switch (aEvent.type) {
495 zoomTouchID = aEvent.changedTouches.item(0).identifier;
496 coordObj = aEvent.changedTouches.identifiedTouch(zoomTouchID);
498 // left button - start dragzoom
499 zoomstart = {x: coordObj.clientX - canvas.offsetLeft,
500 y: coordObj.clientY - canvas.offsetTop};
501 imgBackup = context.getImageData(0, 0, canvas.width, canvas.height);
506 context.putImageData(imgBackup, 0, 0);
507 let zoomend = {x: coordObj.clientX - canvas.offsetLeft,
508 y: coordObj.clientY - canvas.offsetTop};
510 // make sure zoomend is bigger than zoomstart
511 if ((zoomend.x == zoomstart.x) || (zoomend.y == zoomstart.y)) {
512 // cannot zoom what has no area, discard it
513 zoomstart = undefined;
516 if (zoomend.x < zoomstart.x)
517 [zoomend.x, zoomstart.x] = [zoomstart.x, zoomend.x];
518 if (zoomend.y < zoomstart.y)
519 [zoomend.y, zoomstart.y] = [zoomstart.y, zoomend.y];
521 // determine new "coordinates"
522 let CWidth = gCurrentImageData.C_max.r - gCurrentImageData.C_min.r;
523 let CHeight = gCurrentImageData.C_max.i - gCurrentImageData.C_min.i;
524 let newC_min = new complex(
525 gCurrentImageData.C_min.r + zoomstart.x / gCurrentImageData.iWidth * CWidth,
526 gCurrentImageData.C_min.i + zoomstart.y / gCurrentImageData.iHeight * CHeight);
527 let newC_max = new complex(
528 gCurrentImageData.C_min.r + zoomend.x / gCurrentImageData.iWidth * CWidth,
529 gCurrentImageData.C_min.i + zoomend.y / gCurrentImageData.iHeight * CHeight);
531 adjustCoordsAndDraw(newC_min, newC_max);
533 zoomstart = undefined;
538 context.putImageData(imgBackup, 0, 0);
539 context.strokeStyle = "rgb(255,255,31)";
540 context.strokeRect(zoomstart.x, zoomstart.y,
541 coordObj.clientX - canvas.offsetLeft - zoomstart.x,
542 coordObj.clientY - canvas.offsetTop - zoomstart.y);
549 function saveImage() {
550 const nsIFilePicker = Components.interfaces.nsIFilePicker;
553 fp = Components.classes["@mozilla.org/filepicker;1"]
554 .createInstance(nsIFilePicker);
557 let promptString = gMbrotBundle.getString("savePrompt");
558 fp.init(window, promptString, nsIFilePicker.modeSave);
559 fp.appendFilter(gMbrotBundle.getString("pngFilterName"), "*.png");
560 fp.defaultString = "mandelbrot.png";
562 let fpResult = fp.show();
563 if (fpResult != nsIFilePicker.returnCancel) {
564 saveCanvas(document.getElementById("mbrotImage"), fp.file);
568 function updateBookmarkMenu(aParent) {
569 document.getElementById("bookmarkSave").disabled =
570 (!document.getElementById("drawButton").hidden || (gStartTime > 0));
572 document.getElementById("bookmarkBack").disabled = !gLastImageData;
574 while (aParent.hasChildNodes() &&
575 aParent.lastChild.id != "bookmarkSeparator")
576 aParent.removeChild(aParent.lastChild);
578 let file = Components.classes["@mozilla.org/file/directory_service;1"]
579 .getService(Components.interfaces.nsIProperties)
580 .get("ProfD", Components.interfaces.nsIFile);
581 file.append("mandelbookmarks.sqlite");
583 let connection = Components.classes["@mozilla.org/storage/service;1"]
584 .getService(Components.interfaces.mozIStorageService)
587 if (connection.tableExists("bookmarks")) {
588 let statement = connection.createStatement(
589 "SELECT name,ROWID FROM bookmarks ORDER BY ROWID ASC");
590 while (statement.executeStep()) {
591 let newItem = aParent.appendChild(document.createElement("menuitem"));
592 newItem.setAttribute("label", statement.getString(0));
593 newItem.setAttribute("bmRowID", statement.getString(1));
596 statement.finalize();
603 // Create the "Nothing Available" Menu item and disable it.
604 let na = aParent.appendChild(document.createElement("menuitem"));
605 na.setAttribute("label", gMbrotBundle.getString("noBookmarks"));
606 na.setAttribute("disabled", "true");
610 if (gLastImageData) {
611 Services.prefs.setIntPref("mandelbrot.iteration_max",
612 gLastImageData.iterMax);
613 // use gLastImageData.iWidth, gLastImageData.iHeight ???
614 adjustCoordsAndDraw(gLastImageData.C_min, gLastImageData.C_max);
615 gLastImageData = undefined;
619 function callBookmark(evtarget) {
620 if (evtarget.id == "bookmarkSave" || evtarget.id == "bookmarkSeparator")
622 if (evtarget.id == "bookmarkOverview") {
623 adjustCoordsAndDraw(new complex(0,0), new complex(0,0));
627 if (evtarget.getAttribute('bmRowID')) {
632 let file = Components.classes["@mozilla.org/file/directory_service;1"]
633 .getService(Components.interfaces.nsIProperties)
634 .get("ProfD", Components.interfaces.nsIFile);
635 file.append("mandelbookmarks.sqlite");
636 let connection = Components.classes["@mozilla.org/storage/service;1"]
637 .getService(Components.interfaces.mozIStorageService)
639 let statement = connection.createStatement(
640 "SELECT iteration_max,Cr_min,Cr_max,Ci_min,Ci_max FROM bookmarks WHERE ROWID=?1");
641 statement.bindStringParameter(0, evtarget.getAttribute('bmRowID'));
642 while (statement.executeStep()) {
643 iterMax = statement.getInt32(0);
644 C_min = new complex(statement.getDouble(1), statement.getDouble(3));
645 C_max = new complex(statement.getDouble(2), statement.getDouble(4));
647 statement.finalize();
650 if (iterMax && C_min && C_max) {
651 Services.prefs.setIntPref("mandelbrot.iteration_max", iterMax);
652 adjustCoordsAndDraw(C_min, C_max);
657 function saveBookmark() {
658 // retrieve wanted bookmark name with a prompt
659 let prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
660 .getService(Components.interfaces.nsIPromptService);
661 let input = {value: ""}; // empty default value
662 let ok = prompts.prompt(null, gMbrotBundle.getString("saveBookmarkTitle"), gMbrotBundle.getString("saveBookmarkLabel"), input, null, {});
663 // ok is true if OK is pressed, false if Cancel. input.value holds the value of the edit field if "OK" was pressed.
664 if (!ok || !input.value)
667 let bmName = input.value;
669 // Open or create the bookmarks database.
670 let file = Components.classes["@mozilla.org/file/directory_service;1"]
671 .getService(Components.interfaces.nsIProperties)
672 .get("ProfD", Components.interfaces.nsIFile);
673 file.append("mandelbookmarks.sqlite");
674 let connection = Components.classes["@mozilla.org/storage/service;1"]
675 .getService(Components.interfaces.mozIStorageService)
677 connection.beginTransaction();
678 if (!connection.tableExists("bookmarks"))
679 connection.createTable("bookmarks", "name TEXT, iteration_max INTEGER, Cr_min REAL, Cr_max REAL, Ci_min REAL, Ci_max REAL");
680 // NULL. The value is a NULL value.
681 // INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
682 // REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.
683 // TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16-LE).
685 // Put value of the current image into the bookmarks table
686 let statement = connection.createStatement(
687 "INSERT INTO bookmarks (name,iteration_max,Cr_min,Cr_max,Ci_min,Ci_max) VALUES (?1,?2,?3,?4,?5,?6)");
688 statement.bindStringParameter(0, bmName);
689 statement.bindStringParameter(1, gCurrentImageData.iterMax);
690 statement.bindStringParameter(2, gCurrentImageData.C_min.r);
691 statement.bindStringParameter(3, gCurrentImageData.C_max.r);
692 statement.bindStringParameter(4, gCurrentImageData.C_min.i);
693 statement.bindStringParameter(5, gCurrentImageData.C_max.i);
695 statement.finalize();
696 connection.commitTransaction();
700 function imgSettings() {
702 let position = "before_start";
703 if (document.getElementById("mandelbrot-page").nodeName == "page") {
704 anchor = document.getElementById("mandelbrotToolbar");
707 anchor = document.getElementById("mandelbrotMenubar");
708 position = "after_start";
710 document.getElementById("imgSettingsPanel").showPopup(anchor, position);
713 function initImgSettings() {
714 // Get values from prefs.
715 for each (let coord in ["Cr", "Ci"]) {
716 let coord_vals = getAdjustPref("last_image." + coord + "_*");
717 document.getElementById("is_" + coord + "_min").value = coord_vals[coord + "_min"];
718 document.getElementById("is_" + coord + "_max").value = coord_vals[coord + "_max"];
720 for each (let dim in ["width", "height"]) {
721 document.getElementById("is_img_" + dim).value = getAdjustPref("image." + dim);
723 document.getElementById("is_syncProp").checked = getAdjustPref("syncProportions");
726 recalcCoord("Cr", "scale");
727 recalcCoord("Ci", "scale");
729 // Clear the preview.
730 let canvas = document.getElementById("is_mbrotPreview");
731 let context = canvas.getContext("2d");
732 context.fillStyle = "rgba(255, 255, 255, 127)";
733 context.fillRect(0, 0, canvas.width, canvas.height);
735 // Set lists to correct values.
741 function closeImgSettings() {
742 // Hide popup, which will automatically make a call to save values.
743 document.getElementById("imgSettingsPanel").hidePopup();
746 function saveImgSettings() {
747 // Get values to prefs.
748 for each (let coord in ["Cr_min", "Cr_max", "Ci_min", "Ci_max"]) {
749 Services.prefs.setCharPref("mandelbrot.last_image." + coord,
750 document.getElementById("is_" + coord).value);
752 for each (let dim in ["width", "height"]) {
753 Services.prefs.setIntPref("mandelbrot.image." + dim,
754 document.getElementById("is_img_" + dim).value);
756 Services.prefs.setBoolPref("mandelbrot.syncProportions",
757 document.getElementById("is_syncProp").checked);
760 function checkISValue(textbox, type) {
761 if (type == "coord") {
762 textbox.value = roundCoord(parseFloat(textbox.value));
764 else if (type == "dim") {
765 textbox.value = parseInt(textbox.value);
769 function drawPreview() {
770 let canvas = document.getElementById("is_mbrotPreview");
771 let context = canvas.getContext("2d");
773 if (document.getElementById("is_img_width").value /
774 document.getElementById("is_img_height").value
777 canvas.width = canvas.height *
778 document.getElementById("is_img_width").value /
779 document.getElementById("is_img_height").value;
783 canvas.height = canvas.width *
784 document.getElementById("is_imgHeight").value /
785 document.getElementById("is_imgWidth").value;
788 let Cr_min = parseFloat(document.getElementById("is_Cr_min").value);
789 let Cr_max = parseFloat(document.getElementById("is_Cr_max").value);
790 if ((Cr_min < -2) || (Cr_min > 2) ||
791 (Cr_max < -2) || (Cr_max > 2) || (Cr_min >= Cr_max)) {
792 Cr_min = -2.0; Cr_max = 1.0;
795 let Ci_min = parseFloat(document.getElementById("is_Ci_min").value);
796 let Ci_max = parseFloat(document.getElementById("is_Ci_max").value);
797 if ((Ci_min < -2) || (Ci_min > 2) ||
798 (Ci_max < -2) || (Ci_max > 2) || (Ci_min >= Ci_max)) {
799 Ci_min = -2.0; Ci_max = 1.0;
802 let iterMax = getAdjustPref("iteration_max");
803 let algorithm = getAdjustPref("use_algorithm");
805 context.fillStyle = "rgba(255, 255, 255, 127)";
806 context.fillRect(0, 0, canvas.width, canvas.height);
808 let currentPalette = getAdjustPref("color_palette");
809 gColorPalette = getColorPalette(currentPalette);
811 drawLine(0, [Cr_min, Cr_max, Ci_min, Ci_max],
812 canvas, context, iterMax, algorithm);
815 function recalcCoord(coord, target) {
816 let othercoord = (coord == "Ci") ? "Cr" : "Ci";
817 let owndim = (coord == "Ci") ? "height" : "width";
818 let otherdim = (coord == "Ci") ? "width" : "height";
820 if (target == "scale") {
822 parseFloat(document.getElementById("is_" + coord + "_max").value) -
823 parseFloat(document.getElementById("is_" + coord + "_min").value);
824 document.getElementById("is_" + coord + "_scale").value = roundCoord(myscale);
826 else if (target == 'max') {
828 parseFloat(document.getElementById("is_" + coord + "_min").value) +
829 parseFloat(document.getElementById("is_" + coord + "_scale").value);
830 document.getElementById("is_" + coord + "_max").value = roundCoord(mymax);
831 myscale = document.getElementById("is_" + coord + "_scale").value;
833 if (document.getElementById("is_syncProp").checked) {
834 let otherscale = myscale *
835 document.getElementById("is_img_" + otherdim).value /
836 document.getElementById("is_img_" + owndim).value;
837 document.getElementById("is_" + othercoord + "_scale").value = roundCoord(otherscale);
839 parseFloat(document.getElementById("is_" + othercoord + "_min").value) +
840 parseFloat(document.getElementById("is_" + othercoord + "_scale").value);
841 document.getElementById("is_" + othercoord + "_max").value = roundCoord(othermax);
845 function checkProportions() {
846 if (!document.getElementById("is_syncProp").checked) {
847 recalcCoord("Cr", "scale");
851 function roundCoord(floatval) {
852 // We should round to 10 decimals here or so
853 return parseFloat(floatval.toFixed(10));
856 function updateIterList() {
857 let currentIter = getAdjustPref("iteration_max");
858 document.getElementById("iterList").value = currentIter;
861 function updatePaletteList() {
862 let currentPalette = getAdjustPref("color_palette");
863 if (!gColorPalette || !gColorPalette.length)
864 gColorPalette = getColorPalette(currentPalette);
865 document.getElementById("colorList").value = currentPalette;
868 function updateAlgoList() {
869 let currentAlgo = getAdjustPref("use_algorithm");
870 document.getElementById("algoList").value = currentAlgo;
873 function setIter(aIter) {
874 Services.prefs.setIntPref("mandelbrot.iteration_max", aIter);
877 function setPalette(aPaletteID) {
878 Services.prefs.setCharPref("mandelbrot.color_palette", aPaletteID);
879 gColorPalette = getColorPalette(aPaletteID);
882 function setAlgorithm(algoID) {
883 Services.prefs.setCharPref("mandelbrot.use_algorithm", algoID);
887 /***** helper functions from external sources *****/
889 // function below is based on http://developer.mozilla.org/en/docs/Code_snippets:Canvas
890 // custom modifications:
891 // - use "a"-prefix on function arguments
892 // - take an nsILocalFile as aDestFile argument
893 // - always do silent download
894 function saveCanvas(aCanvas, aDestFile) {
895 // create a data url from the canvas and then create URIs of the source and targets
896 var io = Components.classes["@mozilla.org/network/io-service;1"]
897 .getService(Components.interfaces.nsIIOService);
898 var source = io.newURI(aCanvas.toDataURL("image/png", ""), "UTF8", null);
900 // prepare to save the canvas data
901 var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
902 .createInstance(Components.interfaces.nsIWebBrowserPersist);
904 persist.persistFlags = Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
905 persist.persistFlags |= Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
907 // save the canvas data to the file
908 persist.saveURI(source, null, null, null, null, aDestFile);