Skip to content

Commit 900932d

Browse files
committed
add JS rendering
1 parent a2602e1 commit 900932d

File tree

6 files changed

+36
-14
lines changed

6 files changed

+36
-14
lines changed

05-game-of-life/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ A zero-player game to learn how to use Rust, WebAssembly, and JavaScript togethe
88
- building the project with wasm-pack.
99
- putting it into a web page with wasm-app.
1010
- serving locally with webpack.
11-
- implementing the Game of Life in Rust.
11+
- implementing the Game of Life with Rust.
12+
- rendering with JavaScript.
1213

1314
Based on [The Rust and WebAssembly Book](https://rustwasm.github.io/docs/book/) by The Rust and WebAssembly Working Group (2021).

05-game-of-life/src/lib.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@ mod utils;
33
use std::fmt;
44
use wasm_bindgen::prelude::*;
55

6-
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
7-
// allocator.
8-
#[cfg(feature = "wee_alloc")]
9-
#[global_allocator]
10-
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
11-
126
#[wasm_bindgen]
137
#[repr(u8)]
148
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -37,8 +31,8 @@ impl Universe {
3731
continue;
3832
}
3933
let neighbor_row = (row + delta_row) % self.height;
40-
let neighbor_column = (column + delta_col) % self.width;
41-
let idx = self.get_index(neighbor_row, neighbor_column);
34+
let neighbor_col = (column + delta_col) % self.width;
35+
let idx = self.get_index(neighbor_row, neighbor_col);
4236
count += self.cells[idx] as u8;
4337
}
4438
}

05-game-of-life/www/index.html

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,24 @@
22
<html>
33
<head>
44
<meta charset="utf-8">
5-
<title>Hello wasm-pack!</title>
5+
<title>Game of Life</title>
6+
<style>
7+
body {
8+
position: absolute;
9+
top: 0;
10+
left: 0;
11+
width: 100%;
12+
height: 100%;
13+
display: flex;
14+
flex-direction: column;
15+
align-items: center;
16+
justify-content: center;
17+
}
18+
</style>
619
</head>
720
<body>
821
<noscript>This page contains webassembly and javascript content, please enable javascript in your browser.</noscript>
22+
<pre id="game-of-life-canvas"></pre>
923
<script src="./bootstrap.js"></script>
1024
</body>
1125
</html>

05-game-of-life/www/index.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
import * as wasm from "game-of-life";
1+
import { Universe } from "game-of-life";
22

3-
wasm.greet("WebAssembly");
3+
const pre = document.getElementById("game-of-life-canvas");
4+
const universe = Universe.new();
5+
6+
const renderLoop = () => {
7+
pre.textContent = universe.render();
8+
universe.tick();
9+
10+
requestAnimationFrame(renderLoop);
11+
};
12+
13+
requestAnimationFrame(renderLoop);

05-game-of-life/www/package-lock.json

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ A zero-player game to learn how to use Rust, WebAssembly, and JavaScript togethe
9595
- building the project with wasm-pack.
9696
- putting it into a web page with wasm-app.
9797
- serving locally with webpack.
98-
- implementing the Game of Life in Rust.
98+
- implementing the Game of Life with Rust.
99+
- rendering with JavaScript.
99100

100101
Based on [The Rust and WebAssembly Book](https://rustwasm.github.io/docs/book/) by The Rust and WebAssembly Working Group (2021).

0 commit comments

Comments
 (0)