Wein2D.js bindings for creating browser games in Rust using WebAssembly.

Overview

Wein2D.js-WASM

Wein2D.js bindings for creating browser games in Rust using WebAssembly.

Wein2D.js-WASM requires Wein2d.js to be loaded in the same document.

Code example

This is a simple Example for a program (rust):

mod wein2d_js;

use wein2d_js::*;
use wasm_bindgen::prelude::*;

// code for statically storing a GameState-struct

static mut STATIC_GAME_STATE: Option<GameState> = None;

fn set_game_state(game_state: GameState) { unsafe { STATIC_GAME_STATE = Some(game_state); } }

fn get_game_state_mut() -> &'static mut GameState { unsafe {
    match STATIC_GAME_STATE {
        Some(ref mut game_state) => { game_state }
        None => { panic!("Static Game State not defined.") }
    }
}}

struct GameState {
    height: f32,
    velocity: f32,
}

// on_start and on_frame functions

#[wasm_bindgen]
pub fn on_start() { // gets called directly after initialization
    set_game_state(GameState {
        height: 0.0,
        velocity: 0.0,
    });
}

// cube values (feel free to play with these!)
static CUBE_SIZE: f32 = 25.0; // the width and height of the cube
static CUBE_JUMP_VELOCITY: f32 = 400.0; // the cube's jump velocity (in pixels per second)
static CUBE_GRAVITATION: f32 = 800.0; // gravitation (how much velocity gets removed per second)
static CUBE_BOUNCEBACK_MULTIPLIER: f32 = 0.4; // how much velocity the cube keeps after hitting the ground

#[wasm_bindgen]
pub fn on_frame() { // gets called once per frame
    // get game state
    let mut game_state: &mut GameState = get_game_state_mut();

    // update calls //////////////////////////////////////////////////

    // if space bar or left mouse button pressed, set the cubes velocity (let the cube jump up)
    if get_key(Key::SPACE) || get_mouse_l() { game_state.velocity = CUBE_JUMP_VELOCITY; }

    // move the cube up and down according to it's velocity
    game_state.height += game_state.velocity * get_delta_time();
    // if the cube is not on the ground remove some of the cube's velocity (gravitation)
    if game_state.height > 0.0 { game_state.velocity -= CUBE_GRAVITATION * get_delta_time() }

    // if the cube is below or on the ground, set him onto the ground, invert the cube's velocity (movement) and remove some of it's velocity
    if game_state.height <= 0.0 {
        game_state.velocity = -game_state.velocity * CUBE_BOUNCEBACK_MULTIPLIER;
        game_state.height = 0.0;
    }

    // render calls //////////////////////////////////////////////////

    // fill the screen with blue
    fill(255, 11, 138, 143);

    // draw the cube
    draw_rect(
        (get_width() - CUBE_SIZE) / 2.0, // draw at the center of the screen (x axis)
        get_height() - CUBE_SIZE - game_state.height, // draw at the cube's height (y axis)
        CUBE_SIZE, CUBE_SIZE, // draw the cube with it's width and height
        255, 255, 255, 255 // draw the cube white
    );
}

Include the build in a webpage like this:

<!DOCTYPE html>
<html>
    <head>
        <style>
            * { margin: 0px; overflow: hidden; }
            canvas { width: 100vw; height: 100vh; }
        </style>
        <script src="wein2d.js"></script> <!-- include Wein2D.js -->
        <script src="wein2d-wasm.js"></script> <!-- include Wein2D.js-WASM -->
        <script>
            window.onload = function()
            {
                wein2dWASM_init(
                    document.getElementById("canvas"), // target element with ID "canvas"
                    "/pkg/Wein2D_js_wasm.js" // wasm-bindgen generated js file when building using "wasm-pack build --target web"
                );
            }
        </script>
    </head>
    <body>
        <canvas id="canvas"></canvas> <!-- normal canvas element -->
    </body>
</html>

Documentation

Documentation for Wein2D.js-WASM can be found at https://wein2ddocs.netlify.app.

You might also like...
A single-threaded executor for deferred async code for games.

This crate provides a single-threaded, sequential, parameterized async runtime. In other words, this creates coroutines, specifically targeting video game logic, though cosync is suitable for creating any sequences of directions which take time.

