Giter Site home page Giter Site logo

no_npm_wasm_game_of_life's Introduction

no_npm_wasm_game_of_life

No bundle, no npm, game of life using Rust wasm.

You'll need

wasm-pack


project structure

.
├── Cargo.lock
├── Cargo.toml
├── run.sh
├── src
│   └── lib.rs
└── www
    ├── css
    │   └── styles.css
    ├── favicon_96x96.png
    ├── html
    │   └── index.html
    ├── js
    │   └── index.js
    └── pkg
        ├── game_of_life_pre_bg.wasm
        ├── game_of_life_pre_bg.wasm.d.ts
        ├── game_of_life_pre.d.ts
        ├── game_of_life_pre.js
        └── package.json

First we put all our website related files in www at the root of our project. This keeps out project nice and clean.

in index.html

we must specify that out index.js is a javascript module

<script type="module" src="../js/index.js"></script>

index.js

We need to specify the file extension when we import our glu code generated by wasm-bindgen and build by wasm-pack

we do not need to import game_of_life_canvas_bg.wasm like in the official tutorial as it is not possible at the moment to import a wasm file directly without a bundler.

However, we can still have access to the shared webassembly memory though init()

game_of_life_canvas.js

import init, { Universe, Cell } from "../pkg/game_of_life_canvas.js";

async function run() {
    const wasm = await init().catch(console.error);
    const memory = wasm.memory;

    ...

} 

run();

Build with wasm-pack

We use wasm-pack to build and explicitly specify our output directory for the generated wasm code.

wasm-pack build --target web --out-dir www/pkg

Serve the website

Any minimal http server will do for local development.

ex:

cargo install https

I use this little script to build and serve the website locally

# filename: run.sh

set -ex

wasm-pack build --target web --out-dir www/pkg

echo "Now serving on http://127.0.0.1:8080/html"
http -a 127.0.0.1 -p 8080 www

no_npm_wasm_game_of_life's People

Contributors

lerina avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.