following tutorial

This commit is contained in:
Thilo Behnke
2022-04-15 17:21:26 +02:00
parent 92677d9278
commit 31b2d9ea08
3 changed files with 26 additions and 0 deletions

View File

@@ -7,6 +7,12 @@ edition = "2018"
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies.web-sys]
version = "0.3"
features = [
"console",
]
[features]
default = ["console_error_panic_hook"]

View File

@@ -3,6 +3,15 @@ mod utils;
use std::fmt;
use wasm_bindgen::prelude::*;
extern crate web_sys;
// A macro to provide `println!(..)`-style syntax for `console.log` logging.
macro_rules! log {
( $( $t:tt )* ) => {
web_sys::console::log_1(&format!( $( $t )* ).into());
}
}
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
@@ -63,6 +72,14 @@ impl Universe {
let cell = self.cells[idx];
let live_neighbors = self.live_neighbor_count(row, col);
log!(
"cell[{}, {}] is initially {:?} and has {} live neighbors",
row,
col,
cell,
live_neighbors
);
let next_cell = match (cell, live_neighbors) {
// Rule 1: Any live cell with fewer than two live neighbours
// dies, as if caused by underpopulation.
@@ -88,6 +105,8 @@ impl Universe {
}
pub fn new() -> Universe {
utils::set_panic_hook();
let width = 64;
let height = 64;

View File

@@ -21,6 +21,7 @@ canvas.width = (CELL_SIZE + 1) * width + 1;
const ctx = canvas.getContext('2d');
const renderLoop = () => {
debugger;
universe.tick();
drawGrid();