interplane.io
Source of interplane.io: a Rust generator that renders the page from the protocol repository's public record, deployed hourly from the Spark.
Brought by drakestapleton
interplane.io / public / hop-background-render.swift
| 1 | import AppKit |
| 2 | import Foundation |
| 3 | enum Spirit { static let framesPerSecond = 20.0 } |
| 4 | struct SpiritStar: Equatable { |
| 5 | var x: Double, y: Double // 0 ..< 1 |
| 6 | var size: Double // radius in points |
| 7 | var phase: Double, rate: Double // twinkle |
| 8 | var drift: Double // points a second, to the left |
| 9 | } |
| 10 | |
| 11 | enum SpiritStars { |
| 12 | nonisolated static let count = 110 |
| 13 | /// The same stars every time: made from a fixed number, not from the clock, so two pictures of the same |
| 14 | /// moment are the same picture. |
| 15 | nonisolated static func make(seed: UInt64 = 0x484F_5053_5049_5249, count: Int = SpiritStars.count) -> [SpiritStar] { |
| 16 | var s = seed |
| 17 | func next() -> Double { // SplitMix64, as a share of one |
| 18 | s &+= 0x9E37_79B9_7F4A_7C15 |
| 19 | var z = s |
| 20 | z = (z ^ (z >> 30)) &* 0xBF58_476D_1CE4_E5B9 |
| 21 | z = (z ^ (z >> 27)) &* 0x94D0_49BB_1331_11EB |
| 22 | z ^= z >> 31 |
| 23 | return Double(z >> 11) / Double(1 << 53) |
| 24 | } |
| 25 | return (0..<count).map { _ in |
| 26 | SpiritStar(x: next(), y: next(), size: 0.45 + 0.85 * next() * next(), phase: next() * 2 * .pi, |
| 27 | rate: 0.25 + 0.9 * next(), drift: 0.4 + 2.2 * next()) |
| 28 | } |
| 29 | } |
| 30 | nonisolated static let all = make() |
| 31 | } |
| 32 | |
| 33 | /// Everything that moves is drawn here with plain Core Graphics calls, so `--selftest` can draw the same frames |
| 34 | /// off screen and time them. |
| 35 | enum SpiritPaint { |
| 36 | private nonisolated static let white = CGColor(red: 1, green: 1, blue: 1, alpha: 1) |
| 37 | private nonisolated static let accent = CGColor(red: 99 / 255, green: 179 / 255, blue: 237 / 255, alpha: 1) |
| 38 | |
| 39 | /// Small white stars at 30 to 60 percent, drifting slowly to the left and coming back in on the right. |
| 40 | nonisolated static func stars(_ cg: CGContext, size: CGSize, t: Double, stars: [SpiritStar] = SpiritStars.all) { |
| 41 | guard size.width > 0, size.height > 0 else { return } |
| 42 | for s in stars { |
| 43 | var x = (s.x * size.width - t * s.drift).truncatingRemainder(dividingBy: size.width) |
| 44 | if x < 0 { x += size.width } |
| 45 | let a = 0.30 + 0.30 * (0.5 + 0.5 * sin(t * s.rate + s.phase)) |
| 46 | cg.setFillColor(white.copy(alpha: a) ?? white) |
| 47 | cg.fillEllipse(in: CGRect(x: x - s.size, y: s.y * size.height - s.size, width: s.size * 2, height: s.size * 2)) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /// How strong a ripple is at a share `u` of the way out: 22 percent at the point, 4 percent at the rim, and |
| 52 | /// gone over the last tenth so a ripple does not vanish with a jump. |
| 53 | nonisolated static func rippleStrength(_ u: Double) -> Double { |
| 54 | let v = min(max(u, 0), 1) |
| 55 | let base = 0.04 + 0.18 * (1 - v) * (1 - v) |
| 56 | return v > 0.9 ? base * (1 - v) / 0.1 : base |
| 57 | } |
| 58 | |
| 59 | /// Stylized gravitational waves from a binary system, not a physical simulation. |
| 60 | /// The two-lobed deformation follows the retarded orbital phase as each ring expands. |
| 61 | nonisolated static func ripples(_ cg: CGContext, center: CGPoint, reach: CGFloat, t: Double, rings: Int = 6) { |
| 62 | guard reach > 0, rings > 0 else { return } |
| 63 | cg.saveGState() |
| 64 | defer { cg.restoreGState() } |
| 65 | cg.setLineWidth(0.8) |
| 66 | for k in 0..<rings { |
| 67 | var u = (t / 18 + Double(k) / Double(rings)).truncatingRemainder(dividingBy: 1) |
| 68 | if u < 0 { u += 1 } |
| 69 | let radius = (0.10 + 0.90 * u) * reach |
| 70 | let phase = (t - u * 18) * 2 * Double.pi / 18 |
| 71 | let strength = rippleStrength(u) * min(1, u * 12) |
| 72 | cg.setStrokeColor(accent.copy(alpha: strength) ?? accent) |
| 73 | let path = CGMutablePath() |
| 74 | for sample in 0...96 { |
| 75 | let angle = Double(sample) * 2 * Double.pi / 96 |
| 76 | let r = radius * (1 + 0.045 * cos(2 * (angle - phase))) |
| 77 | let p = CGPoint(x: center.x + cos(angle) * r, y: center.y + sin(angle) * r * 0.76) |
| 78 | if sample == 0 { path.move(to: p) } else { path.addLine(to: p) } |
| 79 | } |
| 80 | path.closeSubpath() |
| 81 | cg.addPath(path) |
| 82 | cg.strokePath() |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /// Two dark horizons orbit their common centre, with steady photon rings and accretion light. |
| 87 | nonisolated static func core(_ cg: CGContext, center: CGPoint, radius: CGFloat, t: Double) { |
| 88 | guard radius > 0 else { return } |
| 89 | let phase = t * 2 * Double.pi / 18 |
| 90 | let orbit = radius * 5.8 |
| 91 | // Draw the farther horizon first; apparent size subtly follows depth, never a brightness pulse. |
| 92 | let phases = [phase, phase + Double.pi].sorted { sin($0) < sin($1) } |
| 93 | for angle in phases { |
| 94 | let r = radius * (1.45 + 0.12 * sin(angle)) |
| 95 | let c = CGPoint(x: center.x + cos(angle) * orbit, y: center.y + sin(angle) * orbit * 0.60) |
| 96 | let warm = CGColor(red: 1, green: 0.73, blue: 0.42, alpha: 0.85) |
| 97 | cg.saveGState() |
| 98 | cg.setShadow(offset: .zero, blur: r * 1.1, color: warm.copy(alpha: 0.45)) |
| 99 | cg.setStrokeColor(warm) |
| 100 | cg.setLineWidth(max(0.65, r * 0.14)) |
| 101 | cg.strokeEllipse(in: CGRect(x: c.x - r * 2.4, y: c.y - r * 0.62, width: r * 4.8, height: r * 1.24)) |
| 102 | cg.setShadow(offset: .zero, blur: 0, color: nil) |
| 103 | cg.setFillColor(CGColor(red: 0.005, green: 0.005, blue: 0.009, alpha: 1)) |
| 104 | cg.fillEllipse(in: CGRect(x: c.x - r, y: c.y - r, width: r * 2, height: r * 2)) |
| 105 | cg.setStrokeColor(CGColor(red: 1, green: 0.88, blue: 0.67, alpha: 0.95)) |
| 106 | cg.setLineWidth(max(0.55, r * 0.10)) |
| 107 | cg.strokeEllipse(in: CGRect(x: c.x - r, y: c.y - r, width: r * 2, height: r * 2)) |
| 108 | cg.restoreGState() |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /// One whole frame of the home screen: the sky, gravitational waves and orbiting horizons. |
| 113 | nonisolated static func frame(_ cg: CGContext, size: CGSize, t: Double) { |
| 114 | stars(cg, size: size, t: t) |
| 115 | let c = CGPoint(x: size.width / 2, y: size.height * 0.4) |
| 116 | ripples(cg, center: c, reach: max(size.width, size.height) * 0.6, t: t) |
| 117 | core(cg, center: c, radius: 5, t: t) |
| 118 | } |
| 119 | |
| 120 | /// Draws `frames` whole frames into an off-screen picture of a window at twice the points (a Retina screen) |
| 121 | /// and returns the average milliseconds of processor time a frame. Processor time of this thread, not time on |
| 122 | /// the clock: a busy Mac makes the clock run long without the drawing costing more. Used by `--selftest`. |
| 123 | nonisolated static func averageFrameMilliseconds(size: CGSize = CGSize(width: 820, height: 520), frames: Int = 60) -> Double? { |
| 124 | let w = Int(size.width * 2), h = Int(size.height * 2) |
| 125 | guard frames > 0, let cg = CGContext(data: nil, width: w, height: h, bitsPerComponent: 8, bytesPerRow: 0, |
| 126 | space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else { return nil } |
| 127 | cg.scaleBy(x: 2, y: 2) |
| 128 | for k in 0..<3 { frame(cg, size: size, t: Double(k)) } // first use of the drawing code is not counted |
| 129 | let start = clock_gettime_nsec_np(CLOCK_THREAD_CPUTIME_ID) |
| 130 | for k in 0..<frames { |
| 131 | cg.clear(CGRect(origin: .zero, size: size)) |
| 132 | frame(cg, size: size, t: 10 + Double(k) / Spirit.framesPerSecond) |
| 133 | } |
| 134 | return Double(clock_gettime_nsec_np(CLOCK_THREAD_CPUTIME_ID) - start) / 1_000_000 / Double(frames) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | struct SpiritClock { |
| 139 | let showing: Bool |
| 140 | let reduceMotion: Bool |
| 141 | var still: Bool { !showing || reduceMotion || SpiritClock.testing } |
| 142 | static let testing = CommandLine.arguments.contains("--uitest") || CommandLine.arguments.contains("--selftest") |
| 143 | /// The moment drawn when nothing moves. |
| 144 | static let restMoment = 12.0 |
| 145 | func moment(_ date: Date) -> Double { still ? Self.restMoment : date.timeIntervalSinceReferenceDate.truncatingRemainder(dividingBy: 86_400) } |
| 146 | } |
| 147 | |
| 148 | for (index, time) in [0.0, 4.5].enumerated() { |
| 149 | let size = CGSize(width: 820, height: 520) |
| 150 | let cg = CGContext(data: nil, width: 1640, height: 1040, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)! |
| 151 | cg.scaleBy(x: 2, y: 2) |
| 152 | cg.setFillColor(CGColor(red: 0.039, green: 0.039, blue: 0.059, alpha: 1)) |
| 153 | cg.fill(CGRect(origin: .zero, size: size)) |
| 154 | SpiritPaint.frame(cg, size: size, t: time) |
| 155 | let image = NSBitmapImageRep(cgImage: cg.makeImage()!) |
| 156 | try image.representation(using: .png, properties: [:])!.write(to: URL(fileURLWithPath: "build/black-holes-\(index).png")) |
| 157 | } |
| 158 | |
| 159 | let paused = SpiritClock(showing: true, reduceMotion: true) |
| 160 | precondition(paused.still && paused.moment(Date(timeIntervalSince1970: 0)) == paused.moment(Date(timeIntervalSince1970: 30))) |
| 161 | precondition(SpiritClock(showing: false, reduceMotion: false).still) |
| 162 | print("PASS: Reduce Motion and hidden window freeze the clock") |
| 163 | if let ms = SpiritPaint.averageFrameMilliseconds() { print("Frame CPU milliseconds: \(ms)"); precondition(ms < 16) } |