Expand description
p5-sys crate(library) is bindings to p5.js for Rust/Wasm.
*** Nightly rustc is required ***
Currently,almost all global function works. The instance methods and properties don’t work.
For example, vector.add() will not work. You are advised to use rust types for such tasks.
Also functions that take arrays and modify them will not work.
The documentation is taken from p5.js reference and are not ported to rust yet.
§Example
use p5::*;
pub struct State {
x: f64,
}
#[wasm_bindgen]
pub fn setup() -> State {
createCanvas(400., 400., RENDERER::Webgl);
background(123., 234., 124.);
return State {
x: 10.,
}
}
#[wasm_bindgen]
pub fn draw(state: &mut State) {
state.x += 1;
rect(state.x, 40., 100., 120.);
}We don’t have mutable global variable in Rust, so we have to use state. setup creates the state. draw and other event handlers recieve a mutable reference to it, means that they can change it.
Re-exports§
Modules§
Structs§
- JsError
- Convenience type for use on exported
fn() -> Result<T, JsError>functions, where you wish to throw a JavaScriptErrorobject. - JsValue
- Representation of an object owned by JS.
- Scoped
Closure - A closure with a lifetime parameter that represents a Rust closure passed to JavaScript.
Traits§
- JsCast
- A trait for dynamic checked and unchecked casting between JS types.
- Unwrap
Throw Ext - An extension trait for
Option<T>andResult<T, E>for unwrapping theTvalue, or throwing a JS error if it is not available. - Upcast
- A trait for type-safe generic upcasting.
Type Aliases§
- Closure
- Alias for
ScopedClosure<'static, T>for backwards compatibility.
Attribute Macros§
- __
wasm_ bindgen_ class_ marker - wasm_
bindgen - A list of all the attributes can be found here: https://wasm-bindgen.github.io/wasm-bindgen/reference/attributes/index.html