A lightweight (?) version of P5 made in Swift.
Since the pandemic I’ve been watching watching some amazing coding challenges made by Daniel Shiffman using a framework called p5.js. But, since I don’t really like web dev I thought it’d be cool if I recreated p5 in Swift and followed one of those challenges/tutorials.
I have to admit that I found some other solutions here in Github (obviously better than mine) which are the following: p5swift, p5-in-swift. So, if you find something simular to those, maybe it’s not a coincidence.
To be honest, I could just use of those other solutions that I found, but it’s just not the same to work with something you know you did all by yourself, right?
P5Swfit is just, let’s say, a wrapper of a CGContext
. All the drawing operations were implemented using the ones that a CoreGraphics context provides us. So… yeah I didn’t do that much.
This is a swift package so installation is straight forward. In XCode just go to File -> Add Package Dependencies
and put the link to this repository.
When you need to create a new canvas o sketch (as the p5 community like to call it) you have to to the following:
class MySktech: P5Sketch {
func setup() {
// it will be called only one time (when it's initialized)
}
func draw() {
// it will be called every frame
}
}
And then, inside your UIViewController
you add the view (canvas) associated with the sketch
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let sketch = MySketch(ofSize: view.frame.size)
view.addSubview(sketch.view)
}
}
That’s it!
I made a little demo app that you can run to see some examples of what you can achieve with P5.
Game of life | Starfield | Fourier Series |
---|---|---|