'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function getOffset(element) { const bounding = element.getBoundingClientRect(); return { top: bounding.top, left: bounding.left }; } function getXY(element, e) { const offset = getOffset(element); const x = e.pageX - offset.left - window.scrollX; const y = e.pageY - offset.top - window.scrollY; return [x, y]; } function intersectRect(r1, r2) { return !(r2.left > r1.right || r2.right < r1.left || r2.top > r1.bottom || r2.bottom < r1.top); } function isIntersected(element, cursor_x, cursor_y, gradientSize) { const cursor_area = { left: cursor_x - gradientSize, right: cursor_x + gradientSize, top: cursor_y - gradientSize, bottom: cursor_y + gradientSize }; const bounding = element.getBoundingClientRect(); const el_area = { left: bounding.left, right: bounding.right, top: bounding.top, bottom: bounding.bottom }; const result = intersectRect(cursor_area, el_area); return result; } function lightHoverEffect(gradientSize, x, y, lightColor) { return `radial-gradient(circle ${gradientSize}px at ${x}px ${y}px, ${lightColor}, rgba(255,255,255,0))`; } function lightClickEffect(gradientSize, x, y, lightColor) { return `${lightHoverEffect(gradientSize, x, y, lightColor)}, radial-gradient(circle ${70}px at ${x}px ${y}px, rgba(255,255,255,0), ${lightColor}, rgba(255,255,255,0), rgba(255,255,255,0))`; } function drawEffect(element, x, y, lightColor, gradientSize, cssLightEffect = null) { const lightBg = cssLightEffect === null ? lightHoverEffect(gradientSize, x, y, lightColor) : cssLightEffect; element.style.backgroundImage = lightBg; } function drawHoverEffect(element, lightColor, gradientSize, e) { const [x, y] = getXY(element, e); drawEffect(element, x, y, lightColor, gradientSize); } function drawClickEffect(element, lightColor, gradientSize, e) { const [x, y] = getXY(element, e); const cssLightEffect = lightClickEffect(gradientSize, x, y, lightColor); drawEffect(element, x, y, lightColor, gradientSize, cssLightEffect); } function clearEffect(resource, is_pressed) { is_pressed[0] = false; resource.el.style.backgroundImage = resource.oriBg; } function drawContainerHoverEffect(resource, lightColor, gradientSize, is_pressed, e) { const element = resource.el; if (isIntersected(element, e.clientX, e.clientY, gradientSize)) drawHoverEffect(element, lightColor, gradientSize, e); else clearEffect(resource, is_pressed); } function enableBackgroundEffects(resource, lightColor, gradientSize, clickEffect, is_pressed) { const element = resource.el; element.addEventListener("mousemove", (e) => { if (clickEffect && is_pressed[0]) drawClickEffect(element, lightColor, gradientSize, e); else drawHoverEffect(element, lightColor, gradientSize, e); }); element.addEventListener("mouseleave", (e) => { clearEffect(resource, is_pressed); }); } function enableBorderEffects(resource, childrenBorders, options, is_pressed) { const element = resource.el; const childrenBorderL = childrenBorders.length; element.addEventListener("mousemove", (e) => { for (let i = 0; i < childrenBorderL; i++) drawContainerHoverEffect(childrenBorders[i], options.lightColor, options.gradientSize, is_pressed, e); }); element.addEventListener("mouseleave", (e) => { for (let i = 0; i < childrenBorderL; i++) clearEffect(childrenBorders[i], is_pressed); }); } function enableClickEffects(resource, lightColor, gradientSize, is_pressed) { const element = resource.el; element.addEventListener("mousedown", (e) => { is_pressed[0] = true; drawClickEffect(element, lightColor, gradientSize, e); }); element.addEventListener("mouseup", (e) => { is_pressed[0] = false; drawHoverEffect(element, lightColor, gradientSize, e); }); } function enableNormalBackgroundEffetcs(resource, options, is_pressed) { enableBackgroundEffects(resource, options.lightColor, options.gradientSize, options.clickEffect, is_pressed); } function enableChildrenBackgroundEffetcs(resource, options, is_pressed) { enableBackgroundEffects(resource, options.children.lightColor, options.children.gradientSize, options.clickEffect, is_pressed); } function enableNormalClickEffects(resource, options, is_pressed) { enableClickEffects(resource, options.lightColor, options.gradientSize, is_pressed); } function enableChildrenClickEffects(resource, options, is_pressed) { enableClickEffects(resource, options.children.lightColor, options.children.gradientSize, is_pressed); } function preProcessElement(element) { return { oriBg: getComputedStyle(element).backgroundImage, el: element }; } function preProcessElements(elements) { const ressources = []; const elementsL = elements.length; for (let i = 0; i < elementsL; i++) { const element = elements[i]; ressources.push(preProcessElement(element)); } return ressources; } function preProcessSelector(selector) { return preProcessElements(document.querySelectorAll(selector)); } function applyEffectOption(userOptions) { const defaultOptions = { lightColor: "rgba(255,255,255,0.25)", gradientSize: 150, clickEffect: false, isContainer: false, children: { borderSelector: ".eff-reveal-border", elementSelector: ".eff-reveal", lightColor: "rgba(255,255,255,0.25)", gradientSize: 150 } }; return Object.assign(defaultOptions, userOptions); } function applyChildrenElementEffect(resource, options, is_pressed, enableBackgroundEffectsFunc, enableClickEffectsFunc) { enableBackgroundEffectsFunc(resource, options, is_pressed); if (options.clickEffect) enableClickEffectsFunc(resource, options, is_pressed); } function applyChildrenEffect(resources, options, is_pressed, enableBackgroundEffectsFunc, enableClickEffectsFunc) { const resourceL = resources.length; for (let i = 0; i < resourceL; i++) { const resource = resources[i]; applyChildrenElementEffect( resource, options, is_pressed, enableBackgroundEffectsFunc, enableClickEffectsFunc ); } } function applyContainerElementEffect(resource, options, is_pressed, enableBackgroundEffectsFunc, enableClickEffectsFunc) { const childrenBorders = preProcessSelector(options.children.borderSelector); enableBorderEffects(resource, childrenBorders, options, is_pressed); const childrens = preProcessSelector(options.children.elementSelector); applyChildrenEffect( childrens, options, is_pressed, enableBackgroundEffectsFunc, enableClickEffectsFunc ); } function applyContainerEffect(resources, options, is_pressed, enableBackgroundEffectsFunc, enableClickEffectsFunc) { const resourceL = resources.length; for (let i = 0; i < resourceL; i++) { const resource = resources[i]; applyContainerElementEffect( resource, options, is_pressed, enableBackgroundEffectsFunc, enableClickEffectsFunc ); } } function applyElementEffect(element, userOptions = {}) { const is_pressed = [false]; const options = applyEffectOption(userOptions); const resource = preProcessElement(element); if (!options.isContainer) { const enableBackgroundEffectsFunc = enableNormalBackgroundEffetcs; const enableClickEffectsFunc = enableNormalClickEffects; applyChildrenElementEffect( resource, options, is_pressed, enableBackgroundEffectsFunc, enableClickEffectsFunc ); } else { const enableBackgroundEffectsFunc = enableChildrenBackgroundEffetcs; const enableClickEffectsFunc = enableChildrenClickEffects; applyContainerElementEffect( resource, options, is_pressed, enableBackgroundEffectsFunc, enableClickEffectsFunc ); } } function applyElementsEffect(elements, userOptions = {}) { const is_pressed = [false]; const options = applyEffectOption(userOptions); const resources = preProcessElements(elements); if (!options.isContainer) { const enableBackgroundEffectsFunc = enableNormalBackgroundEffetcs; const enableClickEffectsFunc = enableNormalClickEffects; applyChildrenEffect( resources, options, is_pressed, enableBackgroundEffectsFunc, enableClickEffectsFunc ); } else { const enableBackgroundEffectsFunc = enableChildrenBackgroundEffetcs; const enableClickEffectsFunc = enableChildrenClickEffects; applyContainerEffect( resources, options, is_pressed, enableBackgroundEffectsFunc, enableClickEffectsFunc ); } } function applyEffect(selector, userOptions = {}) { const is_pressed = [false]; const options = applyEffectOption(userOptions); const resoures = preProcessSelector(selector); if (!options.isContainer) { const enableBackgroundEffectsFunc = enableNormalBackgroundEffetcs; const enableClickEffectsFunc = enableNormalClickEffects; applyChildrenEffect( resoures, options, is_pressed, enableBackgroundEffectsFunc, enableClickEffectsFunc ); } else { const enableBackgroundEffectsFunc = enableChildrenBackgroundEffetcs; const enableClickEffectsFunc = enableChildrenClickEffects; applyContainerEffect( resoures, options, is_pressed, enableBackgroundEffectsFunc, enableClickEffectsFunc ); } } const FluentRevealEffect = { applyEffect, applyElementEffect, applyElementsEffect }; exports.FluentRevealEffect = FluentRevealEffect;