Improve mandelbort example (#308)

This commit is contained in:
Max Graey
2019-01-09 18:26:31 +02:00
committed by Daniel Wirtz
parent 2ec89ee669
commit d82995c686
8 changed files with 861 additions and 353 deletions

View File

@ -30,14 +30,19 @@ var ctx = cnv.getContext("2d");
var bcr = cnv.getBoundingClientRect();
// Compute the size of the viewport
var width = bcr.width | 0;
var width = bcr.width | 0;
var height = bcr.height | 0;
var ratio = window.devicePixelRatio || 1;
width *= ratio;
height *= ratio;
var size = width * height;
var byteSize = size << 1; // discrete color indices in range [0, 2047] (here: 2b per pixel)
cnv.width = width;
cnv.width = width;
cnv.height = height;
ctx.scale(ratio, ratio);
// Compute the size of and instantiate the module's memory
var memory = new WebAssembly.Memory({ initial: ((byteSize + 0xffff) & ~0xffff) >>> 16 });
var mem = new Uint16Array(memory.buffer);
@ -48,7 +53,7 @@ var argb = new Uint32Array(imageData.data.buffer);
fetch("build/optimized.wasm")
.then(response => response.arrayBuffer())
.then(buffer => WebAssembly.instantiate(buffer, {
env: { memory: memory },
env: { memory },
Math
}))
.then(module => {
@ -56,7 +61,9 @@ fetch("build/optimized.wasm")
var computeLine = exports.computeLine;
var updateLine = function(y) {
var yx = y * width;
for (let x = 0; x < width; ++x) argb[yx + x] = colors[mem[yx + x]];
for (let x = 0; x < width; ++x) {
argb[yx + x] = colors[mem[yx + x]];
}
};
// Compute an initial balanced version of the set.