+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);
+}
+
+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;
+ }
+}
+
+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;
+ }
+}
+
+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;
+ }
+ 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;
+ }
+
+ 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();
+}