### Run Nannou Example Source: https://github.com/nannou-org/nannou/blob/master/examples/README.md This command allows you to run any specific example from the Nannou project. Replace `` with the desired example's name. ```bash cargo run --release --example ``` -------------------------------- ### Install Rust using rustup Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/installing_rust.md Use this command to install Rust on macOS and Linux systems. It downloads and executes the rustup installer script. ```bash curl https://sh.rustup.rs -sSf | sh ``` -------------------------------- ### Serve Nannou Guide Locally Source: https://github.com/nannou-org/nannou/blob/master/guide/README.md Serve the Nannou guide locally with live reloading. This command will build the book and host it at localhost:3000. ```bash mdbook serve ``` -------------------------------- ### Install mdBook Tool Source: https://github.com/nannou-org/nannou/blob/master/guide/README.md Install the Rust markdown book tool using Cargo. This tool is required for building and serving the Nannou guide. ```bash cargo install mdbook ``` -------------------------------- ### Install CMake with Homebrew on macOS Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/platform-specific_setup.md Some Nannou examples require CMake. Install it using Homebrew on macOS for easy management. ```bash brew install cmake ``` -------------------------------- ### Initialize App Model Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/basics/anatomy-of-a-nannou-app.md The `model` function is called once at the start of the application to create the initial state. It can be used for setup tasks like creating windows or loading assets. Access to the `App` struct is provided for I/O operations. ```rust #![allow(dead_code)] use nannou::prelude::* struct Model {} fn model(_app: &App) -> Model { Model {} } fn main() {} ``` -------------------------------- ### Install Vulkan on Gentoo Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/platform-specific_setup.md For Gentoo users, install Vulkan tools and headers using the emerge package manager. ```bash sudo emerge --ask --verbose dev-util/vulkan-tools dev-util/vulkan-headers ``` -------------------------------- ### Example Entry in Cargo.toml Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/running_examples.md This TOML structure defines an example, specifying its name and the path to its source file within the project. ```toml # Draw [[example]] name = "draw" path = "draw/draw.rs" ``` -------------------------------- ### Run a Nannou Example Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/running_examples.md Execute a specific nannou example using Cargo. The `--release` flag enables optimizations for better performance. ```bash cargo run --release --example draw ``` -------------------------------- ### Nannou Versioning Convention Example Source: https://github.com/nannou-org/nannou/blob/master/guide/src/contributing/publishing-new-versions.md Illustrates the MAJOR.MINOR.PATCH versioning scheme used in Rust crates, with an example of a specific version. ```text MAJOR.MINOR.PATCH 0.15.3 ``` -------------------------------- ### Nannou WGPU Teapot Example Source: https://github.com/nannou-org/nannou/blob/master/examples/README.md Demonstrates rendering a teapot model using WGPU in Nannou. ```rust use nannou::prelude::*; fn main() { nannou::app(model).update(update).run(); } struct Model {} fn model(_app: &App) -> Model { Model {} } fn update(_app: &App, _model: &mut Model, _update: Update) { // Logic for teapot rendering, camera control, etc. would be here. } fn view(app: &App, _model: &Model) { // This is a placeholder. A real teapot example would involve: // 1. Loading or generating teapot mesh data. // 2. Setting up WGPU render pipeline with appropriate shaders. // 3. Rendering the mesh to the screen. let mut command_encoder = app.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("command encoder"), }); // Example of submitting an empty command buffer (replace with actual rendering) app.queue.submit(std::iter::once(command_encoder.finish())); } ``` -------------------------------- ### Nannou WGPU Image Sequence Example Source: https://github.com/nannou-org/nannou/blob/master/examples/README.md Demonstrates handling image sequences using WGPU in Nannou. ```rust use nannou::prelude::*; fn main() { nannou::app(model).update(update).run(); } struct Model {} fn model(_app: &App) -> Model { Model {} } fn update(_app: &App, _model: &mut Model, _update: Update) { // Logic for loading and processing image sequences would be here. } fn view(app: &App, _model: &Model) { // This is a placeholder. A real example would involve: // 1. Loading multiple image textures. // 2. Creating a WGPU texture array or similar structure. // 3. Rendering these textures, potentially animating through the sequence. let mut command_encoder = app.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("command encoder"), }); // Example of submitting an empty command buffer (replace with actual rendering) app.queue.submit(std::iter::once(command_encoder.finish())); } ``` -------------------------------- ### Install ALSA Dev Package on Fedora Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/platform-specific_setup.md On Fedora, install the ALSA development package, which is required for nannou_audio. ```bash sudo dnf install alsa-lib-devel ``` -------------------------------- ### Install ALSA Dev Package on Debian/Ubuntu Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/platform-specific_setup.md On Debian/Ubuntu, install the ALSA development package, which is required for nannou_audio. ```bash sudo apt-get install libasound2-dev ``` -------------------------------- ### Run Nature of Code Example Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/running_examples.md Execute a specific example from the 'Nature of Code' series using Cargo, referencing its name. ```bash cargo run --release --example 1_1_bouncingball_novectors ``` -------------------------------- ### Nannou WGPU Triangle Example Source: https://github.com/nannou-org/nannou/blob/master/examples/README.md Demonstrates rendering a basic triangle using WGPU in Nannou. ```rust use nannou::prelude::*; fn main() { nannou::app(model).update(update).run(); } struct Model {} fn model(_app: &App) -> Model { Model {} } fn update(_app: &App, _model: &mut Model, _update: Update) { // No updates needed for this simple example. } fn view(app: &App, _model: &Model) { let mut command_encoder = app.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("command encoder"), }); let mut render_pass = command_encoder.begin_render_pass(&wgpu::RenderPassDescriptor { color_attachments: &[wgpu::RenderPassColorAttachment { view: &app.view, resolve_target: None, ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color::WHITE), store: true, }, }], depth_stencil_attachment: None, }); // In a real WGPU application, you would set up pipelines, bind groups, and draw calls here. // This example is a placeholder to show the basic WGPU setup within Nannou. app.queue.submit(std::iter::once(command_encoder.finish())); } ``` -------------------------------- ### Search for Old Version in Guide Source: https://github.com/nannou-org/nannou/blob/master/guide/src/contributing/publishing-new-versions.md This command helps find all occurrences of a specific old version string within the guide directory. Replace '0.14' with the previous version number to ensure all references are updated. ```bash grep -nr "= \"0.14\"" guide/ ``` -------------------------------- ### Nannou Simple UI Example Source: https://github.com/nannou-org/nannou/blob/master/examples/README.md Demonstrates a simple user interface using the egui integration in Nannou. ```rust #[macro_use] extern crate nannou; use nannou::prelude::* use nannou_egui::* struct Model { egui: Egui, } fn model(app: &App) -> Model { let egui = Egui::from_config(&app.conf.borrow()); Model { egui } } fn update(app: &App, model: &mut Model, _update: Update) { model.egui.begin_frame(app.time); egui::Window::new("My Window").show(model.egui.ctx(), |ui| { ui.label("Hello, egui!"); if ui.button("Click me").clicked() { println!("Button clicked!"); } }); } fn view(app: &App, model: &Model) { let draw = app.draw(); draw.background().color(WHITE); model.egui.draw_to_frame(app).unwrap(); } nannou::app(model).update(update).run(); ``` -------------------------------- ### Setup basic Nannou application Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/create_a_project.md Replace the default src/main.rs content with this code to set up a minimal Nannou application with a background color. This includes the main function, model, update, and view functions. ```rust # extern crate nannou; use nannou::prelude.* fn main() { nannou::app(model) .update(update) .simple_window(view) .run(); } struct Model {} fn model(_app: &App) -> Model { Model {} } fn update(_app: &App, _model: &mut Model) { } fn view(app: &App, _model: &Model, _window: Entity) { let draw = app.draw(); draw.background().color(PURPLE); } ``` -------------------------------- ### Nature of Code Example Entry Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/running_examples.md An example entry from the 'Nature of Code' series, showing how to reference a specific chapter's example. ```toml # Chapter 1 Vectors [[example]] name = "1_1_bouncingball_novectors" path = "chp_01_vectors/1_1_bouncingball_novectors.rs" ``` -------------------------------- ### Nannou WGPU Compute Shader Example Source: https://github.com/nannou-org/nannou/blob/master/examples/README.md Demonstrates the use of a compute shader with WGPU in Nannou. ```rust use nannou::prelude::*; fn main() { nannou::app(model).update(update).run(); } struct Model {} fn model(_app: &App) -> Model { Model {} } fn update(_app: &App, _model: &mut Model, _update: Update) { // Logic for compute shader dispatch would go here. } fn view(app: &App, _model: &Model) { // This is a placeholder. A real compute shader example would involve: // 1. Creating a compute pipeline. // 2. Creating storage buffers. // 3. Dispatching compute shaders. // 4. Reading back results from buffers if necessary. let mut command_encoder = app.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("command encoder"), }); // Example of submitting an empty command buffer (replace with actual compute dispatch) app.queue.submit(std::iter::once(command_encoder.finish())); } ``` -------------------------------- ### Install Xcode Command Line Tools Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/platform-specific_setup.md On macOS, install the Xcode command-line tools to ensure you have the necessary development environment. ```bash xcode-select --install ``` -------------------------------- ### Install Vulkan on Fedora (AMD) Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/platform-specific_setup.md For Fedora users with AMD graphics cards, install Vulkan and related tools. ```bash sudo dnf install vulkan vulkan-info ``` -------------------------------- ### Install ALSA Dev Package on Arch Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/platform-specific_setup.md On Arch Linux, install the ALSA development package, which is required for nannou_audio. ```bash sudo pacman -S alsa-lib ``` -------------------------------- ### Clone Nannou Repository Source: https://github.com/nannou-org/nannou/blob/master/guide/README.md Clone the Nannou repository and navigate to the guide directory. This is the first step to setting up a local environment for the book. ```bash git clone https://github.com/nannou-org/nannou cd nannou/guide ``` -------------------------------- ### Install Vulkan on Arch (Intel) Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/platform-specific_setup.md For Arch Linux users with Intel graphics cards, install the Vulkan Intel driver. ```bash sudo pacman -S vulkan-intel ``` -------------------------------- ### Install Vulkan on Ubuntu (AMD/Intel) Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/platform-specific_setup.md For Ubuntu users with AMD or Intel graphics cards, add a PPA for the latest drivers, update, upgrade, and then install Vulkan components. ```bash sudo add-apt-repository ppa:oibaf/graphics-drivers sudo apt-get update sudo apt-get upgrade sudo apt-get install libvulkan1 mesa-vulkan-drivers vulkan-utils ``` -------------------------------- ### Install NVIDIA Drivers and Vulkan on Ubuntu Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/platform-specific_setup.md For Ubuntu users with NVIDIA graphics cards, add a PPA for the latest drivers, update, upgrade, and then install specific NVIDIA drivers and Vulkan components. ```bash sudo add-apt-repository ppa:graphics-drivers/ppa sudo apt-get update sudo apt-get upgrade sudo apt-get install nvidia-graphics-drivers-396 nvidia-settings vulkan vulkan-utils ``` -------------------------------- ### Install XCB Dev Packages on Debian/Ubuntu Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/platform-specific_setup.md For Debian/Ubuntu users, install the XCB development packages required for Xlib interoperability. ```bash sudo apt install libxcb-shape0-dev libxcb-xfixes0-dev ``` -------------------------------- ### Install Basic Dev Packages on Debian/Ubuntu Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/platform-specific_setup.md For Debian/Ubuntu users, install essential development packages including curl, build-essential, python, cmake, and pkg-config. ```bash sudo apt-get install curl build-essential python cmake pkg-config ``` -------------------------------- ### Clone Nannou Repository Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/running_examples.md Use this command to download the nannou project files, including all examples, to your local machine. ```bash git clone https://github.com/nannou-org/nannou ``` -------------------------------- ### Nannou Draw Example Source: https://github.com/nannou-org/nannou/blob/master/examples/README.md Demonstrates basic drawing functionalities in Nannou. ```rust #[macro_use] extern crate nannou; use nannou::prelude::*; fn model() { // The model function is where you can define the state of your application. // For this simple example, we don't need any state. } fn update(app: &App) { // The update function is called on each frame. // You can use it to update the state of your application. } fn view(app: &App) { // The view function is called on each frame to draw the application. let draw = app.draw(); // Clear the background with a white color. draw.background().color(WHITE); // Draw a simple rectangle. draw.rect() .w(100.0) .h(100.0) .color(STEELBLUE); // Draw a circle. draw.ellipse() .x_y(150.0, 0.0) .w_h(100.0, 100.0) .color(SALMON); // Draw a line. draw.line() .start(vec2(-200.0, -100.0)) .end(vec2(200.0, 100.0)) .weight(5.0) .color(GOLD); // Draw text. draw.text("Hello, Nannou!") .x_y(0.0, -150.0) .color(BLACK) .font_size(40); // Write to the buffer immediately. draw.to_frame(app).unwrap(); } nannou::app(model).update(update).simple_graphics_view(view).run(); ``` -------------------------------- ### Install NVIDIA Drivers and Vulkan on Fedora Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/platform-specific_setup.md For Fedora users with NVIDIA graphics cards, add the RPM Fusion repositories and install the necessary drivers and Vulkan tools. ```bash sudo dnf install https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm https://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-$(rpm -E %fedora).noarch.rpm sudo dnf install xorg-x11-drv-nvidia akmod-nvidia vulkan-tools ``` -------------------------------- ### View Function Setup Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/basics/draw-a-sketch.md The `view` function is where drawing commands are issued. It receives the `App` object, from which a `Draw` instance can be obtained. ```rust fn view(app: &App) { let draw = app.draw(); draw.background().color(BLUE); } ``` -------------------------------- ### Install Vulkan on Debian (AMD/Intel) Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/platform-specific_setup.md For Debian users with AMD or Intel graphics cards, install the Vulkan library and mesa drivers. ```bash sudo apt-get install libvulkan1 mesa-vulkan-drivers vulkan-utils ``` -------------------------------- ### Nannou Draw Polyline Example Source: https://github.com/nannou-org/nannou/blob/master/examples/README.md Demonstrates drawing a polyline in Nannou. ```rust #[macro_use] extern crate nannou; use nannou::prelude::*; fn model() { // No model state needed. } fn update(app: &App) { // No updates needed. } fn view(app: &App) { let draw = app.draw(); draw.background().color(WHITE); // Define points for a polyline. let points = vec![ pt2(-100.0, -100.0), pt2(0.0, 100.0), pt2(100.0, -100.0), ]; // Draw the polyline. draw.polyline() .points(points) .weight(5.0) .color(STEELBLUE); draw.to_frame(app).unwrap(); } nannou::app(model).update(update).simple_graphics_view(view).run(); ``` -------------------------------- ### Install Vulkan on Arch (AMD) Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/platform-specific_setup.md For Arch Linux users with AMD graphics cards, install the Vulkan drivers and their 32-bit counterparts. ```bash sudo pacman -S vulkan-radeon lib32-vulkan-radeon ``` -------------------------------- ### Install Vulkan on Arch (NVIDIA) Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/platform-specific_setup.md For Arch Linux users with NVIDIA graphics cards, install the NVIDIA drivers and their 32-bit counterparts for Vulkan support. ```bash sudo pacman -S nvidia lib32-nvidia-utils ``` -------------------------------- ### Nannou Draw Mesh Example Source: https://github.com/nannou-org/nannou/blob/master/examples/README.md Demonstrates drawing a mesh in Nannou. ```rust #[macro_use] extern crate nannou; use nannou::prelude::*; fn model() { // No model state needed for this example. } fn update(app: &App) { // No updates needed for this example. } fn view(app: &App) { let draw = app.draw(); draw.background().color(WHITE); // Define vertices for a simple triangle. let vertices = vec![ vec2(0.0, 100.0), // Top vertex vec2(-100.0, -100.0), // Bottom-left vertex vec2(100.0, -100.0), // Bottom-right vertex ]; // Draw the mesh (triangle). draw.mesh() .vertices(&vertices) .color(SALMON) .draw(); draw.to_frame(app).unwrap(); } nannou::app(model).update(update).simple_graphics_view(view).run(); ``` -------------------------------- ### Nannou App Configuration Details Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/basics/anatomy-of-a-nannou-app.md Illustrates the chained method calls for building a Nannou application: `nannou::app` to specify the model function, `simple_window` for window creation, and `run` to start the application loop. ```rust use nannou::prelude::*; struct Model {} fn main() { nannou::app(model) // Start building the app and specify our `model` .simple_window(view) // Request a simple window to which we'll draw with `view` .run(); // Run it! } fn model(_app: &App) -> Model { Model {} } fn view(_app: &App, _model: &Model, _window: Entity) { } ``` -------------------------------- ### Install Vulkan on Debian (NVIDIA) Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/platform-specific_setup.md For Debian users with NVIDIA graphics cards, install Vulkan utilities. On older versions, this command might be sufficient. ```bash sudo apt-get install vulkan-utils ``` -------------------------------- ### Complete OSC Receiver Application Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/osc/osc-receiver.md This is a full Nannou application example that sets up an OSC receiver, processes incoming messages in the update loop, and visualizes them in the window. Ensure the `PORT` constant matches the sender's port. ```rust use nannou::prelude::* use nannou_osc as osc; // Match this to the port used by the sender. const PORT: u16 = 34254; fn main() { nannou::app(model).update(update).simple_window(view).run(); } struct Model { receiver: osc::Receiver, received_packets: Vec<(std::net::SocketAddr, osc::Packet)> } fn model(_app: &App) -> Model { let receiver = osc::receiver(PORT).unwrap(); let received_packets = vec![]; Model { receiver, received_packets, } } fn update(_app: &App, model: &mut Model) { // Receive any pending OSC packets. for (packet, addr) in model.receiver.try_iter() { model.received_packets.push((addr, packet)); } // We'll display 10 packets at a time, so remove any excess. while model.received_packets.len() > 10 { model.received_packets.remove(0); } } fn view(app: &App, model: &Model, _window: Entity) { let draw = app.draw(); draw.background().color(DARK_BLUE); // Create a string showing all the packets received so far. let mut packets_text = format!("Listening on port {} Received packets:\n", PORT); for &(addr, ref packet) in model.received_packets.iter().rev() { packets_text.push_str(&format!("{}: {:?}\n", addr, packet)); } let rect = app.window_rect().pad(10.0); draw.text(&packets_text) .font_size(16) .align_text_top() .left_justify() .wh(rect.wh()); } ``` -------------------------------- ### Nannou Draw Polygon Example Source: https://github.com/nannou-org/nannou/blob/master/examples/README.md Demonstrates drawing a polygon in Nannou. ```rust #[macro_use] extern crate nannou; use nannou::prelude::*; fn model() { // No model state needed. } fn update(app: &App) { // No updates needed. } fn view(app: &App) { let draw = app.draw(); draw.background().color(WHITE); // Define vertices for a polygon. let vertices = vec![ pt2(0.0, 100.0), pt2(-100.0, 0.0), pt2(-50.0, -100.0), pt2(50.0, -100.0), pt2(100.0, 0.0), ]; // Draw the polygon. draw.polygon() .points(vertices) .color(GOLD); draw.to_frame(app).unwrap(); } nannou::app(model).update(update).simple_graphics_view(view).run(); ``` -------------------------------- ### Basic 2D Shape Drawing Setup Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/draw/drawing-2d-shapes.md This Rust code sets up a basic Nannou application to draw a purple background and a blue ellipse. It requires the `nannou::prelude::*` import. ```rust use nannou::prelude::*; fn main() { nannou::sketch(view).run(); } fn view(app: &App) { // Prepare to draw. let draw = app.draw(); // Clear the background to purple. draw.background().color(PLUM); // Draw a blue ellipse with default size and position. draw.ellipse().color(STEEL_BLUE); } ``` -------------------------------- ### Add Rust Components Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/installing_rust.md After installing Rust, use this command to add essential components like rust-src, rustfmt, and rust-analysis. These components enhance IDE functionality and code formatting. ```bash rustup component add rust-src rustfmt-preview rust-analysis ``` -------------------------------- ### Import Nannou Prelude Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/basics/draw-a-sketch.md Imports the necessary Nannou modules for creative coding. This is a standard starting point for most Nannou sketches. ```rust use nannou::prelude::*; ``` -------------------------------- ### Define an Empty Model Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/basics/anatomy-of-a-nannou-app.md Define the application's state. This example shows an empty model as no state needs to be tracked. ```rust #![allow(dead_code)] struct Model {} ``` -------------------------------- ### Draw a Static Circle in Nannou Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/draw/animating-a-circle.md Initializes a Nannou app and draws a static blue circle at the center of a plum-colored background. This serves as the starting point for animation. ```rust # #![allow(dead_code)] # #![allow(unused_imports)] # extern crate nannou; # use nannou::prelude::*; # struct Model{} # fn main() { # nannou::app(model) # .simple_window(view) # .run(); # } # fn model(_app: &App) -> Model { # Model {} # } fn view(app: &App, _model: &Model, _window: Entity) { // Prepare to draw. let draw = app.draw(); // Clear the background to purple. draw.background().color(PLUM); // Draw a blue ellipse with a radius of 10 at the (x,y) coordinates of (0.0, 0.0) draw.ellipse().color(STEEL_BLUE).x_y(0.0,0.0); } ``` -------------------------------- ### Registering Update Function in Nannou Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/basics/anatomy-of-a-nannou-app.md This example shows how to configure a Nannou application to use the `update` function instead of the `event` function for timed updates. This is useful for game loops or animations. ```rust # #![allow(dead_code)] # use nannou::prelude::*; # struct Model {} fn main() { nannou::app(model) .update(update) // rather than `.event(event)`, now we only subscribe to updates .simple_window(view) .run(); } # fn model(_app: &App) -> Model { # Model {} # } # fn update(_app: &App, _model: &mut Model) { # } # fn view(_app: &App, _model: &Model, _window: Entity) { # } ``` -------------------------------- ### Drawing an Ellipse with Nannou Source: https://github.com/nannou-org/nannou/blob/master/guide/src/why_nannou.md This snippet demonstrates how to draw a basic ellipse using Nannou's Draw API. It requires the `nannou` crate and basic application setup. ```rust draw.ellipse().w_h(20.0, 20.0).color(RED); ``` -------------------------------- ### Nannou OSC Sender Example Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/osc/osc-sender.md This Rust code sets up an OSC sender to send X and Y coordinates derived from sine waves to a specified network address and port. It initializes the sender, maps app time to window boundaries for movement, and sends the coordinates as OSC float types. The drawing part visualizes the movement of a circle. ```rust use nannou::prelude::*; use nannou_osc as osc; fn main() { nannou::app(model).simple_window(view).run(); } struct Model { sender: osc::Sender, } fn model(_app: &App) -> Model { // The network port that data is being sent to let port = 1234; // The osc-sender expects a string in the format "address:port", for example "127.0.0.1:1234" // "127.0.0.1" is equivalent to your computers internal address. let target_addr = format!(“{}:{}”, “127.0.0.1”, port); // This is the osc Sender which contains a couple of expectations in case something goes wrong. let sender = osc::sender() .expect("Could not bind to default socket") .connect(target_addr) .expect("Could not connect to socket at address"); Model { sender } } fn view(app: &App, model: &Model, _window: Entity) { // Use app time to progress through a sine wave let sine = app.time().sin(); let slowersine = (app.time() / 2.0).sin(); // Get boundary of the window (to constrain the movements of our circle) let boundary = app.window_rect(); // Map the sine wave functions to ranges between the boundaries of the window let x = map_range(sine, -1.0, 1.0, boundary.left(), boundary.right()); let y = map_range(slowersine, -1.0, 1.0, boundary.bottom(), boundary.top()); // Send x-y coordinates as OSC let osc_addr = "/circle/position".to_string(); let args = vec![osc::Type::Float(x), osc::Type::Float(y)]; let packet = (osc_addr, args); model.sender.send(packet).ok(); // Prepare to draw. let draw = app.draw(); // Clear the background to purple. draw.background().color(PLUM); // Draw a blue ellipse at the x/y coordinates 0.0, 0.0 draw.ellipse().color(STEEL_BLUE).x_y(x, y); } ``` -------------------------------- ### Change directory to the project Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/create_a_project.md Navigate into the newly created project directory. ```bash cd my-project ``` -------------------------------- ### Converting Sketch to App: Main Function Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/basics/sketch-vs-app.md Shows how to change the `main` function signature to switch from a sketch to an app, enabling state management. ```rust nannou::sketch(view).run() ``` ```rust nannou::app(model).simple_window(view).run() ``` -------------------------------- ### Build and run the Nannou project Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/create_a_project.md Compile and run your Nannou application in release mode. The initial build may take some time as it compiles Nannou and its dependencies. ```bash cargo run --release ``` -------------------------------- ### Create a new Rust project Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/create_a_project.md Use this command to create a new Rust project directory with the specified name. ```bash cargo new my-project ``` -------------------------------- ### Get Window Rect Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/basics/window-coordinates.md Retrieve the `Rect` that describes the bounds of the application window. This is essential for any coordinate-based positioning. ```rust # #![allow(unreachable_code, unused_variables)] # use nannou::prelude.* # fn main() { # let app: App = unimplemented!(); let win = app.window_rect(); # } ``` -------------------------------- ### Get Draw Instance Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/basics/draw-a-sketch.md Obtains a `Draw` instance from the `App` object. This `Draw` instance is used to issue drawing commands. ```rust let draw = app.draw(); ``` -------------------------------- ### Basic App Template Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/basics/sketch-vs-app.md A standard nannou app structure that includes a model for state management and separate update/view functions. ```rust use nannou::prelude.* fn main() { nannou::app(model).update(update).run(); } struct Model { _window: Entity, } fn model(app: &App) -> Model { let _window = app.new_window().view(view).build(); Model { _window } } fn update(_app: &App, _model: &mut Model) {} fn view(app: &App, _model: &Model) { let draw = app.draw(); draw.background().color(PLUM); draw.ellipse().color(STEEL_BLUE); } ``` -------------------------------- ### Create a Nannou Window Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/draw/drawing-images.md This snippet shows the basic structure for creating a window in a Nannou application. It defines the model, main function, and view function required for a minimal Nannou app. ```rust # #![allow(unreachable_code, unused_variables, dead_code)] use nannou::prelude::*; struct Model {} fn main() { nannou::app(model).run(); } fn model(app: &App) -> Model { // Create a new window! app.new_window().size(512, 512).view(view).build(); Model {} } fn view(_app: &App, _model: &Model) { } ``` -------------------------------- ### Get Window Boundaries in Nannou Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/draw/animating-a-circle.md Retrieves the rectangular boundaries of the application's window. This is useful for constraining movement or determining drawing areas. ```rust # #![allow(unreachable_code, unused_variables)] # use nannou::prelude::*; # fn main() { # let app: App = unimplemented!(); let boundary = app.window_rect(); # } ``` -------------------------------- ### Basic Nannou App Structure Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/basics/anatomy-of-a-nannou-app.md This snippet shows the essential functions of a Nannou application: main, model, update, and view. The `main` function sets up the application, `model` initializes the state, `update` modifies the state, and `view` renders the state. ```rust #![allow(dead_code)] use nannou::prelude::*; fn main() { nannou::app(model).update(update).run(); } struct Model {} fn model(_app: &App) -> Model { Model {} } fn update(_app: &App, _model: &mut Model, _update: Update) { } fn view(_app: &App, _model: &Model, _frame: Frame) { let _draw = _app.draw(); } ``` -------------------------------- ### Get Maximum Y Coordinate of Window Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/draw/animating-a-circle.md Accesses the top edge coordinate of the window's rectangle. This value represents the maximum y-position within the window. ```rust # #![allow(unreachable_code, unused_variables)] # use nannou::prelude::*; # fn main() { # let boundary: geom::Rect = unimplemented!(); boundary.top(); # } ``` -------------------------------- ### Run Nannou Book Tests Source: https://github.com/nannou-org/nannou/blob/master/guide/README.md Navigate to the book-tests directory and run the tests using Cargo. This process verifies the integrity of code snippets within the markdown files. ```bash cd book-tests cargo test ``` -------------------------------- ### Get Minimum Y Coordinate of Window Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/draw/animating-a-circle.md Accesses the bottom edge coordinate of the window's rectangle. This value represents the minimum y-position within the window. ```rust # #![allow(unreachable_code, unused_variables)] # use nannou::prelude::*; # fn main() { # let boundary: geom::Rect = unimplemented!(); boundary.bottom(); # } ``` -------------------------------- ### Get Maximum X Coordinate of Window Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/draw/animating-a-circle.md Accesses the right edge coordinate of the window's rectangle. This value represents the maximum x-position within the window. ```rust # #![allow(unreachable_code, unused_variables)] # use nannou::prelude::*; # fn main() { # let boundary: geom::Rect = unimplemented!(); boundary.right(); # } ``` -------------------------------- ### Converting Sketch to App: View Function Signature Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/basics/sketch-vs-app.md Demonstrates the change in the `view` function signature required when converting a sketch to an app, adding `_model` and `_window` parameters. ```rust fn view(app: &App) {} ``` ```rust fn view(app: &App, _model: &Model, _window: Entity) {} ``` -------------------------------- ### Get Minimum X Coordinate of Window Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/draw/animating-a-circle.md Accesses the left edge coordinate of the window's rectangle. This value represents the minimum x-position within the window. ```rust # #![allow(unreachable_code, unused_variables)] # use nannou::prelude::*; # fn main() { # let boundary: geom::Rect = unimplemented!(); boundary.left(); # } ``` -------------------------------- ### Translate and Scale Texture in Nannou Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/draw/drawing-images.md Draws a texture at a specific position and size. This example positions and scales the texture to fit within a 100x100 square at the top-left corner of the window. ```rust #![allow(unreachable_code, unused_variables, dead_code)] # use nannou::prelude::*; # struct Model { # texture: Handle # } # fn main() { # nannou::app(model).run(); # } # fn model(app: &App) -> Model { # // Create a new window! # app.new_window().size(512, 512).view(view).build(); # // Load the image from disk and upload it to a GPU texture. # let texture = app.asset_server().load("images/nature/nature_1.jpg"); # Model { texture } # } fn view(app: &App, model: &Model) { let draw = app.draw(); draw.background().color(BLACK); let win = app.window_rect(); let r = geom::Rect::from_w_h(100.0, 100.0).top_left_of(win); draw .rect() .texture(&model.texture) .xy(r.xy()) .wh(r.wh()); } ``` -------------------------------- ### Navigate to Nannou Directory Source: https://github.com/nannou-org/nannou/blob/master/guide/src/getting_started/running_examples.md After cloning, change your current directory to the newly created nannou folder to access its contents. ```bash cd nannou ``` -------------------------------- ### Initialize OSC Sender in Model Function Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/osc/osc-sender.md Set up the OSC sender by defining the port, target address, and connecting the sender. Handles potential binding and connection errors. ```rust # #![allow(dead_code, unused_imports)] # use nannou_osc as osc; # use nannou::prelude::*; # struct Model { # sender: osc::Sender, # } fn model(_app: &App) -> Model { let port = 1234; let target_addr = format!("{}:{}", "127.0.0.1", port); let sender = osc::sender() .expect("Could not bind to default socket") .connect(target_addr) .expect("Could not connect to socket at address"); Model { sender } } # fn main() {} ``` -------------------------------- ### Convert Points to Pixels in Nannou Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/basics/window-coordinates.md Multiply the point value by the window's scale factor to get the equivalent in physical pixels. This is useful when interacting with lower-level graphics APIs that expect physical pixel dimensions. ```rust # #![allow(unreachable_code, unused_variables)] # use nannou::prelude.* # fn main() { # let points = 100.0; # let window: Window = unimplemented!(); let pixels = points * window.scale_factor(); # } ``` -------------------------------- ### Construct Target Address String Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/osc/osc-sender.md Format the target network address string using the IP address (e.g., "127.0.0.1") and the chosen port. ```rust # #![allow(unused_variables)] # fn main() { # let port = 1234; let target_addr = format!("{}:{}", "127.0.0.1", port); # } ``` -------------------------------- ### Nannou Sketch Entry Point Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/basics/draw-a-sketch.md The main function that initializes and runs a Nannou sketch. It takes a `view` function as an argument, which is responsible for drawing. ```rust nannou::sketch(view).run(); ``` -------------------------------- ### Convert Pixels to Points in Nannou Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/basics/window-coordinates.md Divide the physical pixel value by the window's scale factor to get the equivalent in points. This is useful for translating input events or external measurements into Nannou's coordinate system. ```rust # #![allow(unreachable_code, unused_variables)] # use nannou::prelude.* # fn main() { # let pixels = 100.0; # let window: Window = unimplemented!(); let points = pixels / window.scale_factor(); # } ``` -------------------------------- ### Basic Nannou App Structure Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/basics/anatomy-of-a-nannou-app.md The main function serves as the entry point for Rust programs. In Nannou, it's used to configure and run the application, specifying the model, view, and event handling. ```rust use nannou::prelude::*; struct Model {} fn main() { nannou::app(model) .simple_window(view) .run(); } fn model(_app: &App) -> Model { Model {} } fn view(_app: &App, _model: &Model, _window: Entity) { } ``` -------------------------------- ### Basic Sketch Template Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/basics/sketch-vs-app.md A minimal nannou sketch for quick drawing. It requires only a `view` function and runs directly. ```rust use nannou::prelude.* fn main() { nannou::sketch(view).run() } fn view(app: &App) { let draw = app.draw(); draw.background().color(PLUM); draw.ellipse().color(STEEL_BLUE); } ``` -------------------------------- ### Import nannou_osc Crate Source: https://github.com/nannou-org/nannou/blob/master/guide/src/tutorials/osc/osc-introduction.md Import the OSC functionality into your main.rs file using a use-statement. ```rust # #![allow(unused_imports)] use nannou_osc as osc; # fn main() {} ```