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 whiskersThen, 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 nameDeploying 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§
- Animation
Options - Controls the animation feature of the runner.
- Context
- Context passed to
crate::App::update. - Grid
- 2-dimensional square grid module
- Grid
Cell - Stores basic grid’s cell data, like column, row and canvas position
- HexGrid
- Hexagonal grid module
- HexGrid
Cell - Stores basic hex grid’s cell data, like size, orientation, or canvas position
- Info
Options - Controls the info section of the runner.
- Page
Size Options - Controls the page size feature of the runner.
- Runner
- The
Runneris the main entry point for executing acrate::SketchApp. - Sketch
- Primary interface for drawing.
Enums§
- Layout
Options - Controls the layout feature of the runner.
Traits§
- App
- This is the trait that your sketch app must explicitly implement. The
App::updatefunction is where the sketch draw code goes. - Sketch
App - This trait is implemented by the
whiskers_widgets::Sketchderive macro and makes it possible for theRunnerto execute your sketch.s
Type Aliases§
- Result
- This is a convenience alias to the
anyhow::Resulttype, which you can use for your sketch’s main function.