/* The network core at the middle of "How It Works". * * A ball of nodes wired to each other, drifting slowly. Every few seconds it * arcs out to one of the chapter cards — a jagged Tesla-style bolt — and that * card lights up for a moment. The point is to show one engine feeding every * chapter, without anyone having to say it. */ (function () { document.querySelectorAll('[data-netball]').forEach(build); function build(wrap) { var reduced = matchMedia('(prefers-reduced-motion: reduce)').matches; var NS = 'http://www.w3.org/2000/svg'; var linkG = wrap.querySelector('[data-nblinks]'); var nodeG = wrap.querySelector('[data-nbnodes]'); var zapSvg = wrap.querySelector('[data-nbzaps]'); /* ---- build the ball: points on a sphere, projected flat ---- */ var N = 22, pts = []; for (var i = 0; i < N; i++) { // golden-spiral distribution, so they never clump var y = 1 - (i / (N - 1)) * 2; var rad = Math.sqrt(Math.max(0, 1 - y * y)); var theta = i * 2.399963; pts.push({ x: Math.cos(theta) * rad, y: y, z: Math.sin(theta) * rad }); } // eight of them, picked at random, sit bigger and brighter than the rest var HERO = 8, heroes = {}; var bag = pts.map(function (_, i) { return i; }); for (var h = 0; h < HERO && bag.length; h++) { heroes[bag.splice(Math.floor(Math.random() * bag.length), 1)[0]] = true; } var dots = pts.map(function (_, i) { var c = document.createElementNS(NS, 'circle'); if (heroes[i]) c.setAttribute('class', 'is-hero'); c.setAttribute('r', '2.2'); nodeG.appendChild(c); return c; }); // wire each node to its two nearest neighbours — enough to read as a mesh var pairs = []; pts.forEach(function (p, a) { var d = pts.map(function (q, b) { return { b: b, d: (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y) + (p.z - q.z) * (p.z - q.z) }; }).filter(function (o) { return o.b !== a; }).sort(function (m, n) { return m.d - n.d; }); pairs.push([a, d[0].b]); pairs.push([a, d[1].b]); }); var lines = pairs.map(function () { var l = document.createElementNS(NS, 'line'); linkG.appendChild(l); return l; }); var spin = 0; function draw() { var cos = Math.cos(spin), sin = Math.sin(spin); var proj = pts.map(function (p) { var x = p.x * cos - p.z * sin; var z = p.x * sin + p.z * cos; var depth = (z + 1) / 2; // 0 back … 1 front return { x: 100 + x * 58, y: 100 + p.y * 58, d: depth }; }); proj.forEach(function (p, i) { dots[i].setAttribute('cx', p.x.toFixed(1)); dots[i].setAttribute('cy', p.y.toFixed(1)); dots[i].setAttribute('r', ((heroes[i] ? 2.6 : 1.1) + p.d * (heroes[i] ? 3.2 : 1.7)).toFixed(2)); dots[i].setAttribute('opacity', ((heroes[i] ? 0.5 : 0.18) + p.d * (heroes[i] ? 0.5 : 0.5)).toFixed(2)); }); pairs.forEach(function (pr, i) { var a = proj[pr[0]], b = proj[pr[1]]; lines[i].setAttribute('x1', a.x.toFixed(1)); lines[i].setAttribute('y1', a.y.toFixed(1)); lines[i].setAttribute('x2', b.x.toFixed(1)); lines[i].setAttribute('y2', b.y.toFixed(1)); lines[i].setAttribute('opacity', (0.1 + Math.min(a.d, b.d) * 0.5).toFixed(2)); }); } if (reduced) { draw(); } else { (function loop() { spin += 0.0035; draw(); requestAnimationFrame(loop); })(); } /* ---- the zap ---- */ // A core zaps whatever its section holds. If the named ring is missing — // the tail sections have no orbit — fall back to the section itself and // strike whatever card-like things are in it. var ring = document.getElementById(wrap.dataset.ring || '') || wrap.closest('.slide, .pack'); var TARGETS = '.orbit__node, [data-pop], .news-card, .feed__card, .allies__card,' + ' .grain-row li, .phase-row li, .family__ctas .btn, .certrow li'; if (!ring || reduced) return; function bolt(x1, y1, x2, y2) { // a jagged path between the two points — the Tesla look var segs = 7, d = 'M' + x1 + ',' + y1; for (var i = 1; i < segs; i++) { var t = i / segs; var mx = x1 + (x2 - x1) * t; var my = y1 + (y2 - y1) * t; var off = (Math.random() - 0.5) * 34 * Math.sin(Math.PI * t); d += ' L' + (mx + off).toFixed(1) + ',' + (my - off * 0.6).toFixed(1); } return d + ' L' + x2 + ',' + y2; } function zap() { // the ball charges first, so the bolt reads as a discharge wrap.classList.add('is-charging'); setTimeout(function () { wrap.classList.remove('is-charging'); }, 620); setTimeout(fire, 420); } function fire() { var cards = Array.prototype.slice.call(ring.querySelectorAll(TARGETS)) .filter(function (c) { return c.getBoundingClientRect().width > 8; }); // only strike cards on the near side, or the bolt crosses the whole ring var near = cards.filter(function (c) { return parseFloat(c.style.opacity || 1) > 0.72; }); var target = (near.length ? near : cards)[Math.floor(Math.random() * (near.length || cards.length))]; if (!target) return; var rb = ring.getBoundingClientRect(); var tb = target.getBoundingClientRect(); zapSvg.setAttribute('viewBox', '0 0 ' + rb.width + ' ' + rb.height); var p = document.createElementNS(NS, 'path'); p.setAttribute('class', 'netball__bolt'); p.setAttribute('d', bolt(rb.width / 2, rb.height / 2, tb.left - rb.left + tb.width / 2, tb.top - rb.top + tb.height / 2)); zapSvg.appendChild(p); target.classList.add('is-zapped'); setTimeout(function () { target.classList.remove('is-zapped'); }, 620); setTimeout(function () { p.remove(); }, 420); } // only while the section is actually on screen var live = false; new IntersectionObserver(function (es) { es.forEach(function (e) { live = e.isIntersecting; }); }, { threshold: 0.4 }).observe(ring); setInterval(function () { if (live && document.visibilityState === 'visible') zap(); }, 2600); wrap._zap = zap; // so it can be driven directly when testing } })();