X-Git-Url: https://git-public.kairo.at/?p=mandelbrot-web.git;a=blobdiff_plain;f=js%2Fmandelbrot.js;h=385ff2eeb7c0ce7d6f16cfce02e35dfa1c5fec08;hp=ed49cc4fe2ed8de4b9060d383108e6f9a3c78a26;hb=51442fd43fc24ec7e8c2cf3d7231b21ff59e5911;hpb=6a7aa57d0bffbcea1bb023ceb86dd47d8710214b diff --git a/js/mandelbrot.js b/js/mandelbrot.js index ed49cc4..385ff2e 100644 --- a/js/mandelbrot.js +++ b/js/mandelbrot.js @@ -1,83 +1,207 @@ -/* ***** 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-210 - * 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/. */ +var gMainCanvas, gMainContext; var gColorPalette = []; var gStartTime = 0; +var gCurrentImageData; +var gLastImageData; -function drawImage() { - var canvas = document.getElementById("mbrotImage"); - var context = canvas.getContext("2d"); +function Startup() { + gMainCanvas = document.getElementById("mbrotImage"); + gMainContext = gMainCanvas.getContext("2d"); - document.getElementById("calcTime").textContent = "--"; + 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); - gColorPalette = getColorPalette(document.getElementById("palette").value); + var initTile = new Image(); + initTile.src = "style/initial-overview.png"; + gMainContext.drawImage(initTile, 0, 0); +} + +function getAdjustVal(aName) { + var value; + switch (aName) { + case "image.width": + case "image.height": + value = 0; + try { + value = document.getElementById(aName.replace(".", "_")).value; + } + catch (e) { } + if ((value < 10) || (value > 5000)) { + value = 300; + //document.getElementById(aName.replace(".", "_")).value = value; + } + return value; + case "last_image.Cr_*": + var Cr_min = -2.0; + var Cr_max = 1.0; + try { + Cr_min = parseFloat(document.getElementById("Cr_min").value); + Cr_max = parseFloat(document.getElementById("Cr_max").value); + } + catch (e) { } + if ((Cr_min < -3) || (Cr_min > 2) || + (Cr_max < -3) || (Cr_max > 2) || (Cr_min >= Cr_max)) { + Cr_min = -2.0; Cr_max = 1.0; + } + document.getElementById("Cr_min").value = Cr_min; + document.getElementById("Cr_max").value = Cr_max; + return {Cr_min: Cr_min, Cr_max: Cr_max}; + case "last_image.Ci_*": + var Ci_min = -1.5; + var Ci_max = 1.5; + try { + Ci_min = parseFloat(document.getElementById("Ci_min").value); + Ci_max = parseFloat(document.getElementById("Ci_max").value); + } + catch (e) { } + if ((Ci_min < -2.5) || (Ci_min > 2.5) || + (Ci_max < -2.5) || (Ci_max > 2.5) || (Ci_min >= Ci_max)) { + Ci_min = -1.5; Ci_max = 1.5; + } + document.getElementById("Ci_min").value = Ci_min; + document.getElementById("Ci_max").value = Ci_max; + return {Ci_min: Ci_min, Ci_max: Ci_max}; + case "iteration_max": + value = 500; + try { + value = document.getElementById("iterMax").value; + } + catch (e) { + setIter(value); + } + if (value < 10 || value > 10000) { + value = 500; + setIter(value); + } + return value; + case "use_algorithm": + value = "numeric"; + try { + value = document.getElementById("algorithm").value; + } + catch (e) { + setAlgorithm(value); + } + return value; + case "color_palette": + value = "kairo"; + try { + value = document.getElementById("palette").value; + } + catch(e) { + setPalette(value); + } + return value; + case "syncProportions": + value = true; + try { + value = document.getElementById("proportional").value; + } + catch(e) { + document.getElementById("proportional").value = value; + } + return value; + default: + return false; + } +} - var Cr_min = -2.0; - var Cr_max = 1.0; - try { - Cr_min = parseFloat(document.getElementById("Cr_min").value); - Cr_max = parseFloat(document.getElementById("Cr_max").value); +function setVal(aName, aValue) { + switch (aName) { + case "image.width": + case "image.height": + document.getElementById(aName.replace(".", "_")).value = value; + break; + case "last_image.Cr_*": + document.getElementById("Cr_min").value = aValue.Cr_min; + document.getElementById("Cr_max").value = aValue.Cr_max; + break; + case "last_image.Ci_*": + document.getElementById("Ci_min").value = aValue.Ci_min; + document.getElementById("Ci_max").value = aValue.Ci_max; + break; + case "iteration_max": + setIter(aValue); + break; + case "use_algorithm": + setAlgorithm(aValue); + break; + case "color_palette": + setPalette(valueaValue); + break; + case "syncProportions": + document.getElementById("proportional").value = aValue; + break; } - catch (e) { } - if ((Cr_min < -2) || (Cr_min > 2) || - (Cr_max < -2) || (Cr_max > 2) || (Cr_min >= Cr_max)) { - Cr_min = -2.0; Cr_max = 1.0; +} + +function adjustCoordsAndDraw(aC_min, aC_max) { + var iWidth = getAdjustVal("image.width"); + var iHeight = getAdjustVal("image.height"); + + // correct coordinates + if (aC_min.r < -2) + aC_min.r = -2; + if (aC_max.r > 2) + aC_max.r = 2; + if ((aC_min.r > 2) || (aC_max.r < -2) || (aC_min.r >= aC_max.r)) { + aC_min.r = -2.0; aC_max.r = 1.0; } - document.getElementById("Cr_min").value = Cr_min; - document.getElementById("Cr_max").value = Cr_max; - - var Ci_min = -1.5; - var Ci_max = 1.5; - try { - Ci_min = parseFloat(document.getElementById("Ci_min").value); - Ci_max = parseFloat(document.getElementById("Ci_max").value); + if (aC_min.i < -2) + aC_min.i = -2; + if (aC_max.i > 2) + aC_max.i = 2; + if ((aC_min.i > 2) || (aC_max.i < -2) || (aC_min.i >= aC_max.i)) { + aC_min.i = -1.3; aC_max.i = 1.3; } - catch (e) { } - if ((Ci_min < -2) || (Ci_min > 2) || - (Ci_max < -2) || (Ci_max > 2) || (Ci_min >= Ci_max)) { - Ci_min = -1.5; Ci_max = 1.5; + + var CWidth = aC_max.r - aC_min.r; + var CHeight = aC_max.i - aC_min.i; + var C_mid = new complex(aC_min.r + CWidth / 2, aC_min.i + CHeight / 2); + + var CRatio = Math.max(CWidth / iWidth, CHeight / iHeight); + + setVal("last_image.Cr_*", {Cr_min: C_mid.r - iWidth * CRatio / 2, + Cr_max: C_mid.r + iWidth * CRatio / 2}); + setVal("last_image.Ci_*", {Ci_min: C_mid.i - iHeight * CRatio / 2, + Ci_max: C_mid.i + iHeight * CRatio / 2}); + + drawImage(); +} + +function drawImage() { + var canvas = gMainCanvas; + var context = gMainContext; + + document.getElementById("calcTime").textContent = "--"; + + if (gCurrentImageData) { + gLastImageData = gCurrentImageData; + document.getElementById("backButton").disabled = false; } - document.getElementById("Ci_min").value = Ci_min; - document.getElementById("Ci_max").value = Ci_max; - //var algorithm = gPref.getCharPref("mandelbrot.use_algorithm"); - var iterMax = parseInt(document.getElementById("iterMax").value); - var algorithm = "numeric"; //"oo" + gColorPalette = getColorPalette(document.getElementById("palette").value); + + var Cr_vals = getAdjustVal("last_image.Cr_*"); + var Cr_min = Cr_vals.Cr_min; + var Cr_max = Cr_vals.Cr_max; + + var Ci_vals = getAdjustVal("last_image.Ci_*"); + var Ci_min = Ci_vals.Ci_min; + var Ci_max = Ci_vals.Ci_max; + + var iterMax = getAdjustVal("iteration_max"); + var algorithm = getAdjustVal("use_algorithm"); var iWidth = canvas.width; if ((iWidth < 10) || (iWidth > 5000)) { @@ -90,6 +214,14 @@ function drawImage() { canvas.height = iHeight; } + gCurrentImageData = { + C_min: new complex(Cr_min, Ci_min), + C_max: new complex(Cr_max, Ci_max), + iWidth: iWidth, + iHeight: iHeight, + iterMax: iterMax + }; + context.fillStyle = "rgba(255, 255, 255, 127)"; context.fillRect(0, 0, canvas.width, canvas.height); @@ -243,6 +375,59 @@ function getColorPalette(palName) { } palette[1024] = [0, 0, 0, 255]; break; + case 'rainbow-squared1': + for (var i = 0; i < 34; i++) { + var cc = Math.floor(i * 255 / 33); + palette[i] = [cc, 0, 0, 255]; + } + for (var i = 0; i < 137; i++) { + var cc = Math.floor(i * 255 / 136); + palette[34 + i] = [255, cc, 0, 255]; + } + for (var i = 0; i < 307; i++) { + var cc = Math.floor(i * 255 / 306); + palette[171 + i] = [255 - cc, 255, cc, 255]; + } + for (var i = 0; i < 546; i++) { + var cc = Math.floor(i * 255 / 545); + palette[478 + i] = [cc, 255 - cc, 255, 255]; + } + palette[1024] = [0, 0, 0, 255]; + break; + case 'rainbow-linear2': + for (var i = 0; i < 205; i++) { + var cc = Math.floor(i * 255 / 204); + palette[i] = [255, cc, 0, 255]; + palette[204 + i] = [255 - cc, 255, 0, 255]; + palette[409 + i] = [0, 255, cc, 255]; + palette[614 + i] = [0, 255 - cc, 255, 255]; + palette[819 + i] = [cc, 0, 255, 255]; + } + palette[1024] = [0, 0, 0, 255]; + break; + case 'rainbow-squared2': + for (var i = 0; i < 19; i++) { + var cc = Math.floor(i * 255 / 18); + palette[i] = [255, cc, 0, 255]; + } + for (var i = 0; i < 74; i++) { + var cc = Math.floor(i * 255 / 73); + palette[19 + i] = [255 - cc, 255, 0, 255]; + } + for (var i = 0; i < 168; i++) { + var cc = Math.floor(i * 255 / 167); + palette[93 + i] = [0, 255, cc, 255]; + } + for (var i = 0; i < 298; i++) { + var cc = Math.floor(i * 255 / 297); + palette[261 + i] = [0, 255 - cc, 255, 255]; + } + for (var i = 0; i < 465; i++) { + var cc = Math.floor(i * 255 / 464); + palette[559 + i] = [cc, 0, 255, 255]; + } + palette[1024] = [0, 0, 0, 255]; + break; } return palette; } @@ -263,6 +448,99 @@ function drawPoint(context, img_x, img_y, C, iterMax, algorithm) { // ########## UI functions ########## +var zoomstart; +var imgBackup; +var zoomTouchID; + +var imgEvHandler = { + handleEvent: function(aEvent) { + var canvas = document.getElementById("mbrotImage"); + var context = canvas.getContext("2d"); + var touchEvent = aEvent.type.indexOf('touch') != -1; + + // Bail out if this is neither a touch nor left-click. + if (!touchEvent && aEvent.button != 0) + return; + + // Bail out if the started touch can't be found. + if (touchEvent && zoomstart && + !aEvent.changedTouches.identifiedTouch(zoomTouchID)) + return; + + var coordObj = touchEvent ? + aEvent.changedTouches.identifiedTouch(zoomTouchID) : + aEvent; + + switch (aEvent.type) { + case 'mousedown': + case 'touchstart': + if (touchEvent) { + zoomTouchID = aEvent.changedTouches.item(0).identifier; + coordObj = aEvent.changedTouches.identifiedTouch(zoomTouchID); + } + // left button - start dragzoom + zoomstart = {x: coordObj.clientX - canvas.offsetLeft, + y: coordObj.clientY - canvas.offsetTop}; + imgBackup = context.getImageData(0, 0, canvas.width, canvas.height); + break; + case 'mouseup': + case 'touchend': + if (zoomstart) { + context.putImageData(imgBackup, 0, 0); + var zoomend = {x: coordObj.clientX - canvas.offsetLeft, + y: coordObj.clientY - canvas.offsetTop}; + + // make sure zoomend is bigger than zoomstart + if ((zoomend.x == zoomstart.x) || (zoomend.y == zoomstart.y)) { + // cannot zoom what has no area, discard it + zoomstart = undefined; + return; + } + if (zoomend.x < zoomstart.x) + [zoomend.x, zoomstart.x] = [zoomstart.x, zoomend.x]; + if (zoomend.y < zoomstart.y) + [zoomend.y, zoomstart.y] = [zoomstart.y, zoomend.y]; + + if (gCurrentImageData) { + // determine new "coordinates" + var CWidth = gCurrentImageData.C_max.r - gCurrentImageData.C_min.r; + var CHeight = gCurrentImageData.C_max.i - gCurrentImageData.C_min.i; + var newC_min = new complex( + gCurrentImageData.C_min.r + zoomstart.x / gCurrentImageData.iWidth * CWidth, + gCurrentImageData.C_min.i + zoomstart.y / gCurrentImageData.iHeight * CHeight); + var newC_max = new complex( + gCurrentImageData.C_min.r + zoomend.x / gCurrentImageData.iWidth * CWidth, + gCurrentImageData.C_min.i + zoomend.y / gCurrentImageData.iHeight * CHeight); + } + else { + var newC_min = new complex(-2, -1.5); + var newC_max = new complex(1, 1.5); + } + + adjustCoordsAndDraw(newC_min, newC_max); + } + zoomstart = undefined; + break; + case 'mousemove': + case 'touchmove': + if (zoomstart) { + context.putImageData(imgBackup, 0, 0); + context.strokeStyle = "rgb(255,255,31)"; + context.strokeRect(zoomstart.x, zoomstart.y, + coordObj.clientX - canvas.offsetLeft - zoomstart.x, + coordObj.clientY - canvas.offsetTop - zoomstart.y); + } + break; + } + } +}; + +function drawIfEmpty() { + if (!gCurrentImageData) { + drawImage(); + } +} + function toggleSettings() { var fs = document.getElementById("settings"); if (fs.style.display != "block") { @@ -271,4 +549,27 @@ function toggleSettings() { else { fs.style.display = "none"; } -} \ No newline at end of file +} + +function goBack() { + if (gLastImageData) { + document.getElementById("iterMax").value = gLastImageData.iterMax; + // use gLastImageData.iWidth, gLastImageData.iHeight ??? + adjustCoordsAndDraw(gLastImageData.C_min, gLastImageData.C_max); + gLastImageData = undefined; + document.getElementById("backButton").disabled = true; + } +} + +function setIter(aIter) { + document.getElementById("iterMax").value = aIter; +} + +function setPalette(aPaletteID) { + document.getElementById("palette").value = aPaletteID; + gColorPalette = getColorPalette(aPaletteID); +} + +function setAlgorithm(algoID) { + //document.getElementById("algorithm").value = algoID; +}