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§

prelude
This module re-export all the types, traits, macros, and dependencies needed to run a sketch.

Macros§

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

Structs§

AnimationOptions
Controls the animation feature of the runner.
Context
Context passed to crate::App::update.
Grid
2-dimensional square grid module
GridCell
Stores basic grid’s cell data, like column, row and canvas position
HexGrid
Hexagonal grid module
HexGridCell
Stores basic hex grid’s cell data, like size, orientation, or canvas position
InfoOptions
Controls the info section of the runner.
PageSizeOptions
Controls the page size feature of the runner.
Runner
The Runner is the main entry point for executing a crate::SketchApp.
Sketch
Primary interface for drawing.

Enums§

LayoutOptions
Controls the layout feature of the runner.

Traits§

App
This is the trait that your sketch app must explicitly implement. The App::update function is where the sketch draw code goes.
SketchApp
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§

Result
This is a convenience alias to the anyhow::Result type, which you can use for your sketch’s main function.