My First Sketch
This tutorial will guide you through creating your first Peruna sketch using SwiftUI. Peruna is designed to work seamlessly with SwiftUI, making it easy to create interactive graphics and animations.
Basic Setup
To create a Peruna sketch, you need to use the PerunaCanvas
view. This view takes two closures: a setup closure and a draw closure.
1struct ContentView: View {2 var body: some View {3 PerunaCanvas { p in4 // Setup code here (initialize arrays, objects, etc)5 } draw: { p, frameCount in6 // Drawing code here7 }8 }9}
Complete Example
Here's a complete example that creates a colorful animation with rectangles and ellipses:
1struct ContentView: View {2 // Declare variables and constants in SwiftUI34 var body: some View {5 PerunaCanvas { p in6 // Setup here (initialize arrays, objects, etc)78 } draw: { p, frameCount in9 // Set background color with p.background10 // You can use 1, or 3 arguments. 1 argument will11 // provide you with a gray color, 3 arguments will12 // let you choose the red, green, and blue values13 p.background(color: p.color(14 r: sin(Float(frameCount)*0.01)*255,15 g: sin(Float(frameCount)*0.02)*255,16 b: sin(Float(frameCount)*0.03)*25517 )18 )1920 // Draw a rectangle, give colors to the outline with stroke()21 // and fill it with color with p.fill()22 // smuRed and smuBlue are easy to use built-in colors23 // meant for quick prototyping24 p.fill(p.smuRed)25 p.stroke(p.smuBlue)26 p.rect(27 x: p.width/2,28 y: p.height/2,29 width: p.width/2,30 height: p.height/2 )3132 // Draw an ellipse, give colors to the outline with stroke()33 // and fill it with color with p.fill()34 p.fill(p.smuBlue)35 p.stroke(p.smuRed)36 p.ellipse(37 x: p.width/2,38 y: p.height/4,39 width: p.width/3,40 height: p.width/3 )41 }42 }43}
Key Concepts
PerunaCanvas
The main view that provides the drawing context. It has two closures:
setup
: Called once when the view is createddraw
: Called repeatedly for animation
Drawing Context (p)
The p
parameter provides access to all drawing functions:
p.background()
: Sets the background colorp.fill()
: Sets the fill colorp.stroke()
: Sets the stroke colorp.rect()
: Draws a rectanglep.ellipse()
: Draws an ellipse
Frame Count
The frameCount
parameter tracks how many frames have been drawn. It's useful for creating animations that change over time.
Built-in Colors
Peruna includes several built-in colors for quick prototyping:
p.smuRed
: SMU's red colorp.smuBlue
: SMU's blue color
Next Steps
Now that you've created your first sketch, you can explore more advanced features:
- Add user interaction with
p.mouseX
andp.mouseY
- Create more complex shapes with
p.triangle()
andp.line()
- Use
p.random()
to add randomness to your sketches - Explore the
p.noise()
function for organic patterns
Check out the other examples in the sidebar to learn more about specific features.