Код полностью скопировать и вставить в T123:
  1. Работает только для типа Изображение
  2. Внутри изображения должен быть SVG элемент с полу прозрачной фоновой заливкой (прозрачность любая). Обводка по необходимости
  3. На элемент в зеро вешаем Фоновую размытость в любом проценте
  4. T123 с кодом можно сделать общий в head сайта, в шапку или подвал (как удобнее)
  5. Работает для всех изображений на сайте с фоновым размытием
  6. Готово
<!-- Автоматическая маска по прозрачности изображений Zero Block с включённым размытием фона; отдельный класс не нужен. --> <script> (function () { 'use strict'; if (window.tildaAutoGlassStarted) return; window.tildaAutoGlassStarted = true; var states = new WeakMap(); var properties = ['mask-image', '-webkit-mask-image', 'mask-size', '-webkit-mask-size', 'mask-repeat', '-webkit-mask-repeat', 'mask-mode']; var scanTimer; function hasBlur(element) { var style = getComputedStyle(element); return /\bblur\(/.test(style.backdropFilter || style.webkitBackdropFilter || ''); } function restore(element, state) { state.token++; state.pending = ''; if (!state.owned) return; state.owned = false; properties.forEach(function (name) { var previous = state.previous[name]; if (previous.value) element.style.setProperty(name, previous.value, previous.priority); else element.style.removeProperty(name); }); } function apply(element, state) { if (state.owned && element.style.getPropertyValue('mask-image') === state.mask) return; if (!state.owned) { state.previous = {}; properties.forEach(function (name) { state.previous[name] = { value: element.style.getPropertyValue(name), priority: element.style.getPropertyPriority(name) }; }); state.owned = true; } element.style.setProperty('-webkit-mask-image', state.mask); element.style.setProperty('mask-image', state.mask); element.style.setProperty('-webkit-mask-size', '100% 100%'); element.style.setProperty('mask-size', '100% 100%'); element.style.setProperty('-webkit-mask-repeat', 'no-repeat'); element.style.setProperty('mask-repeat', 'no-repeat'); element.style.setProperty('mask-mode', 'alpha'); } function prepare(element) { var image = element.querySelector('img'); if (!image) return; var state = states.get(element); if (!hasBlur(element)) { if (state) restore(element, state); return; } var existing = getComputedStyle(element).maskImage || getComputedStyle(element).webkitMaskImage; if ((!state || !state.owned) && existing && existing !== 'none') return; if (!state) { state = { source: '', pending: '', failed: '', token: 0, owned: false, mask: '' }; states.set(element, state); } function update() { var source = image.getAttribute('data-original') || image.currentSrc || image.src; if (!source || source === state.pending || source === state.failed) return; if (source === state.source && state.mask) { apply(element, state); return; } var token = ++state.token; state.pending = source; var copy = new Image(); copy.crossOrigin = 'anonymous'; copy.onload = function () { if (token !== state.token || !element.isConnected || !hasBlur(element)) return; try { var width = copy.naturalWidth; var height = copy.naturalHeight; if (!width || !height) throw new Error('Пустое изображение'); var scale = Math.min(3, 2048 / Math.max(width, height)); var canvas = document.createElement('canvas'); canvas.width = Math.max(1, Math.round(width * scale)); canvas.height = Math.max(1, Math.round(height * scale)); var context = canvas.getContext('2d', { willReadFrequently: true }); context.drawImage(copy, 0, 0, canvas.width, canvas.height); var pixels = context.getImageData(0, 0, canvas.width, canvas.height); for (var i = 0; i < pixels.data.length; i += 4) { pixels.data[i] = pixels.data[i + 1] = pixels.data[i + 2] = 255; pixels.data[i + 3] = pixels.data[i + 3] > 0 ? 255 : 0; } context.putImageData(pixels, 0, 0); state.mask = 'url("' + canvas.toDataURL('image/png') + '")'; state.source = source; state.pending = ''; state.failed = ''; apply(element, state); } catch (error) { state.pending = ''; state.failed = source; console.warn('Auto Glass: не удалось подготовить силуэт изображения.', error); } }; copy.onerror = function () { if (token !== state.token) return; state.pending = ''; state.failed = source; console.warn('Auto Glass: файл изображения не загрузился. Используйте файл, загруженный в Тильду.'); }; copy.src = source; } update(); } function scan() { document.querySelectorAll('.tn-elem[data-elem-type="image"]').forEach(prepare); } function scheduleScan() { clearTimeout(scanTimer); scanTimer = setTimeout(scan, 50); } function start() { scan(); new MutationObserver(scheduleScan).observe(document.getElementById('allrecords') || document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['class', 'style', 'src', 'srcset', 'data-original'] }); document.addEventListener('load', scheduleScan, true); window.addEventListener('resize', scheduleScan); } if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', start); else start(); }()); </script>
Made on
Tilda