Crate whiskers

source ·
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::*;

#[sketch_app]
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 {
    MySketch::runner()
        .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;

#[sketch_app]
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!(MySketch::runner());

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.

Macros

  • Declare the binary entry point in Wasm-ready sketch crates.
  • Declare the main entry point for wasm builds.

Structs

Enums

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_widgets::Sketch derive macro and makes it possible for the Runner 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.