| 1 |
document.addEventListener('DOMContentLoaded', function () { |
| 2 |
var _typoKeys = { |
| 3 |
family:'font-family', weight:'font-weight', style:'font-style', |
| 4 |
transform:'text-transform', decoration:'text-decoration', |
| 5 |
sizeDesktop:'font-size-d', sizeTablet:'font-size-t', sizeMobile:'font-size-m', |
| 6 |
lineHeightDesktop:'line-height-d', lineHeightTablet:'line-height-t', lineHeightMobile:'line-height-m', |
| 7 |
letterSpacingDesktop:'letter-spacing-d', letterSpacingTablet:'letter-spacing-t', letterSpacingMobile:'letter-spacing-m', |
| 8 |
wordSpacingDesktop:'word-spacing-d', wordSpacingTablet:'word-spacing-t', wordSpacingMobile:'word-spacing-m' |
| 9 |
}; |
| 10 |
var _typoUnits = { size:'sizeUnit', lineHeight:'lineHeightUnit', letterSpacing:'letterSpacingUnit', wordSpacing:'wordSpacingUnit' }; |
| 11 |
var _typoUnitDefaults = { size:'px', lineHeight:'', letterSpacing:'px', wordSpacing:'px' }; |
| 12 |
function typoCssVarsForEl(el, obj, prefix) { |
| 13 |
if (!obj || typeof obj !== 'object') return; |
| 14 |
Object.keys(_typoKeys).forEach(function (k) { |
| 15 |
var v = obj[k]; if (v === undefined || v === '') return; |
| 16 |
var prop = _typoKeys[k]; |
| 17 |
var base = k.replace(/Desktop|Tablet|Mobile/, ''); |
| 18 |
var uKey = _typoUnits[base]; |
| 19 |
if (uKey && typeof v === 'number') v = v + (obj[uKey] || _typoUnitDefaults[base] || ''); |
| 20 |
el.style.setProperty('--' + prefix + prop, v); |
| 21 |
}); |
| 22 |
} |
| 23 |
|
| 24 |
document.querySelectorAll('.bkbg-3dt-app').forEach(function (root) { |
| 25 |
var opts = JSON.parse(root.dataset.opts || '{}'); |
| 26 |
init3DText(root, opts); |
| 27 |
}); |
| 28 |
}); |
| 29 |
|
| 30 |
function make3dShadow(a, depthOverride) { |
| 31 |
var depth = depthOverride !== undefined ? depthOverride : (a.depth || 8); |
| 32 |
var angle = (a.angle || 45) * Math.PI / 180; |
| 33 |
var dx = Math.cos(angle); |
| 34 |
var dy = Math.sin(angle); |
| 35 |
var shads = []; |
| 36 |
var style3d = a.style3d || 'extrude'; |
| 37 |
|
| 38 |
if (style3d === 'extrude') { |
| 39 |
for (var i = 1; i <= depth; i++) { |
| 40 |
shads.push((dx * i).toFixed(1) + 'px ' + (dy * i).toFixed(1) + 'px 0 ' + (a.depthColor || '#312e81')); |
| 41 |
} |
| 42 |
shads.push((dx * depth + 4).toFixed(1) + 'px ' + (dy * depth + 6).toFixed(1) + 'px 8px ' + (a.shadowColor || 'rgba(0,0,0,0.25)')); |
| 43 |
} else if (style3d === 'longshadow') { |
| 44 |
var steps = depth * 4; |
| 45 |
for (var i = 1; i <= steps; i++) { |
| 46 |
shads.push(i + 'px ' + i + 'px 0 ' + (a.depthColor || '#312e81')); |
| 47 |
} |
| 48 |
} else if (style3d === 'retro') { |
| 49 |
for (var i = 1; i <= depth; i++) { |
| 50 |
shads.push(i + 'px ' + i + 'px 0 ' + (a.depthColor || '#312e81')); |
| 51 |
} |
| 52 |
shads.push('0 0 0 3px ' + (a.depthColor || '#312e81')); |
| 53 |
shads.push((depth + 4) + 'px ' + (depth + 6) + 'px 10px ' + (a.shadowColor || 'rgba(0,0,0,0.25)')); |
| 54 |
} else if (style3d === 'isometric') { |
| 55 |
for (var i = 1; i <= depth; i++) { |
| 56 |
shads.push(i + 'px ' + Math.round(i * 0.5) + 'px 0 ' + (a.depthColor || '#312e81')); |
| 57 |
} |
| 58 |
shads.push((depth + 3) + 'px ' + Math.round(depth * 0.5 + 4) + 'px 8px ' + (a.shadowColor || 'rgba(0,0,0,0.25)')); |
| 59 |
} else if (style3d === 'emboss') { |
| 60 |
shads.push('-1px -1px 2px rgba(255,255,255,0.3)'); |
| 61 |
shads.push('1px 1px 2px rgba(0,0,0,0.5)'); |
| 62 |
for (var i = 1; i <= depth; i++) { |
| 63 |
shads.push(i + 'px ' + i + 'px 0 ' + (a.depthColor || '#312e81')); |
| 64 |
} |
| 65 |
} |
| 66 |
return shads.join(', '); |
| 67 |
} |
| 68 |
|
| 69 |
function init3DText(root, a) { |
| 70 |
var Tag = a.tag || 'h2'; |
| 71 |
var text = a.text || '3D TEXT'; |
| 72 |
var depth = a.depth || 8; |
| 73 |
|
| 74 |
/* wrap */ |
| 75 |
var wrap = document.createElement('div'); |
| 76 |
wrap.className = 'bkbg-3dt-wrap bkbg-3dt-hover-' + (a.hoverEffect || 'tilt'); |
| 77 |
wrap.style.cssText = [ |
| 78 |
'background:' + (a.showBg ? (a.backgroundColor || '#0f172a') : 'transparent'), |
| 79 |
'padding:' + (a.paddingV || 48) + 'px ' + (a.paddingH || 32) + 'px', |
| 80 |
'border-radius:' + (a.borderRadius || 0) + 'px', |
| 81 |
'text-align:' + (a.textAlign || 'center'), |
| 82 |
'overflow:hidden', |
| 83 |
'position:relative', |
| 84 |
].join(';'); |
| 85 |
|
| 86 |
/* text element */ |
| 87 |
var textEl = document.createElement(Tag); |
| 88 |
textEl.className = 'bkbg-3dt-text bkbg-3dt-anim-' + (a.animation || 'none'); |
| 89 |
textEl.textContent = text; |
| 90 |
|
| 91 |
var baseTextShadow = make3dShadow(a); |
| 92 |
|
| 93 |
var ts = [ |
| 94 |
'margin:0', |
| 95 |
'display:block', |
| 96 |
'cursor:default', |
| 97 |
'text-shadow:' + baseTextShadow, |
| 98 |
]; |
| 99 |
|
| 100 |
if (a.gradientText) { |
| 101 |
ts.push('background:linear-gradient(135deg,' + (a.gradientFrom || '#a78bfa') + ' 0%,' + (a.gradientTo || '#60a5fa') + ' 100%)'); |
| 102 |
ts.push('-webkit-background-clip:text'); |
| 103 |
ts.push('-webkit-text-fill-color:transparent'); |
| 104 |
ts.push('background-clip:text'); |
| 105 |
ts.push('color:transparent'); |
| 106 |
} else { |
| 107 |
ts.push('color:' + (a.textColor || '#6366f1')); |
| 108 |
} |
| 109 |
textEl.style.cssText = ts.join(';'); |
| 110 |
|
| 111 |
wrap.appendChild(textEl); |
| 112 |
|
| 113 |
/* reflection */ |
| 114 |
if (a.showReflection) { |
| 115 |
var reflEl = document.createElement(Tag); |
| 116 |
reflEl.className = 'bkbg-3dt-text bkbg-3dt-reflection'; |
| 117 |
reflEl.textContent = text; |
| 118 |
reflEl.style.cssText = textEl.style.cssText; |
| 119 |
wrap.appendChild(reflEl); |
| 120 |
} |
| 121 |
|
| 122 |
/* Apply typo CSS vars on root (cascades to .bkbg-3dt-text) */ |
| 123 |
typoCssVarsForEl(root, a.textTypo, 'bk3dt-tx-'); |
| 124 |
|
| 125 |
root.appendChild(wrap); |
| 126 |
|
| 127 |
/* hover: tilt */ |
| 128 |
if (a.hoverEffect === 'tilt') { |
| 129 |
wrap.style.perspective = '600px'; |
| 130 |
wrap.addEventListener('mousemove', function (e) { |
| 131 |
var rect = wrap.getBoundingClientRect(); |
| 132 |
var cx = rect.left + rect.width / 2; |
| 133 |
var cy = rect.top + rect.height / 2; |
| 134 |
var rx = ((e.clientY - cy) / (rect.height / 2)) * -12; |
| 135 |
var ry = ((e.clientX - cx) / (rect.width / 2)) * 12; |
| 136 |
textEl.style.transform = 'rotateX(' + rx + 'deg) rotateY(' + ry + 'deg)'; |
| 137 |
}); |
| 138 |
wrap.addEventListener('mouseleave', function () { |
| 139 |
textEl.style.transform = 'rotateX(0) rotateY(0)'; |
| 140 |
}); |
| 141 |
} |
| 142 |
|
| 143 |
/* hover: press — animate depth collapse */ |
| 144 |
if (a.hoverEffect === 'press') { |
| 145 |
var pressing = false; |
| 146 |
wrap.addEventListener('mouseenter', function () { |
| 147 |
if (pressing) return; |
| 148 |
pressing = true; |
| 149 |
var cur = depth; |
| 150 |
var iv = setInterval(function () { |
| 151 |
cur = Math.max(0, cur - 1); |
| 152 |
textEl.style.textShadow = make3dShadow(a, cur); |
| 153 |
if (cur <= 0) clearInterval(iv); |
| 154 |
}, 20); |
| 155 |
}); |
| 156 |
wrap.addEventListener('mouseleave', function () { |
| 157 |
pressing = false; |
| 158 |
var cur = 0; |
| 159 |
var iv = setInterval(function () { |
| 160 |
cur = Math.min(depth, cur + 1); |
| 161 |
textEl.style.textShadow = make3dShadow(a, cur); |
| 162 |
if (cur >= depth) clearInterval(iv); |
| 163 |
}, 20); |
| 164 |
}); |
| 165 |
} |
| 166 |
} |
| 167 |
|