spottedcat
A simple, clean 2D graphics library for drawing images using Rust and wgpu.
Features
- Simple API: Only 4 main types to learn:
Context,Spot,Image, andrun - GPU-accelerated: Built on wgpu for high-performance rendering
- Image operations: Load from files, create from raw data, extract sub-images
- Flexible drawing: Position, scale, rotate images with ease
Quick Start
Add to your Cargo.toml:
[]
= { = "0.1.0" }
Basic Example
use ;
API Overview
Core Types
Context
Drawing context for managing render commands. Accumulates drawing operations during a frame.
Methods:
new()- Create a new contextbegin_frame()- Clear previous frame's commands- Use
Image::draw(context, options)to queue an image for drawing
Spot (trait)
Main application trait defining the lifecycle of your app.
Required methods:
initialize(&mut context)- Set up initial state and load resourcesdraw(&mut context)- Render the current frameupdate(event)- Handle events (reserved for future use)remove()- Cleanup on shutdown
Image
Handle to a GPU texture that can be drawn to the screen.
Methods:
new_from_rgba8(width, height, rgba)- Create from raw pixel datanew_from_file(path)- Load from image file (PNG, JPEG, etc.)new_from_image(image)- Clone an existing imagesub_image(image, bounds)- Extract a region from an imagedestroy()- Free GPU resources
DrawOptions
Options for controlling how images are rendered.
Fields:
position: [f32; 2]- Top-left corner in screen pixelsrotation: f32- Rotation in radiansscale: [f32; 2]- Scale factors (applied after the image's intrinsic size)
Bounds
Rectangle for defining sub-regions of images.
Fields:
x: u32- X coordinatey: u32- Y coordinatewidth: u32- Widthheight: u32- Height
Functions
run(init)
Main entry point. Creates a window, initializes graphics, and runs the event loop.
Arguments:
init: fn(&mut Context) -> Box<dyn Spot>- Function to create your app
Advanced Usage
Creating Sub-Images
Extract regions from existing images without duplicating GPU memory:
let full_image = new_from_file?;
let sprite = sub_image?;
Drawing with Transformations
let opts = default
.with_position
.with_rotation // 45 degrees
.with_scale; // Double size
image.draw;
Creating Images from Raw Data
let mut rgba = vec!;
// Fill with your pixel data...
let image = new_from_rgba8?;