Skip to content

juandahurt/P5Swift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

P5Swift

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.

Why?

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?

How?

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.

Usage

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.

Sketch

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!

Some examples

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
246639163-0fd1f710-a26f-4ed8-a88f-4a7475e39fe0 246866221-fc850022-898d-4808-80ce-18ce688b703b 247984017-0efc0ce0-5139-4c6b-a27f-c053a6e9433b

About

An implementation of P5js in Swift

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages