Skip to content

Commit 4f7d454

Browse files
author
Shrike
committed
Add mandelbrot render to demo page
1 parent d97e2f4 commit 4f7d454

File tree

6 files changed

+151
-299
lines changed

6 files changed

+151
-299
lines changed

app.js

+109
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ function run_wasm(obj) {
2424
w = obj.instance;
2525
memory = new Uint8Array(w.exports.memory.buffer);
2626

27+
let m = new Mandel(w);
28+
2729
test1(w);
2830
test2(w);
2931
test3(w);
@@ -125,3 +127,110 @@ function test4(w) {
125127
console.log("test4", reverseResult, typeof(reverseResult));
126128
element.innerHTML = result;
127129
}
130+
131+
class Mandel
132+
{
133+
constructor(w) {
134+
this.buttonStartC = document.getElementById("btnStartC");
135+
this.buttonStopC = document.getElementById("btnStopC");
136+
137+
this.canvasC = document.getElementById("canvasC");
138+
this.w = w;
139+
140+
this.ctxC = this.canvasC.getContext("2d");
141+
142+
this.canvasWidth = this.canvasC.width;
143+
this.canvasHeight = this.canvasC.height;
144+
145+
this.boundHandleClickStartC = this.startC.bind(this);
146+
this.boundHandleClickStopC = this.stopC.bind(this);
147+
148+
this.boundLoopC = this.loopC.bind(this);
149+
150+
this.buttonStartC.addEventListener("click", this.boundHandleClickStartC);
151+
this.buttonStopC.addEventListener("click", this.boundHandleClickStopC);
152+
153+
this.buttonStopC.style.display = "none";
154+
155+
this.runC = false;
156+
157+
this.startC();
158+
}
159+
160+
startC()
161+
{
162+
this.buttonStartC.style.display = "none";
163+
this.buttonStopC.style.display = "block";
164+
this.initMandel();
165+
166+
this.ctxC.fillStyle = "rgba(10,10,10,255)";
167+
this.ctxC.fillRect( 0, 0, this.canvasWidth, this.canvasHeight );
168+
169+
this.runC = true;
170+
requestAnimationFrame(this.boundLoopC);
171+
}
172+
173+
stopC()
174+
{
175+
this.buttonStartC.style.display = "block";
176+
this.buttonStopC.style.display = "none";
177+
this.runC = false;
178+
}
179+
180+
initMandel()
181+
{
182+
this.minR = -1.016104875794718741911;
183+
this.maxR = -1.016069839835083325286;
184+
this.minI = 0.277341371563625000140;
185+
this.maxI = 0.277367718605270833442;
186+
187+
// this.minR = -1.016097461503191826601;
188+
// this.maxR = -1.016097461213187951076;
189+
// this.minI = 0.277356844127615701971;
190+
// this.maxI = 0.277356844344781394806;
191+
192+
this.minR = -2;
193+
this.maxR = 1;
194+
this.minI = -1;
195+
this.maxI = 1;
196+
197+
this.minR = -1.293019999999999985104;
198+
this.maxR = -1.193859999999999987472;
199+
this.minI = 0.12808;
200+
this.maxI = 0.20245;
201+
202+
this.minR = -0.868819999999999995828;
203+
this.maxR = -0.858714999999999996086;
204+
this.minI = 0.239415;
205+
this.maxI = 0.246935;
206+
207+
this.maxIterations = 5000;
208+
this.x = 0;
209+
this.y = 0;
210+
211+
this.colors = new Array(16).fill(0).map((_, i) => i === 0 ? '#000' : `#${((1 << 24) * Math.random() | 0).toString(16)}`);
212+
213+
this.w.exports.initMandel(this.minR, this.maxR, this.minI, this.maxI, this.maxIterations, this.canvasWidth, this.canvasHeight);
214+
}
215+
216+
loopC()
217+
{
218+
for (let x = 0; x < this.canvasWidth; ++x)
219+
{
220+
const iterations = this.w.exports.calcPixel(x, this.y);
221+
this.ctxC.fillStyle = this.colors[iterations >= this.maxIterations ? 0 : (iterations % this.colors.length - 1) + 1];
222+
this.ctxC.fillRect( x, this.y, 1, 1 );
223+
}
224+
225+
this.y += 1;
226+
if (this.y >= this.canvasHeight)
227+
{
228+
this.stopC();
229+
}
230+
231+
if (this.runC)
232+
{
233+
requestAnimationFrame(this.boundLoopC);
234+
}
235+
}
236+
}

c_wasm_screenshot.png

215 KB
Loading

index.html

+31-19
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,37 @@
1515

1616
<h1>Running C functions in WebAssembly</h1>
1717
<hr/>
18-
<p>
19-
<h2>C function with 2 integer arguments</h2>
20-
add(42, 666) = <span class="result" id="test1result"></span>
21-
</p>
22-
23-
<p>
24-
<h2>Pass integer array to C function</h2>
25-
sum([1, 2, ... 100]) = <span class="result" id="test2result"></span>
26-
</p>
27-
28-
<p>
29-
<h2>C function with float argument</h2>
30-
square_root(2) = <span class="result" id="test3result"></span>
31-
</p>
32-
33-
<p>
34-
<h2>C function with string argument</h2>
35-
reverse("WebAssembly") = <span class="result" id="test4result"></span>
36-
</p>
18+
19+
<div class="wrapper">
20+
<div>
21+
<p>
22+
<h2>C function with 2 integer arguments</h2>
23+
add(42, 666) = <span class="result" id="test1result"></span>
24+
</p>
25+
26+
<p>
27+
<h2>Pass integer array to C function</h2>
28+
sum([1, 2, ... 100]) = <span class="result" id="test2result"></span>
29+
</p>
30+
31+
<p>
32+
<h2>C function with float argument</h2>
33+
square_root(2) = <span class="result" id="test3result"></span>
34+
</p>
35+
36+
<p>
37+
<h2>C function with string argument</h2>
38+
reverse("WebAssembly") = <span class="result" id="test4result"></span>
39+
</p>
40+
</div>
41+
42+
<div class="cnvC">
43+
<div style="margin:0; padding:0;"><canvas id="canvasC" width="480" height="360"></canvas></div>
44+
<button type="button" id="btnStartC">Start</button>
45+
<button type="button" id="btnStopC" style="display:none;">Stop</button>
46+
</div>
47+
48+
</div>
3749

3850
<script type="module">
3951
init();

main.c

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/////////////
2-
// Example //
3-
/////////////
1+
///////////
2+
// Setup //
3+
///////////
44

5-
// Function declared in Javascript
5+
// Function implemented in Javascript
66
void print(char* msg);
77
float sqrt(float number);
88

@@ -22,7 +22,10 @@ void c_free(void* p)
2222
// Not implemented yet
2323
}
2424

25-
// Function to be called from Javascript
25+
////////////////////
26+
// Test Functions //
27+
////////////////////
28+
2629
int add(int a, int b)
2730
{
2831
print("In C add()\n");
@@ -47,8 +50,6 @@ float square_root(float number)
4750
return sqrt(number);
4851
}
4952

50-
#define MAX_MSG_LENGTH 100
51-
5253
char* reverse(char* msg, int len)
5354
{
5455
print(msg);
@@ -62,8 +63,9 @@ char* reverse(char* msg, int len)
6263
return result;
6364
}
6465

65-
/*********************************************/
66-
/* Mandelbrot ********************************/
66+
////////////////
67+
// Mandelbrot //
68+
////////////////
6769

6870
double minR;
6971
double maxR;

0 commit comments

Comments
 (0)