A sandbox library for making FAST voxel games

voxelize WIP A well-optimized web-based voxel engine. Development Before starting, make sure to install the following: rust node.js cargo-watch # clon

A framework for modding and instrumenting games.

me3 A framework for modifying and instrumenting games. Explore the docs » Report Bug · Request Feature About The Project Built With Getting Started Pr

A framework for building adventure games in Bevy.

Bevy_adventure A framework for building 3d adventure games in Bevy. preview.mp4 Features Interactive trait is the backbone of the framework, allowing

Bevy virtual Joystick for mobile games (Works with mouse on desktop)
Bevy virtual Joystick for mobile games (Works with mouse on desktop)

Bevy Virtual Joystick Create and use a Virtual Joystick in a UI for bevy Game Engine. Versions Aviable and compatible versions bevy VirtualJoystick 0.

Explicitly set sprite layers for sprites in Bevy games.
Explicitly set sprite layers for sprites in Bevy games.

extol_sprite_layer lets you specify the drawing order for sprites in your Bevy game using a separate component rather than via the z-coordinate of you

Highly customizable splash screen library for Bevy games.

bevy_intro_screen This is a versatile Bevy library designed to create engaging and customizable introductory screens for your game. Initially conceive

Hierarchical Task Network Planning deeply integrated with bevy, which I use in my games :P

Note that CI currently tests against a matrix of (windows, mac, linux) * (toolchain stable, nightly) * (cargo build, test, clippy), which ensures vali

Conway's Game of Life, visualised using Rust and WebAssembly

Game of Life, using Rust and WebAssembly Tutorial Built with 🦀 🕸 by The Rust and WebAssembly Working Group About Conway's Game of Life, which consis

Owner
DevTaube
:keyboard: Hobby-Developer
DevTaube
The Bloat-Free Browser Game in Rust but in C and not in a Browser

rust-browser-game but native and rendered with SDL in C without the Browser The original idea of rust-browser-game is that the game.wasm module is com

Tsoding 12 Sep 26, 2022
A framework for making games using Macroquad.

Omegaquad A framework for making games using Macroquad. After writing maybe 5 games and finding myself always going to the previous project to copy-pa

null 14 Oct 13, 2022
Using USBPcap to side-step anticheat in games, in order to reroute rumble packets to sex toys via The Buttplug Sex Toy Control Library

Using USBPcap to side-step anticheat in games, in order to reroute rumble packets to sex toys via The Buttplug Sex Toy Control Library.

qDot 23 Jan 3, 2023
A single-threaded polling-based Rust async executor suitable for use in games, embedded systems or WASM.

simple async local executor An Enlightware® software. Overview A single-threaded polling-based executor suitable for use in games, embedded systems or

Enlightware GmbH 16 Nov 15, 2022
A Rust promise for games and immediate mode GUIs

⌛ poll-promise A Rust promise for games and immediate mode GUIs Description poll-promise is a Rust crate for polling the result of a concurrent (e.g.

Embark 82 Dec 17, 2022
Cross-platform (including wasm) persistent key value store plugin for rust games/apps

bevy_pkv bevy_pkv is a cross-platform persistent key value store for rust apps. Use it for storing things like settings, save games etc. Currently, it

Johan Klokkhammer Helsing 25 Jan 9, 2023
An ergonomic physics API for bevy games.

Heron An ergonomic physics API for 2d and 3d bevy games. (powered by rapier) How it looks like fn main() { App::build() .add_plugins(DefaultPlug

Jonathan Cornaz 313 Dec 16, 2022
A frontend to Assets purchased on Epic Games Store

Epic-Asset-Manager A frontend to Assets purchased on Epic Games Store Current Screenshot Install Arch Linux Use the AUR package Build flatpak meson _b

Acheta Games 202 Jan 3, 2023
Synchronize games from other platforms into your Steam library

BoilR Description This little tool will synchronize games from other platforms into your Steam library, using the Steam Shortcuts feature. The goal is

Philip Kristoffersen 823 Jan 9, 2023
A simple camera for properly displaying tile-based low resolution pixel perfect 2D games in bevy.

Bevy Tiled Camera A simple camera for properly displaying low resolution pixel perfect 2D games in bevy. The camera will adjust the viewport to scale

sark 10 Oct 5, 2022