Skip to content

Commit 8b23ca8

Browse files
authored
Update mandelbrot example (#25)
1 parent 8d16bf6 commit 8b23ca8

File tree

7 files changed

+60
-26
lines changed

7 files changed

+60
-26
lines changed

mandelbrot/asconfig.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"options": {
3+
"runtime": "stub",
4+
"use": "Math=JSMath",
5+
"importMemory": true,
6+
"sourceMap": true,
7+
"measure": true
8+
},
9+
"targets": {
10+
"debug": {
11+
"outFile": "build/debug.wasm",
12+
"textFile": "build/debug.wat",
13+
"debug": true
14+
},
15+
"release": {
16+
"outFile": "build/release.wasm",
17+
"textFile": "build/release.wat",
18+
"optimizeLevel": 3,
19+
"shrinkLevel": 0,
20+
"noAssert": true
21+
}
22+
}
23+
}

mandelbrot/build/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
*
22
!.gitignore
3-
!optimized.wasm
3+
!release.wasm

mandelbrot/build/optimized.wasm

-538 Bytes
Binary file not shown.

mandelbrot/build/release.wasm

541 Bytes
Binary file not shown.

mandelbrot/index.html

+26-20
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,27 @@ <h1>
4646
ctx.scale(ratio, ratio);
4747

4848
// Compute the size of and instantiate the module's memory
49-
var memory = new WebAssembly.Memory({ initial: ((byteSize + 0xffff) & ~0xffff) >>> 16 });
50-
var mem = new Uint16Array(memory.buffer);
51-
var imageData = ctx.createImageData(width, height);
52-
var argb = new Uint32Array(imageData.data.buffer);
49+
const memory = new WebAssembly.Memory({
50+
initial: ((byteSize + 0xffff) & ~0xffff) >>> 16
51+
});
52+
const mem = new Uint16Array(memory.buffer);
53+
const imageData = ctx.createImageData(width, height);
54+
const argb = new Uint32Array(imageData.data.buffer);
5355

5456
// Fetch and instantiate the module
55-
fetch("build/optimized.wasm")
57+
fetch("build/release.wasm")
5658
.then(response => response.arrayBuffer())
5759
.then(buffer => WebAssembly.instantiate(buffer, {
58-
env: { memory },
59-
Math
60+
env: {
61+
memory,
62+
"Math.log": Math.log,
63+
"Math.log2": Math.log2
64+
},
6065
}))
6166
.then(module => {
62-
var exports = module.instance.exports;
63-
var computeLine = exports.computeLine;
64-
var updateLine = function(y) {
67+
const exports = module.instance.exports;
68+
const computeLine = exports.computeLine;
69+
const updateLine = function (y) {
6570
var yx = y * width;
6671
for (let x = 0; x < width; ++x) {
6772
argb[yx + x] = colors[mem[yx + x]];
@@ -85,14 +90,15 @@ <h1>
8590

8691
// Let it glow a bit by occasionally shifting the limit...
8792
var currentLimit = limit;
88-
var shiftRange = 10;
93+
const shiftRange = 10;
94+
8995
(function updateShift() {
9096
currentLimit = limit + (2 * Math.random() * shiftRange - shiftRange) | 0
9197
setTimeout(updateShift, 1000 + (1500 * Math.random()) | 0);
9298
})();
9399

94100
// ...while continously recomputing a subset of it.
95-
var flickerRange = 3;
101+
const flickerRange = 3;
96102
(function updateFlicker() {
97103
for (let i = 0, k = (0.05 * height) | 0; i < k; ++i) {
98104
let ry = (Math.random() * height) | 0;
@@ -110,15 +116,15 @@ <h1>
110116
});
111117

112118
// Compute a nice set of colors using a gradient
113-
var colors = (() => {
114-
var cnv = document.createElement("canvas");
115-
cnv.width = 2048;
119+
const colors = (() => {
120+
const cnv = document.createElement("canvas");
121+
cnv.width = 2048;
116122
cnv.height = 1;
117-
var ctx = cnv.getContext("2d");
118-
var grd = ctx.createLinearGradient(0, 0, 2048, 0);
119-
grd.addColorStop(0.00, "#000764");
120-
grd.addColorStop(0.16, "#2068CB");
121-
grd.addColorStop(0.42, "#EDFFFF");
123+
const ctx = cnv.getContext("2d");
124+
const grd = ctx.createLinearGradient(0, 0, 2048, 0);
125+
grd.addColorStop(0.0000, "#000764");
126+
grd.addColorStop(0.1600, "#2068CB");
127+
grd.addColorStop(0.4200, "#EDFFFF");
122128
grd.addColorStop(0.6425, "#FFAA00");
123129
grd.addColorStop(0.8575, "#000200");
124130
ctx.fillStyle = grd;

mandelbrot/index.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
const fs = require("fs");
2-
const compiled = new WebAssembly.Module(fs.readFileSync(__dirname + "/build/optimized.wasm"));
3-
const imports = { JSMath: Math };
2+
const compiled = new WebAssembly.Module(fs.readFileSync(__dirname + "/build/release.wasm"));
3+
const imports = {
4+
env: {
5+
"Math.log": Math.log,
6+
"Math.log2": Math.log2,
7+
}
8+
};
49
Object.defineProperty(module, "exports", {
510
get: () => new WebAssembly.Instance(compiled, imports).exports
611
});

mandelbrot/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"license": "Apache-2.0",
55
"private": true,
66
"scripts": {
7-
"asbuild:untouched": "asc assembly/index.ts -o build/untouched.wasm -t build/untouched.wat --use Math=JSMath --runtime stub --importMemory --sourceMap --debug --measure",
8-
"asbuild:optimized": "asc assembly/index.ts -o build/optimized.wasm -t build/optimized.wat --use Math=JSMath --runtime stub -O3 --importMemory --sourceMap --measure",
9-
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized",
7+
"asbuild:debug": "asc assembly/index.ts --target debug",
8+
"asbuild:release": "asc assembly/index.ts --target release",
9+
"asbuild": "npm run asbuild:debug && npm run asbuild:release",
1010
"start": "npx serve"
1111
},
1212
"devDependencies": {

0 commit comments

Comments
 (0)