This was a lot of effort to get working, especially in the constrained Swift Playgrounds App. But I learned a ton.
Big shootout to the contributors of Tokamak who already solved a lot of problems. Full source code and the Playground are available on GitHub.