Turn any SVG into a professional 60 fps animation. A Claude Code skill that brings anything drawn to life: live faces, full-body acting and whole scenes, in React Native and SwiftUI.
Swift
Two new demos, garden and chef, replace the room
Eder Rosa9b20964e32f0
diff --git a/README.md b/README.mdindex 7242b04..b922be6 100644--- a/README.md+++ b/README.md@@ -50,6 +50,24 @@ For motion in an interface, it follows [Emil Kowalski's](https://github.com/emil- a ball rolls in from off screen, the eyes follow it, the kick hits at the middle of the swing, and the ball flies off spinning; then the kid turns to you and waves;- a toy-like build seen from a little above: a big head, a short torso, round shoes and a wide oval shadow.+**Garden** ([source](demo/GardenScene.swift), [MP4](demo/garden.mp4)), a 10-second loop:++<img width="360" alt="A girl in a sun hat waters a sprout; it grows and blooms, a butterfly lands on the flower, she hops with joy, then the petals fall and the sprout returns" src="demo/garden.gif" />++- she pulls the can back, then pours: drops fall in seeded arcs with gravity and the soil darkens as it gets wet;+- the stem grows in two pushes, the leaves unfurl one after another, and the petals open one by one with a small twist;+- a butterfly wanders in from off screen and lands, the stem dips under its weight, and her head and eyes track it all the way;+- she grins and hops, with her pigtails, hat and can trailing the motion; then the petals drift down and the plant settles back into the sprout.++**Chef** ([source](demo/ChefScene.swift), [MP4](demo/chef.mp4)), a 9.6-second loop:++<img width="360" alt="A chef flips a pancake; the second flip goes too high, lands on his hat, wobbles, slides off and he catches it in the pan" src="demo/chef.gif" />++- steam rises from the pan, the flame flickers on two waves, and the burner knob turns up before each flip;+- the first flip: knees dip, the pancake turns in the air under gravity, his eyes follow it and his head a beat later, and it lands with a squash;+- the second flip goes too high: surprise, it lands on his hat, the hat squashes and wobbles, the pancake slides off and he catches it with the pan;+- contact stays exact: every throw starts and ends where the pan or the hat really is, and the hand is solved onto the handle.+**Orbit** ([source](demo/OrbitScene.swift), [MP4](demo/orbit.mp4)):<img width="360" alt="A round bird with closed eyes floats while a tilted ring of cards orbits around it, with sparkles twinkling" src="demo/orbit.gif" />@@ -58,21 +76,11 @@ For motion in an interface, it follows [Emil Kowalski's](https://github.com/emil- the bird floats and tilts, its wings flutter, and its feet and tufts follow through;- sparkles that appear and disappear once per loop, at seeded times.-**Room** ([source](demo/DemoScene.swift), [MP4](demo/demo.mp4)):--<img width="360" alt="A creature with live eyes, snow in the window, steam, a candle and a swaying plant, all on one clock" src="demo/demo.gif" />--- a creature that breathes, with ears that follow through and live eyes that look, blink and smile;-- snow in the window, with seeded variety;-- steam that rises and fades;-- a candle flickering on two waves;-- a plant swaying around its base, with leaves that lag behind.-To render them again on macOS 14+:```bashswiftc -parse-as-library -target arm64-apple-macos14 \templates/swiftui/Motion.swift templates/swiftui/LiveFace.swift demo/*.swift -o /tmp/render-demo-/tmp/render-demo reveal /tmp/frames phone fps=60 # or: orbit, room+/tmp/render-demo reveal /tmp/frames phone fps=60 # or: garden, chef, orbitffmpeg -framerate 60 -i /tmp/frames/%04d.png -c:v libx264 -pix_fmt yuv420p -crf 20 demo/reveal.mp4```diff --git a/demo/ChefScene.swift b/demo/ChefScene.swiftnew file mode 100644index 0000000..6e0da2b--- /dev/null+++ b/demo/ChefScene.swift@@ -0,0 +1,826 @@+// ChefScene.swift: a chef flips a pancake, twice. The first flip is clean: the pan dips, flicks,+// the pancake turns over in the air and lands with a puff of steam, and he gives a proud little+// nod. The second goes too high: it sails past his eyes, lands on his hat, wobbles there, slides+// off and drops into the pan he raises to catch it. A relieved grin and a shrug. One clock, a+// 9.6-second loop. Every acting channel is a `flow` of key poses plus idle life plus+// follow-through; the pancake is a prop with real contact (it leaves and lands exactly where the+// pan or the hat is at that moment), and the eyes track it. Render it at 60 fps.+//+// The chef uses the same shapes as the kid in RevealScene: a 100-wide tombstone head centred on+// (0, 0) with y growing down, pill eyes, capsule arms whose shoulders hide behind the torso,+// circle hands, flat colours with at most one shade. He stands behind a stove, so the counter+// hides his legs.+import SwiftUI++struct ChefScene: View {+ let t: Double+ /// Taller than the scene (a phone screen): the wall fills the top, the stove the bottom.+ var height = 720.0+ /// Where the 720-tall stage starts inside that height; centred when nil.+ var stageTop: Double? = nil+ private var top: Double { stageTop ?? (height - Self.size.height) / 2 }+ static let size = CGSize(width: 720, height: 720)+ static let loopMs = 9600.0++ private enum Ink {+ static let wall = Color(hex: 0xFFE7BF)+ static let tile = Color(hex: 0xF6D6A2)+ static let wood = Color(hex: 0xD99A5B)+ static let woodDark = Color(hex: 0xBF7F45)+ static let steel = Color(hex: 0xAEB8C4)+ static let steelDark = Color(hex: 0x8C98A6)+ static let jar = Color(hex: 0xDDF1F4)+ static let flour = Color.white+ static let pasta = Color(hex: 0xF7C548)+ static let lidRed = Color(hex: 0xF0645A)+ static let lidBlue = Color(hex: 0x5FB0E8)+ static let leaf = Color(hex: 0x58C27D)+ static let pot = Color(hex: 0xE58A55)+ static let counterTop = Color(hex: 0xF3C27E)+ static let counterLip = Color(hex: 0xE2A862)+ static let stove = Color(hex: 0x33B2A6)+ static let stoveDark = Color(hex: 0x27978D)+ static let knob = Color(hex: 0xFFF3DC)+ static let glass = Color(hex: 0x1F5E5A)+ static let glow = Color(hex: 0xE9893B)+ static let glowCore = Color(hex: 0xF8B55A)+ static let ring = Color(hex: 0x34343D)+ static let ringIn = Color(hex: 0x4A4A55)+ static let flame = Color(hex: 0x4E9EFF)+ static let flameCore = Color(hex: 0xBFE3FF)+ static let pan = Color(hex: 0x3A3F4B)+ static let panSide = Color(hex: 0x2A2E37)+ static let panIn = Color(hex: 0x4D5464)+ static let grip = Color(hex: 0xB8743E)+ static let cakeA = Color(hex: 0xF8CB69)+ static let cakeB = Color(hex: 0xDE9440)+ static let cakeBIn = Color(hex: 0xEDAE58)+ static let cakeEdge = Color(hex: 0xC97F34)+ static let bubble = Color(hex: 0xFCE09A)+ static let steam = Color(hex: 0xE4EBF2)+ static let skin = Color(hex: 0xF2B994)+ static let feature = Color(hex: 0xD27E58)+ static let blush = Color(hex: 0xF79A86)+ static let hair = Color(hex: 0x5B3A28)+ static let white = Color.white+ static let shade = Color(hex: 0xE3E8EE)+ static let button = Color(hex: 0xC9D2DC)+ static let scarf = Color(hex: 0xFF5A4E)+ static let scarfDark = Color(hex: 0xE5443A)+ static let pen = Color(hex: 0x4F8FE8)+ static let pupil = Color(hex: 0x3C3C3C)+ static let mouth = Color(hex: 0xB03A3A)+ static let tongue = Color(hex: 0xF15A5A)+ }++ // MARK: - Tokens++ /// The chef's head centre on stage, and chef units (head = 100 wide) to stage points.+ private let anchor = CGPoint(x: 360, y: 300)+ private let chefScale = 1.45+ /// The build, seen from a little above: a big head right on the collar, a short torso narrower+ /// than the head. The body leans around the waist, which the counter hides.+ private let build = (torsoTop: 50.0, torsoBottom: 112.0, shoulders: 42.0, waist: 38.0, waistY: 112.0)+ /// Arms: shoulder (inside the torso), sleeve thickness, hand radius, upper arm and forearm+ /// (similar lengths, one shared round elbow).+ private let arm = (x: 36.0, y: 66.0, sleeve: 24.0, hand: 14.0, upper: 30.0, lower: 28.0)+ /// The counter's top seen from above (back and front edge, stage y) and the burner on it.+ private let counter = (back: 440.0, front: 478.0, lip: 10.0)+ private let burner = (x: 355.0, y: 462.0, w: 100.0, h: 30.0)+ /// The pan (stage units): rim radius, how flat the floor makes it look from above, the depth+ /// of its wall, and where the hand holds the handle (from the pan's centre).+ private let pan = (r: 58.0, ratio: 0.34, wall: 10.0, grip: CGPoint(x: 105, y: -4), handleW: 13.0)+ /// The pancake: radius, the camera's tilt (a flat disc shows sin(tilt) of its height), thickness.+ private let cake = (r: 40.0, tilt: asin(0.34), thick: 7.0)+ /// The beats of the loop, in ms: the two tosses, the landings, the slide off the hat.+ private let beat = (toss1: 2100.0, catch1: 2930.0, toss2: 4900.0, onHat: 5700.0, slide: 6500.0, drop: 6750.0,+ catch2: 7250.0)+ /// Gravity for the pancake, stage units a second squared.+ private let gravity = 2450.0++ private static let ink = FaceInk(white: .white, pupil: Ink.pupil, skin: Ink.skin, line: Ink.feature, lineWidth: 5)+ /// Butter blinks on the turns, plus a flinch when the pancake hits the hat and a focused squint+ /// before the big toss.+ private static let lid: Keys = {+ func blink(_ b: Double) -> Keys { [Key(b, 0), Key(b + 110, 1), Key(b + 150, 1), Key(b + 340, 0, out: true)] }+ var keys = [Key(0, 0)] + blink(900) + blink(3150)+ keys += [Key(4550, 0), Key(4700, 0.28), Key(4880, 0.28), Key(5050, 0, out: true)]+ keys += [Key(5700, 0), Key(5770, 0.85), Key(5830, 0.85), Key(6010, 0, out: true)]+ return keys + blink(6250) + blink(8800) + [Key(loopMs, 0)]+ }()+ /// A smug half-squint after the first flip, the full happy squint after the catch.+ private static let cheek: Keys = [Key(0, 0), Key(3130, 0), Key(3290, 0.42, out: true), Key(3650, 0.42), Key(3850, 0),+ Key(7330, 0), Key(7470, 1, out: true), Key(8100, 1), Key(8300, 0), Key(loopMs, 0)]+ /// The open mouth's depth: a small smile at rest, a grin, an "O" of surprise, a big relieved grin.+ private static let mouth: Keys = [+ Key(0, 8), Key(1900, 6), Key(2150, 12), Key(2830, 10), Key(3130, 10), Key(3290, 18, out: true), Key(3650, 16),+ Key(3950, 8), Key(4600, 5), Key(4950, 10), Key(5200, 12), Key(5400, 20, out: true), Key(5700, 20), Key(5770, 12),+ Key(6000, 14), Key(6400, 10), Key(6600, 18), Key(7250, 16), Key(7420, 24, out: true), Key(8100, 22), Key(8400, 9),+ Key(loopMs, 8),+ ]++ var body: some View {+ Canvas { ctx, _ in+ ctx.fill(Path(CGRect(x: 0, y: 0, width: Self.size.width, height: height)), with: .color(Ink.wall))+ var stage = ctx+ stage.translateBy(x: 0, y: top)+ drawKitchen(stage)+ let p = pose(t)+ drawChef(stage, p)+ drawCounter(stage)+ drawBurner(stage)+ let c = cakeState(t)+ drawPan(stage)+ if c.mode == .pan { drawCake(stage, c) }+ drawHands(stage, p)+ if c.mode != .pan { drawCake(stage, c) }+ drawSteam(stage)+ }+ .frame(width: Self.size.width, height: height)+ }++ // MARK: - Acting: smooth channels through key poses, plus idle life and follow-through++ /// One arm: `a` the upper arm's outward angle (0 hangs down), `e` the elbow bend+ /// (the forearm points at a + e; positive folds it up and out, negative in toward the body).+ private struct Arm { var a = 20.0, e = 0.0 }++ private struct Pose {+ var lift = 0.0, sx = 1.0, sy = 1.0, lean = 0.0, shift = 0.0, yaw = 0.0+ /// Head: tilt in degrees and a bob down (positive) in chef units.+ var head = 0.0, headDy = 0.0+ var browL = 0.0, browR = 0.0, widen = 0.0, mouthW = 28.0+ var armL = Arm()+ /// The hat: its squash and its tilt, both on top of the head.+ var hatSy = 1.0, hatTilt = 0.0+ }++ /// The key poses. The pan leads the right arm (the hand follows the handle); the left arm,+ /// body and face act around it.+ private enum Act {+ // Body: dips before each toss (knees bending behind the counter), rises with it, ducks+ // when the pancake hits the hat, bounces with relief.+ static let lift: FlowKeys = [(0, 0), (1700, 0), (1950, -9), (2050, -6), (2110, 4), (2250, 2), (2830, 0), (2930, -5),+ (3080, 0), (3230, 1), (3430, 0), (4400, 0), (4750, -12), (4850, -9), (4920, 6),+ (5100, 3), (5600, 0), (5700, 0), (5780, -7), (5950, 0), (6500, 0), (6750, -2),+ (7250, -6), (7450, 6), (7650, 0), (7850, 3), (8050, 0), (9600, 0)]+ static let sy: FlowKeys = [(0, 1), (1700, 1), (1950, 0.96), (2110, 1.04), (2300, 1), (4400, 1), (4750, 0.95),+ (4920, 1.05), (5150, 1), (5700, 1), (5780, 0.95), (5950, 1), (7100, 1), (7250, 0.96),+ (7450, 1.03), (7650, 1), (9600, 1)]+ static let lean: FlowKeys = [(0, 0), (1700, 0), (1950, -3), (2110, 2), (2450, -1), (2930, 1), (3130, -2), (3430, 0),+ (4400, 0), (4750, -4), (4920, 3), (5300, -2), (5700, 1), (6000, 0), (6500, 0),+ (6900, 3), (7250, 5), (7500, -2), (8000, 2), (8400, 0), (9600, 0)]+ static let shift: FlowKeys = [(0, 0), (1600, 0), (2100, 2), (2930, -2), (3430, 0), (4500, -3), (5000, 3), (5700, 0),+ (6800, 4), (7250, 7), (7800, 0), (9600, 0)]+ static let yaw: FlowKeys = [(0, 0.12), (1700, 0.12), (2100, 0.04), (3030, 0.08), (3230, -0.06), (3700, -0.06),+ (3950, 0.12), (4800, 0.12), (5000, 0), (6000, 0), (6250, -0.06), (6450, 0), (7000, 0.18),+ (7300, 0.12), (7450, -0.08), (8100, -0.08), (8500, 0.12), (9600, 0.12)]+ static let head: FlowKeys = [(0, 0), (1700, 0), (2000, -2), (2450, -5), (2930, 2), (3180, 6), (3500, 5), (3800, 0),+ (4600, -2), (5000, -3), (5700, 3), (5900, -2), (6250, -6), (6500, -3), (7250, 3),+ (7500, 8), (8100, 6), (8500, 0), (9600, 0)]+ /// The proud nod (down, up, down a little), and the duck when the pancake lands on the hat.+ static let headDy: FlowKeys = [(0, 0), (3080, 0), (3210, 4), (3360, -1), (3510, 2.5), (3730, 0), (5700, 0),+ (5770, 5), (6000, 1), (6400, 0), (9600, 0)]+ /// Where he looks: 1 at the pancake, 0 at the viewer.+ static let attention: FlowKeys = [(0, 1), (3030, 1), (3180, 0), (3750, 0), (3950, 1), (5950, 1), (6150, 0),+ (6450, 0), (6520, 1), (7300, 1), (7450, 0), (8400, 0), (8700, 1), (9600, 1)]+ /// Surprise opens the eyes wider; the brows go up, or down and in to focus.+ static let widen: FlowKeys = [(0, 0), (5150, 0), (5350, 1), (5700, 1), (5770, 0.2), (5950, 0.8), (6400, 0.6),+ (6550, 1), (7250, 0.8), (7400, 0), (9600, 0)]+ static let brow: FlowKeys = [(0, 0), (1700, 0), (1950, -0.5), (2150, 0.2), (3030, 0), (4400, 0), (4750, -0.8),+ (4950, 0), (5200, 0.2), (5350, 1), (5700, 1), (5770, 0.3), (5950, 0.8), (6400, 0.6),+ (6550, 1), (7250, 0.9), (7450, 0.6), (8100, 0.4), (8500, 0), (9600, 0)]+ /// One brow up after the first flip: smug.+ static let smug: FlowKeys = [(0, 0), (3080, 0), (3230, 0.9), (3600, 0.8), (3850, 0), (9600, 0)]+ static let mouthW: FlowKeys = [(0, 28), (2100, 28), (2200, 22), (2830, 24), (3130, 30), (3650, 32), (3950, 28),+ (5200, 26), (5400, 16), (5700, 16), (5770, 22), (6000, 18), (6400, 20), (6600, 15),+ (7250, 16), (7420, 34), (8100, 34), (8400, 28), (9600, 28)]+ /// The free arm: resting on the counter, out for balance on each toss, a little "voila"+ /// after the first, up by the face in surprise, out for the catch, a palm-up shrug.+ static let armLa: FlowKeys = [(0, 35), (1700, 35), (1950, 45), (2100, 62), (2450, 55), (2930, 48), (3130, 45),+ (3650, 45), (3950, 35), (4400, 35), (4750, 50), (4920, 78), (5200, 70), (5350, 150),+ (5700, 150), (5770, 160), (6000, 150), (6400, 140), (6600, 80), (7000, 62),+ (7250, 70), (7450, 30), (8100, 30), (8400, 35), (9600, 35)]+ static let armLe: FlowKeys = [(0, -25), (1700, -25), (1950, -10), (2100, 20), (2450, 25), (2930, 15), (3130, 40),+ (3290, 95), (3650, 90), (3950, -25), (4400, -25), (4750, -5), (4920, 30), (5200, 25),+ (5350, 15), (5700, 15), (5770, 25), (6000, 15), (6400, 20), (6600, 15), (7000, 10),+ (7250, 10), (7450, 75), (8100, 75), (8400, -20), (9600, -25)]+ /// The pan, in stage points: rests over the burner, dips and flicks for each toss, waits under+ /// the falling pancake, absorbs the catch, reaches up and right for the one that fell off the hat.+ static let panX: FlowKeys = [(0, 355), (1700, 355), (1950, 350), (2050, 352), (2130, 356), (2400, 358), (2930, 358),+ (3130, 356), (3330, 355), (4400, 355), (4750, 350), (4850, 352), (4930, 356),+ (5300, 357), (5700, 356), (6400, 356), (6800, 370), (7250, 378), (7500, 375),+ (8300, 360), (8800, 355), (9600, 355)]+ static let panY: FlowKeys = [(0, 432), (1700, 432), (1950, 437), (2050, 434), (2130, 402), (2400, 396), (2750, 402),+ (2930, 410), (3030, 424), (3180, 428), (3330, 420), (3600, 424), (3850, 432),+ (4400, 432), (4750, 437), (4850, 434), (4930, 378), (5200, 385), (5600, 410),+ (6400, 415), (6800, 400), (7100, 390), (7250, 395), (7400, 412), (7700, 420),+ (8300, 428), (8800, 432), (9600, 432)]+ static let panRoll: FlowKeys = [(0, 0), (1700, 0), (1950, -6), (2050, -4), (2100, 10), (2250, 4), (2930, 0),+ (2980, -4), (3130, 0), (4600, 0), (4750, -9), (4850, -6), (4900, 14), (5100, 5),+ (5600, 0), (7100, -3), (7250, 0), (7300, -6), (7500, 0), (9600, 0)]+ }++ private func pose(_ t: Double) -> Pose {+ var p = Pose()+ // Breathing, a slow sway of the weight and a head that drifts: never a frozen frame.+ let s = flow(t, Act.sy)+ p.sy = s * (1 + 0.012 * wave(t, period: 2400))+ p.sx = 1 / pow(s, 0.6)+ p.lift = flow(t, Act.lift) + 1.2 * wave(t, period: 2400, phase: 0.25)+ p.lean = flow(t, Act.lean) + 1.2 * wave(t, period: 4800, phase: 0.2)+ p.shift = flow(t, Act.shift) + 1.5 * wave(t, period: 4800)+ p.yaw = flow(t, Act.yaw) + 0.05 * wave(t, period: 3200, phase: 0.1)+ // The head rights itself a beat after the body leans, on top of its own acting.+ p.head = flow(t, Act.head) - 0.45 * flow(t - 110, Act.lean) + 1.8 * wave(t, period: 4800, phase: 0.6)+ p.headDy = flow(t, Act.headDy)+ let brow = flow(t, Act.brow) + 0.08 * wave(t, period: 3200, phase: 0.4)+ p.browL = brow+ p.browR = brow + flow(t, Act.smug) + 0.06 * wave(t, period: 2400, phase: 0.1)+ p.widen = flow(t, Act.widen)+ p.mouthW = flow(t, Act.mouthW)+ // The free arm: key poses, a small sway that never stops, a forearm that trails the upper+ // arm, and a little bounce in the palm-up shrug.+ let a = flow(t, Act.armLa) + 3 * wave(t, period: 3200, phase: 0.5) + 2 * wave(t, period: 1920, phase: 0.8)+ let trail = 0.45 * (flow(t - 90, Act.armLa) - flow(t, Act.armLa))+ let shrug = 6 * wave(t, period: 600) * seg(t, 7450, 7600) * (1 - seg(t, 7950, 8100))+ p.armL = Arm(a: a, e: flow(t, Act.armLe) + 4 * wave(t, period: 2400, phase: 0.1) + trail + shrug)+ // The hat follows the head a beat late, squashes and wobbles when the pancake lands on it,+ // sags a little under its weight, leans as it slides off and springs back when it leaves.+ p.hatTilt = 0.5 * (flow(t - 120, Act.head) - flow(t, Act.head))+ let carrying = seg(t, beat.onHat, beat.onHat + 120) * (1 - seg(t, beat.drop, beat.drop + 100))+ var squash = 0.03 * carrying+ let d = (t - beat.onHat) / 1000+ if d > 0, d < 0.9 {+ squash += 0.14 * exp(-5 * d) * cos(2 * .pi * 3.2 * d)+ p.hatTilt += 6 * exp(-4 * d) * sin(2 * .pi * 2.4 * d)+ }+ p.hatTilt += 4 * seg(t, beat.slide, beat.drop) * (1 - seg(t, beat.drop, beat.drop + 150))+ let d2 = (t - beat.drop) / 1000+ if d2 > 0, d2 < 0.8 { p.hatTilt -= 4 * exp(-5 * d2) * sin(2 * .pi * 2.4 * d2) }+ p.hatSy = 1 - squash+ return p+ }++ // MARK: - Transforms (shared by drawing and by the prop's contact points)++ /// Stage from chef units: the body leans and squashes around the waist.+ private func bodyM(_ p: Pose) -> CGAffineTransform {+ CGAffineTransform.identity+ .translatedBy(x: anchor.x, y: anchor.y + build.waistY * chefScale - p.lift)+ .rotated(by: p.lean * .pi / 180)+ .scaledBy(x: chefScale * p.sx, y: chefScale * p.sy)+ .translatedBy(x: 0, y: -build.waistY)+ }++ /// Chef units from the head's frame: tilts where it meets the collar, bobs for the nod.+ private func headM(_ p: Pose) -> CGAffineTransform {+ CGAffineTransform.identity+ .translatedBy(x: p.shift, y: 56 + p.headDy)+ .rotated(by: p.head * .pi / 180)+ .translatedBy(x: 0, y: -56)+ }++ /// The head's frame from the hat's: it squashes and tilts on the band's bottom edge.+ private func hatM(_ p: Pose) -> CGAffineTransform {+ CGAffineTransform.identity+ .translatedBy(x: 0, y: -44)+ .rotated(by: p.hatTilt * .pi / 180)+ .scaledBy(x: 1 / pow(p.hatSy, 0.6), y: p.hatSy)+ .translatedBy(x: 0, y: 44)+ }++ private func panState(_ t: Double) -> (c: CGPoint, roll: Double) {+ // Shaking the pan at the start: quick little slides that the pancake trails.+ let shake = 4 * wave(t, period: 400) * seg(t, 250, 450) * (1 - seg(t, 1300, 1500))+ let x = flow(t, Act.panX) + shake + 1.2 * wave(t, period: 3200, phase: 0.3)+ let y = flow(t, Act.panY) + 1.0 * wave(t, period: 2400, phase: 0.5)+ return (CGPoint(x: x, y: y), flow(t, Act.panRoll) + 1.5 * wave(t, period: 1920, phase: 0.2))+ }++ private func rotate(_ v: CGPoint, _ deg: Double) -> CGPoint {+ let r = deg * .pi / 180+ return CGPoint(x: v.x * cos(r) - v.y * sin(r), y: v.x * sin(r) + v.y * cos(r))+ }++ // MARK: - The pancake: in the pan, in the air, on the hat++ private enum CakeMode { case pan, air, hat }+ private struct Cake {+ var p: CGPoint+ /// The flip around its horizontal axis (0: the pale side up, π: the brown side up).+ var theta: Double+ var rot: Double+ var squash: Double+ var mode: CakeMode+ }++ /// Where the pancake sits in the pan: it trails the pan when the pan moves (it slides).+ private func inPan(_ t: Double) -> (p: CGPoint, roll: Double) {+ let now = panState(t), before = panState(t - 90)+ let slide = max(-10, min(10, 0.6 * (before.c.x - now.c.x)))+ let o = rotate(CGPoint(x: slide, y: -3), now.roll)+ return (CGPoint(x: now.c.x + o.x, y: now.c.y + o.y), now.roll)+ }++ /// The top of the hat on stage, where the pancake sits, and the angle it sits at.+ private func onHat(_ t: Double, along: Double = 0) -> (p: CGPoint, rot: Double) {+ let p = pose(t)+ let m = hatM(p).concatenating(headM(p)).concatenating(bodyM(p))+ let pt = CGPoint(x: along, y: -178).applying(m)+ return (pt, p.hatTilt + p.head + p.lean)+ }++ private func cakeState(_ t: Double) -> Cake {+ // Squashes for a moment on each landing.+ var squash = 0.0+ for land in [beat.catch1, beat.onHat, beat.catch2] where t >= land && t < land + 140 {+ squash = 1 - seg(t, land, land + 140)+ }+ if t >= beat.toss1 && t < beat.catch1 {+ // A clean half flip: a throw from where the pan was to where the pan is, under gravity.+ let a = inPan(beat.toss1).p, b = inPan(beat.catch1).p+ let T = (beat.catch1 - beat.toss1) / 1000, s = (t - beat.toss1) / (beat.catch1 - beat.toss1)+ let h = gravity * T * T / 8+ let p = CGPoint(x: a.x + (b.x - a.x) * s, y: a.y + (b.y - a.y) * s - 4 * h * s * (1 - s))+ return Cake(p: p, theta: .pi * s, rot: 8 * sin(.pi * s), squash: 0, mode: .air)+ }+ if t >= beat.toss2 && t < beat.onHat {+ // Too much: a full flip, past his eyes, onto the top of the hat.+ let a = inPan(beat.toss2).p, b = onHat(beat.onHat).p+ let T = (beat.onHat - beat.toss2) / 1000, s = (t - beat.toss2) / (beat.onHat - beat.toss2)+ let h = gravity * T * T / 8+ let p = CGPoint(x: a.x + (b.x - a.x) * s, y: a.y + (b.y - a.y) * s - 4 * h * s * (1 - s))+ return Cake(p: p, theta: .pi + 2 * .pi * s, rot: -10 * sin(.pi * s), squash: 0, mode: .air)+ }+ if t >= beat.onHat && t < beat.drop {+ // On the hat: it rides the hat's wobble a moment late, then slides forward and right,+ // tipping toward us.+ let slide = seg(t, beat.slide, beat.drop, easeOut: false)+ let hat = onHat(t, along: 14 * slide)+ let lag = onHat(t - 80).rot - hat.rot+ return Cake(p: hat.p, theta: 3 * .pi + 0.5 * slide, rot: hat.rot + 0.6 * lag + 14 * slide, squash: squash,+ mode: .hat)+ }+ if t >= beat.drop && t < beat.catch2 {+ // Off the hat: it falls from rest (no upward arc), turning the last half over, into the+ // pan that came up to meet it.+ let a = onHat(beat.drop, along: 14).p, b = inPan(beat.catch2).p+ let s = (t - beat.drop) / (beat.catch2 - beat.drop)+ let p = CGPoint(x: a.x + (b.x - a.x) * s, y: a.y + (b.y - a.y) * s * s)+ return Cake(p: p, theta: 3 * .pi + 0.5 + (.pi - 0.5) * s, rot: 14 * (1 - s), squash: 0, mode: .air)+ }+ let rest = inPan(t)+ let flipped = t >= beat.catch1 && t < beat.toss2+ return Cake(p: rest.p, theta: flipped ? .pi : 0, rot: rest.roll, squash: squash, mode: .pan)+ }++ // MARK: - The kitchen: tiles, hanging utensils that sway, a shelf with a plant++ private func drawKitchen(_ ctx: GraphicsContext) {+ // A band of tiles behind the stove.+ for row in 0..<3 {+ for col in 0..<12 {+ let x = Double(col) * 40 + (row % 2 == 0 ? 0 : 20) + 120, y = 322 + Double(row) * 40+ ctx.fill(Path(roundedRect: CGRect(x: x, y: y, width: 36, height: 36), cornerRadius: 6), with: .color(Ink.tile))+ }+ }+ // A rail with three utensils on hooks. Each swings around its hook on its own period; the+ // long ones swing slower.+ ctx.fill(pill(cx: 236, cy: 70, w: 64, h: 10), with: .color(Ink.woodDark))+ let hooks: [(x: Double, period: Double, amp: Double)] = [(222, 4800, 4), (250, 2400, 6)]+ for (i, hk) in hooks.enumerated() {+ var u = ctx+ u.translateBy(x: hk.x, y: 72)+ u.rotate(by: .degrees(hk.amp * wave(t, period: hk.period, phase: seeded(i, salt: 3))))+ u.stroke(Path(ellipseIn: CGRect(x: -5, y: -4, width: 10, height: 12)), with: .color(Ink.steelDark), lineWidth: 3)+ switch i {+ case 0: // ladle+ u.fill(pill(cx: 0, cy: 52, w: 9, h: 96), with: .color(Ink.steel))+ u.fill(Path(ellipseIn: CGRect(x: -17, y: 92, width: 34, height: 26)), with: .color(Ink.steelDark))+ case 2: // whisk+ u.fill(pill(cx: 0, cy: 36, w: 10, h: 54), with: .color(Ink.wood))+ for w in [10.0, 18, 26] {+ u.stroke(Path(ellipseIn: CGRect(x: -w / 2, y: 58, width: w, height: 52)), with: .color(Ink.steel), lineWidth: 3)+ }+ default: // spatula+ u.fill(pill(cx: 0, cy: 40, w: 10, h: 64), with: .color(Ink.wood))+ u.fill(Path(roundedRect: CGRect(x: -15, y: 68, width: 30, height: 40), cornerRadius: 8), with: .color(Ink.woodDark))+ }+ }+ // A shelf with two jars and a plant whose leaves sway, each a beat after the last.+ ctx.fill(Path(roundedRect: CGRect(x: 452, y: 236, width: 100, height: 10), cornerRadius: 5), with: .color(Ink.woodDark))+ let jars: [(x: Double, h: Double, fill: Color, lid: Color)] = [(492, 62, Ink.flour, Ink.lidRed)]+ for j in jars {+ ctx.fill(Path(roundedRect: CGRect(x: j.x, y: 236 - j.h, width: 32, height: j.h), cornerRadius: 9), with: .color(Ink.jar))+ ctx.fill(Path(roundedRect: CGRect(x: j.x + 4, y: 236 - j.h * 0.6, width: 24, height: j.h * 0.6 - 4), cornerRadius: 6),+ with: .color(j.fill))+ ctx.fill(Path(roundedRect: CGRect(x: j.x - 2, y: 236 - j.h - 8, width: 36, height: 11), cornerRadius: 5), with: .color(j.lid))+ }+ var plant = ctx+ plant.translateBy(x: 470, y: 214)+ for (i, ang) in [-28.0, 0, 26].enumerated() {+ var l = plant+ l.rotate(by: .degrees(ang + 4 * wave(t, period: 3200, phase: 0.15 * Double(i))))+ l.fill(Path(ellipseIn: CGRect(x: -8, y: -42, width: 16, height: 40)), with: .color(Ink.leaf))+ }+ plant.fill(topRound(x: -14, y: -4, w: 28, h: 26, r: 6), with: .color(Ink.pot))+ }++ // MARK: - The chef (behind the counter)++ private func armPoints(_ limb: Arm, side: Double, shift: Double) -> (from: CGPoint, elbow: CGPoint, hand: CGPoint) {+ let a = limb.a * .pi / 180, f = (limb.a + limb.e) * .pi / 180+ let from = CGPoint(x: side * arm.x + shift, y: arm.y)+ let elbow = CGPoint(x: from.x + side * sin(a) * arm.upper, y: from.y + cos(a) * arm.upper)+ let hand = CGPoint(x: elbow.x + side * sin(f) * arm.lower, y: elbow.y + cos(f) * arm.lower)+ return (from, elbow, hand)+ }++ /// The pan arm: the hand goes where the handle is, and the elbow finds its own place (two+ /// segments, elbow out). The prop leads; the arm follows, so the grip never slips.+ private func panArm(_ p: Pose) -> Arm {+ let ps = panState(t)+ let grip = rotate(pan.grip, ps.roll)+ let target = CGPoint(x: ps.c.x + grip.x, y: ps.c.y + grip.y).applying(bodyM(p).inverted())+ let fx = arm.x + p.shift, fy = arm.y+ let dx = target.x - fx, dy = target.y - fy+ let l1 = arm.upper, l2 = arm.lower+ let dist = max(0.001, (dx * dx + dy * dy).squareRoot())+ let d = min(l1 + l2 - 0.5, max(abs(l1 - l2) + 0.5, dist))+ let tx = dx / dist * d, ty = dy / dist * d+ let phi = atan2(tx, ty)+ let alpha = acos(max(-1, min(1, (l1 * l1 + d * d - l2 * l2) / (2 * l1 * d))))+ let a = phi + alpha+ let f = atan2(tx - sin(a) * l1, ty - cos(a) * l1)+ return Arm(a: a * 180 / .pi, e: (f - a) * 180 / .pi)+ }++ private func drawChef(_ ctx: GraphicsContext, _ p: Pose) {+ var c = ctx+ c.concatenate(bodyM(p))+ func capsules(_ g: GraphicsContext, _ pts: [CGPoint], _ w: Double, _ color: Color) {+ var path = Path()+ path.addLines(pts)+ g.stroke(path, with: .color(color), style: StrokeStyle(lineWidth: w, lineCap: .round, lineJoin: .round))+ }+ // Arms behind the torso, so the shoulder never shows: long white sleeves, one round elbow.+ // The forearms and hands are drawn again in front of the counter.+ for (side, limb) in [(-1.0, p.armL), (1.0, panArm(p))] {+ let pts = armPoints(limb, side: side, shift: p.shift)+ capsules(c, [pts.from, pts.elbow, pts.hand], arm.sleeve, Ink.white)+ }+ var up = c+ up.translateBy(x: p.shift, y: 0)+ let yaw = p.yaw+ // The jacket: short, narrowing a little to the waist, double-breasted buttons and a pocket+ // with a pen that slide as he turns.+ let torso = roundedPolygon([CGPoint(x: -build.shoulders, y: build.torsoTop), CGPoint(x: build.shoulders, y: build.torsoTop),+ CGPoint(x: build.waist, y: build.torsoBottom), CGPoint(x: -build.waist, y: build.torsoBottom)], r: 18)+ up.fill(torso, with: .color(Ink.white))+ var chest = up+ chest.clip(to: torso)+ for bx in [-11.0, 11.0] {+ for by in [72.0, 88.0] {+ chest.fill(Path(ellipseIn: CGRect(x: bx + 6 * yaw - 3.5, y: by - 3.5, width: 7, height: 7)), with: .color(Ink.button))+ }+ }+ chest.fill(pill(cx: 27 + 7 * yaw, cy: 72, w: 4, h: 14), with: .color(Ink.pen))+ chest.fill(Path(roundedRect: CGRect(x: 18 + 7 * yaw, y: 74, width: 17, height: 14), cornerRadius: 4), with: .color(Ink.shade))+ // The neckerchief's knot peeks out under the chin.+ let kx = 5 * yaw+ up.fill(roundedPolygon([CGPoint(x: kx - 16, y: 52), CGPoint(x: kx + 16, y: 52), CGPoint(x: kx, y: 74)], r: 6), with: .color(Ink.scarf))+ up.fill(Path(ellipseIn: CGRect(x: kx - 7, y: 54, width: 14, height: 12)), with: .color(Ink.scarfDark))++ var h = c+ h.concatenate(headM(p))+ drawHead(h, p)+ }++ private func drawHead(_ h: GraphicsContext, _ p: Pose) {+ let yaw = p.yaw+ let look = gaze(t)+ // Looking up or down slides the face (2.5D pitch), a little behind the eyes.+ let dy = 5 * look.pitch+ for side in [-1.0, 1.0] {+ h.fill(Path(ellipseIn: CGRect(x: side * 50 - 9 * yaw - 11, y: -6 - 3 * look.pitch, width: 22, height: 22)), with: .color(Ink.skin))+ }+ h.fill(tomb(cx: 0, top: -66, w: 100, h: 122, r: 26), with: .color(Ink.skin))+ drawFace(h, p, look: look, dy: dy)+ // The hat: a tall puff and a band, white with one shade. The puff overlaps itself (no edge+ // lands on another), and the band hides the top of the head.+ var hat = h+ hat.concatenate(hatM(p))+ for lobe in [(x: -38.0, y: -102.0, r: 34.0), (x: 38, y: -104, r: 33), (x: 2, y: -132, r: 42)] {+ hat.fill(Path(ellipseIn: CGRect(x: lobe.x - lobe.r, y: lobe.y - lobe.r, width: lobe.r * 2, height: lobe.r * 2)),+ with: .color(Ink.white))+ }+ hat.fill(Path(CGRect(x: -52, y: -112, width: 104, height: 50)), with: .color(Ink.white))+ hat.fill(pill(cx: -20 + 3 * yaw, cy: -96, w: 7, h: 30), with: .color(Ink.shade))+ hat.fill(pill(cx: 18 + 3 * yaw, cy: -98, w: 7, h: 26), with: .color(Ink.shade))+ hat.fill(Path(roundedRect: CGRect(x: -54, y: -80, width: 108, height: 36), cornerRadius: 12), with: .color(Ink.shade))+ }++ /// Where the eyes look (pupil offset, eye units) and how far the face pitches, from where the+ /// pancake is a moment ago: the eyes track it, the face follows a little later.+ private func gaze(_ t: Double) -> (x: Double, y: Double, pitch: Double) {+ func aim(_ t: Double) -> (x: Double, y: Double) {+ let p = pose(t)+ let eye = CGPoint(x: 0, y: -6).applying(headM(p).concatenating(bodyM(p)))+ let c = cakeState(t).p+ return ((c.x - eye.x) / 22, (c.y - eye.y) / 26)+ }+ let att = flow(t, Act.attention)+ let now = aim(t - 50), late = aim(t - 140)+ let x = max(-6, min(6, now.x)) * att+ let y = max(-7, min(8, now.y)) * att+ let pitch = max(-1, min(1, late.y / 7)) * att+ return (x + 0.6 * wave(t, period: 3200, phase: 0.7) * (1 - att), y, pitch)+ }++ private func drawFace(_ h: GraphicsContext, _ p: Pose, look: (x: Double, y: Double, pitch: Double), dy: Double) {+ let yaw = p.yaw+ let dx = 10 * yaw+ let lid = track(t, Self.lid)+ let face = Choreo(period: Self.loopMs, lookX: [Key(0, look.x + 5 * yaw)], lookY: [Key(0, look.y)],+ lid: [Key(0, lid)], cheek: [Key(0, max(track(t, Self.cheek), 0.18 * lid))], mouth: Self.mouth)+ // Rosy cheeks, then the eyes: surprise opens them wider and shrinks the pupils.+ for side in [-1.0, 1.0] {+ h.fill(Path(ellipseIn: CGRect(x: side * 31 + dx - 9, y: 10 + dy, width: 18, height: 10)), with: .color(Ink.blush))+ }+ let w = 28 * (1 + 0.1 * p.widen), eh = 36 * (1 + 0.16 * p.widen)+ let pw = 12 * (1 - 0.12 * p.widen), ph = 18 * (1 - 0.12 * p.widen)+ for side in [-1.0, 1.0] {+ let cx = side * 19 + dx, cy = -6 + dy+ h.fill(pill(cx: cx, cy: cy, w: w, h: eh), with: .color(Ink.white))+ drawLiveEye(h, side: side,+ eye: EyeGeometry(cx: cx, cy: cy, w: w, h: eh, pupilW: pw, pupilH: ph, pupilTop: (eh - ph) / 2 - 1, shine: 2.6),+ ink: Self.ink, choreo: face, t: t)+ }+ // Brows: up in surprise, down and in to focus.+ for (side, b) in [(-1.0, p.browL), (1.0, p.browR)] {+ var g = h+ g.translateBy(x: side * 20 + dx, y: -33 + dy - 5 * max(0, b) + 2 * max(0, -b) - 0.12 * p.widen * 6)+ g.rotate(by: .degrees(-side * 16 * max(0, -b) + side * 4 * max(0, b)))+ g.fill(pill(cx: 0, cy: 0, w: 22, h: 8), with: .color(Ink.hair))+ }+ // The mouth opens under the mustache; the nose sticks out the most, so it slides the most.+ let depth = track(t, Self.mouth)+ drawLiveMouth(h, x0: -p.mouthW / 2 + dx, x1: p.mouthW / 2 + dx, top: 31 + dy, tilt: 3, mouth: Ink.mouth,+ tongue: Ink.tongue, teeth: Ink.white, choreo: face, t: t)+ let curl = max(track(t, Self.cheek), 0) - 0.5 * p.widen + 0.15 * wave(t, period: 1920, phase: 0.3)+ h.fill(mustache(cx: 12 * yaw, cy: 22 + dy - 0.12 * depth, curl: curl), with: .color(Ink.hair))+ h.fill(Path(ellipseIn: CGRect(x: 13 * yaw - 10, y: 1 + dy, width: 20, height: 18)), with: .color(Ink.feature))+ }++ /// A bushy mustache in one outline: two lobes from the middle, tips that curl up (curl > 0) or droop.+ private func mustache(cx: Double, cy: Double, curl: Double) -> Path {+ var p = Path()+ p.move(to: CGPoint(x: cx, y: cy - 6))+ for side in [-1.0, 1.0] {+ let tip = CGPoint(x: cx + side * 34, y: cy - 6 - 6 * curl)+ if side < 0 {+ p.addQuadCurve(to: CGPoint(x: cx - 24, y: cy - 5), control: CGPoint(x: cx - 12, y: cy - 12))+ p.addQuadCurve(to: tip, control: CGPoint(x: cx - 32, y: cy - 2))+ p.addQuadCurve(to: CGPoint(x: cx - 14, y: cy + 9), control: CGPoint(x: cx - 32, y: cy + 10))+ p.addQuadCurve(to: CGPoint(x: cx, y: cy + 4), control: CGPoint(x: cx - 6, y: cy + 9))+ } else {+ p.addQuadCurve(to: CGPoint(x: cx + 14, y: cy + 9), control: CGPoint(x: cx + 6, y: cy + 9))+ p.addQuadCurve(to: tip, control: CGPoint(x: cx + 32, y: cy + 10))+ p.addQuadCurve(to: CGPoint(x: cx + 24, y: cy - 5), control: CGPoint(x: cx + 32, y: cy - 2))+ p.addQuadCurve(to: CGPoint(x: cx, y: cy - 6), control: CGPoint(x: cx + 12, y: cy - 12))+ }+ }+ p.closeSubpath()+ return p+ }++ /// The forearms and hands again, in front of the counter and the pan: the right hand closes+ /// over the handle.+ private func drawHands(_ ctx: GraphicsContext, _ p: Pose) {+ var c = ctx+ c.concatenate(bodyM(p))+ for (side, limb) in [(-1.0, p.armL), (1.0, panArm(p))] {+ let pts = armPoints(limb, side: side, shift: p.shift)+ var fore = Path()+ fore.addLines([pts.elbow, pts.hand])+ c.stroke(fore, with: .color(Ink.white), style: StrokeStyle(lineWidth: arm.sleeve, lineCap: .round))+ // A cuff band at the wrist, then the round hand.+ let dx = pts.hand.x - pts.elbow.x, dy = pts.hand.y - pts.elbow.y, l = max(0.001, (dx * dx + dy * dy).squareRoot())+ var cuff = Path()+ cuff.addLines([CGPoint(x: pts.hand.x - dx / l * 13, y: pts.hand.y - dy / l * 13),+ CGPoint(x: pts.hand.x - dx / l * 7, y: pts.hand.y - dy / l * 7)])+ c.stroke(cuff, with: .color(Ink.shade), style: StrokeStyle(lineWidth: arm.sleeve, lineCap: .butt))+ c.fill(Path(ellipseIn: CGRect(x: pts.hand.x - arm.hand, y: pts.hand.y - arm.hand, width: arm.hand * 2, height: arm.hand * 2)),+ with: .color(Ink.skin))+ }+ }++ // MARK: - The stove: a butcher-block top, an oven with knobs, a burner with a live flame++ private func drawCounter(_ ctx: GraphicsContext) {+ let bottom = height - top + 10+ ctx.fill(Path(CGRect(x: 0, y: counter.back, width: Self.size.width, height: counter.front - counter.back)),+ with: .color(Ink.counterTop))+ ctx.fill(Path(CGRect(x: 0, y: counter.front, width: Self.size.width, height: counter.lip)), with: .color(Ink.counterLip))+ ctx.fill(Path(CGRect(x: 0, y: counter.front + counter.lip, width: Self.size.width, height: bottom - counter.front)),+ with: .color(Ink.stove))+ // Knobs; the burner's own knob turns up the gas for each flip.+ for (i, x) in [282.0, 330, 390, 438].enumerated() {+ ctx.fill(Path(ellipseIn: CGRect(x: x - 9, y: 492, width: 18, height: 18)), with: .color(Ink.knob))+ var k = ctx+ k.translateBy(x: x, y: 501)+ k.rotate(by: .degrees(Double(i) * 50 - 60 + (i == 1 ? 70 * flare(t) : 0)))+ k.fill(pill(cx: 0, cy: -4, w: 3.5, h: 8), with: .color(Ink.stoveDark))+ }+ // The oven door, its handle, and a warm glow in the window that breathes.+ ctx.fill(Path(roundedRect: CGRect(x: 220, y: 514, width: 280, height: 210), cornerRadius: 18), with: .color(Ink.stoveDark))+ ctx.fill(pill(cx: 360, cy: 524, w: 200, h: 8), with: .color(Ink.knob))+ let window = Path(roundedRect: CGRect(x: 250, y: 536, width: 220, height: 130), cornerRadius: 14)+ ctx.fill(window, with: .color(Ink.glass))+ var glow = ctx+ glow.clip(to: window)+ let b = 1 + 0.06 * wave(t, period: 2400, phase: 0.3) + 0.03 * wave(t, period: 1600)+ glow.fill(Path(ellipseIn: CGRect(x: 360 - 92 * b, y: 568 - 30 * b, width: 184 * b, height: 60 * b)), with: .color(Ink.glow))+ glow.fill(Path(ellipseIn: CGRect(x: 360 - 50 * b, y: 570 - 15 * b, width: 100 * b, height: 30 * b)), with: .color(Ink.glowCore))+ ctx.fill(Path(roundedRect: CGRect(x: 266, y: 544, width: 54, height: 9), cornerRadius: 4.5), with: .color(Ink.stove))+ }++ /// 0 to 1: the gas turned up for a flip, and back down after the landing.+ private func flare(_ t: Double) -> Double {+ seg(t, 1750, 2000) * (1 - seg(t, beat.catch1, beat.catch1 + 400))+ + seg(t, 4450, 4750) * (1 - seg(t, 5600, 6000))+ }++ private func drawBurner(_ ctx: GraphicsContext) {+ let b = burner+ ctx.fill(Path(ellipseIn: CGRect(x: b.x - b.w / 2, y: b.y - b.h / 2, width: b.w, height: b.h)), with: .color(Ink.ring))+ ctx.fill(Path(ellipseIn: CGRect(x: b.x - 34, y: b.y - 10, width: 68, height: 20)), with: .color(Ink.ringIn))+ // The flame: petals round the ring, each flickering on two waves of its own.+ for i in 0..<12 {+ let a = Double(i) / 12 * 2 * .pi+ let x = b.x + 40 * cos(a), y = b.y + 11 * sin(a)+ let flick = 1 + 0.18 * wave(t, period: 400, phase: seeded(i, salt: 1)) + 0.12 * wave(t, period: 640, phase: seeded(i, salt: 2))+ let hgt = 15 * flick * (1 + 0.45 * flare(t))+ ctx.fill(petal(x: x, y: y, w: 10, h: hgt), with: .color(Ink.flame))+ ctx.fill(petal(x: x, y: y, w: 5, h: hgt * 0.55), with: .color(Ink.flameCore))+ }+ }++ private func petal(x: Double, y: Double, w: Double, h: Double) -> Path {+ var p = Path()+ p.move(to: CGPoint(x: x, y: y - h))+ p.addQuadCurve(to: CGPoint(x: x + w / 2, y: y - 2), control: CGPoint(x: x + w / 2, y: y - h * 0.4))+ p.addQuadCurve(to: CGPoint(x: x - w / 2, y: y - 2), control: CGPoint(x: x, y: y + w * 0.5))+ p.addQuadCurve(to: CGPoint(x: x, y: y - h), control: CGPoint(x: x - w / 2, y: y - h * 0.4))+ p.closeSubpath()+ return p+ }++ // MARK: - The pan and the pancake++ private func drawPan(_ ctx: GraphicsContext) {+ let ps = panState(t)+ var g = ctx+ g.translateBy(x: ps.c.x, y: ps.c.y)+ g.rotate(by: .degrees(ps.roll))+ let r = pan.r, rh = r * pan.ratio+ // The handle, its wooden grip under the hand.+ var handle = Path()+ handle.addLines([CGPoint(x: r - 6, y: 0), CGPoint(x: pan.grip.x - 30, y: pan.grip.y)])+ g.stroke(handle, with: .color(Ink.pan), style: StrokeStyle(lineWidth: pan.handleW, lineCap: .round))+ var wood = Path()+ wood.addLines([CGPoint(x: pan.grip.x - 32, y: pan.grip.y), CGPoint(x: pan.grip.x + 8, y: pan.grip.y)])+ g.stroke(wood, with: .color(Ink.grip), style: StrokeStyle(lineWidth: pan.handleW + 3, lineCap: .round))+ // The wall below the rim, the rim, the cooking surface.+ g.fill(Path(ellipseIn: CGRect(x: -r, y: pan.wall - rh, width: r * 2, height: rh * 2)), with: .color(Ink.panSide))+ g.fill(Path(CGRect(x: -r, y: 0, width: r * 2, height: pan.wall)), with: .color(Ink.panSide))+ g.fill(Path(ellipseIn: CGRect(x: -r, y: -rh, width: r * 2, height: rh * 2)), with: .color(Ink.pan))+ g.fill(Path(ellipseIn: CGRect(x: -r + 6, y: -rh + 4, width: r * 2 - 12, height: rh * 2 - 6)), with: .color(Ink.panIn))+ }++ private func drawCake(_ ctx: GraphicsContext, _ c: Cake) {+ // A flat disc seen from a little above shows sin(tilt) of its height; flipping it about its+ // horizontal axis turns that into sin(tilt + theta): it thins to its edge, then shows the other side.+ let ratio = sin(cake.tilt + c.theta)+ let paleUp = ratio > 0+ let r = cake.r, rh = max(1.5, r * abs(ratio))+ var g = ctx+ g.translateBy(x: c.p.x, y: c.p.y)+ g.rotate(by: .degrees(c.rot))+ g.scaleBy(x: 1 + 0.14 * c.squash, y: 1 - 0.28 * c.squash)+ g.fill(Path(ellipseIn: CGRect(x: -r, y: -rh + cake.thick * 0.5, width: r * 2, height: rh * 2 + cake.thick * 0.5)),+ with: .color(Ink.cakeEdge))+ let face = Path(ellipseIn: CGRect(x: -r, y: -rh - cake.thick * 0.3, width: r * 2, height: rh * 2))+ g.fill(face, with: .color(paleUp ? Ink.cakeA : Ink.cakeB))+ var surface = g+ surface.translateBy(x: 0, y: -cake.thick * 0.3)+ surface.scaleBy(x: 1, y: abs(ratio) / sin(cake.tilt))+ if paleUp {+ for i in 0..<5 {+ let bx = (seeded(i, salt: 6) - 0.5) * 48, by = (seeded(i, salt: 7) - 0.5) * 16+ let br = 2.5 + seeded(i, salt: 8) * 2+ surface.fill(Path(ellipseIn: CGRect(x: bx - br, y: by - br * 0.6, width: br * 2, height: br * 1.2)), with: .color(Ink.bubble))+ }+ } else {+ surface.fill(Path(ellipseIn: CGRect(x: -26, y: -8, width: 52, height: 16)), with: .color(Ink.cakeBIn))+ }+ }++ // MARK: - Steam: wisps that rise from the pancake in the pan, and puffs on each landing++ private func drawSteam(_ ctx: GraphicsContext) {+ // Wisps leave from the pancake's edges and curl outward as they rise, so they frame the face+ // instead of crossing it. A cool off-white, so they show on the white jacket too.+ let period = 1920.0+ for i in 0..<5 {+ let p = loop(t, period: period, phase: seeded(i, salt: 11))+ let life = 0.75+ guard p < life else { continue }+ let q = p / life+ var born = t - p * period+ if born < 0 { born += Self.loopMs }+ let state = cakeState(born)+ guard state.mode == .pan else { continue }+ let side = i % 2 == 0 ? -1.0 : 1.0+ let x0 = state.p.x + side * (14 + seeded(i, salt: 12) * 22)+ let rise = easeOut.value(at: q)+ let y = state.p.y - 8 - 46 * rise+ var wisp = Path()+ for k in 0...6 {+ let s = Double(k) / 6+ let pt = CGPoint(x: x0 + side * 22 * rise * (0.6 + 0.4 * s) + 4 * sin(s * 3.4 + q * 4 + Double(i)),+ y: y - 20 * s)+ if k == 0 { wisp.move(to: pt) } else { wisp.addLine(to: pt) }+ }+ var g = ctx+ g.opacity = bell(q)+ g.stroke(wisp, with: .color(Ink.steam), style: StrokeStyle(lineWidth: 6.5 - 2.5 * q, lineCap: .round, lineJoin: .round))+ }+ // A puff on each landing: a few small clouds that swell, rise and fade.+ for land in [beat.catch1, beat.onHat, beat.catch2] {+ let q = (t - land) / 480+ guard q > 0, q < 1 else { continue }+ let at = cakeState(land + 1).p+ var g = ctx+ g.opacity = 0.9 * bell(q)+ // Out to the sides, at the pancake's level, never up over the face.+ for k in 0..<6 {+ let side = k % 2 == 0 ? -1.0 : 1.0+ let d = (40 + 34 * easeOut.value(at: q)) * (0.75 + 0.5 * seeded(k, salt: 10))+ let rr = (3 + 5 * easeOut.value(at: q)) * (0.8 + 0.4 * seeded(k, salt: 13))+ let cx = at.x + side * d, cy = at.y - 4 - 10 * seeded(k, salt: 9) - 6 * q+ g.fill(Path(ellipseIn: CGRect(x: cx - rr, y: cy - rr, width: rr * 2, height: rr * 2)), with: .color(Ink.steam))+ }+ }+ }++ // MARK: - Shapes++ /// A tombstone: a half-circle top, straight sides, bottom corners of radius r.+ private func tomb(cx: Double, top: Double, w: Double, h: Double, r: Double) -> Path {+ let R = w / 2, yb = top + h+ let q = min(r, R, h - R)+ var p = Path()+ p.move(to: CGPoint(x: cx - R, y: top + R))+ p.addArc(center: CGPoint(x: cx, y: top + R), radius: R, startAngle: .degrees(180), endAngle: .degrees(0), clockwise: false)+ p.addLine(to: CGPoint(x: cx + R, y: yb - q))+ p.addQuadCurve(to: CGPoint(x: cx + R - q, y: yb), control: CGPoint(x: cx + R, y: yb))+ p.addLine(to: CGPoint(x: cx - R + q, y: yb))+ p.addQuadCurve(to: CGPoint(x: cx - R, y: yb - q), control: CGPoint(x: cx - R, y: yb))+ p.closeSubpath()+ return p+ }++ /// A rectangle with only its top corners rounded.+ private func topRound(x: Double, y: Double, w: Double, h: Double, r: Double) -> Path {+ let q = min(r, w / 2, h)+ var p = Path()+ p.move(to: CGPoint(x: x, y: y + h))+ p.addLine(to: CGPoint(x: x, y: y + q))+ p.addQuadCurve(to: CGPoint(x: x + q, y: y), control: CGPoint(x: x, y: y))+ p.addLine(to: CGPoint(x: x + w - q, y: y))+ p.addQuadCurve(to: CGPoint(x: x + w, y: y + q), control: CGPoint(x: x + w, y: y))+ p.addLine(to: CGPoint(x: x + w, y: y + h))+ p.closeSubpath()+ return p+ }++ /// A polygon with every corner rounded (radius r).+ private func roundedPolygon(_ pts: [CGPoint], r: Double) -> Path {+ let n = pts.count+ var p = Path()+ for i in 0..<n {+ let pt = pts[i], prev = pts[(i + n - 1) % n], next = pts[(i + 1) % n]+ func toward(_ q: CGPoint) -> CGPoint {+ let dx = q.x - pt.x, dy = q.y - pt.y, l = max(0.001, (dx * dx + dy * dy).squareRoot())+ return CGPoint(x: pt.x + dx / l * r, y: pt.y + dy / l * r)+ }+ let s = toward(prev), e = toward(next)+ if i == 0 { p.move(to: s) } else { p.addLine(to: s) }+ p.addQuadCurve(to: e, control: pt)+ }+ p.closeSubpath()+ return p+ }+}diff --git a/demo/DemoScene.swift b/demo/DemoScene.swiftdeleted file mode 100644index 8c2c4b1..0000000--- a/demo/DemoScene.swift+++ /dev/null@@ -1,229 +0,0 @@-// DemoScene.swift: the demo in the README, drawn with the SwiftUI templates.-//-// One clock (`t`, in ms) drives everything: a creature with live eyes that-// breathes and lets its ears follow through, snow in the window, steam from a-// mug, a candle flame, and a plant swaying. Every period divides 8000 ms, so the-// 8-second loop closes without a jump.-import SwiftUI--struct DemoScene: View {- /// The clock, in ms.- let t: Double-- static let size = CGSize(width: 800, height: 450)- static let loopMs = 8000.0-- var body: some View {- Canvas { ctx, _ in- drawRoom(ctx)- drawWindow(ctx)- drawTable(ctx)- drawMug(ctx)- drawCandle(ctx)- drawPlant(ctx)- drawCreature(ctx)- }- .frame(width: Self.size.width, height: Self.size.height)- }-- // MARK: - Palette-- private enum Ink {- static let wall = Color(hex: 0x221E38)- static let floor = Color(hex: 0x2D2848)- static let frame = Color(hex: 0x3B3560)- static let sky = Color(hex: 0x141836)- static let moon = Color(hex: 0xF2EBD3)- static let table = Color(hex: 0x5A4B82)- static let tableEdge = Color(hex: 0x4A3D6E)- static let mug = Color(hex: 0xE8DDF5)- static let steam = Color(hex: 0xCFC6E8)- static let candle = Color(hex: 0xF3E9D2)- static let flame = Color(hex: 0xFFB547)- static let flameCore = Color(hex: 0xFFF1B8)- static let pot = Color(hex: 0xC9734F)- static let leaf = Color(hex: 0x6FCF97)- static let leafDark = Color(hex: 0x4CAF7D)- static let body = Color(hex: 0x6C8CFF)- static let belly = Color(hex: 0x9EB3FF)- static let earInner = Color(hex: 0xFF9EB5)- static let line = Color(hex: 0x2B2F5C)- static let cheek = Color(hex: 0xFF8FB1)- }-- // MARK: - Room-- private func drawRoom(_ ctx: GraphicsContext) {- ctx.fill(Path(CGRect(origin: .zero, size: Self.size)), with: .color(Ink.wall))- ctx.fill(Path(CGRect(x: 0, y: 390, width: 800, height: 60)), with: .color(Ink.floor))- }-- // MARK: - Window with falling snow-- private let win = CGRect(x: 40, y: 50, width: 210, height: 200)-- private func drawWindow(_ ctx: GraphicsContext) {- ctx.fill(Path(roundedRect: win.insetBy(dx: -10, dy: -10), cornerRadius: 14), with: .color(Ink.frame))- ctx.drawLayer { c in- c.clip(to: Path(roundedRect: win, cornerRadius: 6))- c.fill(Path(win), with: .color(Ink.sky))- c.fill(Path(ellipseIn: CGRect(x: win.maxX - 62, y: win.minY + 18, width: 34, height: 34)), with: .color(Ink.moon))- // Snow: each flake its own seeded phase, size and drift; 1× or 2× speed so the loop closes.- for i in 0..<38 {- let speed = seeded(i) < 0.5 ? 1.0 : 2.0- let p = loop(t, period: Self.loopMs / speed, phase: seeded(i, salt: 1))- let x = win.minX + seeded(i, salt: 2) * win.width + 5 * wave(t, period: 2000 * speed, phase: seeded(i, salt: 3))- let y = win.minY - 6 + p * (win.height + 12)- let r = 1.2 + seeded(i, salt: 4) * 1.8- c.fill(Path(ellipseIn: CGRect(x: x - r, y: y - r, width: r * 2, height: r * 2)), with: .color(.white.opacity(0.9)))- }- }- // The window cross.- ctx.fill(Path(CGRect(x: win.midX - 4, y: win.minY, width: 8, height: win.height)), with: .color(Ink.frame))- ctx.fill(Path(CGRect(x: win.minX, y: win.midY - 4, width: win.width, height: 8)), with: .color(Ink.frame))- }-- // MARK: - Table, mug and steam, candle-- private func drawTable(_ ctx: GraphicsContext) {- ctx.fill(Path(CGRect(x: 490, y: 300, width: 14, height: 90)), with: .color(Ink.tableEdge))- ctx.fill(Path(CGRect(x: 746, y: 300, width: 14, height: 90)), with: .color(Ink.tableEdge))- ctx.fill(Path(roundedRect: CGRect(x: 470, y: 288, width: 310, height: 16), cornerRadius: 6), with: .color(Ink.table))- }-- private func drawMug(_ ctx: GraphicsContext) {- // Steam: three wisps rising out of phase, fading in and out (opacity only to appear and disappear).- for i in 0..<3 {- let p = loop(t, period: 2000, phase: Double(i) / 3)- let x = 512 + Double(i) * 11- let y0 = 236 - p * 18- var wisp = Path()- wisp.move(to: CGPoint(x: x, y: y0 + 16))- wisp.addCurve(to: CGPoint(x: x, y: y0), control1: CGPoint(x: x - 6, y: y0 + 10), control2: CGPoint(x: x + 6, y: y0 + 6))- ctx.stroke(wisp, with: .color(Ink.steam.opacity(0.85 * bell(p))), style: StrokeStyle(lineWidth: 3, lineCap: .round))- }- ctx.fill(Path(roundedRect: CGRect(x: 496, y: 250, width: 54, height: 40), cornerRadius: 8), with: .color(Ink.mug))- var handle = Path()- handle.addArc(center: CGPoint(x: 552, y: 268), radius: 10, startAngle: .degrees(-80), endAngle: .degrees(80), clockwise: false)- ctx.stroke(handle, with: .color(Ink.mug), lineWidth: 6)- }-- private func drawCandle(_ ctx: GraphicsContext) {- let base = CGPoint(x: 616, y: 240)- // The glow grows a few percent, slowly (light, not dimming).- let glow = 46 * (1 + 0.04 * wave(t, period: 2000))- ctx.fill(Path(ellipseIn: CGRect(x: base.x - glow, y: base.y - 14 - glow, width: glow * 2, height: glow * 2)),- with: .radialGradient(Gradient(colors: [Ink.flame.opacity(0.28), Ink.flame.opacity(0)]),- center: CGPoint(x: base.x, y: base.y - 14), startRadius: 0, endRadius: glow))- ctx.fill(Path(roundedRect: CGRect(x: 604, y: 240, width: 24, height: 50), cornerRadius: 4), with: .color(Ink.candle))- // Flicker: a lean and a stretch on two waves of different periods; the core on a third.- let lean = 3 * wave(t, period: 800) + 1.5 * wave(t, period: 1600, phase: 0.3)- let stretch = 1 + 0.05 * wave(t, period: 1000, phase: 0.2)- var flame = ctx- flame.translateBy(x: base.x, y: base.y)- flame.rotate(by: .degrees(lean))- flame.scaleBy(x: 1, y: stretch)- flame.fill(teardrop(width: 16, height: 30), with: .color(Ink.flame))- var core = flame- core.rotate(by: .degrees(1.5 * wave(t, period: 1333.3333, phase: 0.6)))- core.fill(teardrop(width: 8, height: 16), with: .color(Ink.flameCore))- }-- /// A flame shape standing on (0, 0), pointing up.- private func teardrop(width w: CGFloat, height h: CGFloat) -> Path {- var p = Path()- p.move(to: CGPoint(x: 0, y: -h))- p.addCurve(to: CGPoint(x: 0, y: 0), control1: CGPoint(x: w * 0.9, y: -h * 0.45), control2: CGPoint(x: w * 0.55, y: 0))- p.addCurve(to: CGPoint(x: 0, y: -h), control1: CGPoint(x: -w * 0.55, y: 0), control2: CGPoint(x: -w * 0.9, y: -h * 0.45))- return p- }-- // MARK: - Plant: sway around the base, tips later and further-- private func drawPlant(_ ctx: GraphicsContext) {- let base = CGPoint(x: 712, y: 246)- let stems: [(angle: Double, length: Double)] = [(-28, 62), (-8, 84), (12, 74), (32, 56)]- for (i, stem) in stems.enumerated() {- let sway = (2.2 + Double(i) * 0.5) * wave(t, period: 4000, phase: 0.06 * Double(i))- var c = ctx- c.translateBy(x: base.x, y: base.y)- c.rotate(by: .degrees(stem.angle + sway))- var s = Path()- s.move(to: .zero)- s.addQuadCurve(to: CGPoint(x: 0, y: -stem.length), control: CGPoint(x: 6, y: -stem.length / 2))- c.stroke(s, with: .color(Ink.leafDark), style: StrokeStyle(lineWidth: 4, lineCap: .round))- // The leaf at the tip follows through: a little more sway, a little later.- var leaf = c- leaf.translateBy(x: 0, y: -stem.length)- leaf.rotate(by: .degrees(2.5 * wave(t, period: 4000, phase: 0.06 * Double(i) + 0.12)))- leaf.fill(Path(ellipseIn: CGRect(x: -11, y: -26, width: 22, height: 30)), with: .color(Ink.leaf))- }- var pot = Path()- pot.move(to: CGPoint(x: 686, y: 244))- pot.addLine(to: CGPoint(x: 738, y: 244))- pot.addLine(to: CGPoint(x: 730, y: 288))- pot.addLine(to: CGPoint(x: 694, y: 288))- pot.closeSubpath()- ctx.fill(pot, with: .color(Ink.pot))- }-- // MARK: - The creature: breathing, ears following through, live eyes-- /// Looks at the window, blinks, looks at the mug, smiles with its eyes, blinks twice.- private static let choreo = Choreo(- period: 8000,- lookX: [Key(0, 0), Key(1200, 0), Key(1700, -7), Key(2900, -7), Key(3400, 0), Key(4300, 0), Key(4800, 7), Key(5900, 7), Key(6400, 0), Key(8000, 0)],- lookY: [Key(0, 2), Key(1200, 2), Key(1700, -5), Key(2900, -5), Key(3400, 2), Key(4300, 2), Key(4800, 5), Key(5900, 5), Key(6400, 2), Key(8000, 2)],- lid: [Key(0, 0), Key(3700, 0), Key(3790, 1), Key(3840, 1), Key(3990, 0, out: true),- Key(7300, 0), Key(7390, 1), Key(7540, 0, out: true), Key(7640, 0), Key(7730, 1), Key(7880, 0, out: true), Key(8000, 0)],- cheek: [Key(0, 0), Key(6400, 0), Key(6700, 1, out: true), Key(7000, 1), Key(7200, 0), Key(8000, 0)]- )-- private func drawCreature(_ ctx: GraphicsContext) {- let breath = -2.5 * (wave(t, period: 4000, phase: -0.25) + 1) / 2- // The shadow stays on the floor.- ctx.fill(Path(ellipseIn: CGRect(x: 225, y: 384, width: 150, height: 14)), with: .color(.black.opacity(0.25)))-- var body = ctx- body.translateBy(x: 0, y: breath)- // Ears: rotate around their roots, a touch later than the breath (follow-through).- for side in [-1.0, 1.0] {- var ear = body- ear.translateBy(x: 300 + side * 46, y: 214)- ear.rotate(by: .degrees(side * 10 + 5 * wave(t, period: 4000, phase: -0.1 + side * 0.05)))- ear.fill(roundedTriangle(w: 40, h: 44), with: .color(Ink.body))- ear.fill(roundedTriangle(w: 20, h: 24), with: .color(Ink.earInner))- }- body.fill(Path(roundedRect: CGRect(x: 215, y: 200, width: 170, height: 192), cornerRadius: 80), with: .color(Ink.body))- body.fill(Path(ellipseIn: CGRect(x: 250, y: 300, width: 100, height: 80)), with: .color(Ink.belly))-- // The head's own coordinates: (0, 0) between the eyes.- var head = body- head.translateBy(x: 300, y: 262)- let whiteL = EyeGeometry(cx: -32, cy: 0, w: 34, h: 44, pupilW: 15, pupilH: 22, pupilTop: 9, shine: 3)- let whiteR = EyeGeometry(cx: 32, cy: 0, w: 34, h: 44, pupilW: 15, pupilH: 22, pupilTop: 9, shine: 3)- for eye in [whiteL, whiteR] {- head.fill(pill(cx: eye.cx, cy: eye.cy, w: eye.w, h: eye.h), with: .color(.white))- }- let ink = FaceInk(white: .white, pupil: Color(hex: 0x1B1B2E), skin: Ink.body, line: Ink.line, lineWidth: 4)- drawLiveEye(head, side: -1, eye: whiteL, ink: ink, choreo: Self.choreo, t: t)- drawLiveEye(head, side: 1, eye: whiteR, ink: ink, choreo: Self.choreo, t: t)- for side in [-1.0, 1.0] {- head.fill(Path(ellipseIn: CGRect(x: side * 52 - 11, y: 30, width: 22, height: 12)), with: .color(Ink.cheek.opacity(0.7)))- }- var mouth = Path()- mouth.move(to: CGPoint(x: -14, y: 38))- mouth.addQuadCurve(to: CGPoint(x: 14, y: 38), control: CGPoint(x: 0, y: 50))- head.stroke(mouth, with: .color(Ink.line), style: StrokeStyle(lineWidth: 4, lineCap: .round))- }-- /// A soft triangle standing on (0, 0), pointing up.- private func roundedTriangle(w: CGFloat, h: CGFloat) -> Path {- var p = Path()- p.move(to: CGPoint(x: -w / 2, y: 6))- p.addQuadCurve(to: CGPoint(x: 0, y: -h), control: CGPoint(x: -w * 0.35, y: -h * 0.6))- p.addQuadCurve(to: CGPoint(x: w / 2, y: 6), control: CGPoint(x: w * 0.35, y: -h * 0.6))- p.closeSubpath()- return p- }-}diff --git a/demo/GardenScene.swift b/demo/GardenScene.swiftnew file mode 100644index 0000000..03f93c5--- /dev/null+++ b/demo/GardenScene.swift@@ -0,0 +1,886 @@+// GardenScene.swift: a small garden, and a second demo of SKILL.md section 5 (acting).+// A girl waters a sprout. It grows in steps that flow into each other: the stem stretches, the+// leaves unfurl one after another, a bud swells and the flower opens petal by petal. A butterfly+// wanders in, her eyes follow it, it lands on the flower, and she grins and hops. When it flies+// off, the petals drift down one by one and the plant settles back into the sprout. One clock, a+// 10-second loop. Every acting channel is a `flow` of key poses plus idle life plus+// follow-through, so nothing ever freezes. Render it at 60 fps.+//+// The girl is drawn in the same flat style as the kid in RevealScene: a 100-wide tombstone head+// centred on (0, 0) with y growing down, pill eyes with no brows, capsule arms whose shoulders+// hide behind the torso, circle hands, flat colours with at most one shade.+import SwiftUI++struct GardenScene: View {+ let t: Double+ /// Taller than the scene (a phone screen): the stage stays put, the sky and the meadow fill the rest.+ var height = 720.0+ /// Where the 720-tall stage starts inside that height; centred when nil.+ var stageTop: Double? = nil+ private var top: Double { stageTop ?? (height - Self.size.height) / 2 }+ static let size = CGSize(width: 720, height: 720)+ static let loopMs = 10000.0++ private enum Ink {+ static let skyTop = Color(hex: 0xA9E4FF)+ static let skyLow = Color(hex: 0xE8F8FF)+ static let cloud = Color.white+ static let hillFar = Color(hex: 0xD6F0BF)+ static let hillNear = Color(hex: 0xC9EBAE)+ static let ground = Color(hex: 0xE3F6D1)+ static let shadow = Color(hex: 0xCDEAB4)+ static let grass = Color(hex: 0x9CD481)+ static let skin = Color(hex: 0xD9966E)+ static let feature = Color(hex: 0xA65E3E)+ static let freckle = Color(hex: 0xBF7853)+ static let hair = Color(hex: 0x2B2433)+ static let shirt = Color(hex: 0xFF7A6B)+ static let stripe = Color(hex: 0xFFD2CB)+ static let denim = Color(hex: 0x4A90E2)+ static let denimShade = Color(hex: 0x3A78C2)+ static let button = Color(hex: 0xFFD84D)+ static let boot = Color(hex: 0x37AD74)+ static let bootRim = Color(hex: 0x5CC892)+ static let sole = Color(hex: 0x23704B)+ static let hat = Color(hex: 0xFFC6D3)+ static let brim = Color(hex: 0xF7AFC2)+ static let band = Color(hex: 0xFF7A6B)+ static let daisyEye = Color(hex: 0xFFD84D)+ static let can = Color(hex: 0x3BB8C7)+ static let canShade = Color(hex: 0x2B9AA9)+ static let pot = Color(hex: 0xE57F5E)+ static let potShade = Color(hex: 0xCC6849)+ static let potRim = Color(hex: 0xF29676)+ static let soilDry: UInt32 = 0x8A6552+ static let soilWet: UInt32 = 0x573C30+ static let stem = Color(hex: 0x5CB862)+ static let leaf = Color(hex: 0x6CCB72)+ static let vein = Color(hex: 0x4EA756)+ static let petal = Color(hex: 0xC9A3FF)+ static let petalShade = Color(hex: 0xB283F7)+ static let center = Color(hex: 0xFFCF4A)+ static let sparkle = Color(hex: 0xFFD84D)+ static let wing = Color(hex: 0xFFA24C)+ static let wingEdge = Color(hex: 0x3B3452)+ static let heart = Color(hex: 0xFF7A9A)+ static let water = Color(hex: 0x6EC3FF)+ static let white = Color.white+ static let pupil = Color(hex: 0x3C3C3C)+ static let mouth = Color(hex: 0xB03A3A)+ static let tongue = Color(hex: 0xF15A5A)+ }++ // MARK: - Tokens++ /// Where the girl's feet stand, and girl units (head = 100 wide) to stage points.+ private let feet = CGPoint(x: 285, y: 620)+ private let girlScale = 1.32+ /// The same toy-like build as the kid, seen from a little above.+ private let build = (torsoTop: 50.0, torsoBottom: 112.0, shoulders: 42.0, waist: 38.0, hip: 22.0, hipY: 132.0,+ shoeBottom: 62.0, shadow: 140.0, shadowRatio: 0.32, shadowUp: 7.0)+ private var floorY: Double { build.hipY + build.shoeBottom }+ private let arm = (x: 34.0, y: 66.0, w: 26.0, hand: 13.0, upper: 30.0, fore: 28.0, reach: 12.0)+ /// The happy hop's height (stage units).+ private let jump = 26.0+ /// The beats of the loop, in ms.+ private let beat = (pour: 1900.0, pourEnd: 2820.0, bud: 4850.0, bloom: 5300.0, enter: 5700.0, land: 6900.0,+ hop: 7150.0, hopEnd: 7490.0, takeoff: 7900.0, gone: 9100.0, wilt: 9000.0)+ /// The pot: centre, bottom, the rim line, the body's widths at the top and the bottom, and the lip.+ private let pot = (x: 470.0, base: 636.0, rim: 546.0, top: 106.0, bottom: 80.0, lip: 122.0)+ private var soilY: Double { pot.rim + 2 }+ /// The plant: the sprout's height and the full stem's, where the true leaves grow (stem+ /// units from the soil), when each one unfurls and when it folds away again (top one first).+ private let plant = (sprout: 18.0, full: 176.0, leaves: [52.0, 92.0, 128.0],+ unfurl: [3550.0, 4200.0, 4500.0], fold: [9330.0, 9175.0, 9030.0])+ /// The watering can in its own frame, where the hand grips the top of the handle at (0, 0):+ /// the spout runs from the body's lower front up and out to the rose.+ private let can = (spoutFrom: CGPoint(x: 20, y: 40), spoutTip: CGPoint(x: 58, y: 4))+ /// Water: drops leave the rose every `every` ms, at `speed` units a second, under `g`.+ private let water = (every: 45.0, speed: 70.0, g: 1300.0)++ private static let ink = FaceInk(white: .white, pupil: Ink.pupil, skin: Ink.skin, line: Ink.feature, lineWidth: 5)+ /// Blinks land on the head turns.+ private static let lid = blinks([900, 3500, 6150, 8950])+ private static let cheek: Keys = [Key(0, 0), Key(6950, 0), Key(7050, 1, out: true), Key(7700, 1), Key(7880, 0), Key(loopMs, 0)]+ /// The open mouth: a small smile at rest, pursed while pouring, a "wow" at the bloom, the grin.+ private static let mouth: Keys = [+ Key(0, 11), Key(1500, 11), Key(1800, 8), Key(2850, 8), Key(3200, 12), Key(5200, 12), Key(5400, 24, out: true),+ Key(5900, 20), Key(6200, 13), Key(6950, 11), Key(7050, 22, out: true), Key(7700, 22), Key(7900, 14), Key(9600, 11),+ Key(loopMs, 11),+ ]++ private static func blinks(_ at: [Double]) -> Keys {+ var keys = [Key(0, 0)]+ // Close in 110 ms, hold 40, reopen in 190 with ease-out.+ for b in at { keys += [Key(b, 0), Key(b + 110, 1), Key(b + 150, 1), Key(b + 340, 0, out: true)] }+ return keys + [Key(loopMs, 0)]+ }++ var body: some View {+ Canvas { ctx, _ in+ drawLand(ctx)+ var stage = ctx+ stage.translateBy(x: 0, y: top)+ drawPot(stage)+ drawPlant(stage)+ drawGirlOnStage(stage)+ drawWater(stage)+ drawSparkles(stage)+ drawFallingPetals(stage)+ drawButterfly(stage)+ drawHearts(stage)+ }+ .frame(width: Self.size.width, height: height)+ }++ // MARK: - Acting: smooth channels through key poses, plus idle life and follow-through++ /// One arm: `a` the upper arm's outward angle (0 hangs down), `e` the elbow bend.+ private struct Arm { var a = 12.0, e = 0.0 }++ private struct Pose {+ var lift = 0.0, sx = 1.0, sy = 1.0, lean = 0.0+ var shift = 0.0, yaw = 0.0, head = 0.0+ var armL = Arm(), armR = Arm()+ var legL = 3.0, legR = 3.0+ /// The can's tilt in degrees; positive pours.+ var tilt = 0.0+ /// Follow-through for the pigtails and the hat: how far the head turned in the last moment+ /// (degrees), and how far it dropped (stage units).+ var hairSwing = 0.0, hairUp = 0.0+ }++ /// The key poses: a glance at the sprout, the pour, watching it grow, "wow" at the bloom,+ /// following the butterfly in, the grin and the hop, waving it goodbye, the petals falling.+ private enum Act {+ static let sy: FlowKeys = [(0, 1), (1350, 1), (1500, 0.97), (1650, 1.02), (1800, 1), (7000, 1), (7100, 0.9),+ (7180, 1.07), (7420, 1.02), (7500, 0.88), (7600, 1.03), (7730, 1), (10000, 1)]+ static let lean: FlowKeys = [(0, 0), (1250, 0), (1500, -3), (1850, 4), (2850, 4), (3300, 0), (4300, -1), (5300, -3),+ (5900, -1), (6400, 2), (6900, 1), (7100, 0), (7650, 0), (8300, -3), (9000, -2), (9600, 0),+ (10000, 0)]+ static let shift: FlowKeys = [(0, 0), (1300, 0), (1850, 6), (2850, 6), (3400, 0), (6300, 2), (7000, 0), (8300, -5),+ (9300, 0), (10000, 0)]+ static let yaw: FlowKeys = [(0, 0.15), (1300, 0.15), (1850, 0.4), (2850, 0.4), (3400, 0.25), (5600, 0.25), (6000, 0.38),+ (6800, 0.4), (7050, 0.08), (7650, 0.05), (8300, -0.3), (9000, -0.25), (9600, 0.15),+ (10000, 0.15)]+ static let head: FlowKeys = [(0, 0), (300, 0), (600, 5), (1200, 4), (1500, 0), (1900, 6), (2850, 5), (3400, 2), (4600, -1),+ (5300, -4), (5900, -3), (6300, 4), (6800, 6), (7050, 0), (7650, -3), (8300, -6), (9000, -3),+ (9600, 0), (10000, 0)]+ /// The eyes: down to the sprout, on the water, up the stem as it grows, on the butterfly+ /// all the way in and all the way out, down to the falling petals.+ static let lookX: FlowKeys = [(0, 0), (300, 0), (550, 3), (1200, 3), (1400, 1), (1800, 3), (5600, 3), (5900, 5),+ (6400, 4.5), (6900, 3), (7050, -0.4), (7650, -0.25), (8100, -2), (8600, -5), (9100, -4),+ (9350, 1.5), (9700, 0), (10000, 0)]+ static let lookY: FlowKeys = [(0, 1), (300, 1), (550, 5), (1200, 5), (1400, 2), (1800, 5), (3300, 5), (3800, 3),+ (4600, 1), (5300, 0), (5600, 0), (5900, -5), (6300, -4), (6800, 0), (6900, 1), (7050, 0),+ (7650, 0), (8100, -4), (8700, -7), (9100, -2), (9350, 4), (9700, 2), (10000, 1)]+ /// The right arm carries the can: a small pull back, up and out over the pot, back down.+ static let armRa: FlowKeys = [(0, 16), (1250, 16), (1500, 8), (1900, 92), (2850, 90), (3250, 40), (3600, 16), (7000, 16),+ (7150, 20), (7400, 24), (7650, 16), (10000, 16)]+ static let armRe: FlowKeys = [(0, 8), (1250, 8), (1500, 14), (1900, 36), (2850, 36), (3250, 18), (3600, 8), (7000, 8),+ (7200, 22), (7650, 8), (10000, 8)]+ /// The free arm: out for balance while pouring, "wow", up in the hop, goodbye.+ static let armLa: FlowKeys = [(0, 12), (1300, 12), (1900, 28), (2850, 26), (3400, 12), (5200, 12), (5450, 48), (5850, 42),+ (6200, 12), (7000, 12), (7200, 128), (7500, 120), (7800, 28), (8200, 36), (8400, 104),+ (9000, 104), (9350, 14), (10000, 12)]+ static let armLe: FlowKeys = [(0, -4), (1300, -4), (1900, -10), (2850, -10), (3400, -4), (5200, -4), (5450, 70), (5850, 64),+ (6200, -4), (7000, -4), (7200, 12), (7500, 18), (7800, 6), (8200, 18), (8400, 80), (9000, 80),+ (9350, 0), (10000, -4)]+ /// The can's tilt: a small pull back (anticipation), the pour, back level.+ static let tilt: FlowKeys = [(0, 0), (1300, 0), (1500, -12), (1900, 50), (2850, 48), (3200, 0), (10000, 0)]+ /// The plant, 0 the sprout to 1 full grown: two pushes with a slow stretch between, and+ /// back down once the petals have fallen.+ static let grow: FlowKeys = [(0, 0), (3150, 0), (3700, 0.4), (4000, 0.47), (4600, 0.88), (4900, 1), (9000, 1), (9900, 0),+ (10000, 0)]+ }++ private func lift(_ t: Double) -> Double {+ guard t >= beat.hop, t < beat.hopEnd else { return 0 }+ let k = (t - beat.hop) / (beat.hopEnd - beat.hop)+ return jump * 4 * k * (1 - k)+ }++ /// A damped swing after a jolt at `at`: one quick back-and-forth that dies out.+ private func swing(_ t: Double, at: Double, amp: Double, period: Double = 600) -> Double {+ guard t > at else { return 0 }+ let s = t - at+ return amp * sin(2 * .pi * s / period) * exp(-s / (period * 0.5))+ }++ private func pose(_ t: Double) -> Pose {+ var p = Pose()+ p.lift = lift(t)+ // Squash and stretch keep the volume; breathing on top.+ let s = flow(t, Act.sy)+ p.sx = 1 / pow(s, 0.6)+ p.sy = s * (1 + 0.014 * wave(t, period: 2500))+ p.lean = flow(t, Act.lean) + 1.5 * wave(t, period: 5000, phase: 0.2)+ p.shift = 0.6 * flow(t, Act.shift) + 1.2 * wave(t, period: 5000)+ p.yaw = flow(t, Act.yaw) + 0.06 * wave(t, period: 2500, phase: 0.1)+ // The head follows the body a beat later and the other way, on top of its own acting.+ p.head = flow(t, Act.head) - 0.45 * flow(t - 110, Act.lean) + 2 * wave(t, period: 5000, phase: 0.6)+ // Arms: the key poses, a small sway that never stops, and a forearm that trails the upper arm.+ func arm(_ ka: FlowKeys, _ ke: FlowKeys, phase: Double, sway k: Double) -> Arm {+ let sway = 4 * wave(t, period: 2500, phase: phase) + 2 * wave(t, period: 2000, phase: phase + 0.3)+ let trail = 0.45 * (flow(t - 90, ka) - flow(t, ka))+ return Arm(a: flow(t, ka) + sway * k, e: flow(t, ke) + 5 * wave(t, period: 2500, phase: phase + 0.6) * k + trail)+ }+ p.armR = arm(Act.armRa, Act.armRe, phase: 0, sway: 0.5)+ p.armL = arm(Act.armLa, Act.armLe, phase: 0.5, sway: 1)+ p.armL.e += 14 * wave(t, period: 500) * seg(t, 8400, 8550) * (1 - seg(t, 8850, 9000))+ // Legs: splay on the squash; the free leg eases out on the weight shift.+ let splay = max(0, 1 - s) * 50+ p.legR = 3 + splay + max(0, -p.shift) * 0.9+ p.legL = 3 + splay + max(0, p.shift) * 0.9+ // The can hangs from the hand, so it lags it: it swings back when the arm lifts, trembles+ // a little while pouring and swings on the hop.+ let pouring = seg(t, beat.pour, beat.pour + 100) * (1 - seg(t, beat.pourEnd, beat.pourEnd + 100))+ p.tilt = flow(t - 70, Act.tilt) + 0.3 * (flow(t - 150, Act.armRa) - flow(t - 40, Act.armRa))+ + 2.5 * wave(t, period: 500) * pouring + swing(t, at: beat.hop, amp: -10) + swing(t, at: beat.hopEnd, amp: 8)+ return p+ }++ /// The girl's frame on stage: feet, hop, lean, squash, then her own units with the head at (0, 0).+ private func frame(_ p: Pose) -> CGAffineTransform {+ CGAffineTransform(translationX: feet.x, y: feet.y - p.lift)+ .rotated(by: p.lean * .pi / 180)+ .scaledBy(x: p.sx * girlScale, y: p.sy * girlScale)+ .translatedBy(x: 0, y: -floorY)+ }++ /// One arm's elbow and hand (in the upper body's frame), and the forearm's direction.+ private func reach(side: Double, _ limb: Arm) -> (elbow: CGPoint, hand: CGPoint, dir: CGPoint) {+ let a = limb.a * .pi / 180, f = (limb.a + limb.e) * .pi / 180+ let straight = min(1, max(0, 1 - abs(limb.e) / 40))+ let extra = arm.reach * min(1, max(0, (limb.a - 100) / 50)) * straight+ let from = CGPoint(x: side * arm.x, y: arm.y)+ let elbow = CGPoint(x: from.x + side * sin(a) * arm.upper, y: from.y + cos(a) * arm.upper)+ let uf = CGPoint(x: side * sin(f), y: cos(f))+ return (elbow, CGPoint(x: elbow.x + uf.x * (arm.fore + extra), y: elbow.y + uf.y * (arm.fore + extra)), uf)+ }++ /// Where the can's rose is on stage at time `t`, and which way the water leaves it (radians).+ private func rose(_ t: Double) -> (at: CGPoint, dir: Double) {+ let p = pose(t)+ let grip = reach(side: 1, p.armR).hand+ let m = frame(p).translatedBy(x: p.shift, y: 0).translatedBy(x: grip.x, y: grip.y).rotated(by: (p.tilt - p.lean) * .pi / 180)+ let d = atan2(can.spoutTip.y - can.spoutFrom.y, can.spoutTip.x - can.spoutFrom.x)+ return (can.spoutTip.applying(m), d + p.tilt * .pi / 180)+ }++ // MARK: - The land: sky, clouds, hills and a meadow with swaying tufts++ private func drawLand(_ ctx: GraphicsContext) {+ let horizon = top + 490+ ctx.fill(Path(CGRect(x: 0, y: 0, width: Self.size.width, height: height)),+ with: .linearGradient(Gradient(colors: [Ink.skyTop, Ink.skyLow]), startPoint: .zero, endPoint: CGPoint(x: 0, y: horizon)))+ let clouds: [(x: Double, y: Double, s: Double)] = [(220, 150, 1), (520, 90, 0.8), (640, 230, 0.7), (90, 260, 0.75)]+ for (i, c) in clouds.enumerated() {+ var g = ctx+ g.translateBy(x: c.x + 10 * wave(t, period: Self.loopMs, phase: Double(i) * 0.27), y: top + c.y)+ g.scaleBy(x: c.s, y: c.s)+ for b in [(-30.0, 0.0, 24.0), (0.0, -14.0, 30.0), (32.0, -2.0, 22.0)] {+ g.fill(Path(ellipseIn: CGRect(x: b.0 - b.2, y: b.1 - b.2, width: b.2 * 2, height: b.2 * 2)), with: .color(Ink.cloud))+ }+ g.fill(Path(roundedRect: CGRect(x: -56, y: -4, width: 112, height: 26), cornerRadius: 13), with: .color(Ink.cloud))+ }+ ctx.fill(Path(ellipseIn: CGRect(x: -260, y: horizon - 52, width: 760, height: 150)), with: .color(Ink.hillFar))+ ctx.fill(Path(ellipseIn: CGRect(x: 300, y: horizon - 38, width: 700, height: 130)), with: .color(Ink.hillNear))+ ctx.fill(Path(CGRect(x: 0, y: horizon, width: Self.size.width, height: height - horizon)), with: .color(Ink.ground))+ // Tufts sway around their roots, each on its own phase: nothing in sync.+ let tufts: [(x: Double, y: Double, s: Double, daisy: Bool)] = [+ (196, 522, 0.6, true), (556, 514, 0.55, false), (372, 506, 0.45, false), (176, 702, 1, false),+ (566, 694, 0.9, true), (338, 766, 1.1, false), (470, 806, 0.95, true), (232, 836, 0.9, false),+ ]+ for (i, f) in tufts.enumerated() {+ var g = ctx+ g.translateBy(x: f.x, y: top + f.y)+ g.scaleBy(x: f.s, y: f.s)+ g.rotate(by: .degrees(4 * wave(t, period: 2500, phase: seeded(i, salt: 21)) + 2 * wave(t, period: 1250, phase: seeded(i, salt: 22))))+ for (dx, h, lean) in [(-7.0, 22.0, -18.0), (0.0, 30.0, 4.0), (7.0, 20.0, 22.0)] {+ var blade = g+ blade.translateBy(x: dx, y: 0)+ blade.rotate(by: .degrees(lean))+ blade.fill(roundedTriangle(CGPoint(x: -4, y: 0), CGPoint(x: 4, y: 0), CGPoint(x: 0, y: -h), r: 2), with: .color(Ink.grass))+ }+ if f.daisy {+ var d = g+ d.translateBy(x: 0, y: -34)+ drawDaisy(d, r: 4)+ }+ }+ }++ private func drawDaisy(_ g: GraphicsContext, r: Double) {+ for i in 0..<5 {+ let a = Double(i) / 5 * 2 * .pi - .pi / 2+ g.fill(Path(ellipseIn: CGRect(x: cos(a) * r * 1.2 - r, y: sin(a) * r * 1.2 - r, width: r * 2, height: r * 2)), with: .color(Ink.white))+ }+ g.fill(Path(ellipseIn: CGRect(x: -r * 0.8, y: -r * 0.8, width: r * 1.6, height: r * 1.6)), with: .color(Ink.daisyEye))+ }++ // MARK: - The pot and the soil, which darkens where the water lands and dries again++ private func drawPot(_ ctx: GraphicsContext) {+ let sw = 136.0+ ctx.fill(Path(ellipseIn: CGRect(x: pot.x - sw / 2, y: pot.base - 6 - sw * build.shadowRatio / 2, width: sw, height: sw * build.shadowRatio)),+ with: .color(Ink.shadow))+ let body = roundedPolygon([CGPoint(x: pot.x - pot.top / 2, y: pot.rim + 8), CGPoint(x: pot.x + pot.top / 2, y: pot.rim + 8),+ CGPoint(x: pot.x + pot.bottom / 2, y: pot.base), CGPoint(x: pot.x - pot.bottom / 2, y: pot.base)], r: 10)+ ctx.fill(body, with: .color(Ink.pot))+ var shade = ctx+ shade.clip(to: body)+ shade.fill(Path(CGRect(x: pot.x + pot.top / 2 - 28, y: pot.rim, width: 40, height: pot.base - pot.rim)), with: .color(Ink.potShade))+ // The lip: a band and its top, seen from a little above; the inner wall shows at the back.+ ctx.fill(Path(roundedRect: CGRect(x: pot.x - pot.lip / 2, y: pot.rim, width: pot.lip, height: 22), cornerRadius: 8),+ with: .color(Ink.potRim))+ ctx.fill(Path(ellipseIn: CGRect(x: pot.x - pot.lip / 2, y: pot.rim - 11, width: pot.lip, height: 22)), with: .color(Ink.potRim))+ ctx.fill(Path(ellipseIn: CGRect(x: pot.x - 55, y: pot.rim - 8, width: 110, height: 16)), with: .color(Ink.potShade))+ let wet = seg(t, beat.pour + 100, beat.pourEnd + 100) * (1 - seg(t, beat.wilt, 9900, easeOut: false))+ ctx.fill(Path(ellipseIn: CGRect(x: pot.x - 50, y: pot.rim - 4, width: 100, height: 12)), with: .color(mix(Ink.soilDry, Ink.soilWet, wet)))+ }++ private func mix(_ a: UInt32, _ b: UInt32, _ k: Double) -> Color {+ func c(_ v: UInt32, _ s: UInt32) -> Double { Double((v >> s) & 0xFF) / 255 }+ return Color(red: c(a, 16) + (c(b, 16) - c(a, 16)) * k, green: c(a, 8) + (c(b, 8) - c(a, 8)) * k,+ blue: c(a, 0) + (c(b, 0) - c(a, 0)) * k)+ }++ // MARK: - The plant: a stem that bends from the soil, leaves that unfurl, a bud, the flower++ private struct Stem { var base: CGPoint, ctrl: CGPoint, tip: CGPoint, len: Double }++ private func stem(_ t: Double) -> Stem {+ let g = flow(t, Act.grow)+ let len = plant.sprout + (plant.full - plant.sprout) * g+ // It sways from the soil on two waves; the butterfly's landing dips it, its takeoff kicks it.+ let a = (2.5 * wave(t, period: 2500) + 1.5 * wave(t, period: 5000, phase: 0.3)) * (0.4 + 0.6 * g)+ + swing(t, at: beat.land, amp: 5, period: 800) + swing(t, at: beat.takeoff, amp: -7, period: 700)+ let r = a * .pi / 180+ let base = CGPoint(x: pot.x, y: soilY)+ let tip = CGPoint(x: base.x + sin(r) * len, y: base.y - cos(r) * len)+ // The bend: the middle swings less than the tip, and a grown stem curves a little.+ let ctrl = CGPoint(x: base.x + sin(r * 0.35) * len * 0.55 - 6 * g, y: base.y - cos(r * 0.35) * len * 0.55)+ return Stem(base: base, ctrl: ctrl, tip: tip, len: len)+ }++ /// The point `h` up the stem, and the stem's angle there (degrees, 0 straight up).+ private func along(_ s: Stem, _ h: Double) -> (pt: CGPoint, angle: Double) {+ let u = min(1, max(0, h / max(1, s.len))), a = 1 - u+ let pt = CGPoint(x: a * a * s.base.x + 2 * a * u * s.ctrl.x + u * u * s.tip.x,+ y: a * a * s.base.y + 2 * a * u * s.ctrl.y + u * u * s.tip.y)+ let dx = 2 * a * (s.ctrl.x - s.base.x) + 2 * u * (s.tip.x - s.ctrl.x)+ let dy = 2 * a * (s.ctrl.y - s.base.y) + 2 * u * (s.tip.y - s.ctrl.y)+ return (pt, atan2(dx, -dy) * 180 / .pi)+ }++ private func drawPlant(_ ctx: GraphicsContext) {+ let s = stem(t)+ var path = Path()+ path.move(to: s.base)+ path.addQuadCurve(to: s.tip, control: s.ctrl)+ ctx.stroke(path, with: .color(Ink.stem), style: StrokeStyle(lineWidth: 8, lineCap: .round))+ // The seed leaves stay low on the stem.+ let low = along(s, min(14, s.len - 3))+ for side in [-1.0, 1.0] {+ let sway = 5 * wave(t - 100, period: 2500, phase: side * 0.1)+ drawLeaf(ctx, at: low.pt, angle: low.angle + side * 58 + sway, length: 20, width: 24, vein: false)+ }+ // True leaves unfurl one after another as the stem passes them: they open out from the+ // stem, then widen (unroll); each lags the stem's sway a little more than the one below.+ for (i, h) in plant.leaves.enumerated() {+ let side = i % 2 == 0 ? -1.0 : 1.0+ let fit = min(1, max(0, (s.len - h - 4) / 14))+ let open = seg(t, plant.unfurl[i], plant.unfurl[i] + 520) * (1 - seg(t, plant.fold[i], plant.fold[i] + 300, easeOut: false)) * fit+ guard open > 0.01 else { continue }+ let unroll = seg(t, plant.unfurl[i] + 120, plant.unfurl[i] + 640) * (1 - seg(t, plant.fold[i], plant.fold[i] + 260, easeOut: false))+ let at = along(s, h)+ let lag = 6 * wave(t - 140 * Double(i + 1), period: 2500)+ drawLeaf(ctx, at: at.pt, angle: at.angle + side * (12 + 46 * open) + lag, length: 46 * (0.25 + 0.75 * open),+ width: 28 * (0.15 + 0.85 * unroll * fit), vein: true)+ }+ drawFlower(ctx, s)+ }++ private func drawLeaf(_ ctx: GraphicsContext, at: CGPoint, angle: Double, length L: Double, width W: Double, vein: Bool) {+ var g = ctx+ g.translateBy(x: at.x, y: at.y)+ g.rotate(by: .degrees(angle))+ var p = Path()+ p.move(to: .zero)+ p.addQuadCurve(to: CGPoint(x: 0, y: -L), control: CGPoint(x: W, y: -L * 0.45))+ p.addQuadCurve(to: .zero, control: CGPoint(x: -W, y: -L * 0.55))+ p.closeSubpath()+ g.fill(p, with: .color(Ink.leaf))+ if vein {+ var v = Path()+ v.move(to: CGPoint(x: 0, y: -3))+ v.addQuadCurve(to: CGPoint(x: 0, y: -L * 0.72), control: CGPoint(x: W * 0.12, y: -L * 0.4))+ g.stroke(v, with: .color(Ink.vein), style: StrokeStyle(lineWidth: 2.5, lineCap: .round))+ }+ }++ /// Petal i's direction around the flower (degrees, -90 up), back petals first.+ private func petalAngle(_ i: Int) -> Double { -90 + 60 * Double(i) }+ /// When each petal lets go, in a scattered order, after the butterfly takes off.+ private func petalFall(_ i: Int) -> Double { beat.takeoff + 160 * Double([4, 0, 2, 5, 1, 3][i]) }++ private func drawFlower(_ ctx: GraphicsContext, _ s: Stem) {+ let bud = seg(t, beat.bud, beat.bud + 500) * (1 - seg(t, beat.wilt, beat.wilt + 350, easeOut: false))+ guard bud > 0.01 else { return }+ let top = along(s, s.len)+ var f = ctx+ f.translateBy(x: top.pt.x, y: top.pt.y)+ f.rotate(by: .degrees(top.angle))+ // The bud swells, then stays as the green cup under the petals.+ f.fill(roundedTriangle(CGPoint(x: -12 * bud, y: 2), CGPoint(x: 12 * bud, y: 2), CGPoint(x: 0, y: -30 * bud), r: 8 * bud),+ with: .color(Ink.stem))+ var c = f+ c.translateBy(x: 0, y: -14)+ // Petals open one after another: each grows out from the centre with a small twist, and+ // keeps a little flutter of its own.+ for i in [1, 3, 5, 0, 2, 4] where t < petalFall(i) {+ let open = seg(t, beat.bloom + 90 * Double(i), beat.bloom + 90 * Double(i) + 520)+ guard open > 0 else { continue }+ var p = c+ p.rotate(by: .degrees(petalAngle(i) + 25 * (1 - open) + 1.5 * wave(t - 60 * Double(i), period: 1250)))+ let sc = 0.25 + 0.75 * open, d = 2 + 9 * open+ p.fill(Path(ellipseIn: CGRect(x: d, y: -12 * sc, width: 34 * sc, height: 24 * sc)),+ with: .color(i % 2 == 0 ? Ink.petal : Ink.petalShade))+ }+ let heart = seg(t, beat.bloom - 50, beat.bloom + 350) * (1 - seg(t, beat.wilt, beat.wilt + 350, easeOut: false))+ if heart > 0 {+ c.fill(Path(ellipseIn: CGRect(x: -12 * heart, y: -12 * heart, width: 24 * heart, height: 24 * heart)), with: .color(Ink.center))+ }+ }++ // MARK: - Water: drops leave the rose in arcs, land on the soil and splash++ private func drawWater(_ ctx: GraphicsContext) {+ guard t >= beat.pour, t < beat.pourEnd + 900 else { return }+ var n = 0+ var te = beat.pour+ while te <= beat.pourEnd {+ defer { te += water.every; n += 1 }+ guard t >= te, t < te + 900 else { continue }+ let r = rose(te)+ for j in 0..<2 {+ let i = n * 2 + j+ // Two holes of the rose, a little spread, a little variety in speed.+ let o = (j == 0 ? -4.0 : 4.0) + (seeded(i, salt: 1) - 0.5) * 3+ let x0 = r.at.x - sin(r.dir) * o, y0 = r.at.y + cos(r.dir) * o+ let v = water.speed + 50 * seeded(i, salt: 2)+ let vx = cos(r.dir) * v + (seeded(i, salt: 3) - 0.5) * 24, vy = sin(r.dir) * v+ let land = soilY + 3 * seeded(i, salt: 4)+ let sLand = (-vy + (vy * vy + 2 * water.g * max(0, land - y0)).squareRoot()) / water.g+ let s = (t - te) / 1000+ if s < sLand {+ var d = ctx+ d.translateBy(x: x0 + vx * s, y: y0 + vy * s + water.g * s * s / 2)+ d.rotate(by: .radians(atan2(vy + water.g * s, vx)))+ d.fill(pill(cx: 0, cy: 0, w: 11, h: 6), with: .color(Ink.water))+ } else if s < sLand + 0.22 {+ let p = (s - sLand) / 0.22+ let xl = x0 + vx * sLand+ for side in [-1.0, 1.0] {+ let rr = 2.6 * (1 - 0.5 * p)+ let cx = xl + side * 14 * p, cy = land - 16 * 4 * p * (1 - p)+ ctx.fill(Path(ellipseIn: CGRect(x: cx - rr, y: cy - rr, width: rr * 2, height: rr * 2)), with: .color(Ink.water))+ }+ }+ }+ }+ }++ // MARK: - Sparkles when the flower opens++ private func drawSparkles(_ ctx: GraphicsContext) {+ guard t > 5400, t < 6600 else { return }+ let s = stem(t)+ let c = along(s, s.len)+ let center = CGPoint(x: c.pt.x + sin(c.angle * .pi / 180) * 14, y: c.pt.y - cos(c.angle * .pi / 180) * 14)+ for i in 0..<7 {+ let start = 5550 + seeded(i, salt: 13) * 450+ let p = (t - start) / 650+ guard p > 0, p < 1 else { continue }+ let a = Double(i) / 7 * 2 * .pi + seeded(i, salt: 14)+ let r = 46 + 22 * seeded(i, salt: 15) + 16 * easeOut.value(at: p)+ var g = ctx+ g.translateBy(x: center.x + cos(a) * r, y: center.y + sin(a) * r * 0.9)+ g.scaleBy(x: bell(p), y: bell(p))+ g.fill(fourPointStar(r: 12 + 7 * seeded(i, salt: 16)), with: .color(Ink.sparkle))+ }+ }++ // MARK: - Falling petals: each lets go, flutters down with spin and sway, settles and fades++ private func drawFallingPetals(_ ctx: GraphicsContext) {+ for i in 0..<6 {+ let start = petalFall(i)+ let s = (t - start) / 1000+ guard s > 0 else { continue }+ let st = stem(start)+ let top = along(st, st.len)+ let m = CGAffineTransform(translationX: top.pt.x, y: top.pt.y).rotated(by: top.angle * .pi / 180)+ .translatedBy(x: 0, y: -14).rotated(by: petalAngle(i) * .pi / 180)+ let p0 = CGPoint(x: 28, y: 0).applying(m)+ let a0 = top.angle + petalAngle(i)+ let wind = -40 + 30 * seeded(i, salt: 7), phase = seeded(i, salt: 8) * 2 * .pi+ func x(_ s: Double) -> Double { p0.x + wind * s + 18 * (sin(2 * .pi * s / 0.8 + phase) - sin(phase)) }+ // It lands on the soil if it falls over the pot, on the meadow if not.+ let inPot = abs(x(0.8) - pot.x) < 44+ let land = inPot ? soilY + 2 * seeded(i, salt: 9) : 640 + 24 * seeded(i, salt: 9)+ let sLand = (-110 + (110 * 110 + 4 * 150 * max(0, land - p0.y)).squareRoot()) / (2 * 150)+ let sc = min(s, sLand)+ let fade = 1 - seg(t, start + sLand * 1000 + 100, start + sLand * 1000 + 300, easeOut: false)+ guard fade > 0 else { continue }+ var g = ctx+ g.opacity = fade+ g.translateBy(x: x(sc), y: p0.y + 110 * sc + 150 * sc * sc)+ let dir = seeded(i, salt: 10) < 0.5 ? -1.0 : 1.0+ g.rotate(by: .degrees(a0 + dir * 160 * sc + 18 * sin(2 * .pi * sc / 0.8 + phase) - 18 * sin(phase)))+ // Turning over as it falls: the petal's width flips through its edge.+ let flip = 0.3 + 0.7 * abs(cos(.pi * sc / 0.55))+ g.scaleBy(x: 1, y: flip)+ g.fill(Path(ellipseIn: CGRect(x: -17, y: -12, width: 34, height: 24)), with: .color(i % 2 == 0 ? Ink.petal : Ink.petalShade))+ }+ }++ // MARK: - The butterfly: wanders in, perches on the flower, flies off++ private func butterfly(_ t: Double) -> (pos: CGPoint, flying: Double, tilt: Double)? {+ guard t >= beat.enter, t < beat.gone else { return nil }+ let s = stem(t)+ let top = along(s, s.len)+ let r = top.angle * .pi / 180+ let perch = CGPoint(x: top.pt.x + sin(r) * 16, y: top.pt.y - cos(r) * 16 - 8)+ if t < beat.land {+ // In from beyond the screen's edge, wandering on two waves that die out at the perch.+ let k = (t - beat.enter) / (beat.land - beat.enter)+ let from = CGPoint(x: 700, y: 210)+ let x = from.x + (perch.x - from.x) * easeOut.value(at: k) + 26 * wave(t, period: 1250) * bell(k)+ let y = from.y + (perch.y - from.y) * easeInOut.value(at: k) + 30 * wave(t, period: 625, phase: 0.25) * bell(k)+ return (CGPoint(x: x, y: y), 1 - seg(t, beat.land - 160, beat.land + 40), -8 + 8 * wave(t, period: 1250, phase: 0.2))+ }+ if t < beat.takeoff { return (perch, 0, top.angle * 0.6) }+ // Off: up first, then away to the left, speeding up.+ let k = (t - beat.takeoff) / (beat.gone - beat.takeoff)+ let to = CGPoint(x: 90, y: 60)+ let x = perch.x + (to.x - perch.x) * pow(k, 1.6) + 20 * wave(t, period: 1250, phase: 0.4) * bell(k)+ let y = perch.y + (to.y - perch.y) * easeOut.value(at: k) + 24 * wave(t, period: 625) * bell(k)+ return (CGPoint(x: x, y: y), seg(t, beat.takeoff, beat.takeoff + 80), -18 + 8 * wave(t, period: 1250))+ }++ private func drawButterfly(_ ctx: GraphicsContext) {+ guard let b = butterfly(t) else { return }+ // Fast flaps in the air, slow open-and-close while it sits.+ let fast = 0.12 + 0.88 * (0.5 + 0.5 * wave(t, period: 200))+ let slow = 0.55 + 0.45 * (0.5 + 0.5 * wave(t, period: 1250))+ let open = b.flying * fast + (1 - b.flying) * slow+ var g = ctx+ g.translateBy(x: b.pos.x, y: b.pos.y)+ g.rotate(by: .degrees(b.tilt))+ for side in [-1.0, 1.0] {+ var w = g+ w.scaleBy(x: side * open, y: 1)+ w.fill(Path(ellipseIn: CGRect(x: 1, y: -25, width: 30, height: 27)), with: .color(Ink.wingEdge))+ w.fill(Path(ellipseIn: CGRect(x: 3, y: -21, width: 23, height: 20)), with: .color(Ink.wing))+ w.fill(Path(ellipseIn: CGRect(x: 1, y: -3, width: 21, height: 20)), with: .color(Ink.wingEdge))+ w.fill(Path(ellipseIn: CGRect(x: 3, y: 0, width: 15, height: 14)), with: .color(Ink.wing))+ for d in [(26.0, -17.0, 2.4), (28.5, -9.0, 1.8)] {+ w.fill(Path(ellipseIn: CGRect(x: d.0 - d.2, y: d.1 - d.2, width: d.2 * 2, height: d.2 * 2)), with: .color(Ink.white))+ }+ }+ g.fill(pill(cx: 0, cy: -2, w: 7, h: 26), with: .color(Ink.wingEdge))+ g.fill(Path(ellipseIn: CGRect(x: -4.5, y: -21, width: 9, height: 9)), with: .color(Ink.wingEdge))+ for side in [-1.0, 1.0] {+ var a = Path()+ a.move(to: CGPoint(x: side * 1.5, y: -19))+ a.addQuadCurve(to: CGPoint(x: side * 9, y: -31), control: CGPoint(x: side * 2, y: -29))+ g.stroke(a, with: .color(Ink.wingEdge), style: StrokeStyle(lineWidth: 1.8, lineCap: .round))+ g.fill(Path(ellipseIn: CGRect(x: side * 9 - 2, y: -33, width: 4, height: 4)), with: .color(Ink.wingEdge))+ }+ }++ // MARK: - Hearts on the grin++ private func drawHearts(_ ctx: GraphicsContext) {+ let spots: [(dx: Double, start: Double)] = [(-74, 7080), (8, 7220), (78, 7360)]+ let base = CGPoint(x: feet.x, y: feet.y - (floorY + 104) * girlScale)+ for (i, h) in spots.enumerated() {+ let p = (t - h.start) / 900+ guard p > 0, p < 1 else { continue }+ var g = ctx+ g.translateBy(x: base.x + h.dx + 6 * wave(t, period: 1000, phase: Double(i) * 0.3), y: base.y - 56 * easeOut.value(at: p))+ let sc = bell(p) * (0.85 + 0.3 * seeded(i, salt: 11))+ g.scaleBy(x: sc, y: sc)+ g.rotate(by: .degrees(-12 + 24 * seeded(i, salt: 12)))+ g.fill(heartShape(r: 11), with: .color(Ink.heart))+ }+ }++ // MARK: - The girl on stage++ private func drawGirlOnStage(_ ctx: GraphicsContext) {+ var p = pose(t)+ let past = pose(t - 140)+ p.hairSwing = (past.head + past.lean) - (p.head + p.lean)+ p.hairUp = lift(t - 90) - p.lift+ let ground = 1 - min(1, p.lift / 250)+ let sw = build.shadow * girlScale * ground * p.sx+ ctx.fill(Path(ellipseIn: CGRect(x: feet.x + p.shift * 0.4 * girlScale - sw / 2,+ y: feet.y - build.shadowUp * girlScale - sw * build.shadowRatio / 2,+ width: sw, height: sw * build.shadowRatio)), with: .color(Ink.shadow))+ var g = ctx+ g.concatenate(frame(p))+ drawGirl(g, pose: p)+ }++ // MARK: - The girl, in the head frame++ private func drawGirl(_ c: GraphicsContext, pose: Pose) {+ func capsules(_ g: GraphicsContext, _ pts: [CGPoint], _ w: Double, _ color: Color) {+ var p = Path()+ p.addLines(pts)+ g.stroke(p, with: .color(color), style: StrokeStyle(lineWidth: w, lineCap: .round, lineJoin: .round))+ }+ let yaw = pose.yaw++ // Short legs in rain boots: a round dark sole, the boot's shaft and a lighter rim.+ for (side, angle) in [(-1.0, pose.legL), (1.0, pose.legR)] {+ var leg = c+ leg.translateBy(x: side * build.hip + pose.shift, y: build.hipY)+ leg.rotate(by: .degrees(-side * angle + pose.shift))+ leg.fill(Path(roundedRect: CGRect(x: -15 + side * 2, y: 42, width: 30, height: 20), cornerRadius: 10), with: .color(Ink.sole))+ capsules(leg, [.zero, CGPoint(x: 0, y: 24)], 20, Ink.skin)+ leg.fill(Path(roundedRect: CGRect(x: -13, y: 14, width: 26, height: 37), cornerRadius: 9), with: .color(Ink.boot))+ leg.fill(Path(roundedRect: CGRect(x: -14, y: 12, width: 28, height: 9), cornerRadius: 4.5), with: .color(Ink.bootRim))+ }++ var up = c+ up.translateBy(x: pose.shift, y: 0)+ up.fill(Path(roundedRect: CGRect(x: -40, y: 104, width: 80, height: 34), cornerRadius: 12), with: .color(Ink.denim))++ // Arms behind the torso: upper arm and forearm share one round elbow, a circle hand, a sleeve.+ for (side, limb) in [(-1.0, pose.armL), (1.0, pose.armR)] {+ let r = reach(side: side, limb)+ let from = CGPoint(x: side * arm.x, y: arm.y)+ capsules(up, [from, r.elbow, CGPoint(x: r.hand.x - r.dir.x * 8, y: r.hand.y - r.dir.y * 8)], arm.w, Ink.skin)+ up.fill(Path(ellipseIn: CGRect(x: r.hand.x - arm.hand, y: r.hand.y - arm.hand, width: arm.hand * 2, height: arm.hand * 2)),+ with: .color(Ink.skin))+ let a = limb.a * .pi / 180+ capsules(up, [from, CGPoint(x: from.x + side * sin(a) * 16, y: from.y + cos(a) * 16)], arm.w + 10, Ink.shirt)+ }+ // The can hangs in front of the arm that carries it, and the hand closes over its handle.+ let grip = reach(side: 1, pose.armR).hand+ drawCan(up, grip: grip, angle: pose.tilt - pose.lean)+ up.fill(Path(ellipseIn: CGRect(x: grip.x - arm.hand, y: grip.y - arm.hand, width: arm.hand * 2, height: arm.hand * 2)),+ with: .color(Ink.skin))++ // A striped shirt under dungarees: the bib and its straps slide when she turns.+ let torso = roundedPolygon([CGPoint(x: -build.shoulders, y: build.torsoTop), CGPoint(x: build.shoulders, y: build.torsoTop),+ CGPoint(x: build.waist, y: build.torsoBottom), CGPoint(x: -build.waist, y: build.torsoBottom)], r: 18)+ up.fill(torso, with: .color(Ink.shirt))+ var chest = up+ chest.clip(to: torso)+ for y in [62.0, 80.0, 98.0] {+ chest.fill(Path(CGRect(x: -50, y: y, width: 100, height: 6)), with: .color(Ink.stripe))+ }+ let bx = 6 * yaw+ var straps = Path()+ straps.move(to: CGPoint(x: bx - 17, y: 84))+ straps.addLine(to: CGPoint(x: -27 + 3 * yaw, y: 46))+ straps.move(to: CGPoint(x: bx + 17, y: 84))+ straps.addLine(to: CGPoint(x: 27 + 3 * yaw, y: 46))+ chest.stroke(straps, with: .color(Ink.denim), lineWidth: 9)+ chest.fill(Path(roundedRect: CGRect(x: bx - 24, y: 78, width: 48, height: 40), cornerRadius: 10), with: .color(Ink.denim))+ chest.fill(Path(roundedRect: CGRect(x: bx - 11, y: 89, width: 22, height: 14), cornerRadius: 4), with: .color(Ink.denimShade))+ for side in [-1.0, 1.0] {+ chest.fill(Path(ellipseIn: CGRect(x: bx + side * 16 - 3.6, y: 81.4, width: 7.2, height: 7.2)), with: .color(Ink.button))+ }++ // Head, tilting where it meets the collar. Pigtails behind it, the bob and the hat on top.+ var h = up+ h.translateBy(x: 0, y: 56)+ h.rotate(by: .degrees(pose.head))+ h.translateBy(x: 0, y: -56)+ drawPigtails(h, pose: pose)+ h.fill(tomb(cx: 0, top: -66, w: 100, h: 122, r: 26), with: .color(Ink.skin))+ drawFace(h, yaw: yaw)+ // The bob is ONE outline around the face (no cutout), so no hairline can land on the eyes.+ h.fill(hairBob(window: -40 + 9 * yaw), with: .color(Ink.hair))+ for side in [-1.0, 1.0] {+ h.fill(pill(cx: side * 52 - 6 * yaw, cy: 22, w: 12, h: 10), with: .color(Ink.band))+ }+ drawHat(h, pose: pose)+ }++ /// Pigtails hang from the ties and trail the head: they swing when it turns, fly up a little+ /// when she drops and settle with a small swing when she lands.+ private func drawPigtails(_ h: GraphicsContext, pose: Pose) {+ let drop = max(-12, min(20, pose.hairUp))+ for side in [-1.0, 1.0] {+ var g = h+ g.translateBy(x: side * 50 - 6 * pose.yaw, y: 20)+ let out = 24 + 1.1 * drop + 3 * wave(t, period: 2500, phase: side * 0.15) + swing(t, at: beat.hopEnd, amp: 10)+ g.rotate(by: .degrees(-side * out + 0.9 * pose.hairSwing))+ g.fill(Path(ellipseIn: CGRect(x: -14, y: -2, width: 28, height: 38)), with: .color(Ink.hair))+ }+ }++ /// A sun hat: the brim seen from a little above, the crown sitting on its back half, a band+ /// with a daisy. It trails the head's turns and lifts a little when she drops.+ private func drawHat(_ h: GraphicsContext, pose: Pose) {+ var g = h+ g.translateBy(x: 0, y: -62 - 0.35 * max(0, pose.hairUp))+ g.rotate(by: .degrees(0.4 * pose.hairSwing))+ let dx = 3 * pose.yaw+ g.fill(Path(ellipseIn: CGRect(x: -82 + 2 * dx, y: -15, width: 164, height: 30)), with: .color(Ink.brim))+ g.fill(tomb(cx: 1.5 * dx, top: -50, w: 96, h: 46, r: 0), with: .color(Ink.hat))+ g.fill(pill(cx: 1.5 * dx, cy: -9, w: 96, h: 12), with: .color(Ink.band))+ var d = g+ d.translateBy(x: 26 + 3 * dx, y: -10)+ d.rotate(by: .degrees(3 * wave(t, period: 2500, phase: 0.4)))+ drawDaisy(d, r: 4.6)+ }++ /// The watering can, hanging from the grip (the top of its handle), tilted by `angle`.+ private func drawCan(_ c: GraphicsContext, grip: CGPoint, angle: Double) {+ var g = c+ g.translateBy(x: grip.x, y: grip.y)+ g.rotate(by: .degrees(angle))+ var spout = Path()+ spout.move(to: can.spoutFrom)+ spout.addLine(to: can.spoutTip)+ g.stroke(spout, with: .color(Ink.canShade), style: StrokeStyle(lineWidth: 9, lineCap: .round))+ var rose = g+ rose.translateBy(x: can.spoutTip.x, y: can.spoutTip.y)+ rose.rotate(by: .radians(atan2(can.spoutTip.y - can.spoutFrom.y, can.spoutTip.x - can.spoutFrom.x)))+ rose.fill(Path(roundedRect: CGRect(x: -2, y: -10, width: 9, height: 20), cornerRadius: 4.5), with: .color(Ink.can))+ var handle = Path()+ handle.move(to: CGPoint(x: -20, y: 18))+ handle.addQuadCurve(to: CGPoint(x: 18, y: 16), control: CGPoint(x: -2, y: -16))+ g.stroke(handle, with: .color(Ink.canShade), style: StrokeStyle(lineWidth: 7, lineCap: .round))+ let body = Path(roundedRect: CGRect(x: -24, y: 14, width: 48, height: 38), cornerRadius: 10)+ g.fill(body, with: .color(Ink.can))+ var shade = g+ shade.clip(to: body)+ shade.fill(Path(CGRect(x: 10, y: 14, width: 20, height: 40)), with: .color(Ink.canShade))+ g.fill(Path(roundedRect: CGRect(x: -26, y: 12, width: 52, height: 8), cornerRadius: 4), with: .color(Ink.canShade))+ }++ private func drawFace(_ h: GraphicsContext, yaw: Double) {+ // The lower lid rises a little to meet the upper one, so the eye closes nearer its middle.+ let lid = track(t, Self.lid)+ let face = Choreo(period: Self.loopMs, lookX: [Key(0, 5 * yaw + flow(t, Act.lookX))], lookY: [Key(0, flow(t, Act.lookY))],+ lid: [Key(0, lid)], cheek: [Key(0, max(track(t, Self.cheek), 0.18 * lid))], mouth: Self.mouth)+ let dx = 10 * yaw+ for side in [-1.0, 1.0] {+ h.fill(pill(cx: side * 19.5 + dx, cy: -8, w: 30, h: 40), with: .color(Ink.white))+ drawLiveEye(h, side: side,+ eye: EyeGeometry(cx: side * 19.5 + dx, cy: -8, w: 30, h: 40, pupilW: 13, pupilH: 20, pupilTop: 7, shine: 2.8),+ ink: Self.ink, choreo: face, t: t)+ // Freckles ride on the cheeks.+ for f in [(26.0, 15.0), (21.0, 19.0), (31.0, 20.0)] {+ h.fill(Path(ellipseIn: CGRect(x: side * f.0 + dx - 1.9, y: f.1 - 1.9, width: 3.8, height: 3.8)), with: .color(Ink.freckle))+ }+ }+ let nx = 13 * yaw+ h.fill(pill(cx: 2 + nx, cy: -2, w: 8, h: 20), with: .color(Ink.feature))+ h.fill(Path(ellipseIn: CGRect(x: -4.5 + nx, y: 2, width: 13, height: 13)), with: .color(Ink.feature))+ drawLiveMouth(h, x0: -15 + dx, x1: 17 + dx, top: 25, tilt: 5, mouth: Ink.mouth, tongue: Ink.tongue, teeth: Ink.white,+ choreo: face, t: t)+ }++ // MARK: - The style's shapes++ /// A tombstone: a half-circle top, straight sides, bottom corners of radius r.+ /// When h <= w/2 it is a cap: an arc cut flat at the bottom.+ private func tomb(cx: Double, top: Double, w: Double, h: Double, r: Double) -> Path {+ let R = w / 2, yb = top + h+ var p = Path()+ if h <= R {+ let half = (R * R - (R - h) * (R - h)).squareRoot()+ let a = asin((R - h) / R)+ p.move(to: CGPoint(x: cx - half, y: yb))+ p.addArc(center: CGPoint(x: cx, y: top + R), radius: R, startAngle: .radians(.pi + a), endAngle: .radians(-a), clockwise: false)+ p.closeSubpath()+ return p+ }+ let q = min(r, R, h - R)+ p.move(to: CGPoint(x: cx - R, y: top + R))+ p.addArc(center: CGPoint(x: cx, y: top + R), radius: R, startAngle: .degrees(180), endAngle: .degrees(0), clockwise: false)+ p.addLine(to: CGPoint(x: cx + R, y: yb - q))+ p.addQuadCurve(to: CGPoint(x: cx + R - q, y: yb), control: CGPoint(x: cx + R, y: yb))+ p.addLine(to: CGPoint(x: cx - R + q, y: yb))+ p.addQuadCurve(to: CGPoint(x: cx - R, y: yb - q), control: CGPoint(x: cx - R, y: yb))+ p.closeSubpath()+ return p+ }++ /// The bob as one outline: a half-circle top, sides down to the jaw, and the face's opening+ /// starting at `window`, 80 wide, its top edge swept lower on the left like a fringe.+ private func hairBob(window x0: Double) -> Path {+ let R = 55.0, cy = -19.0, bottom = 26.0, q = 14.0, w = 80.0, r = 16.0, topL = -31.0, topR = -38.0+ var p = Path()+ p.move(to: CGPoint(x: -R, y: cy))+ p.addArc(center: CGPoint(x: 0, y: cy), radius: R, startAngle: .degrees(180), endAngle: .degrees(0), clockwise: false)+ p.addLine(to: CGPoint(x: R, y: bottom - q))+ p.addQuadCurve(to: CGPoint(x: R - q, y: bottom), control: CGPoint(x: R, y: bottom))+ p.addLine(to: CGPoint(x: x0 + w, y: bottom))+ p.addLine(to: CGPoint(x: x0 + w, y: topR + r))+ p.addQuadCurve(to: CGPoint(x: x0 + w - r, y: topR), control: CGPoint(x: x0 + w, y: topR))+ p.addQuadCurve(to: CGPoint(x: x0 + r, y: topL), control: CGPoint(x: x0 + w * 0.42, y: topR + 7))+ p.addQuadCurve(to: CGPoint(x: x0, y: topL + r), control: CGPoint(x: x0, y: topL))+ p.addLine(to: CGPoint(x: x0, y: bottom))+ p.addLine(to: CGPoint(x: -R + q, y: bottom))+ p.addQuadCurve(to: CGPoint(x: -R, y: bottom - q), control: CGPoint(x: -R, y: bottom))+ p.closeSubpath()+ return p+ }++ private func roundedTriangle(_ a: CGPoint, _ b: CGPoint, _ c: CGPoint, r: Double) -> Path {+ roundedPolygon([a, b, c], r: r)+ }++ /// A polygon with every corner rounded (radius r).+ private func roundedPolygon(_ pts: [CGPoint], r: Double) -> Path {+ let n = pts.count+ var p = Path()+ for i in 0..<n {+ let pt = pts[i], prev = pts[(i + n - 1) % n], next = pts[(i + 1) % n]+ func toward(_ q: CGPoint) -> CGPoint {+ let dx = q.x - pt.x, dy = q.y - pt.y, l = max(0.001, (dx * dx + dy * dy).squareRoot())+ return CGPoint(x: pt.x + dx / l * r, y: pt.y + dy / l * r)+ }+ let s = toward(prev), e = toward(next)+ if i == 0 { p.move(to: s) } else { p.addLine(to: s) }+ p.addQuadCurve(to: e, control: pt)+ }+ p.closeSubpath()+ return p+ }++ /// A soft four-point star centred on (0, 0).+ private func fourPointStar(r: CGFloat) -> Path {+ var p = Path()+ let w = r * 0.32+ p.move(to: CGPoint(x: 0, y: -r))+ p.addQuadCurve(to: CGPoint(x: r, y: 0), control: CGPoint(x: w, y: -w))+ p.addQuadCurve(to: CGPoint(x: 0, y: r), control: CGPoint(x: w, y: w))+ p.addQuadCurve(to: CGPoint(x: -r, y: 0), control: CGPoint(x: -w, y: w))+ p.addQuadCurve(to: CGPoint(x: 0, y: -r), control: CGPoint(x: -w, y: -w))+ return p+ }++ private func heartShape(r: Double) -> Path {+ var p = Path()+ p.move(to: CGPoint(x: 0, y: r * 0.9))+ p.addCurve(to: CGPoint(x: -r, y: -r * 0.2), control1: CGPoint(x: -r * 0.3, y: r * 0.55), control2: CGPoint(x: -r, y: r * 0.3))+ p.addCurve(to: CGPoint(x: 0, y: -r * 0.45), control1: CGPoint(x: -r, y: -r * 0.85), control2: CGPoint(x: -r * 0.15, y: -r * 0.9))+ p.addCurve(to: CGPoint(x: r, y: -r * 0.2), control1: CGPoint(x: r * 0.15, y: -r * 0.9), control2: CGPoint(x: r, y: -r * 0.85))+ p.addCurve(to: CGPoint(x: 0, y: r * 0.9), control1: CGPoint(x: r, y: r * 0.3), control2: CGPoint(x: r * 0.3, y: r * 0.55))+ p.closeSubpath()+ return p+ }+}diff --git a/demo/chef.gif b/demo/chef.gifnew file mode 100644index 0000000..843bb1dBinary files /dev/null and b/demo/chef.gif differdiff --git a/demo/chef.mp4 b/demo/chef.mp4new file mode 100644index 0000000..15148d2Binary files /dev/null and b/demo/chef.mp4 differdiff --git a/demo/demo.gif b/demo/demo.gifdeleted file mode 100644index cf78878..0000000Binary files a/demo/demo.gif and /dev/null differdiff --git a/demo/demo.mp4 b/demo/demo.mp4deleted file mode 100644index e7d50cd..0000000Binary files a/demo/demo.mp4 and /dev/null differdiff --git a/demo/garden.gif b/demo/garden.gifnew file mode 100644index 0000000..83b1ba0Binary files /dev/null and b/demo/garden.gif differdiff --git a/demo/garden.mp4 b/demo/garden.mp4new file mode 100644index 0000000..eadf9e8Binary files /dev/null and b/demo/garden.mp4 differdiff --git a/demo/render.swift b/demo/render.swiftindex 327e943..506c757 100644--- a/demo/render.swift+++ b/demo/render.swift@@ -2,7 +2,7 @@//// swiftc -parse-as-library -target arm64-apple-macos14 \// templates/swiftui/Motion.swift templates/swiftui/LiveFace.swift demo/*.swift -o /tmp/render-demo-// /tmp/render-demo reveal /tmp/frames # or: orbit, room+// /tmp/render-demo reveal /tmp/frames # or: garden, chef, orbit// /tmp/render-demo reveal /tmp/frames phone # inside an iPhone, on a square page// /tmp/render-demo reveal /tmp/frames phone fps=60 # smoother, for posting// /tmp/render-demo reveal /tmp/frames phone 0 40 # only these frames (to check before a full render)@@ -17,8 +17,26 @@ func scene(_ name: String, _ t: Double, phone: Bool) -> (view: AnyView, loopMs:switch name {case "orbit":return (AnyView(OrbitScene(t: t)), OrbitScene.loopMs)- case "room":- return (AnyView(DemoScene(t: t)), DemoScene.loopMs)+ case "garden":+ guard phone else { return (AnyView(GardenScene(t: t)), GardenScene.loopMs) }+ // A little wider than the reveal, so the girl, the pot and the flower all fit; feet at 73%.+ let screen = PhoneMockup<EmptyView>.screen+ let k = screen.width / 720 * 1.8+ return (AnyView(PhoneMockup(darkStatus: true) {+ GardenScene(t: t, height: screen.height / k, stageTop: screen.height * 0.73 / k - 620)+ .scaleEffect(k, anchor: .topLeading)+ .offset(x: -(720 * k - screen.width) / 2)+ }), GardenScene.loopMs)+ case "chef":+ guard phone else { return (AnyView(ChefScene(t: t)), ChefScene.loopMs) }+ // Close on the chef: the counter hides the legs, so the stage sits low and the head is big.+ let screen = PhoneMockup<EmptyView>.screen+ let k = screen.width / 720 * 2.35+ return (AnyView(PhoneMockup(darkStatus: true) {+ ChefScene(t: t, height: screen.height / k, stageTop: 82)+ .scaleEffect(k, anchor: .topLeading)+ .offset(x: -(720 * k - screen.width) / 2)+ }), ChefScene.loopMs)default:guard phone else { return (AnyView(RevealScene(t: t)), RevealScene.loopMs) }// The 720-wide scene, as big as the screen allows, with the feet at 73% of the screen.