/* Creative Paper theme behaviour: scroll progress, page wipe, hiding header, custom cursor, word-by-word headings, reveal on scroll, parallax collage, speed-reactive marquee, work filters, carousel. Dependency-free; every block is a no-op when its markup is absent, so it is safe on blog, shop and builder pages alike. Nothing is hidden until this script has run, and nothing is hidden at all for visitors who ask for reduced motion. */ (function () { 'use strict'; var B = document.body; if (!B || !B.classList.contains('creative-paper')) { return; } var $ = function (s, c) { return (c || document).querySelector(s); }; var $$ = function (s, c) { return Array.prototype.slice.call((c || document).querySelectorAll(s)); }; var RM = window.matchMedia && matchMedia('(prefers-reduced-motion: reduce)').matches; var FINE = window.matchMedia && matchMedia('(pointer:fine)').matches; var root = document.documentElement; root.style.scrollBehavior = RM ? 'auto' : 'smooth'; root.style.overflowX = 'clip'; // ---- carousel (photo showcase) $$('[data-cp-carousel]').forEach(function (car) { var id = car.getAttribute('data-cp-carousel'); var step = function (dir) { car.scrollBy({ left: dir * car.clientWidth * 0.72, behavior: 'smooth' }); }; var next = $('[data-cp-next="' + id + '"]'), prev = $('[data-cp-prev="' + id + '"]'); if (next) { next.addEventListener('click', function () { step(1); }); } if (prev) { prev.addEventListener('click', function () { step(-1); }); } }); // ---- tilt on the hero collage photos $$('.cp-photo').forEach(function (el, i) { el.addEventListener('mousemove', function (e) { var r = el.getBoundingClientRect(), x = (e.clientX - r.left) / r.width - 0.5, y = (e.clientY - r.top) / r.height - 0.5; el.style.transform = 'rotate(' + (i === 1 ? 3 : -1) + 'deg) perspective(800px) rotateY(' + (x * 7) + 'deg) rotateX(' + (-y * 7) + 'deg) scale(1.025)'; }); el.addEventListener('mouseleave', function () { el.style.transform = ''; }); }); // ---- work filters (buttons filter the cards of their own section) $$('.cp-filters').forEach(function (bar) { var scope = bar.closest('section') || document; var buttons = $$('button', bar); buttons.forEach(function (b) { b.addEventListener('click', function () { buttons.forEach(function (x) { x.classList.toggle('cp-on', x === b); }); var f = b.getAttribute('data-f'); $$('.cp-card[data-cats]', scope).forEach(function (c) { var cats = (c.getAttribute('data-cats') || '').split(' '); c.classList.toggle('cp-hide', f !== 'all' && cats.indexOf(f) === -1); }); }); }); }); // ---- progress, hiding header, parallax, marquee speed var pg = $('.cp-progress'), nv = $('.cp-nav'), mn = $('.cp-mnav'), col = $('.cp-collage'); var ph = $('.cp-pageHero h1, .cp-pageHero h2'), mq = $('.cp-manifestoLine'); var ly = window.scrollY, vel = 0, rate = 1, tick = false; function frame() { tick = false; var y = window.scrollY, m = root.scrollHeight - window.innerHeight; if (pg) { pg.style.transform = 'scaleX(' + (m > 0 ? y / m : 0) + ')'; } if (nv && !(mn && mn.open)) { nv.classList.toggle('cp-hid', y > ly && y > 320); } vel = Math.abs(y - ly); ly = y; if (!RM) { if (col) { col.style.transform = 'translateY(' + (y * -0.07) + 'px)'; } if (ph && y < 900) { ph.style.transform = 'translateY(' + (y * 0.12) + 'px)'; } } } window.addEventListener('scroll', function () { if (!tick) { tick = true; requestAnimationFrame(frame); } }, { passive: true }); frame(); if (mq && mq.getAnimations && !RM) { (function loop() { var an = mq.getAnimations()[0], tg = 1 + Math.min(vel / 6, 5); rate += (tg - rate) * 0.08; vel *= 0.9; if (an) { an.playbackRate = rate; } requestAnimationFrame(loop); })(); } // ---- page wipe between pages (same-site links only) var wipe = $('.cp-wipe'); if (wipe) { document.addEventListener('click', function (e) { var a = e.target.closest && e.target.closest('a'); if (!a || e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) { return; } if ((a.target && a.target !== '_self') || a.hasAttribute('download') || a.hasAttribute('data-no-transition')) { return; } var u; try { u = new URL(a.href, location.href); } catch (x) { return; } if (u.origin !== location.origin || /^\/(admin|logout|user-logout)/.test(u.pathname)) { return; } if (u.pathname === location.pathname && u.search === location.search) { return; } e.preventDefault(); B.classList.add('cp-leaving'); setTimeout(function () { location.href = u.href; }, RM ? 0 : 520); }); window.addEventListener('pageshow', function (e) { if (e.persisted) { B.classList.remove('cp-leaving'); } }); } if (RM) { return; } // ---- reveal on scroll var fired = false; var io = new IntersectionObserver(function (es) { fired = true; es.forEach(function (e) { if (!e.isIntersecting) { return; } var t = e.target; t.classList.add('cp-in'); io.unobserve(t); if (t.dataset.scr) { scramble(t); } if (t.classList.contains('cp-rv')) { setTimeout(function () { t.classList.remove('cp-rv', 'cp-in'); t.style.removeProperty('--d'); }, 1300 + (parseFloat(t.style.getPropertyValue('--d')) || 0) * 1000); } if (t.classList.contains('cp-clip')) { setTimeout(function () { t.classList.remove('cp-clip', 'cp-in'); }, 1700); } }); }, { threshold: 0.12, rootMargin: '0px 0px -6% 0px' }); // headings: one word at a time $$('.cp-hero h1,.cp-hero h2,.cp-pageHero h1,.cp-pageHero h2,.cp-section h1,.cp-section h2,.cp-links h2,.cp-footer h2,.cp-showcase h2,.cp-recruit h1,.cp-recruit h2,.cp-intro h2,.cp-testimonials h2,.cp-nextProj b').forEach(function (el) { var i = 0, d = function () { return (i++ * 0.055 + 0.08) + 's'; }; Array.prototype.slice.call(el.childNodes).forEach(function (n) { if (n.nodeType === 3) { var f = document.createDocumentFragment(); var parts = n.textContent.split(/(\s+)/); // punctuation right after an element ("notice" + ", understand") stays with that word, so a line never starts with a comma var prev = n.previousSibling; if (parts[0] && !/^\s+$/.test(parts[0]) && prev && prev.nodeType === 1 && prev.classList.contains('cp-w')) { prev.lastChild.appendChild(document.createTextNode(parts.shift())); } parts.forEach(function (t) { if (!t) { return; } if (/^\s+$/.test(t)) { f.appendChild(document.createTextNode(t)); return; } var w = document.createElement('span'); w.className = 'cp-w'; var k = document.createElement('i'); k.textContent = t; k.style.transitionDelay = d(); w.appendChild(k); f.appendChild(w); }); n.replaceWith(f); } else if (n.nodeType === 1 && n.nodeName !== 'BR') { var w2 = document.createElement('span'); w2.className = 'cp-w cp-keep'; var k2 = document.createElement('i'); k2.style.transitionDelay = d(); n.replaceWith(w2); k2.appendChild(n); w2.appendChild(k2); } }); el.classList.add('cp-sx'); io.observe(el); }); // groups: children rise one after another $$('.cp-cards,.cp-processGrid,.cp-hireCards,.cp-disciplineGrid,.cp-timeline,.cp-faq,.cp-status,.cp-footCols,.cp-proofStrip,.cp-galleryStrip,.cp-caseMeta,.cp-tags,.cp-who').forEach(function (g) { Array.prototype.slice.call(g.children).forEach(function (c, i) { c.classList.add('cp-rv'); c.style.setProperty('--d', ((i % 6) * 0.08) + 's'); io.observe(c); }); }); $$('.cp-svcRow').forEach(function (r) { var idx = Array.prototype.indexOf.call(r.parentNode.children, r); r.classList.add('cp-rv'); r.style.setProperty('--d', ((idx % 6) * 0.08) + 's'); io.observe(r); }); // images: curtain reveal // (not observed: a browser's IntersectionObserver treats an element whose whole area is clipped by clip-path as never // intersecting, so these curtains would stay shut forever; the scroll sweep below reveals them by their box instead) $$('.cp-card .cp-thumb,.cp-caseCover,.cp-project,.cp-galleryStrip figure,.cp-cvAside img').forEach(function (el) { el.classList.add('cp-clip'); }); // small caps labels: scramble into place var CH = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ/ยท0123456789'; function scramble(el) { var t = el.dataset.scr, f = 0, N = 26; var iv = setInterval(function () { var k = f / N * t.length; el.textContent = t.split('').map(function (c, i) { return (i < k || c === ' ') ? c : CH[Math.random() * CH.length | 0]; }).join(''); if (++f > N) { clearInterval(iv); el.textContent = t; } }, 24); } $$('.cp-eyebrow,.cp-label').forEach(function (el) { if (el.children.length || el.closest('.cp-nextProj') || !el.textContent.trim()) { return; } el.dataset.scr = el.textContent; io.observe(el); }); // anything already on screen (or a browser that never fires the observer) must not stay hidden var all = function () { return $$('.cp-sx:not(.cp-in),.cp-rv:not(.cp-in),.cp-clip:not(.cp-in)'); }; var done = function (t) { t.classList.add('cp-in'); io.unobserve(t); if (t.classList.contains('cp-rv')) { setTimeout(function () { t.classList.remove('cp-rv', 'cp-in'); }, 1400); } if (t.classList.contains('cp-clip')) { setTimeout(function () { t.classList.remove('cp-clip', 'cp-in'); }, 1700); } }; var sweep = function () { var limit = window.innerHeight * 0.94; all().forEach(function (t) { var r = t.getBoundingClientRect(); if (r.top < limit && r.bottom > 0) { done(t); } }); }; requestAnimationFrame(sweep); var sweepTick = false; var sweepSoon = function () { if (!sweepTick) { sweepTick = true; requestAnimationFrame(function () { sweepTick = false; sweep(); }); } }; window.addEventListener('scroll', sweepSoon, { passive: true }); window.addEventListener('resize', sweepSoon); window.addEventListener('load', sweepSoon); if (document.hidden) { all().forEach(done); } setTimeout(function () { if (!fired) { all().forEach(done); } }, 2500); if (!FINE) { return; } // ---- custom cursor (mouse only) var c = $('.cp-cursor'); if (!c) { return; } window.addEventListener('mousemove', function (e) { c.classList.add('cp-live'); c.style.left = e.clientX + 'px'; c.style.top = e.clientY + 'px'; }); $$('a,.cp-project,.cp-discipline').forEach(function (x) { x.addEventListener('mouseenter', function () { c.style.width = '48px'; c.style.height = '48px'; c.style.background = 'rgba(255,91,33,.2)'; }); x.addEventListener('mouseleave', function () { c.style.width = ''; c.style.height = ''; c.style.background = ''; }); }); $$('.cp-card,.cp-project,.cp-slide,.cp-videoCard,.cp-nextProj,.cp-behance').forEach(function (el) { el.addEventListener('mouseenter', function () { c.dataset.l = el.matches('.cp-videoCard') ? 'PLAY' : el.matches('.cp-slide') ? 'DRAG' : 'VIEW'; c.classList.add('cp-view'); c.style.width = '96px'; c.style.height = '96px'; c.style.background = ''; }); el.addEventListener('mouseleave', function () { c.classList.remove('cp-view'); c.style.width = ''; c.style.height = ''; }); }); // magnetic buttons $$('.cp-pill,.cp-circleBtn,.cp-filters button').forEach(function (el) { el.classList.add('cp-mag'); el.addEventListener('mousemove', function (e) { var r = el.getBoundingClientRect(); el.style.transform = 'translate(' + ((e.clientX - r.left - r.width / 2) * 0.28) + 'px,' + ((e.clientY - r.top - r.height / 2) * 0.4) + 'px)'; }); el.addEventListener('mouseleave', function () { el.style.transform = ''; }); }); })();