X-Git-Url: https://git-public.kairo.at/?p=mandelbrot-web.git;a=blobdiff_plain;f=js%2Fmandelbrot.js;h=fad30134a9eeb917a109c8af5839dc63440f8238;hp=5873478487df6597927bd7683d5ad9f4224e3088;hb=5d56a513a8ece482f7223dfb8946d8c464f48170;hpb=95d05599800b415b161db8a622d058b22459052c diff --git a/js/mandelbrot.js b/js/mandelbrot.js index 5873478..fad3013 100644 --- a/js/mandelbrot.js +++ b/js/mandelbrot.js @@ -1,56 +1,63 @@ -/* ***** BEGIN LICENSE BLOCK ***** - * Version: MPL 1.1/GPL 2.0/LGPL 2.1 - * - * The contents of this file are subject to the Mozilla Public License Version - * 1.1 (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * http://www.mozilla.org/MPL/ - * - * Software distributed under the License is distributed on an "AS IS" basis, - * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License - * for the specific language governing rights and limitations under the - * License. - * - * The Original Code is KaiRo.at Mandelbrot, XULRunner version. - * - * The Initial Developer of the Original Code is - * Robert Kaiser . - * Portions created by the Initial Developer are Copyright (C) 2008-2011 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * Robert Kaiser - * Boris Zbarsky - * - * Alternatively, the contents of this file may be used under the terms of - * either the GNU General Public License Version 2 or later (the "GPL"), or - * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), - * in which case the provisions of the GPL or the LGPL are applicable instead - * of those above. If you wish to allow use of your version of this file only - * under the terms of either the GPL or the LGPL, and not to allow others to - * use your version of this file under the terms of the MPL, indicate your - * decision by deleting the provisions above and replace them with the notice - * and other provisions required by the GPL or the LGPL. If you do not delete - * the provisions above, a recipient may use your version of this file under - * the terms of any one of the MPL, the GPL or the LGPL. - * - * ***** END LICENSE BLOCK ***** */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ +// Get the best-available indexedDB object. +var iDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB; +var mainDB; + +var gMainCanvas, gMainContext; var gColorPalette = []; var gStartTime = 0; var gCurrentImageData; var gLastImageData; function Startup() { - var img = document.getElementById("mbrotImage"); - img.addEventListener("mouseup", imgEvHandler, false); - img.addEventListener("mousedown", imgEvHandler, false); - img.addEventListener("mousemove", imgEvHandler, false); - img.addEventListener("touchstart", imgEvHandler, false); - img.addEventListener("touchend", imgEvHandler, false); - img.addEventListener("touchcancel", imgEvHandler, false); - img.addEventListener("touchleave", imgEvHandler, false); - img.addEventListener("touchmove", imgEvHandler, false); + initDB(); + + gMainCanvas = document.getElementById("mbrotImage"); + gMainContext = gMainCanvas.getContext("2d"); + + gMainCanvas.addEventListener("mouseup", imgEvHandler, false); + gMainCanvas.addEventListener("mousedown", imgEvHandler, false); + gMainCanvas.addEventListener("mousemove", imgEvHandler, false); + gMainCanvas.addEventListener("touchstart", imgEvHandler, false); + gMainCanvas.addEventListener("touchend", imgEvHandler, false); + gMainCanvas.addEventListener("touchcancel", imgEvHandler, false); + gMainCanvas.addEventListener("touchleave", imgEvHandler, false); + gMainCanvas.addEventListener("touchmove", imgEvHandler, false); + + var initTile = new Image(); + initTile.src = "style/initial-overview.png"; + gMainContext.drawImage(initTile, 0, 0); +} + +function initDB() { + // Open DB. + var request = iDB.open("MainDB", 1); + request.onerror = function(event) { + // Errors can be handled here. Error codes explain in: + // https://developer.mozilla.org/en/IndexedDB/IDBDatabaseException#Constants + //document.getElementById("debug").textContent = + // "error opening mainDB: " + event.target.errorCode; + }; + request.onsuccess = function(event) { + //document.getElementById("debug").textContent = "mainDB opened."; + mainDB = request.result; + }; + request.onupgradeneeded = function(event) { + mainDB = request.result; + //document.getElementById("debug").textContent = "mainDB upgraded."; + // Create a "prefs" objectStore. + var prefsStore = mainDB.createObjectStore("prefs"); + // Create a "bookmarks" objectStore. + var bmStore = mainDB.createObjectStore("bookmarks"); + mainDB.onversionchange = function(event) { + mainDB.close(); + mainDB = undefined; + initDB(); + }; + }; } function getAdjustVal(aName) { @@ -65,6 +72,7 @@ function getAdjustVal(aName) { catch (e) { } if ((value < 10) || (value > 5000)) { value = 300; + gPrefs.set(prefname, value); //document.getElementById(aName.replace(".", "_")).value = value; } return value; @@ -80,6 +88,8 @@ function getAdjustVal(aName) { (Cr_max < -3) || (Cr_max > 2) || (Cr_min >= Cr_max)) { Cr_min = -2.0; Cr_max = 1.0; } + gPrefs.set("Cr_min", Cr_min); + gPrefs.set("Cr_max", Cr_max); document.getElementById("Cr_min").value = Cr_min; document.getElementById("Cr_max").value = Cr_max; return {Cr_min: Cr_min, Cr_max: Cr_max}; @@ -95,6 +105,8 @@ function getAdjustVal(aName) { (Ci_max < -2.5) || (Ci_max > 2.5) || (Ci_min >= Ci_max)) { Ci_min = -1.5; Ci_max = 1.5; } + gPrefs.set("Ci_min", Ci_min); + gPrefs.set("Ci_max", Ci_max); document.getElementById("Ci_min").value = Ci_min; document.getElementById("Ci_max").value = Ci_max; return {Ci_min: Ci_min, Ci_max: Ci_max}; @@ -135,6 +147,7 @@ function getAdjustVal(aName) { value = document.getElementById("proportional").value; } catch(e) { + gPrefs.set(prefname, value); document.getElementById("proportional").value = value; } return value; @@ -147,13 +160,18 @@ function setVal(aName, aValue) { switch (aName) { case "image.width": case "image.height": + gPrefs.set(aName, value); document.getElementById(aName.replace(".", "_")).value = value; break; case "last_image.Cr_*": + gPrefs.set("Cr_min", Cr_min); + gPrefs.set("Cr_max", Cr_max); document.getElementById("Cr_min").value = aValue.Cr_min; document.getElementById("Cr_max").value = aValue.Cr_max; break; case "last_image.Ci_*": + gPrefs.set("Ci_min", Ci_min); + gPrefs.set("Ci_max", Ci_max); document.getElementById("Ci_min").value = aValue.Ci_min; document.getElementById("Ci_max").value = aValue.Ci_max; break; @@ -167,11 +185,62 @@ function setVal(aName, aValue) { setPalette(valueaValue); break; case "syncProportions": + gPrefs.set(aName, value); document.getElementById("proportional").value = aValue; break; } } +function checkISValue(textbox, type) { + if (type == "coord") { + textbox.value = roundCoord(parseFloat(textbox.value)); + } + else if (type == "dim") { + textbox.value = parseInt(textbox.value); + } +} + +function recalcCoord(coord, target) { + var othercoord = (coord == "Ci") ? "Cr" : "Ci"; + var owndim = (coord == "Ci") ? "height" : "width"; + var otherdim = (coord == "Ci") ? "width" : "height"; + var myscale; + if (target == "scale") { + myscale = + parseFloat(document.getElementById(coord + "_max").value) - + parseFloat(document.getElementById(coord + "_min").value); + document.getElementById(coord + "_scale").value = roundCoord(myscale); + } + else if (target == 'max') { + var mymax = + parseFloat(document.getElementById(coord + "_min").value) + + parseFloat(document.getElementById(coord + "_scale").value); + document.getElementById(coord + "_max").value = roundCoord(mymax); + myscale = document.getElementById(coord + "_scale").value; + } + if (document.getElementById("proportional").checked) { + var otherscale = myscale * + document.getElementById("image_" + otherdim).value / + document.getElementById("image_" + owndim).value; + document.getElementById(othercoord + "_scale").value = roundCoord(otherscale); + var othermax = + parseFloat(document.getElementById(othercoord + "_min").value) + + parseFloat(document.getElementById(othercoord + "_scale").value); + document.getElementById(othercoord + "_max").value = roundCoord(othermax); + } +} + +function checkProportions() { + if (!document.getElementById("proportional").checked) { + recalcCoord("Cr", "scale"); + } +} + +function roundCoord(floatval) { + // We should round to 10 decimals here or so + return parseFloat(floatval.toFixed(10)); +} + function adjustCoordsAndDraw(aC_min, aC_max) { var iWidth = getAdjustVal("image.width"); var iHeight = getAdjustVal("image.height"); @@ -207,8 +276,8 @@ function adjustCoordsAndDraw(aC_min, aC_max) { } function drawImage() { - var canvas = document.getElementById("mbrotImage"); - var context = canvas.getContext("2d"); + var canvas = gMainCanvas; + var context = gMainContext; document.getElementById("calcTime").textContent = "--"; @@ -230,16 +299,8 @@ function drawImage() { var iterMax = getAdjustVal("iteration_max"); var algorithm = getAdjustVal("use_algorithm"); - var iWidth = canvas.width; - if ((iWidth < 10) || (iWidth > 5000)) { - iWidth = 300; - canvas.width = iWidth; - } - var iHeight = canvas.height; - if ((iHeight < 10) || (iHeight > 5000)) { - iHeight = 300; - canvas.height = iHeight; - } + var iWidth = getAdjustVal("image.width"); + var iHeight = getAdjustVal("image.height"); gCurrentImageData = { C_min: new complex(Cr_min, Ci_min), @@ -249,6 +310,9 @@ function drawImage() { iterMax: iterMax }; + canvas.width = iWidth; + canvas.height = iHeight; + context.fillStyle = "rgba(255, 255, 255, 127)"; context.fillRect(0, 0, canvas.width, canvas.height); @@ -589,14 +653,170 @@ function goBack() { } function setIter(aIter) { + gPrefs.set("iteration_max", aIter); document.getElementById("iterMax").value = aIter; } function setPalette(aPaletteID) { + gPrefs.set("color_palette", aPaletteID); document.getElementById("palette").value = aPaletteID; gColorPalette = getColorPalette(aPaletteID); } function setAlgorithm(algoID) { + gPrefs.set("use_algorithm", algoID); //document.getElementById("algorithm").value = algoID; } + +var gPrefs = { + objStore: "prefs", + + get: function(aKey, aCallback) { + if (!mainDB) + return; + var transaction = mainDB.transaction([this.objStore]); + var request = transaction.objectStore(this.objStore).get(aKey); + request.onsuccess = function(event) { + aCallback(request.result, event); + }; + request.onerror = function(event) { + // Errors can be handled here. + aCallback(undefined, event); + }; + }, + + set: function(aKey, aValue, aCallback) { + if (!mainDB) + return; + var success = false; + var transaction = mainDB.transaction([this.objStore], "readwrite"); + var objStore = transaction.objectStore(this.objStore); + var request = objStore.put(aValue, aKey); + request.onsuccess = function(event) { + success = true; + if (aCallback) + aCallback(success, event); + }; + request.onerror = function(event) { + // Errors can be handled here. + if (aCallback) + aCallback(success, event); + }; + }, + + unset: function(aKey, aCallback) { + if (!mainDB) + return; + var success = false; + var transaction = mainDB.transaction([this.objStore], "readwrite"); + var request = transaction.objectStore(this.objStore).delete(aKey); + request.onsuccess = function(event) { + success = true; + if (aCallback) + aCallback(success, event); + }; + request.onerror = function(event) { + // Errors can be handled here. + if (aCallback) + aCallback(success, event); + } + } +}; + +var gBMStore = { + objStore: "bookmarks", + + getList: function(aCallback) { + if (!mainDB) + return; + var transaction = mainDB.transaction([this.objStore]); + var objStore = transaction.objectStore(this.objStore); + if (objStore.getAll) { // currently Mozilla-specific + objStore.getAll().onsuccess = function(event) { + aCallback(event.target.result); + }; + } + else { // Use cursor (standard method). + var BMs = {}; + objStore.openCursor().onsuccess = function(event) { + var cursor = event.target.result; + if (cursor) { + BMs[cursor.key] = cursor.value; + cursor.continue(); + } + else { + aCallback(BMs); + } + }; + } + }, + + get: function(aKey, aCallback) { + if (!mainDB) + return; + var transaction = mainDB.transaction([this.objStore]); + var request = transaction.objectStore(this.objStore).get(aKey); + request.onsuccess = function(event) { + aCallback(request.result, event); + }; + request.onerror = function(event) { + // Errors can be handled here. + aCallback(undefined, event); + }; + }, + + set: function(aKey, aValue, aCallback) { + if (!mainDB) + return; + var success = false; + var transaction = mainDB.transaction([this.objStore], "readwrite"); + var objStore = transaction.objectStore(this.objStore); + var request = objStore.put(aValue, aKey); + request.onsuccess = function(event) { + success = true; + if (aCallback) + aCallback(success, event); + }; + request.onerror = function(event) { + // Errors can be handled here. + if (aCallback) + aCallback(success, event); + }; + }, + + unset: function(aKey, aCallback) { + if (!mainDB) + return; + var success = false; + var transaction = mainDB.transaction([this.objStore], "readwrite"); + var request = transaction.objectStore(this.objStore).delete(aKey); + request.onsuccess = function(event) { + success = true; + if (aCallback) + aCallback(success, event); + }; + request.onerror = function(event) { + // Errors can be handled here. + if (aCallback) + aCallback(success, event); + } + }, + + clear: function(aCallback) { + if (!mainDB) + return; + var success = false; + var transaction = mainDB.transaction([this.objStore], "readwrite"); + var request = transaction.objectStore(this.objStore).clear(); + request.onsuccess = function(event) { + success = true; + if (aCallback) + aCallback(success, event); + }; + request.onerror = function(event) { + // Errors can be handled here. + if (aCallback) + aCallback(success, event); + } + } +};