| new file mode 100644 |
| index 0000000..eee3179 |
| --- /dev/null |
| +++ b/internal/home/wisp/wisp.go |
| @@ -0,0 +1,373 @@ |
| +// Package wisp draws the front page's wisps: a filament from the hero's |
| +// nebula that drifts across the screen at each section heading — in from |
| +// off the left edge, round the heading, and away off the right edge. |
| +// |
| +// A wisp is smoke, not string: a wide soft glow, a brighter body, and four |
| +// to six fine fibres braiding through them, every one a filled outline |
| +// that thickens in the middle and thins to nothing at both ends, blurred |
| +// by an SVG filter. Each heading has its own variant, so no two match. |
| +// |
| +// Everything here is drawn once, into one SVG sprite of symbols (Sprite), |
| +// set into the page itself; each heading's wisp points into it (Markup). |
| +// Inline, not a separate cached file: Safari draws symbols from another |
| +// file but drops their filters, so every blur vanished and the wisps |
| +// turned into hard-edged bands. |
| +// The motion is CSS (the front page's home.css): each wisp is cut into |
| +// seven overlapping stretches, and the stretches are faded in and out in |
| +// turn, so only opacity and transform ever animate — composited, never |
| +// repainted. |
| +// |
| +// The variants, their braids and their glow widths were tuned by eye in |
| +// the design mock-ups (design-directions/wisp, build.py) and are carried |
| +// here as fixed numbers, so the page draws exactly what was approved. |
| +package wisp |
| + |
| +import ( |
| + "fmt" |
| + "math" |
| + "strings" |
| + "sync" |
| +) |
| + |
| +type pt struct{ x, y float64 } |
| + |
| +// fibre is one strand: half-width, how far it wanders from the centre |
| +// line, how many times it wanders along a stretch, its phase, its opacity. |
| +type fibre struct{ hw, amp, cycles, phase, opacity float64 } |
| + |
| +type variant struct { |
| + name string |
| + loop []pt // round the heading, top-left corner to bottom-right |
| + enter, leave float64 // the tails' heights at the screen's edges |
| + stray string // "in" or "out": a strand that peels away there |
| + shift int // a head start or lag, in twentieths of a second |
| + fibres []fibre |
| + glowBody [7][2]float64 // per stretch: the glow's and the body's half-widths |
| +} |
| + |
| +// The boxes, in viewBox units. The tails' boxes are pixel-sized (home.css), |
| +// so their widths and blurs are true; the loop's follows the heading, and |
| +// is shaped like a typical heading's to stay near 1:1. |
| +var ( |
| + loopBox = pt{1000, 190} |
| + inBox = pt{1400, 90} |
| + outBox = pt{1600, 160} |
| +) |
| + |
| +const sx, sy = 0.71, 0.6 // a typical heading's loop box, px per viewBox unit |
| + |
| +var variants = []variant{ |
| + { |
| + name: "dip-and-rise", enter: 10, leave: 70, stray: "out", shift: -2, |
| + loop: []pt{{0, 0}, {60, 70}, {180, 146}, {380, 160}, {560, 136}, {700, 70}, {820, 22}, {930, 30}, {985, 90}, {992, 150}, {1000, 190}}, |
| + fibres: []fibre{ |
| + {0.9194453711499528, 8.998041044693247, 2.874923332911433, 2.8465436197772616, 0.5108623216941499}, |
| + {0.5139412187844232, 8.62730218904734, 2.141831766901303, 3.867940654573994, 0.33026689511100454}, |
| + {0.667955891654199, 3.992565904816193, 2.2928644205864286, 5.609397099678683, 0.4513022513785041}, |
| + {0.7572619035044018, 5.773119835207644, 2.0871876773939584, 4.655170044884421, 0.4555191629567996}, |
| + {0.7738770709112319, 8.821911377449847, 1.1515958081403572, 0.22461251689726125, 0.5174825425842748}, |
| + }, |
| + glowBody: [7][2]float64{{17.869030417162957, 4.9162485283850055}, {14.149368203441966, 5.49862163374645}, {16.382816316208235, 3.763774181234161}, {14.40762965341738, 4.449011440369359}, {13.523289678955368, 5.389633287936679}, {16.58480447629008, 3.6753077101062317}, {14.742083403838693, 4.16554875761766}}, |
| + }, |
| + { |
| + name: "curl-and-tuck", enter: 2, leave: 150, stray: "", shift: 0, |
| + loop: []pt{{0, 0}, {130, 11}, {330, 29}, {560, 28}, {790, 23}, {930, 45}, {985, 90}, {950, 138}, {860, 164}, {770, 170}, {690, 162}, {668, 142}, {700, 124}, {790, 128}, {890, 157}, {1000, 190}}, |
| + fibres: []fibre{ |
| + {0.9354600064291476, 3.5848547378147853, 2.4208653445257644, 2.6696086718802565, 0.4231237627318618}, |
| + {0.47818176474848834, 4.343980102775049, 2.066976577295231, 1.3925467908904803, 0.4028588988888835}, |
| + {0.4148601804937411, 3.600418603933758, 2.704019680587784, 2.653864678169423, 0.41841115958593617}, |
| + {0.8405254472700974, 5.512974518503398, 1.1381308711581244, 4.933544843811176, 0.43906334034844136}, |
| + {0.7947476260599373, 7.346940185966469, 3.329764546189497, 2.2855928664120833, 0.4851423853057534}, |
| + }, |
| + glowBody: [7][2]float64{{14.816383634374912, 4.10230589380269}, {17.602132304066274, 3.9399286575166172}, {17.10815160293447, 4.935768143165508}, {12.729796173810602, 4.061209069036683}, {15.324653028784795, 4.408407069531941}, {13.101867305629467, 4.781556267720861}, {14.897329062202902, 3.7080779085553335}}, |
| + }, |
| + { |
| + name: "weave", enter: 70, leave: 110, stray: "in", shift: 3, |
| + loop: []pt{{0, 0}, {140, 8}, {320, 22}, {480, 60}, {600, 130}, {720, 166}, {860, 163}, {950, 130}, {985, 80}, {960, 40}, {905, 55}, {910, 110}, {955, 160}, {1000, 190}}, |
| + fibres: []fibre{ |
| + {0.7646540452164527, 9.547194587623435, 2.5970387609348276, 4.704879126382371, 0.4512953282077222}, |
| + {0.4212651593880562, 9.886099924032456, 2.050580654049252, 4.971400029804536, 0.5452772187024093}, |
| + {0.6741132345825509, 8.568762995105025, 3.220649827335587, 6.1303199316384545, 0.3942718353111067}, |
| + {0.4866386635286334, 8.601256525507507, 3.1669797808831204, 0.3119220768919083, 0.3915388761482478}, |
| + {0.7262013746287792, 9.50791886097006, 2.5557540006331125, 2.7238139242633035, 0.43870357403091026}, |
| + {0.6196963592135507, 7.936277061786937, 3.3290310925374613, 5.0846841060964705, 0.4472378279833185}, |
| + }, |
| + glowBody: [7][2]float64{{16.604840845882713, 5.2391856808423665}, {12.592720425674786, 4.304067940469753}, {17.672557786849104, 5.178733466382322}, {12.497712599953376, 4.175850193465255}, {16.601847608666407, 3.9224792350675743}, {12.652377775290686, 3.6916157842398505}, {14.788069760959797, 3.6297593927463465}}, |
| + }, |
| + { |
| + name: "high-loop", enter: 0, leave: 130, stray: "", shift: -3, |
| + loop: []pt{{0, 0}, {150, 20}, {380, 30}, {600, 20}, {760, 10}, {860, 25}, {900, 70}, {860, 105}, {800, 90}, {810, 50}, {880, 40}, {960, 80}, {990, 140}, {1000, 190}}, |
| + fibres: []fibre{ |
| + {0.5993348683299982, 9.948204816596634, 3.1648134335368887, 2.4294673162774427, 0.5220313359523998}, |
| + {0.5700746516275085, 4.937503477072778, 3.0447122108630693, 4.808496248211682, 0.28251861595307726}, |
| + {0.5494424789916619, 9.984907648526773, 1.0440009314905614, 5.977468202090347, 0.546399601403292}, |
| + {0.9879825669072703, 5.232189209554907, 1.6180315015552376, 4.3545818277513435, 0.437622581094986}, |
| + {0.47189123385093046, 7.114289553694752, 2.0382338082830613, 1.7755670934810406, 0.4784041344991759}, |
| + }, |
| + glowBody: [7][2]float64{{15.68682257205295, 4.012732175493016}, {16.861213916195993, 5.0477096674663064}, {15.950982475061341, 4.5222458926817986}, {14.789900563204064, 4.9256848713644885}, {16.213590003158465, 5.299547822380571}, {15.145140951103027, 3.7795061572239073}, {14.112489128169598, 4.9404515620651654}}, |
| + }, |
| + { |
| + name: "open-arc", enter: 60, leave: 40, stray: "out", shift: 2, |
| + loop: []pt{{0, 0}, {200, 10}, {450, 15}, {700, 22}, {880, 50}, {960, 100}, {985, 150}, {1000, 190}}, |
| + fibres: []fibre{ |
| + {0.9119236047125096, 4.5123737990514226, 2.2049529977692024, 3.0409836970491866, 0.47383438922875487}, |
| + {0.616423438789726, 6.1824945442097405, 1.0749177072916019, 5.853237387774827, 0.5260559016891486}, |
| + {0.9865271121128315, 5.40633452853273, 1.3176759230076913, 5.549511542002782, 0.29514103670190284}, |
| + {0.4281286583553424, 4.603771083700763, 1.4756945472147724, 0.7971407374043049, 0.37482430060979605}, |
| + {0.8592105298356515, 8.96374100616438, 2.2277593373108653, 2.5403834774078455, 0.5341465616589269}, |
| + {0.599372817330031, 4.072545257460819, 2.8853556813707173, 3.4841196648727495, 0.38451654723767104}, |
| + }, |
| + glowBody: [7][2]float64{{12.672113021893889, 4.645730216648505}, {14.876703440749925, 3.6679764311793965}, {15.31219574248183, 4.3980766510654945}, {13.956458933133801, 4.010995329861434}, {15.886388361749637, 4.326108843030688}, {16.55489732524959, 4.033458698025226}, {12.745203181764076, 3.6941444767458096}}, |
| + }, |
| +} |
| + |
| +// Count is how many variants there are: one per section heading. |
| +var Count = len(variants) |
| + |
| +// stretches are the overlapping lengths of each piece, in the order they |
| +// light: piece, start, length (fractions of the piece's length). |
| +var stretches = []struct { |
| + piece string |
| + start, length float64 |
| +}{ |
| + {"in", 0, 0.62}, {"in", 0.38, 0.62}, {"loop", 0, 0.42}, {"loop", 0.3, 0.42}, |
| + {"loop", 0.6, 0.4}, {"out", 0, 0.62}, {"out", 0.38, 0.62}, |
| +} |
| + |
| +// Stretches is how many stretches each wisp has. |
| +var Stretches = len(stretches) |
| + |
| +// ── Geometry ──────────────────────────────────────────────────────────── |
| + |
| +type bez struct{ p1, c1, c2, p2 pt } |
| + |
| +// chain is Catmull-Rom through pts, as cubic Bezier segments. |
| +func chain(pts []pt) []bez { |
| + var segs []bez |
| + n := len(pts) |
| + for i := 0; i < n-1; i++ { |
| + p0, p1, p2, p3 := pts[max(i-1, 0)], pts[i], pts[i+1], pts[min(i+2, n-1)] |
| + segs = append(segs, bez{p1, |
| + pt{p1.x + (p2.x-p0.x)/6, p1.y + (p2.y-p0.y)/6}, |
| + pt{p2.x - (p3.x-p1.x)/6, p2.y - (p3.y-p1.y)/6}, p2}) |
| + } |
| + return segs |
| +} |
| + |
| +type curve struct { |
| + pts []pt |
| + acc []float64 // cumulative arc length at each point |
| +} |
| + |
| +// sample is dense points along the curve through pts, with arc length. |
| +func sample(pts []pt) curve { |
| + per := 600 / (len(pts) - 1) |
| + var out []pt |
| + for _, s := range chain(pts) { |
| + for k := 0; k < per; k++ { |
| + t := float64(k) / float64(per) |
| + u := 1 - t |
| + out = append(out, pt{ |
| + u*u*u*s.p1.x + 3*u*u*t*s.c1.x + 3*u*t*t*s.c2.x + t*t*t*s.p2.x, |
| + u*u*u*s.p1.y + 3*u*u*t*s.c1.y + 3*u*t*t*s.c2.y + t*t*t*s.p2.y}) |
| + } |
| + } |
| + out = append(out, pts[len(pts)-1]) |
| + acc := []float64{0} |
| + for i := 1; i < len(out); i++ { |
| + acc = append(acc, acc[i-1]+math.Hypot(out[i].x-out[i-1].x, out[i].y-out[i-1].y)) |
| + } |
| + return curve{out, acc} |
| +} |
| + |
| +// station is a point on a stretch: where it is, the unit normal there, |
| +// and the radius of the turn the curve is making. |
| +type station struct{ x, y, nx, ny, radius float64 } |
| + |
| +// stretchLine is k stations evenly along [start, start+length] of c. |
| +func stretchLine(c curve, start, length float64, k int) []station { |
| + total := c.acc[len(c.acc)-1] |
| + res := make([]station, k) |
| + j := 0 |
| + for i := 0; i < k; i++ { |
| + s := (start + length*float64(i)/float64(k-1)) * total |
| + for j < len(c.acc)-2 && c.acc[j+1] < s { |
| + j++ |
| + } |
| + f := (s - c.acc[j]) / math.Max(c.acc[j+1]-c.acc[j], 1e-9) |
| + a, b := c.pts[j], c.pts[j+1] |
| + dx, dy := b.x-a.x, b.y-a.y |
| + m := math.Hypot(dx, dy) |
| + if m == 0 { |
| + m = 1 |
| + } |
| + res[i] = station{a.x + dx*f, a.y + dy*f, -dy / m, dx / m, 0} |
| + } |
| + // The local radius of the turn, from how fast the direction changes; |
| + // ribbon keeps every outline inside it. |
| + step := length * total / float64(k-1) |
| + for i := range res { |
| + a, b := res[max(i-1, 0)], res[min(i+1, k-1)] |
| + turn := math.Abs(math.Atan2(a.nx*b.ny-a.ny*b.nx, a.nx*b.nx+a.ny*b.ny)) |
| + span := step * float64(min(i+1, k-1)-max(i-1, 0)) |
| + res[i].radius = 1e9 |
| + if turn > 1e-6 { |
| + res[i].radius = span / turn |
| + } |
| + } |
| + return res |
| +} |
| + |
| +// ribbon is a filled outline round line, its half-width and sideways |
| +// offset varying along it, as path data in relative whole-unit steps. |
| +func ribbon(line []station, halfWidth, offset func(u float64) float64) string { |
| + k := len(line) |
| + left := make([]pt, k) |
| + right := make([]pt, k) |
| + for i, s := range line { |
| + u := float64(i) / float64(k-1) |
| + w, o := halfWidth(u), offset(u) |
| + // In a turn tighter than the shape is wide, the outline would fold |
| + // back over itself into a hard corner: shrink it to fit the turn. |
| + fit := math.Min(1, 0.75*s.radius/(math.Abs(o)+w+1e-6)) |
| + w, o = w*fit, o*fit |
| + left[i] = pt{s.x + s.nx*(o+w), s.y + s.ny*(o+w)} |
| + right[i] = pt{s.x + s.nx*(o-w), s.y + s.ny*(o-w)} |
| + } |
| + ring := left |
| + for i := k - 1; i >= 0; i-- { |
| + ring = append(ring, right[i]) |
| + } |
| + var d strings.Builder |
| + px, py := round(ring[0].x), round(ring[0].y) |
| + fmt.Fprintf(&d, "M%d %dl", px, py) |
| + for i, p := range ring[1:] { |
| + x, y := round(p.x), round(p.y) |
| + if i > 0 { |
| + d.WriteByte(' ') |
| + } |
| + fmt.Fprintf(&d, "%d %d", x-px, y-py) |
| + px, py = x, y |
| + } |
| + d.WriteByte('z') |
| + return d.String() |
| +} |
| + |
| +func round(f float64) int { return int(math.RoundToEven(f)) } |
| + |
| +func taper(u, p float64) float64 { return math.Pow(math.Sin(math.Pi*u), p) } |
| + |
| +func zero(float64) float64 { return 0 } |
| + |
| +// slope is a->b's slope in pixels, at a typical heading's scale. |
| +func slope(a, b pt) float64 { return ((b.y - a.y) * sy) / math.Max((b.x-a.x)*sx, 1e-6) } |
| + |
| +// tailIn comes in from off the left edge at height enter, arriving on the |
| +// loop's own heading so the two join without a kink. |
| +func tailIn(v variant) []pt { |
| + w, h := inBox.x, inBox.y |
| + k := math.Min(slope(v.loop[0], v.loop[1]), 0.9) |
| + y0 := v.enter |
| + return []pt{{0, y0}, {w * 0.3, y0 + (h-y0)*0.08}, {w * 0.62, y0 + (h-y0)*0.3}, |
| + {w - 260, h - 260*k*0.55}, {w - 110, h - 110*k}, {w, h}} |
| +} |
| + |
| +// tailOut leaves the loop on its own heading and levels off at height |
| +// leave, off the right edge. |
| +func tailOut(v variant) []pt { |
| + w := outBox.x |
| + n := len(v.loop) |
| + k := math.Min(slope(v.loop[n-2], v.loop[n-1]), 1.2) |
| + y1 := v.leave |
| + return []pt{{0, 0}, {90, 90 * k}, {260, 260*k*0.6 + y1*0.1}, {560, y1 * 0.75}, {1000, y1 * 0.95}, {w, y1}} |
| +} |
| + |
| +// ── The sprite ────────────────────────────────────────────────────────── |
| + |
| +func symbolID(vi, i int) string { return fmt.Sprintf("wisp-v%d-s%d", vi, i) } |
| + |
| +func symbol(vi int, v variant, curves map[string]curve, i int) string { |
| + s := stretches[i] |
| + box := map[string]pt{"in": inBox, "loop": loopBox, "out": outBox}[s.piece] |
| + k := 34 |
| + if s.piece == "loop" { |
| + k = 84 |
| + } |
| + line := stretchLine(curves[s.piece], s.start, s.length, k) |
| + glow, body := v.glowBody[i][0], v.glowBody[i][1] |
| + var b strings.Builder |
| + fmt.Fprintf(&b, `<symbol id="%s" viewBox="0 0 %g %g" preserveAspectRatio="none" overflow="visible">`, symbolID(vi, i), box.x, box.y) |
| + shape := func(opacity, filter, d string) { |
| + fmt.Fprintf(&b, `<path fill="currentColor" opacity="%s" filter="url(#wisp-%s)" d="%s"/>`, opacity, filter, d) |
| + } |
| + // The glow, a wide and very soft band of light; the body, a narrower, |
| + // brighter haze along the middle. |
| + shape("0.16", "glow", ribbon(line, func(u float64) float64 { return glow * taper(u, 0.7) }, zero)) |
| + shape("0.3", "body", ribbon(line, func(u float64) float64 { return body * taper(u, 0.9) }, zero)) |
| + // The fibres, braiding in and out of one another; no two stretches alike. |
| + for _, f := range v.fibres { |
| + f, ph := f, f.phase+float64(i)*0.9 |
| + shape(fmt.Sprintf("%.2f", f.opacity), "fibre", ribbon(line, |
| + func(u float64) float64 { return f.hw * taper(u, 1.2) }, |
| + func(u float64) float64 { return f.amp * math.Sin(2*math.Pi*f.cycles*u+ph) * taper(u, 0.5) })) |
| + } |
| + // A stray: one strand that peels away from the rest and fades, on the |
| + // first stretch in or the last stretch out. |
| + if (v.stray == "in" && i == 0) || (v.stray == "out" && i == len(stretches)-1) { |
| + peel := func(u float64) float64 { return 26 * u * u } |
| + if i == 0 { |
| + peel = func(u float64) float64 { return -26 * (1 - u) * (1 - u) } |
| + } |
| + shape("0.32", "fibre", ribbon(line, func(u float64) float64 { return 0.6 * taper(u, 1.0) }, peel)) |
| + } |
| + b.WriteString("</symbol>") |
| + return b.String() |
| +} |
| + |
| +var ( |
| + spriteOnce sync.Once |
| + sprite []byte |
| +) |
| + |
| +// Sprite is every wisp, as one SVG of symbols with the blurs they share, |
| +// to set into the page: it takes no space and is hidden from assistive |
| +// technology. Deterministic: the same bytes on every start. |
| +func Sprite() []byte { |
| + spriteOnce.Do(func() { |
| + var b strings.Builder |
| + b.WriteString(`<svg class="wisp-defs" width="0" height="0" aria-hidden="true" focusable="false">`) |
| + // In user space with a generous region, so a thin shape's blur is |
| + // never clipped to its own bounding box. |
| + for _, f := range []struct { |
| + name string |
| + sd float64 |
| + }{{"glow", 9}, {"body", 3}, {"fibre", 0.8}} { |
| + fmt.Fprintf(&b, `<filter id="wisp-%s" filterUnits="userSpaceOnUse" x="-200" y="-200" width="2200" height="600"><feGaussianBlur stdDeviation="%g"/></filter>`, f.name, f.sd) |
| + } |
| + for vi, v := range variants { |
| + curves := map[string]curve{"in": sample(tailIn(v)), "loop": sample(v.loop), "out": sample(tailOut(v))} |
| + for i := range stretches { |
| + b.WriteString(symbol(vi, v, curves, i)) |
| + } |
| + } |
| + b.WriteString("</svg>\n") |
| + sprite = []byte(b.String()) |
| + }) |
| + return sprite |
| +} |
| + |
| +// Markup is the wisp for the heading it sits in: its seven stretches, each |
| +// an empty SVG pointing at its symbol in the page's sprite. The heading |
| +// carries data-wisp, which home.css reads for the variant's timing; the |
| +// stretches' order is their order here. |
| +func Markup(variant int) string { |
| + var b strings.Builder |
| + for i, s := range stretches { |
| + fmt.Fprintf(&b, `<svg class="wisp wisp-%s" aria-hidden="true" focusable="false"><use href="#%s"/></svg>`, |
| + s.piece, symbolID(variant, i)) |
| + } |
| + return b.String() |
| +} |
| + |
| +// Delay is the variant's head start or lag, in seconds, for home.css. |
| +func Delay(variant int) float64 { return float64(variants[variant].shift+3) * 5 / 100 } |