GithubHelp home page GithubHelp logo

mgord9518 / minimal-zig-wasm-canvas Goto Github PK

View Code? Open in Web Editor NEW

This project forked from daneelsan/minimal-zig-wasm-canvas

0.0 1.0 0.0 281 KB

A minimal example showing how HTML5's canvas, wasm memory and zig can interact.

JavaScript 36.77% HTML 6.95% Zig 56.27%

minimal-zig-wasm-canvas's Introduction

Minimal zig-wasm-canvas example

A minimal example showing how HTML5's canvas, wasm memory and zig can interact.

https://daneelsan.github.io/minimal-zig-wasm-canvas/

checkerboard

This example was mostly adapted from: https://wasmbyexample.dev/examples/reading-and-writing-graphics/reading-and-writing-graphics.rust.en-us.html. The difference (aside from using Zig instead of Rust), is that this example does not depend on any external package, library or extra bindings. As with most Zig related projects, the zig command is everything you will need.

Summary

checkerboard.zig defines a global 8 by 8 pixel array: checkerboard_buffer. There are two functions exported: getCheckerboardBufferPointer() and colorCheckerboard(...). getCheckerboardBufferPointer() returns a pointer to the start of checkerboard_buffer, which will be used inside script.js (described later). The other function, colorCheckerboard(...), is in charge of (re-)coloring the different squares in the checkerboard according to colors passed down by script.js.

checkerboard.zig is compiled into the wasm module checkerboard.wasm by build.zig (see the Build Section).

script.js first creates a WebAssembly.Memory, named memory, which will be used by the wasm module's memory (see the --import-memory flag in the the Build Section). Next step is to compile and instantiate the fetched wasm module using WebAssembly.instantiateStreaming(). The returned object is a Promise which contains the instance field (WebAssembly.Instance). This is where all the exported symbols from checkerboard.wasm will be stored.

What follows is mostly straightforward:

  • Create a Uint8Array from the WebAssembly.Memory memory buffer, called wasmMemoryArray.
  • Get the "checkerboard" canvas defined in index.html and create an ImageData object from the canvas' context, called imageData.
  • In a loop:
    • Call colorCheckerboard(...), passing down random RGB colors. This will modify the values inside checkerboard_buffer.
    • Extract the contents of the checkerboard_buffer, which is an slice of wasmMemoryArray offseted by getCheckerboardBufferPointer() and of length 8 * 8 * 4 (bytes).
    • Copy the contents of the previous slice, i.e. the contents of checkerboard_buffer, into the imageData's data.
    • Put the imageData into the canvas.

Build

The default (and only) target for this example is wasm32-freestanding-musl.

To build the wasm module, run:

$ zig build checkerboard -Drelease=true

$ ls checkerboard.*
checkerboard.o    checkerboard.wasm    checkerboard.zig

Note: build.zig specifies various wasm-ld parameters. For example, it sets the initial memory size and maximum size to be 2 pages, where each page consists of 64kB. Use the --verbose flag to see the complete list of flags the build uses.

Run

Start up the server in this repository's directory:

python3 -m http.server

Go to your favorite browser and type to the URL localhost:8000. You should see the checkboard changing colors.

Resources

minimal-zig-wasm-canvas's People

Contributors

mgord9518 avatar daneelsan avatar

Watchers

 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.