| 1 |
(function () { |
| 2 |
|
| 3 |
var _typoKeys = { |
| 4 |
family:'font-family', weight:'font-weight', style:'font-style', |
| 5 |
decoration:'text-decoration', transform:'text-transform', |
| 6 |
sizeDesktop:'font-size-d', sizeTablet:'font-size-t', sizeMobile:'font-size-m', |
| 7 |
lineHeightDesktop:'line-height-d', lineHeightTablet:'line-height-t', lineHeightMobile:'line-height-m', |
| 8 |
letterSpacingDesktop:'letter-spacing-d', letterSpacingTablet:'letter-spacing-t', letterSpacingMobile:'letter-spacing-m', |
| 9 |
wordSpacingDesktop:'word-spacing-d', wordSpacingTablet:'word-spacing-t', wordSpacingMobile:'word-spacing-m' |
| 10 |
}; |
| 11 |
function typoCssVarsForEl(el, obj, prefix) { |
| 12 |
if (!obj || typeof obj !== 'object') return; |
| 13 |
Object.keys(_typoKeys).forEach(function (k) { |
| 14 |
var v = obj[k]; |
| 15 |
if (v === undefined || v === '' || v === null) return; |
| 16 |
if (k === 'sizeDesktop' || k === 'sizeTablet' || k === 'sizeMobile') v = v + (obj.sizeUnit || 'px'); |
| 17 |
else if (k === 'lineHeightDesktop' || k === 'lineHeightTablet' || k === 'lineHeightMobile') v = v + (obj.lineHeightUnit || ''); |
| 18 |
else if (k === 'letterSpacingDesktop' || k === 'letterSpacingTablet' || k === 'letterSpacingMobile') v = v + (obj.letterSpacingUnit || 'px'); |
| 19 |
else if (k === 'wordSpacingDesktop' || k === 'wordSpacingTablet' || k === 'wordSpacingMobile') v = v + (obj.wordSpacingUnit || 'px'); |
| 20 |
el.style.setProperty(prefix + _typoKeys[k], String(v)); |
| 21 |
}); |
| 22 |
} |
| 23 |
|
| 24 |
function initNoiseSection(appEl) { |
| 25 |
var opts; |
| 26 |
try { opts = JSON.parse(appEl.getAttribute('data-opts') || '{}'); } catch(e){ opts={}; } |
| 27 |
|
| 28 |
var a = { |
| 29 |
heading: opts.heading || '', |
| 30 |
subtext: opts.subtext || '', |
| 31 |
tag: opts.tag || 'h1', |
| 32 |
ctaLabel: opts.ctaLabel || 'Get Started', |
| 33 |
ctaUrl: opts.ctaUrl || '#', |
| 34 |
ctaLabel2: opts.ctaLabel2 || '', |
| 35 |
ctaUrl2: opts.ctaUrl2 || '#', |
| 36 |
showCta: opts.showCta !== false, |
| 37 |
showCta2: opts.showCta2 || false, |
| 38 |
textAlign: opts.textAlign || 'center', |
| 39 |
sectionHeight: opts.sectionHeight || 600, |
| 40 |
bgType: opts.bgType || 'gradient', |
| 41 |
bgColor: opts.bgColor || '#0a0a0f', |
| 42 |
bgColor2: opts.bgColor2 || '#1e1b4b', |
| 43 |
bgAngle: opts.bgAngle || 135, |
| 44 |
bgImage: opts.bgImage || '', |
| 45 |
bgImageOpacity:opts.bgImageOpacity !== undefined ? opts.bgImageOpacity : 0.5, |
| 46 |
noiseOpacity: opts.noiseOpacity !== undefined ? opts.noiseOpacity : 0.3, |
| 47 |
noiseTint: opts.noiseTint || 'dark', |
| 48 |
noiseFps: opts.noiseFps || 12, |
| 49 |
noiseScale: opts.noiseScale || 1, |
| 50 |
textColor: opts.textColor || '#ffffff', |
| 51 |
subtextColor: opts.subtextColor || '#94a3b8', |
| 52 |
ctaBg: opts.ctaBg || '#ffffff', |
| 53 |
ctaColor: opts.ctaColor || '#0a0a0f', |
| 54 |
ctaBg2: opts.ctaBg2 || 'transparent', |
| 55 |
ctaColor2: opts.ctaColor2 || '#ffffff', |
| 56 |
ctaBorder2: opts.ctaBorder2 || 'rgba(255,255,255,0.4)', |
| 57 |
fontSize: opts.fontSize || 72, |
| 58 |
fontWeight: opts.fontWeight || 800, |
| 59 |
lineHeight: opts.lineHeight || 1.05, |
| 60 |
letterSpacing: opts.letterSpacing === undefined ? -2 : opts.letterSpacing, |
| 61 |
subtextSize: opts.subtextSize || 20, |
| 62 |
maxWidth: opts.maxWidth || 800, |
| 63 |
paddingTop: opts.paddingTop || 100, |
| 64 |
paddingBottom: opts.paddingBottom || 100 |
| 65 |
}; |
| 66 |
|
| 67 |
/* Compute background */ |
| 68 |
var bg; |
| 69 |
if (a.bgType === 'gradient') bg = 'linear-gradient(' + a.bgAngle + 'deg, ' + a.bgColor + ', ' + a.bgColor2 + ')'; |
| 70 |
else bg = a.bgColor; |
| 71 |
|
| 72 |
var justify = a.textAlign === 'center' ? 'center' : a.textAlign === 'right' ? 'flex-end' : 'flex-start'; |
| 73 |
|
| 74 |
/* Section */ |
| 75 |
var section = document.createElement('section'); |
| 76 |
section.className = 'bkbg-ns-section'; |
| 77 |
var cs = section.style; |
| 78 |
cs.setProperty('--bkbg-ns-height', a.sectionHeight + 'px'); |
| 79 |
cs.setProperty('--bkbg-ns-bg', bg); |
| 80 |
cs.setProperty('--bkbg-ns-pt', a.paddingTop + 'px'); |
| 81 |
cs.setProperty('--bkbg-ns-pb', a.paddingBottom + 'px'); |
| 82 |
cs.setProperty('--bkbg-ns-align', a.textAlign); |
| 83 |
cs.setProperty('--bkbg-ns-justify', justify); |
| 84 |
cs.setProperty('--bkbg-ns-max-width', a.maxWidth + 'px'); |
| 85 |
cs.setProperty('--bkbg-ns-text-color',a.textColor); |
| 86 |
cs.setProperty('--bkbg-ns-sub-color', a.subtextColor); |
| 87 |
cs.setProperty('--bkbg-ns-font-size', a.fontSize + 'px'); |
| 88 |
cs.setProperty('--bkbg-ns-fw', String(a.fontWeight)); |
| 89 |
cs.setProperty('--bkbg-ns-lh', String(a.lineHeight)); |
| 90 |
cs.setProperty('--bkbg-ns-ls', a.letterSpacing + 'px'); |
| 91 |
cs.setProperty('--bkbg-ns-sub-size', a.subtextSize + 'px'); |
| 92 |
cs.setProperty('--bkbg-ns-cta-bg', a.ctaBg); |
| 93 |
cs.setProperty('--bkbg-ns-cta-color', a.ctaColor); |
| 94 |
cs.setProperty('--bkbg-ns-cta2-bg', a.ctaBg2); |
| 95 |
cs.setProperty('--bkbg-ns-cta2-color',a.ctaColor2); |
| 96 |
cs.setProperty('--bkbg-ns-cta2-border',a.ctaBorder2); |
| 97 |
|
| 98 |
/* Bg image */ |
| 99 |
if (a.bgType === 'image' && a.bgImage) { |
| 100 |
var imgDiv = document.createElement('div'); |
| 101 |
imgDiv.className = 'bkbg-ns-bg-img'; |
| 102 |
imgDiv.style.backgroundImage = 'url(' + a.bgImage + ')'; |
| 103 |
imgDiv.style.opacity = String(a.bgImageOpacity); |
| 104 |
section.appendChild(imgDiv); |
| 105 |
} |
| 106 |
|
| 107 |
/* Canvas grain */ |
| 108 |
var canvas = document.createElement('canvas'); |
| 109 |
canvas.className = 'bkbg-ns-grain'; |
| 110 |
section.appendChild(canvas); |
| 111 |
|
| 112 |
/* Content */ |
| 113 |
var content = document.createElement('div'); |
| 114 |
content.className = 'bkbg-ns-content'; |
| 115 |
|
| 116 |
var headingEl = document.createElement(a.tag); |
| 117 |
headingEl.className = 'bkbg-ns-heading'; |
| 118 |
headingEl.textContent = a.heading; |
| 119 |
content.appendChild(headingEl); |
| 120 |
|
| 121 |
if (a.subtext) { |
| 122 |
var subEl = document.createElement('p'); |
| 123 |
subEl.className = 'bkbg-ns-subtext'; |
| 124 |
subEl.textContent = a.subtext; |
| 125 |
content.appendChild(subEl); |
| 126 |
} |
| 127 |
|
| 128 |
var ctasDiv = document.createElement('div'); |
| 129 |
ctasDiv.className = 'bkbg-ns-ctas'; |
| 130 |
|
| 131 |
if (a.showCta && a.ctaLabel) { |
| 132 |
var cta1 = document.createElement('a'); |
| 133 |
cta1.className = 'bkbg-ns-cta'; |
| 134 |
cta1.href = a.ctaUrl; |
| 135 |
cta1.textContent = a.ctaLabel; |
| 136 |
ctasDiv.appendChild(cta1); |
| 137 |
} |
| 138 |
if (a.showCta2 && a.ctaLabel2) { |
| 139 |
var cta2 = document.createElement('a'); |
| 140 |
cta2.className = 'bkbg-ns-cta bkbg-ns-cta-secondary'; |
| 141 |
cta2.href = a.ctaUrl2; |
| 142 |
cta2.textContent = a.ctaLabel2; |
| 143 |
ctasDiv.appendChild(cta2); |
| 144 |
} |
| 145 |
if (ctasDiv.children.length) content.appendChild(ctasDiv); |
| 146 |
|
| 147 |
section.appendChild(content); |
| 148 |
appEl.replaceWith(section); |
| 149 |
|
| 150 |
/* Typography CSS vars */ |
| 151 |
typoCssVarsForEl(section, opts.headingTypo, '--bkbg-ns-hd-'); |
| 152 |
typoCssVarsForEl(section, opts.subtextTypo, '--bkbg-ns-st-'); |
| 153 |
|
| 154 |
/* --- Canvas grain animation --- */ |
| 155 |
var ctx = canvas.getContext('2d'); |
| 156 |
var pixelRatio = window.devicePixelRatio || 1; |
| 157 |
var frameInterval = 1000 / Math.max(1, a.noiseFps); |
| 158 |
var lastFrame = 0; |
| 159 |
var rafId = null; |
| 160 |
|
| 161 |
/* Tint color for noise pixels */ |
| 162 |
var tintR = 0, tintG = 0, tintB = 0; |
| 163 |
if (a.noiseTint === 'light') { tintR=255; tintG=255; tintB=220; } |
| 164 |
else if (a.noiseTint === 'colored') { tintR=120; tintG=80; tintB=200; } |
| 165 |
|
| 166 |
function resizeCanvas() { |
| 167 |
var w = section.offsetWidth || 800; |
| 168 |
var h = section.offsetHeight || a.sectionHeight; |
| 169 |
canvas.width = Math.ceil(w * pixelRatio / a.noiseScale); |
| 170 |
canvas.height = Math.ceil(h * pixelRatio / a.noiseScale); |
| 171 |
canvas.style.width = w + 'px'; |
| 172 |
canvas.style.height = h + 'px'; |
| 173 |
} |
| 174 |
|
| 175 |
function drawNoise(now) { |
| 176 |
if (now - lastFrame < frameInterval) return; |
| 177 |
lastFrame = now; |
| 178 |
|
| 179 |
var w = canvas.width, h = canvas.height; |
| 180 |
var imageData = ctx.createImageData(w, h); |
| 181 |
var data = imageData.data; |
| 182 |
var alpha = Math.round(a.noiseOpacity * 255); |
| 183 |
|
| 184 |
for (var i = 0; i < data.length; i += 4) { |
| 185 |
var v = Math.random() * 255 | 0; |
| 186 |
data[i] = tintR || v; |
| 187 |
data[i+1] = tintG || v; |
| 188 |
data[i+2] = tintB || v; |
| 189 |
data[i+3] = alpha; |
| 190 |
} |
| 191 |
ctx.putImageData(imageData, 0, 0); |
| 192 |
} |
| 193 |
|
| 194 |
function loop(now) { |
| 195 |
drawNoise(now); |
| 196 |
rafId = requestAnimationFrame(loop); |
| 197 |
} |
| 198 |
|
| 199 |
resizeCanvas(); |
| 200 |
window.addEventListener('resize', resizeCanvas); |
| 201 |
|
| 202 |
if ('IntersectionObserver' in window) { |
| 203 |
var obs = new IntersectionObserver(function(entries) { |
| 204 |
if (entries[0].isIntersecting) { if (!rafId) rafId = requestAnimationFrame(loop); } |
| 205 |
else { if (rafId) { cancelAnimationFrame(rafId); rafId = null; } } |
| 206 |
}, { threshold: 0 }); |
| 207 |
obs.observe(section); |
| 208 |
} else { |
| 209 |
rafId = requestAnimationFrame(loop); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
document.querySelectorAll('.bkbg-ns-app').forEach(initNoiseSection); |
| 214 |
})(); |
| 215 |
|