mirror of
https://gitlab.com/vyla-entertainment/home-frontend.git
synced 2026-11-17 02:47:19 -05:00
updated site ( crazy )
This commit is contained in:
121
src/scripts/animations/glass-surface.js
Normal file
121
src/scripts/animations/glass-surface.js
Normal file
@@ -0,0 +1,121 @@
|
||||
let globalSvgDefs = null;
|
||||
|
||||
function ensureGlobalDefs() {
|
||||
if (!globalSvgDefs) {
|
||||
globalSvgDefs = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
||||
globalSvgDefs.setAttribute('id', 'glass-surface-defs');
|
||||
globalSvgDefs.setAttribute('aria-hidden', 'true');
|
||||
globalSvgDefs.style.cssText = 'position:fixed;top:0;left:0;width:0;height:0;pointer-events:none;z-index:-9999;opacity:0;';
|
||||
document.body.appendChild(globalSvgDefs);
|
||||
}
|
||||
return globalSvgDefs;
|
||||
}
|
||||
|
||||
export function initGlassSurface(selector, options = {}) {
|
||||
const elements = typeof selector === 'string' ? document.querySelectorAll(selector) : [selector];
|
||||
const defsContainer = ensureGlobalDefs();
|
||||
|
||||
elements.forEach((el, index) => {
|
||||
if (!el || el.dataset.glassInitialized) return;
|
||||
el.dataset.glassInitialized = 'true';
|
||||
|
||||
const id = `glass-surf-${index}-${Math.random().toString(36).slice(2, 7)}`;
|
||||
const filterId = `filter-${id}`;
|
||||
const redGradId = `red-grad-${id}`;
|
||||
const blueGradId = `blue-grad-${id}`;
|
||||
|
||||
const config = {
|
||||
borderRadius: options.borderRadius !== undefined ? Number(options.borderRadius) : 50,
|
||||
borderWidth: options.borderWidth !== undefined ? Number(options.borderWidth) : 0,
|
||||
brightness: options.brightness !== undefined ? Number(options.brightness) : 100,
|
||||
opacity: options.opacity !== undefined ? Number(options.opacity) : 0.93,
|
||||
blur: options.blur !== undefined ? Number(options.blur) : 15,
|
||||
displace: options.displace !== undefined ? Number(options.displace) : 0.5,
|
||||
backgroundOpacity: options.backgroundOpacity !== undefined ? Number(options.backgroundOpacity) : 0,
|
||||
saturation: options.saturation !== undefined ? Number(options.saturation) : 0,
|
||||
distortionScale: options.distortionScale !== undefined ? Number(options.distortionScale) : -180,
|
||||
redOffset: options.redOffset !== undefined ? Number(options.redOffset) : -50,
|
||||
greenOffset: options.greenOffset !== undefined ? Number(options.greenOffset) : -50,
|
||||
blueOffset: options.blueOffset !== undefined ? Number(options.blueOffset) : 20,
|
||||
xChannel: options.xChannel || 'R',
|
||||
yChannel: options.yChannel || 'G',
|
||||
mixBlendMode: options.mixBlendMode || 'difference'
|
||||
};
|
||||
|
||||
const filterGroup = document.createElementNS('http://www.w3.org/2000/svg', 'defs');
|
||||
filterGroup.innerHTML = `
|
||||
<filter id="${filterId}" color-interpolation-filters="sRGB" x="-50%" y="-50%" width="200%" height="200%">
|
||||
<feImage id="fe-map-${id}" x="0" y="0" width="100%" height="100%" preserveAspectRatio="none" result="map" />
|
||||
<feDisplacementMap id="disp-red-${id}" in="SourceGraphic" in2="map" scale="${config.distortionScale + config.redOffset}" xChannelSelector="${config.xChannel}" yChannelSelector="${config.yChannel}" result="dispRed" />
|
||||
<feColorMatrix in="dispRed" type="matrix" values="1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0" result="red" />
|
||||
<feDisplacementMap id="disp-green-${id}" in="SourceGraphic" in2="map" scale="${config.distortionScale + config.greenOffset}" xChannelSelector="${config.xChannel}" yChannelSelector="${config.yChannel}" result="dispGreen" />
|
||||
<feColorMatrix in="dispGreen" type="matrix" values="0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0" result="green" />
|
||||
<feDisplacementMap id="disp-blue-${id}" in="SourceGraphic" in2="map" scale="${config.distortionScale + config.blueOffset}" xChannelSelector="${config.xChannel}" yChannelSelector="${config.yChannel}" result="dispBlue" />
|
||||
<feColorMatrix in="dispBlue" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0" result="blue" />
|
||||
<feBlend in="red" in2="green" mode="screen" result="rg" />
|
||||
<feBlend in="rg" in2="blue" mode="screen" result="output" />
|
||||
<feGaussianBlur id="blur-${id}" in="output" stdDeviation="${config.displace}" />
|
||||
</filter>
|
||||
`;
|
||||
defsContainer.appendChild(filterGroup);
|
||||
|
||||
const rawContent = el.innerHTML;
|
||||
el.innerHTML = '';
|
||||
|
||||
const backdropLayer = document.createElement('span');
|
||||
backdropLayer.className = 'glass-surface__backdrop';
|
||||
|
||||
const contentLayer = document.createElement('span');
|
||||
contentLayer.className = 'glass-surface__content';
|
||||
contentLayer.innerHTML = rawContent;
|
||||
|
||||
el.appendChild(backdropLayer);
|
||||
el.appendChild(contentLayer);
|
||||
el.classList.add('glass-surface');
|
||||
|
||||
el.style.borderRadius = `${config.borderRadius}px`;
|
||||
el.style.setProperty('--glass-radius', `${config.borderRadius}px`);
|
||||
el.style.setProperty('--glass-saturation', config.saturation.toString());
|
||||
|
||||
const feImage = defsContainer.querySelector(`#fe-map-${id}`);
|
||||
|
||||
const updateMap = () => {
|
||||
const rect = el.getBoundingClientRect();
|
||||
const width = Math.max(Math.round(rect.width), 40);
|
||||
const height = Math.max(Math.round(rect.height), 20);
|
||||
const edgeSize = Math.min(width, height) * (config.borderWidth * 0.5);
|
||||
|
||||
const svgContent = `
|
||||
<svg viewBox="0 0 ${width} ${height}" xmlns="http://www.w3.org/2000/svg">
|
||||
<defs>
|
||||
<linearGradient id="${redGradId}" x1="100%" y1="0%" x2="0%" y2="0%">
|
||||
<stop offset="0%" stop-color="#0000"/>
|
||||
<stop offset="100%" stop-color="red"/>
|
||||
</linearGradient>
|
||||
<linearGradient id="${blueGradId}" x1="0%" y1="0%" x2="0%" y2="100%">
|
||||
<stop offset="0%" stop-color="#0000"/>
|
||||
<stop offset="100%" stop-color="blue"/>
|
||||
</linearGradient>
|
||||
</defs>
|
||||
<rect x="0" y="0" width="${width}" height="${height}" fill="black"></rect>
|
||||
<rect x="0" y="0" width="${width}" height="${height}" rx="${config.borderRadius}" fill="url(#${redGradId})" />
|
||||
<rect x="0" y="0" width="${width}" height="${height}" rx="${config.borderRadius}" fill="url(#${blueGradId})" style="mix-blend-mode: ${config.mixBlendMode}" />
|
||||
<rect x="${edgeSize}" y="${edgeSize}" width="${Math.max(0, width - edgeSize * 2)}" height="${Math.max(0, height - edgeSize * 2)}" rx="${config.borderRadius}" fill="hsl(0 0% ${config.brightness}% / ${config.opacity})" style="filter:blur(${config.blur}px)" />
|
||||
</svg>
|
||||
`;
|
||||
|
||||
if (feImage) {
|
||||
feImage.setAttribute('href', `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svgContent)}`);
|
||||
}
|
||||
|
||||
el.style.setProperty('--glass-filter-id', `url(#${filterId})`);
|
||||
el.style.setProperty('--glass-radius', `${config.borderRadius}px`);
|
||||
el.style.setProperty('--glass-saturation', config.saturation.toString());
|
||||
el.style.borderRadius = `${config.borderRadius}px`;
|
||||
};
|
||||
|
||||
updateMap();
|
||||
const ro = new ResizeObserver(() => requestAnimationFrame(updateMap));
|
||||
ro.observe(el);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user