这是一个 Conway‘s game of life 的 wasm 模块,使用 webpack4 在 ES Module 项目中使用,可以通过该模块实现游戏游戏对 wasm 性能有初步的了解,webassembly 兼容性。
npm install @bezos/wasm-game-of-life --save
struct Universe {
width: usize,
height: usize,
cells: &[u8],
};
默认构造一个 64*64 bit 的数组
import { Universe } from '@bezos/wasm-game-of-life'
const universe = Universe.new()
初始化cells
universe.init()
清空cells
universe.reset()
获取cells内存地址
import { memory } from '@bezos/wasm-game-of-life/wasm_game_of_life_bg'
const { width, height } = universe
const cellsPtr = universe.cells()
const cells = new Uint8Array(memory.buffer, cellsPtr, (width * height) / 8)
根据上一个状态更新cells
requestAnimationFrame(function renderLoop() {
universe.tick()
drawSomething()
requestAnimationFrame(renderLoop)
})
根据为止反转状态
universe.toggle_cell(row, col)