krz/daybyday

iOS learning games for young children: days, months, seasons, numbers, letters, animals and more. education ios kids swift https://krz.sh/apps/daybyday/

DayByDay/Quiz/AnimalIcon.swift

main
daybyday/DayByDay/Quiz/AnimalIcon.swift history · blame · raw

140 lines · 5243 bytes

12 symbols in this file
  1//
  2//  AnimalIcon.swift
  3//  DayByDay
  4//
  5
  6import SwiftUI
  7
  8/// An animal's SF Symbol, or a drawn silhouette for animals SF Symbols lacks.
  9/// Both take the current foreground style. Size symbols with `.font` and
 10/// drawings with `.frame`.
 11struct AnimalIcon: View {
 12    let animal: LearnAnimal
 13
 14    var body: some View {
 15        if let symbol = animal.symbol {
 16            Image(systemName: symbol)
 17                .symbolRenderingMode(.monochrome)
 18        } else {
 19            Canvas { context, size in
 20                // 88% so drawings sit at the same visual weight as the symbols.
 21                let scale = min(size.width, size.height) / 100 * 0.88
 22                context.translateBy(x: (size.width - 100 * scale) / 2, y: (size.height - 100 * scale) / 2)
 23                context.scaleBy(x: scale, y: scale)
 24                AnimalDrawing.draw(animal, in: &context)
 25            }
 26            .accessibilityHidden(true)
 27        }
 28    }
 29}
 30
 31/// Silhouettes in a 100×100 space. `fill` adds shape, `cut` removes it, so
 32/// details show whatever is behind the icon.
 33private enum AnimalDrawing {
 34    static func draw(_ animal: LearnAnimal, in context: inout GraphicsContext) {
 35        switch animal {
 36        case .pig:   pig(&context)
 37        case .cow:   cow(&context)
 38        case .sheep: sheep(&context)
 39        case .duck:  duck(&context)
 40        default:     break
 41        }
 42    }
 43
 44    private static func fill(_ context: inout GraphicsContext, _ path: Path) {
 45        context.blendMode = .normal
 46        context.fill(path, with: .foreground)
 47    }
 48
 49    private static func cut(_ context: inout GraphicsContext, _ path: Path) {
 50        context.blendMode = .destinationOut
 51        context.fill(path, with: .color(.black))
 52    }
 53
 54    private static func ellipse(_ x: Double, _ y: Double, _ w: Double, _ h: Double) -> Path {
 55        Path(ellipseIn: CGRect(x: x, y: y, width: w, height: h))
 56    }
 57
 58    private static func circle(_ cx: Double, _ cy: Double, _ r: Double) -> Path {
 59        ellipse(cx - r, cy - r, 2 * r, 2 * r)
 60    }
 61
 62    private static func polygon(_ points: [(Double, Double)]) -> Path {
 63        var path = Path()
 64        path.addLines(points.map { CGPoint(x: $0.0, y: $0.1) })
 65        path.closeSubpath()
 66        return path
 67    }
 68
 69    /// Front face: floppy ears folded forward, round head, big snout with nostrils.
 70    private static func pig(_ c: inout GraphicsContext) {
 71        fill(&c, ellipse(10, 18, 80, 70))
 72        cut(&c, polygon([(12, 20), (46, 16), (26, 50)]))
 73        fill(&c, polygon([(16, 23), (42, 20), (26, 45)]))
 74        cut(&c, polygon([(88, 20), (54, 16), (74, 50)]))
 75        fill(&c, polygon([(84, 23), (58, 20), (74, 45)]))
 76        cut(&c, circle(40, 50, 5))
 77        cut(&c, circle(60, 50, 5))
 78        cut(&c, ellipse(27, 55, 46, 31))
 79        fill(&c, ellipse(31, 59, 38, 23))
 80        cut(&c, ellipse(39, 64, 8, 13))
 81        cut(&c, ellipse(53, 64, 8, 13))
 82    }
 83
 84    /// Front face: horns, side ears, a spot, and a wide muzzle with nostrils.
 85    private static func cow(_ c: inout GraphicsContext) {
 86        fill(&c, polygon([(30, 22), (20, 2), (40, 16)]))
 87        fill(&c, polygon([(70, 22), (80, 2), (60, 16)]))
 88        fill(&c, ellipse(1, 27, 30, 14))
 89        fill(&c, ellipse(69, 27, 30, 14))
 90        fill(&c, Path(roundedRect: CGRect(x: 23, y: 12, width: 54, height: 62), cornerRadius: 22))
 91        cut(&c, ellipse(30, 18, 16, 13))
 92        cut(&c, circle(39, 40, 5))
 93        cut(&c, circle(61, 40, 5))
 94        cut(&c, ellipse(15, 56, 70, 42))
 95        fill(&c, ellipse(19, 60, 62, 34))
 96        cut(&c, ellipse(33, 70, 9, 11))
 97        cut(&c, ellipse(58, 70, 9, 11))
 98    }
 99
100    /// Side view facing right: woolly cloud body, four legs, head and ear.
101    private static func sheep(_ c: inout GraphicsContext) {
102        for x in [24.0, 36, 56, 68] {
103            fill(&c, Path(roundedRect: CGRect(x: x, y: 64, width: 8, height: 28), cornerRadius: 4))
104        }
105        fill(&c, ellipse(16, 36, 62, 32))
106        for (x, y) in [(22.0, 50.0), (30, 38), (44, 33), (58, 34), (70, 42), (68, 58), (54, 63), (38, 63), (24, 60)] {
107            fill(&c, circle(x, y, 12))
108        }
109        cut(&c, ellipse(66, 20, 32, 40))
110        fill(&c, ellipse(70, 24, 24, 32))
111        fill(&c, ellipse(62, 28, 16, 8))
112        cut(&c, circle(84, 36, 3.5))
113    }
114
115    /// Side view facing right: body with raised tail, round head, bill, wing.
116    private static func duck(_ c: inout GraphicsContext) {
117        fill(&c, polygon([(18, 64), (4, 40), (32, 56)]))
118        fill(&c, ellipse(10, 48, 74, 38))
119        fill(&c, ellipse(58, 30, 28, 32))
120        fill(&c, circle(70, 32, 16))
121        fill(&c, Path(roundedRect: CGRect(x: 80, y: 30, width: 19, height: 11), cornerRadius: 5))
122        cut(&c, circle(74, 27, 3.5))
123        cut(&c, ellipse(30, 56, 40, 18))
124        fill(&c, ellipse(30, 60, 40, 18))
125    }
126}
127
128#Preview {
129    HStack(spacing: 24) {
130        ForEach([LearnAnimal.cow, .pig, .sheep, .duck, .cat]) { animal in
131            AnimalIcon(animal: animal)
132                .font(.system(size: 64))
133                .frame(width: 80, height: 80)
134                .foregroundStyle(.white)
135                .padding()
136                .background(RoundedRectangle(cornerRadius: 24).fill(Color.brown))
137        }
138    }
139    .padding()
140}