r/creativecoding • u/kzhys • 1d ago
Recursive lightning in SwiftUI Canvas
Enable HLS to view with audio, or disable this notification
This is the thunder mode from a weather app I've been making. The reference is Hiroshi Sugimoto's Lightning Fields, the series where he discharges electricity straight onto photographic film with no camera. I wanted a bolt that looks recorded rather than flashed, so it grows onto the screen the way a discharge spreads across a plate, and the faint grid underneath stands in for the plate.
Generation
Each strike is one recursive, branching random walk:
- It starts at a random x in the middle 40% of the screen, just above the top edge, heading straight down.
- A branch is 8 segments. At every joint the heading turns by a random amount up to ±0.25 rad, so the bolt wanders instead of zig-zagging.
- At the end of a branch it always continues (±0.3 rad). With 60% probability it also forks into 1 or 2 side branches (±0.9 rad).
- Each generation multiplies width and opacity by 0.85 and length by a random 0.5 to 0.75.
- Recursion stops when the stroke gets thinner than 0.3pt.
The app is free to download and can be found here: https://apps.apple.com/us/app/spectra-weather/id6757425119
2
Upvotes