hesabixWpTheme/assets/js/404.js

222 lines
7.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

(function () {
'use strict';
var root = document.querySelector('.hx-404');
if (!root) {
return;
}
var reducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
/* ——— جملات چرخشی ——— */
var wittyEl = document.getElementById('hx-404-witty');
if (wittyEl && wittyEl.dataset.lines) {
try {
var lines = JSON.parse(wittyEl.dataset.lines);
if (lines.length > 1 && !reducedMotion) {
var lineIdx = 0;
setInterval(function () {
wittyEl.classList.add('is-fading');
setTimeout(function () {
lineIdx = (lineIdx + 1) % lines.length;
wittyEl.textContent = lines[lineIdx];
wittyEl.classList.remove('is-fading');
}, 350);
}, 4500);
}
} catch (e) { /* noop */ }
}
/* ——— گلیچ رقم ۴ ——— */
var digit4b = document.querySelector('.hx-404__digit--4b');
if (digit4b && !reducedMotion) {
setInterval(function () {
digit4b.classList.add('is-glitch');
setTimeout(function () {
digit4b.classList.remove('is-glitch');
}, 350);
}, 3200 + Math.random() * 2000);
}
/* ——— تراز کردن دفتر ——— */
var balanceBtn = document.getElementById('hx-404-balance-btn');
var balanceSuccess = document.getElementById('hx-404-balance-success');
var balanceHint = document.getElementById('hx-404-balance-hint');
var invoiceTotal = document.querySelector('.hx-404__invoice-total');
var scaleRightWeight = document.querySelector('.hx-404__scale-pan--right .hx-404__scale-weight');
function spawnConfetti(originX, originY) {
if (reducedMotion) {
return;
}
var colors = ['#0ea5e9', '#f97316', '#059669', '#7c3aed', '#38bdf8'];
for (var i = 0; i < 24; i++) {
var piece = document.createElement('span');
piece.className = 'hx-404__confetti';
piece.style.left = originX + 'px';
piece.style.top = originY + 'px';
piece.style.background = colors[i % colors.length];
var angle = (Math.PI * 2 * i) / 24;
var dist = 60 + Math.random() * 80;
piece.style.setProperty('--tx', Math.cos(angle) * dist + 'px');
piece.style.setProperty('--ty', Math.sin(angle) * dist + 40 + 'px');
document.body.appendChild(piece);
setTimeout(function (el) {
return function () {
el.remove();
};
}(piece), 1300);
}
}
if (balanceBtn) {
balanceBtn.addEventListener('click', function () {
if (root.classList.contains('is-balanced')) {
return;
}
root.classList.add('is-balanced');
balanceBtn.classList.add('is-done');
var doneLabel = balanceBtn.getAttribute('data-done-label');
var labelEl = balanceBtn.querySelector('.hx-404__balance-btn-label');
if (labelEl && doneLabel) {
labelEl.textContent = doneLabel;
}
if (invoiceTotal) {
invoiceTotal.textContent = '۰';
}
if (scaleRightWeight) {
scaleRightWeight.textContent = '۰';
}
if (balanceHint) {
balanceHint.classList.add('is-hidden');
}
if (balanceSuccess) {
balanceSuccess.classList.remove('is-hidden');
}
var rect = balanceBtn.getBoundingClientRect();
spawnConfetti(rect.left + rect.width / 2, rect.top);
});
}
/* ——— چیپ‌های جستجو ——— */
var searchField = document.getElementById('hesabix2-search-field');
document.querySelectorAll('.hx-404__chip').forEach(function (chip) {
chip.addEventListener('click', function () {
var term = chip.getAttribute('data-search');
if (searchField && term) {
searchField.value = term;
searchField.focus();
}
});
});
/* ——— parallax ——— */
var stage = document.getElementById('hx-404-stage');
var scene = stage && stage.querySelector('.hx-404__scene');
if (stage && scene && !reducedMotion && window.matchMedia('(pointer: fine)').matches) {
stage.classList.add('is-parallax');
stage.addEventListener('mousemove', function (e) {
var rect = stage.getBoundingClientRect();
var x = (e.clientX - rect.left) / rect.width - 0.5;
var y = (e.clientY - rect.top) / rect.height - 0.5;
scene.style.transform = 'translate(' + (x * 12) + 'px, ' + (y * 8) + 'px)';
});
stage.addEventListener('mouseleave', function () {
scene.style.transform = '';
});
}
/* ——— ذرات شناور ——— */
var canvas = document.getElementById('hx-404-particles');
if (!canvas || reducedMotion) {
return;
}
var ctx = canvas.getContext('2d');
var particles = [];
var symbols = ['%', '₮', '+', '−', '÷', '×', '۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹', 'ریال'];
function resizeCanvas() {
var dpr = Math.min(window.devicePixelRatio || 1, 2);
var rect = canvas.parentElement.getBoundingClientRect();
canvas.width = rect.width * dpr;
canvas.height = rect.height * dpr;
canvas.style.width = rect.width + 'px';
canvas.style.height = rect.height + 'px';
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
}
function initParticles() {
particles = [];
var rect = canvas.parentElement.getBoundingClientRect();
var w = rect.width;
var h = rect.height;
var count = Math.min(40, Math.floor((w * h) / 25000));
for (var i = 0; i < count; i++) {
particles.push({
x: Math.random() * w,
y: Math.random() * h,
vx: (Math.random() - 0.5) * 0.35,
vy: (Math.random() - 0.5) * 0.35,
size: 10 + Math.random() * 14,
symbol: symbols[Math.floor(Math.random() * symbols.length)],
alpha: 0.08 + Math.random() * 0.14,
rot: Math.random() * Math.PI * 2
});
}
}
function tick() {
var rect = canvas.parentElement.getBoundingClientRect();
var w = rect.width;
var h = rect.height;
ctx.clearRect(0, 0, w, h);
particles.forEach(function (p) {
p.x += p.vx;
p.y += p.vy;
p.rot += 0.002;
if (p.x < -20) {
p.x = w + 20;
}
if (p.x > w + 20) {
p.x = -20;
}
if (p.y < -20) {
p.y = h + 20;
}
if (p.y > h + 20) {
p.y = -20;
}
ctx.save();
ctx.translate(p.x, p.y);
ctx.rotate(p.rot);
ctx.globalAlpha = p.alpha;
ctx.fillStyle = '#0ea5e9';
ctx.font = '600 ' + p.size + 'px system-ui, sans-serif';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(p.symbol, 0, 0);
ctx.restore();
});
requestAnimationFrame(tick);
}
resizeCanvas();
initParticles();
tick();
var resizeTimer;
window.addEventListener('resize', function () {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function () {
resizeCanvas();
initParticles();
}, 150);
});
})();