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§
- Closure
- A handle to both a closure in Rust as well as JS closure which will invoke the Rust closure.
- JsError
- Convenience type for use on exported
fn() -> Result<T, JsError>
functions, where you wish to throw a JavaScriptError
object. - JsValue
- Representation of an object owned by JS.
Traits§
- JsCast
- A trait for checked and unchecked casting between JS types.
- Unwrap
Throw Ext - An extension trait for
Option<T>
andResult<T, E>
for unwrapping theT
value, or throwing a JS error if it is not available.
Attribute Macros§
- __
wasm_ bindgen_ class_ marker - wasm_
bindgen - A list of all the attributes can be found here: https://rustwasm.github.io/docs/wasm-bindgen/reference/attributes/index.html