|
| 1 | +let w = null; |
| 2 | +let memory = null; |
| 3 | + |
| 4 | +function make_environment(...envs) { |
| 5 | + return new Proxy(envs, { |
| 6 | + get(target, prop, receiver) { |
| 7 | + for (let env of envs) { |
| 8 | + if (env.hasOwnProperty(prop)) { |
| 9 | + return env[prop]; |
| 10 | + } |
| 11 | + } |
| 12 | + |
| 13 | + return (...args) => { console.error("NOT IMPLEMENTED: " + prop, args); } |
| 14 | + } |
| 15 | + }); |
| 16 | +} |
| 17 | + |
| 18 | +const imports = { |
| 19 | + "sqrt": Math.sqrt, |
| 20 | + "print": print, |
| 21 | +}; |
| 22 | + |
| 23 | +function print(value) |
| 24 | +{ |
| 25 | + //const s = extract_string(value); |
| 26 | + //console.log("print:", s); |
| 27 | + console.log("Print:"); |
| 28 | +} |
| 29 | + |
| 30 | +function run_wasm(obj) { |
| 31 | + w = obj.instance; |
| 32 | + memory = new Uint8Array(w.exports.memory.buffer); |
| 33 | + |
| 34 | + let m = new Mandel(w); |
| 35 | +} |
| 36 | + |
| 37 | +async function load_wasm() { |
| 38 | + fetch("main.wasm").then(response => |
| 39 | + response.arrayBuffer() |
| 40 | + ).then(bytes => |
| 41 | + WebAssembly.instantiate(bytes, { "env": make_environment(imports) }) |
| 42 | + ).then(run_wasm); |
| 43 | +} |
| 44 | + |
| 45 | +function init() { |
| 46 | + load_wasm(); |
| 47 | + |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | +class Mandel |
| 52 | +{ |
| 53 | + constructor(w) { |
| 54 | + this.buttonStartC = document.getElementById("btnStartC"); |
| 55 | + this.buttonStartJS = document.getElementById("btnStartJS"); |
| 56 | + this.buttonStopC = document.getElementById("btnStopC"); |
| 57 | + this.buttonStopJS = document.getElementById("btnStopJS"); |
| 58 | + |
| 59 | + this.canvasJS = document.getElementById("canvasJS"); |
| 60 | + this.canvasC = document.getElementById("canvasC"); |
| 61 | + this.w = w; |
| 62 | + |
| 63 | + this.ctxC = this.canvasC.getContext("2d"); |
| 64 | + this.ctxJS = this.canvasJS.getContext("2d"); |
| 65 | + |
| 66 | + this.canvasWidth = this.canvasC.width; |
| 67 | + this.canvasHeight = this.canvasC.height; |
| 68 | + |
| 69 | + this.boundHandleClickStartC = this.startC.bind(this); |
| 70 | + this.boundHandleClickStartJS = this.startJS.bind(this); |
| 71 | + this.boundHandleClickStopC = this.stopC.bind(this); |
| 72 | + this.boundHandleClickStopJS = this.stopJS.bind(this); |
| 73 | + |
| 74 | + this.boundLoopJS = this.loopJS.bind(this); |
| 75 | + this.boundLoopC = this.loopC.bind(this); |
| 76 | + |
| 77 | + this.buttonStartC.addEventListener("click", this.boundHandleClickStartC); |
| 78 | + this.buttonStopC.addEventListener("click", this.boundHandleClickStopC); |
| 79 | + this.buttonStartJS.addEventListener("click", this.boundHandleClickStartJS); |
| 80 | + this.buttonStopJS.addEventListener("click", this.boundHandleClickStopJS); |
| 81 | + |
| 82 | + this.buttonStopC.style.display = "none"; |
| 83 | + this.buttonStopJS.style.display = "none"; |
| 84 | + |
| 85 | + this.runJS = false; |
| 86 | + this.runC = false; |
| 87 | + } |
| 88 | + |
| 89 | + startC() |
| 90 | + { |
| 91 | + this.buttonStartC.style.display = "none"; |
| 92 | + this.buttonStopC.style.display = "block"; |
| 93 | + this.buttonStartJS.style.display = "none"; |
| 94 | + this.initMandel(); |
| 95 | + |
| 96 | + this.ctxC.fillStyle = "rgba(10,10,10,255)"; |
| 97 | + this.ctxC.fillRect( 0, 0, this.canvasWidth, this.canvasHeight ); |
| 98 | + |
| 99 | + this.runC = true; |
| 100 | + requestAnimationFrame(this.boundLoopC); |
| 101 | + } |
| 102 | + |
| 103 | + stopC() |
| 104 | + { |
| 105 | + this.buttonStartC.style.display = "block"; |
| 106 | + this.buttonStopC.style.display = "none"; |
| 107 | + this.buttonStartJS.style.display = "block"; |
| 108 | + this.runC = false; |
| 109 | + } |
| 110 | + |
| 111 | + startJS() |
| 112 | + { |
| 113 | + this.buttonStartJS.style.display = "none"; |
| 114 | + this.buttonStopJS.style.display = "block"; |
| 115 | + this.buttonStartC.style.display = "none"; |
| 116 | + this.initMandel(); |
| 117 | + |
| 118 | + this.ctxJS.fillStyle = "rgba(10,10,10,255)"; |
| 119 | + this.ctxJS.fillRect( 0, 0, this.canvasWidth, this.canvasHeight ); |
| 120 | + |
| 121 | + this.runJS = true; |
| 122 | + requestAnimationFrame(this.boundLoopJS); |
| 123 | + } |
| 124 | + |
| 125 | + stopJS() |
| 126 | + { |
| 127 | + this.buttonStartJS.style.display = "block"; |
| 128 | + this.buttonStopJS.style.display = "none"; |
| 129 | + this.buttonStartC.style.display = "block"; |
| 130 | + this.runJS = false; |
| 131 | + } |
| 132 | + |
| 133 | + initMandel() |
| 134 | + { |
| 135 | + this.minR = -1.016104875794718741911; |
| 136 | + this.maxR = -1.016069839835083325286; |
| 137 | + this.minI = 0.277341371563625000140; |
| 138 | + this.maxI = 0.277367718605270833442; |
| 139 | + |
| 140 | + // this.minR = -1.016097461503191826601; |
| 141 | + // this.maxR = -1.016097461213187951076; |
| 142 | + // this.minI = 0.277356844127615701971; |
| 143 | + // this.maxI = 0.277356844344781394806; |
| 144 | + |
| 145 | + this.minR = -2; |
| 146 | + this.maxR = 1; |
| 147 | + this.minI = -1; |
| 148 | + this.maxI = 1; |
| 149 | + |
| 150 | + this.minR = -1.293019999999999985104; |
| 151 | + this.maxR = -1.193859999999999987472; |
| 152 | + this.minI = 0.12808; |
| 153 | + this.maxI = 0.20245; |
| 154 | + |
| 155 | + this.maxIterations = 5000; |
| 156 | + this.x = 0; |
| 157 | + this.y = 0; |
| 158 | + |
| 159 | + this.colors = new Array(16).fill(0).map((_, i) => i === 0 ? '#000' : `#${((1 << 24) * Math.random() | 0).toString(16)}`); |
| 160 | + |
| 161 | + this.w.exports.initMandel(this.minR, this.maxR, this.minI, this.maxI, this.maxIterations, this.canvasWidth, this.canvasHeight); |
| 162 | + } |
| 163 | + |
| 164 | + loopJS() |
| 165 | + { |
| 166 | + for (let x = 0; x < this.canvasWidth; ++x) |
| 167 | + { |
| 168 | + const iterations = this.calcPixel(x, this.y); |
| 169 | + this.ctxJS.fillStyle = this.colors[iterations >= this.maxIterations ? 0 : (iterations % this.colors.length - 1) + 1]; |
| 170 | + this.ctxJS.fillRect( x, this.y, 1, 1 ); |
| 171 | + } |
| 172 | + |
| 173 | + this.y += 1; |
| 174 | + if (this.y >= this.canvasHeight) |
| 175 | + { |
| 176 | + this.stopJS(); |
| 177 | + } |
| 178 | + |
| 179 | + if (this.runJS) |
| 180 | + { |
| 181 | + requestAnimationFrame(this.boundLoopJS); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + loopC() |
| 186 | + { |
| 187 | + for (let x = 0; x < this.canvasWidth; ++x) |
| 188 | + { |
| 189 | + const iterations = this.w.exports.calcPixel(x, this.y); |
| 190 | + this.ctxC.fillStyle = this.colors[iterations >= this.maxIterations ? 0 : (iterations % this.colors.length - 1) + 1]; |
| 191 | + this.ctxC.fillRect( x, this.y, 1, 1 ); |
| 192 | + } |
| 193 | + |
| 194 | + this.y += 1; |
| 195 | + if (this.y >= this.canvasHeight) |
| 196 | + { |
| 197 | + this.stopC(); |
| 198 | + } |
| 199 | + |
| 200 | + if (this.runC) |
| 201 | + { |
| 202 | + requestAnimationFrame(this.boundLoopC); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + map(value, minin, maxin, minout, maxout) |
| 207 | + { |
| 208 | + return minout + (value - minin) / (maxin - minin) * (maxout - minout); |
| 209 | + } |
| 210 | + |
| 211 | + calcPixel(x, y) |
| 212 | + { |
| 213 | + const x0 = this.map(x, 0, this.canvasWidth - 1, this.minR, this.maxR); |
| 214 | + const y0 = this.map(y, 0, this.canvasHeight - 1, this.minI, this.maxI); |
| 215 | + |
| 216 | + let a = 0; |
| 217 | + let b = 0; |
| 218 | + let rx = 0; |
| 219 | + let ry = 0; |
| 220 | + |
| 221 | + let iterations = 0; |
| 222 | + while (iterations < this.maxIterations && (rx * rx + ry * ry <= 4)) { |
| 223 | + rx = a * a - b * b + x0; |
| 224 | + ry = 2 * a * b + y0; |
| 225 | + |
| 226 | + a = rx; |
| 227 | + b = ry; |
| 228 | + iterations++; |
| 229 | + } |
| 230 | + |
| 231 | + return iterations; |
| 232 | + } |
| 233 | +} |
0 commit comments