Basics - sketch
vs app
Tutorial Info
- Author: mitchmindtree
- Required Knowledge:
- Reading Time: 7 minutes
When creating a new nannou project we have two options for kicking off our program:
nannou::sketch
andnannou::app
.
Let's find out exactly what the differences are!
Note: When referring to app throughout this tutorial, we are referring to a nannou project that is run via
nannou::app
. We are not referring to theApp
type that often appears as the first argument in nannou functions. Hopefully we can point to anApp
oriented tutorial some day soon!
Sketches
Sketches are perfect for quick doodles and casual creations. They only
require a simple view
function designed to make it easy to start drawing
quickly and easily.
Here is what the sketch template looks like:
use nannou::prelude::*; fn main() { nannou::sketch(view).run() } fn view(app: &App, frame: Frame) { let draw = app.draw(); draw.background().color(PLUM); draw.ellipse().color(STEELBLUE); draw.to_frame(app, &frame).unwrap(); }
While you cannot update or track any custom state, we still have access to plenty of fun inputs including time, the state of the keyboard, mouse and more.
Apps
Apps are better suited to more sophisticated artworks or even fully fledged applications. They allow for greater flexibility and finer grained control than sketches, but also require a little more setup.
Here is what the app template looks like:
use nannou::prelude::*; fn main() { nannou::app(model).update(update).run(); } struct Model { _window: window::Id, } fn model(app: &App) -> Model { let _window = app.new_window().view(view).build().unwrap(); Model { _window } } fn update(_app: &App, _model: &mut Model, _update: Update) {} fn view(app: &App, _model: &Model, frame: Frame) { let draw = app.draw(); draw.background().color(PLUM); draw.ellipse().color(STEELBLUE); draw.to_frame(app, &frame).unwrap(); }
More specifically, the primary difference is that an app allows for working with
custom state (i.e. the Model
), whereas a sketch does not.
Hot tip!
The line:
nannou::sketch(view).run()
is simply short-hand for
nannou::app(model).simple_window(view).run()
except without the need for
model
and with a slightly simplerview
function.
Switching from sketch
to app
In the end it does not make a great deal of difference what you choose, you can always switch from one to the other in the middle of a project!
If your sketch is getting particularly fancy and you would like to add some more flexibility, you can turn it into an app by following these steps:
-
Change your
main
function from#![allow(dead_code)] use nannou::prelude::*; fn main() { nannou::sketch(view).run() } fn view(_: &App, _: Frame) {}
to
#![allow(dead_code)] use nannou::prelude::*; fn main() { nannou::app(model).simple_window(view).run() } struct Model {} fn model(_: &App) -> Model { Model {} } fn view(_: &App, _: &Model, _: Frame) {}
-
Add a
Model
for tracking state:#![allow(dead_code)] fn main() {} struct Model {}
-
Add a
model
function for creating theModel
:#![allow(dead_code)] use nannou::prelude::*; fn main() {} struct Model {} fn model(_app: &App) -> Model { Model {} }
-
Change the
view
function signature from:#![allow(dead_code, unused_variables)] use nannou::prelude::*; fn main() {} fn view(app: &App, frame: Frame) { }
to
#![allow(dead_code, unused_variables)] use nannou::prelude::*; fn main() {} struct Model {} fn view(app: &App, _model: &Model, frame: Frame) { }
And that's it! You are now ready to take your sketch to the next level.
Overview
Sketch | App | |
---|---|---|
Easier to start drawing quickly? | Yes | No |
Allows for a Model ? | No | Yes |
Allows for audio/LASER/MIDI/etc? | No | Yes |
The main function looks like: | nannou::sketch(view) | nannou::app(model) |
Templates | template_sketch.rs | template_app.rs |
Can make awesome stuff? | Yes | Yes |
To learn more about nannou sketches visit the Draw a sketch tutorial.
To learn more about nannou apps visit the Anatomy of a nannou app tutorial.