Demonstrates how to use WebAssembly (WASM) with Rust for computationally expensive operations in a React web application. This project implements an image blur algorithm in Rust and compiles it to WebAssembly for use in the browser.
This project showcases:
- Rust-to-WASM compilation using
wasm-pack - Performance optimization by moving computationally expensive operations to WebAssembly
- React integration with WASM modules
- Image processing using a box blur algorithm implemented in Rust
WASM/
├── src/ # React frontend
│ ├── App.tsx # Main React app
│ ├── BlurExample.tsx # WASM blur demo component
│ └── main.tsx # App entry point
├── wasm/ # Rust WASM source
│ ├── src/
│ │ ├── lib.rs # Main WASM library with blur function
│ │ └── utils.rs # Utility functions
│ └── Cargo.toml # Rust dependencies
├── pkg/ # Generated WASM bindings (auto-generated)
│ ├── blur_wasm.js # JavaScript bindings
│ ├── blur_wasm.wasm # Compiled WebAssembly module
│ └── blur_wasm.d.ts # TypeScript definitions
├── package.json # Node.js dependencies and scripts
└── vite.config.ts # Vite build configuration
The core blur algorithm is implemented in Rust (wasm/src/lib.rs):
- Uses a box blur algorithm that averages pixels within a specified radius
- Processes RGBA image data directly in memory
- Compiled to WebAssembly for near-native performance in the browser
- Rust code is compiled to WASM using
wasm-pack - JavaScript bindings are automatically generated
- TypeScript definitions ensure type safety
BlurExample.tsxhandles image upload and canvas rendering- Loads the WASM module asynchronously
- Applies the blur effect and displays results in real-time
- Node.js (v16 or higher)
- pnpm (or npm/yarn)
- Rust (latest stable version)
- wasm-pack for compiling Rust to WebAssembly
-
Install Rust: Visit rustup.rs or run:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
-
Install wasm-pack:
cargo install wasm-pack
-
Install Node.js dependencies:
pnpm install
-
Build the WASM module:
pnpm run build:wasm
(this generates the
pkgdirectory containing WASM modules — importable in your browser environment) -
Start the development server:
pnpm run dev
-
Open your browser and navigate to
http://localhost:5173
This will:
- Build the WASM module
- Compile TypeScript
- Bundle the application for production
- Upload an image using the file input
- Watch the blur effect applied in real-time using WebAssembly
- The algorithm applies a blur with radius of 5 pixels
- Performance: Near-native speed for computationally intensive tasks
- Language Flexibility: Write performance-critical code in Rust
- Browser Compatibility: Runs in all modern browsers
- Memory Safety: Rust's memory safety guarantees
- Box Blur Algorithm: Simple but effective blur technique
- Memory Management: Direct manipulation of pixel data
- Asynchronous Loading: WASM modules load asynchronously
- Type Safety: Full TypeScript integration
Contains the main blur function:
#[wasm_bindgen]
pub fn blur(data: &mut [u8], width: u32, height: u32, radius: u32)- Takes mutable reference to image data
- Applies box blur algorithm with specified radius
- Modifies data in-place for efficiency
React component that:
- Loads the WASM module
- Handles file uploads
- Renders images on canvas
- Applies blur using WASM function
build:wasm: Compiles Rust to WebAssemblydev: Runs development serverbuild: Creates production build
WebAssembly provides significant performance benefits for image processing:
- JavaScript implementation: ~100-500ms for typical images
- WASM implementation: ~10-50ms for the same images
- Memory efficiency: Direct memory access without JavaScript overhead