/* ============================================================ HWAY SITE SCRIPT (hway-site.js, v1.1) Upload under Look and Feel > Customize > Advanced > Scripts. Loads on every page. Each part only runs on the page that contains its block: [data-hway-lp] (Home), [data-hway-contact] (Contact). Replaces hway-landing.js. ============================================================ */ /* ============================================================ LANDING PAGE (.hway-lp) ============================================================ */ (() => { const init = () => { const root = document.querySelector('[data-hway-lp]'); if (!root || root.dataset.hwayLpInit) return; root.dataset.hwayLpInit = '1'; const mq = window.matchMedia ? window.matchMedia('(prefers-reduced-motion: reduce)') : { matches: false }; /* ---------- Hero hex grid ---------- */ const hero = root.querySelector('.hway-lp__hero'); const pinEl = root.querySelector('.hway-lp__pin'); const hint = root.querySelector('.hway-lp__hint'); /* The CMS strips , so create it here */ let cvs = root.querySelector('.hway-lp__grid'); if (!cvs && hero) { cvs = document.createElement('canvas'); cvs.className = 'hway-lp__grid'; cvs.setAttribute('aria-hidden', 'true'); hero.prepend(cvs); } const ctx = cvs && cvs.getContext ? cvs.getContext('2d') : null; /* From here on the script controls the grid and the steps track */ root.classList.add('hway-lp--js'); if (hero && ctx && pinEl) { let W = 0, H = 0, R = 22, hexes = [], pin = { x: 0, y: 0 }, maxD = 1; let ptr = null, visible = false, raf = 0, lastAuto = 0; const waves = []; const SPEED = 230; /* px per second */ const build = () => { const dpr = Math.min(window.devicePixelRatio || 1, 2); W = hero.clientWidth; H = hero.clientHeight; R = W < 600 ? 17 : W < 1100 ? 21 : 24; cvs.width = Math.round(W * dpr); cvs.height = Math.round(H * dpr); ctx.setTransform(dpr, 0, 0, dpr, 0, 0); const hr = hero.getBoundingClientRect(), pr = pinEl.getBoundingClientRect(); pin = { x: pr.left + pr.width / 2 - hr.left, y: pr.top + pr.height / 2 - hr.top }; const w = Math.sqrt(3) * R, rowH = 1.5 * R; hexes = []; maxD = 1; for (let r = -1; r * rowH < H + R * 2; r++) { for (let c = -1; c * w < W + w; c++) { const x = c * w + (r % 2 ? w / 2 : 0), y = r * rowH; hexes.push({ x, y }); maxD = Math.max(maxD, Math.hypot(x - pin.x, y - pin.y)); } } maxD = Math.max(maxD, Math.hypot(W, H)); }; const hexPath = (x, y, r) => { ctx.beginPath(); for (let i = 0; i < 6; i++) { const a = Math.PI / 180 * (60 * i - 90); const px = x + r * Math.cos(a), py = y + r * Math.sin(a); i ? ctx.lineTo(px, py) : ctx.moveTo(px, py); } ctx.closePath(); }; const draw = (now) => { ctx.clearRect(0, 0, W, H); ctx.lineWidth = 1; for (let i = waves.length - 1; i >= 0; i--) { if ((now - waves[i].t) / 1000 * SPEED > maxD) waves.splice(i, 1); } for (const h of hexes) { let v = 0, lime = 0; if (ptr) v = Math.max(v, 1 - Math.hypot(h.x - ptr.x, h.y - ptr.y) / 170); for (const wv of waves) { const rad = (now - wv.t) / 1000 * SPEED; const d = Math.hypot(h.x - wv.x, h.y - wv.y); const band = Math.max(0, 1 - Math.abs(d - rad) / 48) * Math.max(0, 1 - rad / maxD); v = Math.max(v, band * 0.7); if (wv.user) lime = Math.max(lime, band); } hexPath(h.x, h.y, R - 2); if (v > 0.02) { ctx.fillStyle = 'rgba(255,255,255,' + (v * 0.09).toFixed(3) + ')'; ctx.fill(); /* Waves the visitor starts carry a faint lime edge */ ctx.strokeStyle = lime > 0.35 ? 'rgba(212,225,33,' + (lime * 0.55).toFixed(3) + ')' : 'rgba(140,135,137,' + (0.25 + v * 0.6).toFixed(3) + ')'; } else { ctx.strokeStyle = '#3A3536'; } ctx.stroke(); } }; const frame = (now) => { raf = 0; if (!visible || mq.matches) return; if (now - lastAuto > 2600) { waves.push({ x: pin.x, y: pin.y, t: now }); lastAuto = now; } draw(now); raf = requestAnimationFrame(frame); }; const start = () => { if (mq.matches) { waves.length = 0; draw(performance.now()); return; } if (!raf && visible) raf = requestAnimationFrame(frame); }; build(); draw(performance.now()); hero.addEventListener('pointermove', (e) => { const r = hero.getBoundingClientRect(); ptr = { x: e.clientX - r.left, y: e.clientY - r.top }; if (mq.matches) draw(performance.now()); }, { passive: true }); hero.addEventListener('pointerleave', () => { ptr = null; if (mq.matches) draw(performance.now()); }); hero.addEventListener('click', (e) => { if (e.target.closest('a, button, .hway-lp__hero-copy')) return; if (mq.matches) return; const r = hero.getBoundingClientRect(); waves.push({ x: e.clientX - r.left, y: e.clientY - r.top, t: performance.now(), user: true }); if (waves.length > 8) waves.shift(); if (hint) hint.classList.add('hway-lp--gone'); start(); }); if (hint && !mq.matches && window.matchMedia && window.matchMedia('(hover: hover)').matches) hint.hidden = false; if ('ResizeObserver' in window) { let rt = 0; new ResizeObserver(() => { cancelAnimationFrame(rt); rt = requestAnimationFrame(() => { build(); draw(performance.now()); }); }).observe(hero); } /* Rebuild once web fonts finish loading, since text size moves the pin */ if (document.fonts && document.fonts.ready) { document.fonts.ready.then(() => { build(); draw(performance.now()); }); } if ('IntersectionObserver' in window) { new IntersectionObserver((en) => { visible = en[0].isIntersecting; start(); }, { threshold: 0.02 }).observe(hero); } else { visible = true; start(); } if (mq.addEventListener) mq.addEventListener('change', start); } /* ---------- Tile spotlight follows the cursor ---------- */ root.querySelectorAll('.hway-lp__tile').forEach((tile) => { tile.addEventListener('pointermove', (e) => { if (e.pointerType !== 'mouse') return; const r = tile.getBoundingClientRect(); tile.style.setProperty('--hway-lp-mx', (e.clientX - r.left) + 'px'); tile.style.setProperty('--hway-lp-my', (e.clientY - r.top) + 'px'); }, { passive: true }); }); /* ---------- Steps: fill the track once when it scrolls into view ---------- */ const steps = root.querySelector('.hway-lp__steps'); if (steps) { if (!('IntersectionObserver' in window) || mq.matches) { steps.classList.add('hway-lp--in'); } else { const io = new IntersectionObserver((en) => { if (en[0].isIntersecting) { steps.classList.add('hway-lp--in'); io.disconnect(); } }, { threshold: 0.5 }); io.observe(steps); } } }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } })(); /* ============================================================ CONTACT PAGE (.hway-contact) ============================================================ */ (() => { const init = () => { const root = document.querySelector('[data-hway-contact]'); if (!root || root.dataset.hwayContactInit) return; root.dataset.hwayContactInit = '1'; root.classList.add('hway-contact--js'); const mq = window.matchMedia ? window.matchMedia('(prefers-reduced-motion: reduce)') : { matches: false }; /* ---------- Interactive Hexa-H symbol ---------- */ const stage = root.querySelector('.hway-contact__symbol'); if (stage) { const tilt = stage.querySelector('.hway-contact__sym-tilt'); const rings = stage.querySelector('.hway-contact__sym-rings'); const pulse = stage.querySelector('.hway-contact__sym-pulse'); const MAX = 14; const target = { x: 0, y: 0 }; const cur = { x: 0, y: 0 }; const t0 = performance.now(); let hover = false, visible = false, raf = 0; const aim = (e) => { const r = stage.getBoundingClientRect(); target.x = Math.max(-1, Math.min(1, ((e.clientX - r.left) / r.width) * 2 - 1)); target.y = Math.max(-1, Math.min(1, ((e.clientY - r.top) / r.height) * 2 - 1)); }; const frame = (now) => { raf = 0; if (!visible || mq.matches) return; const t = (now - t0) / 1000; const gx = hover ? target.x : Math.sin(t * 0.6) * 0.35; const gy = hover ? target.y : Math.sin(t * 1.2) * 0.2; cur.x += (gx - cur.x) * 0.08; cur.y += (gy - cur.y) * 0.08; tilt.style.transform = 'rotateY(' + (cur.x * MAX).toFixed(2) + 'deg) rotateX(' + (-cur.y * MAX).toFixed(2) + 'deg) ' + 'translate3d(' + (cur.x * 6).toFixed(1) + 'px,' + (cur.y * 6 + Math.sin(t * 1.4) * 4).toFixed(1) + 'px,0)'; rings.style.transform = 'translate3d(' + (-cur.x * 10).toFixed(1) + 'px,' + (-cur.y * 10).toFixed(1) + 'px,0)'; raf = requestAnimationFrame(frame); }; const start = () => { if (!raf && visible && !mq.matches) raf = requestAnimationFrame(frame); }; const fire = () => { if (mq.matches) return; pulse.classList.remove('hway-contact--go'); void pulse.getBoundingClientRect(); pulse.classList.add('hway-contact--go'); }; stage.addEventListener('pointerenter', (e) => { hover = true; aim(e); }); stage.addEventListener('pointermove', aim, { passive: true }); stage.addEventListener('pointerleave', () => { hover = false; }); stage.addEventListener('click', fire); stage.addEventListener('keydown', (e) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); fire(); } }); if ('IntersectionObserver' in window) { new IntersectionObserver((en) => { visible = en[0].isIntersecting; start(); }, { threshold: 0.1 }).observe(stage); } else { visible = true; start(); } if (mq.addEventListener) { mq.addEventListener('change', () => { if (mq.matches) { tilt.style.transform = ''; rings.style.transform = ''; } else start(); }); } } const live = root.querySelector('[data-hway-contact-live]'); const say = (msg) => { if (live) { live.textContent = ''; setTimeout(() => { live.textContent = msg; }, 50); } }; /* ---------- Panel spotlight follows the cursor ---------- */ root.querySelectorAll('.hway-contact__method').forEach((panel) => { panel.addEventListener('pointermove', (e) => { if (e.pointerType !== 'mouse') return; const r = panel.getBoundingClientRect(); panel.style.setProperty('--hway-contact-mx', (e.clientX - r.left) + 'px'); panel.style.setProperty('--hway-contact-my', (e.clientY - r.top) + 'px'); }, { passive: true }); }); /* ---------- Durban local time ---------- */ const timeEl = root.querySelector('[data-hway-contact-time]'); if (timeEl && window.Intl) { try { const fmt = new Intl.DateTimeFormat('en-ZA', { timeZone: 'Africa/Johannesburg', hour: '2-digit', minute: '2-digit', hourCycle: 'h23' }); const tick = () => { timeEl.textContent = fmt.format(new Date()); }; tick(); setInterval(tick, 15000); timeEl.closest('.hway-contact__time').hidden = false; } catch (err) { /* leave the line hidden */ } } /* ---------- Copy buttons ---------- */ const copyText = (text) => { if (navigator.clipboard && window.isSecureContext) return navigator.clipboard.writeText(text); return new Promise((resolve, reject) => { const ta = document.createElement('textarea'); ta.value = text; ta.setAttribute('readonly', ''); ta.style.position = 'fixed'; ta.style.opacity = '0'; document.body.appendChild(ta); ta.select(); const ok = document.execCommand && document.execCommand('copy'); document.body.removeChild(ta); ok ? resolve() : reject(); }); }; root.querySelectorAll('.hway-contact__copy').forEach((btn) => { btn.hidden = false; const label = btn.firstChild; let timer = 0; btn.addEventListener('click', () => { copyText(btn.dataset.copy).then(() => { label.nodeValue = 'Copied'; btn.classList.add('hway-contact--done'); say('Copied ' + btn.dataset.copy); clearTimeout(timer); timer = setTimeout(() => { label.nodeValue = 'Copy'; btn.classList.remove('hway-contact--done'); }, 2000); }).catch(() => say('Copy did not work. Select the text and copy it instead.')); }); }); /* ---------- Map: interactive hex grid until the real map is requested ---------- */ const map = root.querySelector('.hway-contact__map'); if (map) { /* The CMS strips , so create it here */ let cvs = map.querySelector('.hway-contact__hexgrid'); if (!cvs) { cvs = document.createElement('canvas'); cvs.className = 'hway-contact__hexgrid'; cvs.setAttribute('aria-hidden', 'true'); map.prepend(cvs); } const pinEl = map.querySelector('.hway-contact__pin'); const ctx = cvs.getContext ? cvs.getContext('2d') : null; const R = 20; let W = 0, H = 0, hexes = [], pin = { x: 0, y: 0 }, maxD = 1; let ptr = null, visible = false, raf = 0, loaded = false; const t0 = performance.now(); const build = () => { if (!ctx) return; const dpr = Math.min(window.devicePixelRatio || 1, 2); W = cvs.clientWidth; H = cvs.clientHeight; cvs.width = Math.round(W * dpr); cvs.height = Math.round(H * dpr); ctx.setTransform(dpr, 0, 0, dpr, 0, 0); const mr = map.getBoundingClientRect(); const pr = pinEl.getBoundingClientRect(); pin = { x: pr.left + pr.width / 2 - mr.left, y: pr.top + pr.height / 2 - mr.top }; const w = Math.sqrt(3) * R, rowH = 1.5 * R; hexes = []; maxD = 1; for (let r = -1; r * rowH < H + R * 2; r++) { for (let c = -1; c * w < W + w; c++) { const x = c * w + (r % 2 ? w / 2 : 0), y = r * rowH; const d = Math.hypot(x - pin.x, y - pin.y); if (d > maxD) maxD = d; hexes.push({ x, y, d }); } } }; const hexPath = (x, y, r) => { ctx.beginPath(); for (let i = 0; i < 6; i++) { const a = Math.PI / 180 * (60 * i - 90); const px = x + r * Math.cos(a), py = y + r * Math.sin(a); i ? ctx.lineTo(px, py) : ctx.moveTo(px, py); } ctx.closePath(); }; const draw = (now, still) => { if (!ctx) return; ctx.clearRect(0, 0, W, H); const wave = still ? -999 : (((now - t0) / 1000) * 160) % (maxD + 240); ctx.lineWidth = 1; for (const h of hexes) { let v = 0; if (ptr) v = Math.max(v, 1 - Math.hypot(h.x - ptr.x, h.y - ptr.y) / 150); v = Math.max(v, (1 - Math.min(1, Math.abs(h.d - wave) / 46)) * 0.55); hexPath(h.x, h.y, R - 2); if (v > 0.02) { ctx.fillStyle = 'rgba(255,255,255,' + (v * 0.09).toFixed(3) + ')'; ctx.fill(); ctx.strokeStyle = 'rgba(140,135,137,' + (0.25 + v * 0.6).toFixed(3) + ')'; } else { ctx.strokeStyle = '#3A3536'; } ctx.stroke(); } }; const frame = (now) => { raf = 0; if (loaded || !visible || mq.matches) return; draw(now, false); raf = requestAnimationFrame(frame); }; const start = () => { if (loaded) return; if (mq.matches) { draw(0, true); return; } if (!raf && visible) raf = requestAnimationFrame(frame); }; build(); draw(0, true); map.addEventListener('pointermove', (e) => { const r = map.getBoundingClientRect(); ptr = { x: e.clientX - r.left, y: e.clientY - r.top }; if (mq.matches) draw(0, true); }, { passive: true }); map.addEventListener('pointerleave', () => { ptr = null; if (mq.matches) draw(0, true); }); if ('ResizeObserver' in window) { new ResizeObserver(() => { if (!loaded) { build(); draw(performance.now(), mq.matches || !visible); } }).observe(map); } if (document.fonts && document.fonts.ready) { document.fonts.ready.then(() => { if (!loaded) { build(); draw(performance.now(), mq.matches || !visible); } }); } if ('IntersectionObserver' in window) { new IntersectionObserver((en) => { visible = en[0].isIntersecting; start(); }, { threshold: 0.05 }).observe(map); } else { visible = true; start(); } if (mq.addEventListener) mq.addEventListener('change', start); /* Load OpenStreetMap only when asked */ const btn = map.querySelector('.hway-contact__map-btn'); const note = map.querySelector('.hway-contact__map-note'); if (btn) { btn.hidden = false; if (note) note.hidden = false; btn.addEventListener('click', () => { loaded = true; if (raf) cancelAnimationFrame(raf); const lat = parseFloat(map.dataset.lat), lon = parseFloat(map.dataset.lon); const dLat = 0.07, dLon = 0.11; const bbox = [lon - dLon, lat - dLat, lon + dLon, lat + dLat].map((n) => n.toFixed(4)).join('%2C'); const frameEl = document.createElement('iframe'); frameEl.src = 'https://www.openstreetmap.org/export/embed.html?bbox=' + bbox + '&layer=mapnik&marker=' + lat.toFixed(4) + '%2C' + lon.toFixed(4); frameEl.title = 'Map of Durban, KwaZulu-Natal'; frameEl.loading = 'lazy'; frameEl.referrerPolicy = 'strict-origin-when-cross-origin'; frameEl.setAttribute('sandbox', 'allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox'); map.replaceChildren(frameEl); say('Interactive map loaded.'); }); } } }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } })();