-var gColorPalette = getColorPalette('kairo');
+var gColorPalette = [];
+var gPref = Components.classes["@mozilla.org/preferences-service;1"]
+ .getService(Components.interfaces.nsIPrefService)
+ .getBranch(null);
+var gStartTime = 0;
+
+function Startup() {
+ updateIterMenu();
+ updatePaletteMenu();
+ document.getElementById("statusLabel").value =
+ document.getElementById("mbrotBundle").getString("statusEmpty");
+}
function drawImage() {
let canvas = document.getElementById("mbrotImage");
if (canvas.getContext) {
let context = canvas.getContext("2d");
+ document.getElementById("statusLabel").value =
+ document.getElementById("mbrotBundle").getString("statusDrawing");
+
// example:
// context.fillStyle = "rgb(200,0,0)";
// context.fillRect (10, 10, 55, 50); // x, y, width, height
let Ci_max = 1.5;
let Ci_scale = Ci_max - Ci_min;
- let iterMax = 500;
+ let iterMax = gPref.getIntPref("mandelbrot.iteration_max");
+ let algorithm = gPref.getCharPref("mandelbrot.use_algorithm");
+
+ gStartTime = new Date();
for (let img_x = 0; img_x < canvas.width; img_x++) {
for (let img_y = 0; img_y < canvas.height; img_y++) {
let C = new complex(Cr_min + (img_x / canvas.width) * Cr_scale,
Ci_min + (img_y / canvas.height) * Ci_scale);
- window.setTimeout(drawPoint, 0, context, img_x, img_y, C, iterMax);
+ window.setTimeout(drawPoint, 0, context, img_x, img_y, C, iterMax, algorithm);
}
}
+ window.setTimeout(EndCalc, 0);
}
}
+function EndCalc() {
+ let endTime = new Date();
+ let timeUsed = (endTime.getTime() - gStartTime.getTime()) / 1000;
+ document.getElementById("statusLabel").value =
+ document.getElementById("mbrotBundle").getFormattedString("statusTime", [timeUsed.toFixed(3)]);
+}
+
function complex(aReal, aImag) {
this.r = aReal;
this.i = aImag;
}
}
-function mandelbrotValue (aC, aIterMax) {
- /* XXX: this would be nice code but it looks like JS objects are too heavy for this.
+function mandelbrotValueOO (aC, aIterMax) {
+ // this would be nice code in general but it looks like JS objects are too heavy for normal use.
let Z = new complex(0.0, 0.0);
for (var iter = 0; iter < aIterMax; iter++) {
Z = Z.square().add(aC);
if (Z.r * Z.r + Z.i * Z.i > 256) { break; }
}
- */
+ return iter;
+}
- // highly optimized code for fast calculation
+function mandelbrotValueNumeric (aC, aIterMax) {
+ // optimized numeric code for fast calculation
let Cr = aC.r, Ci = aC.i;
let Zr = 0.0, Zi = 0.0;
let Zr2 = Zr * Zr, Zi2 = Zi * Zi;
function getColor(aIterValue, aIterMax) {
let standardizedValue = Math.round(aIterValue * 1024 / aIterMax);
- return gColorPalette[standardizedValue];
- if (aIterValue == aIterMax) {
+ if (gColorPalette && gColorPalette.length)
+ return gColorPalette[standardizedValue];
+
+ // fallback to simple b/w if for some reason we don't have a palette
+ if (aIterValue == aIterMax)
return "rgb(0,0,0)";
- }
- else {
- //return "rgb(" + img_x + "," + img_y + ",0)";
+ else
return "rgb(255,255,255)";
- }
}
function getColorPalette(palName) {
return palette;
}
-function drawPoint(context, img_x, img_y, C, iterMax) {
- let itVal = mandelbrotValue(C, iterMax);
+function drawPoint(context, img_x, img_y, C, iterMax, algorithm) {
+ var itVal;
+ switch (algorithm) {
+ case 'oo':
+ itVal = mandelbrotValueOO(C, iterMax);
+ break;
+ case 'numeric':
+ default:
+ itVal = mandelbrotValueNumeric(C, iterMax);
+ break;
+ }
context.fillStyle = getColor(itVal, iterMax);
context.fillRect (img_x, img_y, 1, 1); // x, y, width, height
}
+/***** pure UI functions *****/
+
function saveImage() {
- // should call filepicker!
+ // XXX: should call filepicker!
saveCanvas(document.getElementById("mbrotImage"), "/home/robert/temp/canvas-save.png")
}
+function updateIterMenu() {
+ try {
+ var currentIter = gPref.getIntPref("mandelbrot.iteration_max");
+ }
+ catch(e) {
+ var currentIter = 0;
+ }
+ if (currentIter < 10) {
+ currentIter = 500;
+ setIter(currentIter);
+ }
+
+ var popup = document.getElementById("menu_iterPopup");
+ var item = popup.firstChild;
+ while (item) {
+ if (item.getAttribute("name") == "iter") {
+ if (item.getAttribute("value") == currentIter)
+ item.setAttribute("checked","true");
+ else
+ item.removeAttribute("checked");
+ }
+ item = item.nextSibling;
+ }
+}
+
+function setIter(aIter) {
+ gPref.setIntPref("mandelbrot.iteration_max", aIter);
+}
+
+function updatePaletteMenu() {
+ try {
+ var currentPalette = gPref.getCharPref("mandelbrot.color_palette");
+ }
+ catch(e) {
+ var currentPalette = '';
+ }
+ if (!currentPalette.length) {
+ currentPalette = 'kairo';
+ setPalette(currentPalette);
+ }
+ if (!gColorPalette || !gColorPalette.length)
+ gColorPalette = getColorPalette(currentPalette);
+
+ var popup = document.getElementById("menu_palettePopup");
+ var item = popup.firstChild;
+ while (item) {
+ if (item.getAttribute("name") == "palette") {
+ if (item.getAttribute("value") == currentPalette)
+ item.setAttribute("checked", "true");
+ else
+ item.removeAttribute("checked");
+ }
+ item = item.nextSibling;
+ }
+}
+
+function setPalette(aPaletteID) {
+ gPref.setCharPref("mandelbrot.color_palette", aPaletteID);
+ gColorPalette = getColorPalette(aPaletteID);
+}
+
+function updateDebugMenu() {
+ var jitMenuItem = document.getElementById("jitEnabled");
+ jitMenuItem.setAttribute("checked", gPref.getBoolPref("javascript.options.jit.chrome"));
+}
+
+function toggleJITState(jitMenuItem) {
+ var jitEnabled = !gPref.getBoolPref("javascript.options.jit.chrome");
+ gPref.setBoolPref("javascript.options.jit.chrome", jitEnabled)
+ jitMenuItem.setAttribute("checked", jitEnabled? "true" : "false");
+}
+
+function updateAlgoMenu() {
+ try {
+ var currentAlgo = gPref.getCharPref("mandelbrot.use_algorithm");
+ }
+ catch(e) {
+ var currentAlgo = '';
+ }
+ if (!currentAlgo.length) {
+ currentAlgo = 'numeric';
+ setAlgorithm(currentAlgo);
+ }
+
+ var popup = document.getElementById("menu_algoPopup");
+ var item = popup.firstChild;
+ while (item) {
+ if (item.getAttribute("name") == "algorithm") {
+ if (item.getAttribute("value") == currentAlgo)
+ item.setAttribute("checked", "true");
+ else
+ item.removeAttribute("checked");
+ }
+ item = item.nextSibling;
+ }
+}
+
+function setAlgorithm(algoID) {
+ gPref.setCharPref("mandelbrot.use_algorithm", algoID);
+}
+
+
+/***** helper functions from external sources *****/
+
// function below is from from http://developer.mozilla.org/en/docs/Code_snippets:Canvas
function saveCanvas(canvas, destFile) {
// convert string filepath to an nsIFile
<window id="mandelbrotWindow" title="&windowTitle;"
width="350" height="450"
+ onload="Startup()"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
xmlns:html="http://www.w3.org/1999/xhtml">
+
<script type="application/x-javascript"
src="chrome://mandelbrot/content/mandelbrot.js"/>
+
+ <stringbundleset id="stringbundleset">
+ <stringbundle id="mbrotBundle" src="chrome://mandelbrot/locale/mandelbrot.properties"/>
+ </stringbundleset>
+
<toolbox>
<menubar>
- <menu id="fileMenu" label="&fileMenu;">
- <menupopup id="filePopup">
- <menuitem id="fileDraw" label="&fileDraw;" oncommand="drawImage();"/>
- <menuitem id="fileSave" label="&fileSave;" oncommand="saveImage();"/>
+ <menu id="fileMenu" label="&fileMenu.label;">
+ <menupopup id="menu_filePopup">
+ <menuitem id="fileDraw" label="&fileDraw.label;" oncommand="drawImage();"/>
+ <menuitem id="fileSave" label="&fileSave.label;" oncommand="saveImage();"/>
<menuseparator/>
- <menuitem id="fileQuit" label="&fileQuit;" oncommand="quitApp(false);"/>
+ <menuitem id="fileQuit" label="&fileQuit.label;" oncommand="quitApp(false);"/>
</menupopup>
</menu>
- <menu id="prefMenu" label="&prefMenu;">
- <menupopup id="prefPopup">
- <menu id="iterMenu" label="&iterMenu;">
- <menupopup id="iterPopup" oncommand="setIter();">
- <menuitem type="radio" id="iter50" label="&iter50;"/>
- <menuitem type="radio" id="iter100" label="&iter100;"/>
- <menuitem type="radio" id="iter500" label="&iter500;"/>
- <menuitem type="radio" id="iter1000" label="&iter1000;"/>
+ <menu id="prefMenu" label="&prefMenu.label;">
+ <menupopup id="menu_prefPopup">
+ <menu id="iterMenu" label="&iterMenu.label;">
+ <menupopup id="menu_iterPopup" onpopupshowing="updateIterMenu();" oncommand="setIter(event.target.value);">
+ <menuitem type="radio" name="iter" value="50" label="&iter50.label;"/>
+ <menuitem type="radio" name="iter" value="100" label="&iter100.label;"/>
+ <menuitem type="radio" name="iter" value="500" label="&iter500.label;"/>
+ <menuitem type="radio" name="iter" value="1000" label="&iter1000.label;"/>
</menupopup>
</menu>
- <menu id="colorMenu" label="&colorMenu;">
- <menupopup id="colorPopup" oncommand="setPalette();">
- <menuitem type="radio" id="colorBW" label="&colorBW;"/>
- <menuitem type="radio" id="colorKairo" label="&colorKairo;"/>
+ <menu id="colorMenu" label="&colorMenu.label;">
+ <menupopup id="menu_palettePopup" onpopupshowing="updatePaletteMenu();" oncommand="setPalette(event.target.value);">
+ <menuitem type="radio" name="palette" value="bw" label="&colorBW.label;"/>
+ <menuitem type="radio" name="palette" value="kairo" label="&colorKairo.label;"/>
+ </menupopup>
+ </menu>
+ </menupopup>
+ </menu>
+ <menu id="debugMenu" label="&debugMenu.label;">
+ <menupopup id="menu_debugPopup" onpopupshowing="updateDebugMenu();">
+ <menuitem type="checkbox" id="jitEnabled" label="&jitEnabled.label;" oncommand="toggleJITState(event.target);"/>
+ <menu id="algoMenu" label="&algoMenu.label;">
+ <menupopup id="menu_algoPopup" onpopupshowing="updateAlgoMenu();" oncommand="setAlgorithm(event.target.value);">
+ <menuitem type="radio" name="algorithm" value="numeric" label="&algoNumeric.label;"/>
+ <menuitem type="radio" name="algorithm" value="oo" label="&algoOO.label;"/>
</menupopup>
</menu>
</menupopup>
</menu>
</menubar>
</toolbox>
- <button id="drawButton" label="&fileDraw;" oncommand="drawImage();"/>
+ <button id="drawButton" label="&fileDraw.label;" oncommand="drawImage();"/>
<hbox flex="1" pack="center" align="center">
<html:canvas id="mbrotImage" width="300" height="300"></html:canvas>
</hbox>
-
+ <hbox pack="end" align="end">
+ <description id="statusLabel"/>
+ </hbox>
</window>