0015-wasm-康威生命游戏
环境
- Time 2022-05-14
- Rust 1.60.0
- Node 12.22.5
- wasm-pack 0.10.2
前言
说明
参考:https://rustwasm.github.io/docs/book/game-of-life/implementing.html
目标
在上一节的基础上进行,继续实现康威生命游戏的前端。
index.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<title>康威生命游戏</title>
<style>
body {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
</style>
</head>
<body>
<pre id="game"></pre>
<script src="./bootstrap.js"></script>
</body>
</html>
index.js
import { Universe } from "game";
const pre = document.getElementById("game");
const universe = Universe.new(64, 32);
const renderLoop = () => {
pre.textContent = universe.render();
universe.tick();
requestAnimationFrame(renderLoop);
};
requestAnimationFrame(renderLoop);
wasm-pack build
打包 Rust 代码。
C:\Users\jiangbo\workspace\game>wasm-pack build
[INFO]: Checking for the Wasm target...
[INFO]: Compiling to Wasm...
Finished release [optimized] target(s) in 0.05s
[WARN]: :-) origin crate has no README
[INFO]: Installing wasm-bindgen...
[INFO]: Optimizing wasm binaries with `wasm-opt`...
[INFO]: Optional fields missing from Cargo.toml: 'description', 'repository', and 'license'. These are not necessary, but recommended
[INFO]: :-) Done in 1.07s
[INFO]: :-) Your wasm pkg is ready to publish at C:\Users\jiangbo\workspace\game\pkg.
启动前端
npm run start
效果展示
总结
实现了康威生命游戏的前端,并且和后端联合,看到了游戏效果。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!