Expand description
Whiskers is an interactive environment for pen-plotter generative art sketches.
Native sketch
To create a whiskers sketch that will only be run natively, create a new Rust project and add the whiskers crate as a dependency:
cargo add whiskers
Then, add the following content to the main.rs
file:
use whiskers::prelude::*;
#[derive(Sketch)]
struct MySketch {
/* add sketch parameters here */
}
impl Default for MySketch {
fn default() -> Self {
Self {
/* initialize sketch parameters to default values here */
}
}
}
impl App for MySketch {
fn update(&mut self, sketch: &mut Sketch, _ctx: &mut Context) -> anyhow::Result<()> {
// draw code goes here
sketch
.color(Color::DARK_RED)
.rect(200., 200., 150., 50.);
Ok(())
}
}
fn main() -> Result {
Runner::new(MySketch::default())
.with_page_size_options(PageSize::A5H)
/* add other Runner default configuration here */
.run()
}
See the crate::Sketch
type documentation for more information on the drawing code. See the
crate::Runner
type documentation for more information on the available configurations.
Sketches with Wasm support
For sketches that target both native and Wasm, your crate should be structured as both a library
and a binary. The library should contain the sketch code as well as the wasm_sketch!
macro:
// lib.rs
use whiskers::prelude::*;
use whiskers::wasm_main;
#[derive(Sketch)]
struct MySketch { }
impl Default for MySketch {
fn default() -> Self {
Self { }
}
}
impl App for MySketch {
fn update(&mut self, sketch: &mut Sketch, _ctx: &mut Context) -> anyhow::Result<()> {
Ok(())
}
}
wasm_sketch!(Runner::new(MySketch::default()));
The binary should then call the wasm_main!
macro:
// main.rs
wasm_main!(my_sketch); // `my_sketch` is the crate name
Deploying the Wasm sketch additionally requires a spacial index.html
file. See the
whiskers-web-demo
crate
for an example.
Modules
- This module re-export all the types, traits, macros, and dependencies needed to run a sketch.
- This module implements the UI widgets used to display the sketch parameters in the UI.
Macros
- Registers a given
Widget
type for given sketch parameter type. - Declare the binary entry point in Wasm-ready sketch crates.
- Declare the main entry point for wasm builds.
Structs
- Controls the animation feature of the runner.
- Context passed to
crate::App::update
. - Controls the info section of the runner.
- Controls the page size feature of the runner.
- The
Runner
is the main entry point for executing acrate::SketchApp
. - Primary interface for drawing.
Enums
- Controls the layout feature of the runner.
Traits
- This is the trait that your sketch app must explicitly implement. The
App::update
function is where the sketch draw code goes. - This trait is implemented by the
whiskers_derive::Sketch
derive macro and makes it possible for theRunner
to execute your sketch.s
Type Aliases
- This is a convenience alias to the
anyhow::Result
type, which you can use for your sketch’s main function.