2601* @param {HTMLElement} animCore - .tn-atom вектора2602* @param {{isHidden: boolean, base: {fill: sbsRGBA | sbsShapeUrlPaint | undefined, stroke: sbsRGBA | sbsShapeUrlPaint | undefined}}[]} shapes2603* - все фигуры вектора: isHidden - внутри clipPath, mask, pattern или градиента, base - собственные заливка и обводка2604* @param {('fill' | 'stroke')[]} props - свойства, цвет которых есть в шагах2605* @param {boolean} isPlaying2606* @returns {{urls: Set<string>, stops: {prop: 'fill' | 'stroke', node: Element, color: sbsRGBA}[]}} - urls - вычисленные2607* значения url(...) фигур на этих градиентах, stops - стопы этих градиентов с исходными цветами2608*/2609function t_animationSBS__getSmoothGradients(animCore, shapes, props, isPlaying) {2610const result = { urls: new Set(), stops: [] };
const usages = new Map(); shapes.forEach(({ isHidden, base }) => { ['fill', 'stroke'].forEach(prop => { const gradient = t_animationSBS__getShapePaintServer(base[prop], animCore); if (!gradient) return; if (!usages.has(gradient)) usages.set(gradient, []); usages.get(gradient).push({ prop, isHidden, value: base[prop] }); }); }); if (!usages.size) return result; const getHref = node => node.getAttribute('href') || node.getAttributeNS('http://www.w3.org/1999/xlink', 'href'); const hrefs = new Set(); animCore.querySelectorAll('*').forEach(node => { const href = getHref(node); if (href) hrefs.add(href); }); usages.forEach((uses, gradient) => { const { prop, value } = uses[0]; const isSingleUse = uses.every( use => !use.isHidden && use.prop === prop && use.value.fillOpacity === value.fillOpacity ); if (!isSingleUse || !props.includes(prop) || getHref(gradient) || hrefs.has(`#${gradient.id}`)) return; const stopNodes = Array.from(gradient.children).filter(node => node.localName.toLowerCase() === 'stop'); const colors = stopNodes.map(node => t_animationSBS__getStopBaseColor(node, isPlaying)); if (!colors.length || colors.some(color => !color)) return; uses.forEach(use => result.urls.add(use.value.url)); const opacity = prop === 'fill' ? value.fillOpacity : 1; stopNodes.forEach((node, i) => { const [r, g, b, a] = colors[i]; result.stops.push({ prop, node, color: [r, g, b, a * opacity] }); }); }); return result;}/** * Градиент, на который ссылается заливка или обводка фигуры: url(#id) резолвится в первый элемент документа * с этим id, он должен быть linearGradient или radialGradient внутри svg вектора * * @param {sbsRGBA | sbsShapeUrlPaint | null | undefined} value - собственная заливка или обводка фигуры * @param {HTMLElement} animCore - .tn-atom вектора * @returns {Element | null} */function t_animationSBS__getShapePaintServer(value, animCore) { if (!t_animationSBS__isShapeUrlPaint(value)) return null; // вычисленное значение - url("#id"), у некоторых браузеров с адресом страницы, может быть с цветом-фолбэком const match = value.url.match(/^url\(\s*(['"]?)([^'"]*?)\1\s*\)/); const ref = match ? match[2] : ''; const hashIndex = ref.indexOf('#'); if (hashIndex === -1) return null; const path = ref.slice(0, hashIndex); if (path && path !== window.location.href.split('#')[0]) return null; const node = document.getElementById(ref.slice(hashIndex + 1)); return node && animCore.contains(node) && t_animationSBS__isGradientNode(node) ? node : null;}/** * Собственный цвет стопа: stop-opacity сразу умножается на прозрачность цвета, а на время анимации стопу ставится * stop-opacity: 1. Так цвет и прозрачность стопа меняются согласованно, с premultiplied alpha: у стопа по умолчанию * (черный со stop-opacity 0) на переходе к цвету шага не появляется черный. Кешируется, как цвета фигур, * см. t_animationSBS__getShapeBaseColors * * @param {Element} stop * @param {boolean} isPlaying * @returns {sbsRGBA | null} - null для нераспознанного цвета */function t_animationSBS__getStopBaseColor(stop, isPlaying) { if (!isPlaying || stop.sbsColorBase === undefined) { const computedStyles = getComputedStyle(stop); const color = t_animationSBS__parseColor(computedStyles.getPropertyValue('stop-color')); let stopOpacity = parseFloat(computedStyles.getPropertyValue('stop-opacity')); if (isNaN(stopOpacity)) stopOpacity = 1; stop.sbsColorBase = color ? [color[0], color[1], color[2], color[3] * stopOpacity] : null; } return stop.sbsColorBase;}/** * @param {Element} shape * @param {HTMLElement} animCore * @returns {boolean} - фигура лежит внутри clipPath, mask, pattern или градиента: ее цвет - не видимая заливка */function t_animationSBS__isShapeInPaintServer(shape, animCore) { const paintServers = ['clippath', 'mask', 'pattern', 'lineargradient', 'radialgradient']; for (let current = shape.parentElement; current && current !== animCore; current = current.parentElement) { if (paintServers.includes(current.localName.toLowerCase())) return true; }