360 lines
9.8 KiB
JavaScript
360 lines
9.8 KiB
JavaScript
/**
|
|
* صفحهٔ دستورالعمل برند — صحنهٔ سهبعدی، ذرات، tilt و کپی رنگ
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
|
|
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
|
|
function initToc() {
|
|
var links = document.querySelectorAll('.hx-brand-toc__list a');
|
|
var sections = [];
|
|
if (!links.length) return;
|
|
|
|
links.forEach(function (link) {
|
|
var id = (link.getAttribute('href') || '').replace('#', '');
|
|
var el = id ? document.getElementById(id) : null;
|
|
if (el) {
|
|
sections.push({ el: el, link: link });
|
|
}
|
|
});
|
|
|
|
if (!sections.length || !('IntersectionObserver' in window)) return;
|
|
|
|
function setActive(activeLink) {
|
|
links.forEach(function (l) {
|
|
l.classList.toggle('is-active', l === activeLink);
|
|
});
|
|
}
|
|
|
|
var current = sections[0].link;
|
|
var io = new IntersectionObserver(
|
|
function (entries) {
|
|
entries.forEach(function (entry) {
|
|
if (entry.isIntersecting) {
|
|
var match = sections.find(function (s) {
|
|
return s.el === entry.target;
|
|
});
|
|
if (match) {
|
|
current = match.link;
|
|
setActive(current);
|
|
}
|
|
}
|
|
});
|
|
},
|
|
{ rootMargin: '-20% 0px -65% 0px', threshold: 0 }
|
|
);
|
|
|
|
sections.forEach(function (s) {
|
|
io.observe(s.el);
|
|
});
|
|
setActive(current);
|
|
}
|
|
|
|
function initCopy() {
|
|
var buttons = document.querySelectorAll('.hx-brand-swatch[data-copy]');
|
|
buttons.forEach(function (btn) {
|
|
btn.addEventListener('click', function () {
|
|
var value = btn.getAttribute('data-copy') || '';
|
|
if (!value) return;
|
|
|
|
function done() {
|
|
btn.classList.add('is-copied');
|
|
var code = btn.querySelector('code');
|
|
var prev = code ? code.textContent : '';
|
|
if (code) {
|
|
code.textContent = 'کپی شد';
|
|
window.setTimeout(function () {
|
|
code.textContent = prev;
|
|
btn.classList.remove('is-copied');
|
|
}, 1200);
|
|
}
|
|
}
|
|
|
|
if (navigator.clipboard && navigator.clipboard.writeText) {
|
|
navigator.clipboard.writeText(value).then(done).catch(function () {
|
|
fallbackCopy(value, done);
|
|
});
|
|
} else {
|
|
fallbackCopy(value, done);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
function fallbackCopy(value, done) {
|
|
var input = document.createElement('textarea');
|
|
input.value = value;
|
|
input.setAttribute('readonly', '');
|
|
input.style.position = 'absolute';
|
|
input.style.left = '-9999px';
|
|
document.body.appendChild(input);
|
|
input.select();
|
|
try {
|
|
document.execCommand('copy');
|
|
done();
|
|
} catch (e) {
|
|
/* ignore */
|
|
}
|
|
document.body.removeChild(input);
|
|
}
|
|
|
|
function initReveal() {
|
|
var nodes = document.querySelectorAll('.hx-brand-reveal');
|
|
if (!nodes.length) return;
|
|
|
|
if (reduce || !('IntersectionObserver' in window)) {
|
|
nodes.forEach(function (n) {
|
|
n.classList.add('is-visible');
|
|
});
|
|
return;
|
|
}
|
|
|
|
var io = new IntersectionObserver(
|
|
function (entries) {
|
|
entries.forEach(function (entry) {
|
|
if (entry.isIntersecting) {
|
|
entry.target.classList.add('is-visible');
|
|
io.unobserve(entry.target);
|
|
}
|
|
});
|
|
},
|
|
{ rootMargin: '0px 0px -10% 0px', threshold: 0.12 }
|
|
);
|
|
|
|
nodes.forEach(function (n) {
|
|
io.observe(n);
|
|
});
|
|
}
|
|
|
|
function initStageTilt() {
|
|
var stage = document.querySelector('[data-brand-tilt]');
|
|
var coin = document.querySelector('[data-brand-coin]');
|
|
if (!stage || !coin) return;
|
|
|
|
var rx = 12;
|
|
var ry = -24;
|
|
var tx = 12;
|
|
var ty = -24;
|
|
var dragging = false;
|
|
var lastX = 0;
|
|
var lastY = 0;
|
|
var idle = true;
|
|
|
|
function apply() {
|
|
coin.style.setProperty('--rx', rx.toFixed(2));
|
|
coin.style.setProperty('--ry', ry.toFixed(2));
|
|
}
|
|
|
|
function pointerPos(e, rect) {
|
|
return {
|
|
px: (e.clientX - rect.left) / Math.max(rect.width, 1) - 0.5,
|
|
py: (e.clientY - rect.top) / Math.max(rect.height, 1) - 0.5
|
|
};
|
|
}
|
|
|
|
stage.addEventListener('pointerdown', function (e) {
|
|
dragging = true;
|
|
idle = false;
|
|
lastX = e.clientX;
|
|
lastY = e.clientY;
|
|
stage.classList.add('is-dragging');
|
|
if (stage.setPointerCapture) {
|
|
stage.setPointerCapture(e.pointerId);
|
|
}
|
|
e.preventDefault();
|
|
});
|
|
|
|
stage.addEventListener('pointermove', function (e) {
|
|
var rect = stage.getBoundingClientRect();
|
|
if (dragging) {
|
|
ty += (e.clientX - lastX) * 0.55;
|
|
tx -= (e.clientY - lastY) * 0.4;
|
|
lastX = e.clientX;
|
|
lastY = e.clientY;
|
|
return;
|
|
}
|
|
var p = pointerPos(e, rect);
|
|
tx = 10 + p.py * -32;
|
|
ty = -20 + p.px * 48;
|
|
idle = false;
|
|
});
|
|
|
|
function endDrag(e) {
|
|
dragging = false;
|
|
stage.classList.remove('is-dragging');
|
|
if (e && stage.releasePointerCapture && e.pointerId != null) {
|
|
try {
|
|
stage.releasePointerCapture(e.pointerId);
|
|
} catch (err) {
|
|
/* ignore */
|
|
}
|
|
}
|
|
}
|
|
|
|
stage.addEventListener('pointerup', endDrag);
|
|
stage.addEventListener('pointercancel', endDrag);
|
|
stage.addEventListener('pointerleave', function () {
|
|
if (!dragging) {
|
|
idle = true;
|
|
tx = 12;
|
|
ty = -24;
|
|
}
|
|
});
|
|
|
|
function tick() {
|
|
rx += (tx - rx) * (dragging ? 0.28 : 0.12);
|
|
ry += (ty - ry) * (dragging ? 0.28 : 0.12);
|
|
if (idle && !dragging && !reduce) {
|
|
ty -= 0.18;
|
|
}
|
|
apply();
|
|
window.requestAnimationFrame(tick);
|
|
}
|
|
|
|
apply();
|
|
window.requestAnimationFrame(tick);
|
|
}
|
|
|
|
function initCardTilt() {
|
|
if (reduce || window.matchMedia('(pointer: coarse)').matches) return;
|
|
|
|
var cards = document.querySelectorAll('[data-brand-tilt-card]');
|
|
cards.forEach(function (card) {
|
|
card.addEventListener('pointermove', function (e) {
|
|
var r = card.getBoundingClientRect();
|
|
var px = (e.clientX - r.left) / r.width - 0.5;
|
|
var py = (e.clientY - r.top) / r.height - 0.5;
|
|
card.style.transform = 'perspective(800px) rotateX(' + (py * -8).toFixed(2) + 'deg) rotateY(' + (px * 10).toFixed(2) + 'deg) translateZ(6px)';
|
|
});
|
|
card.addEventListener('pointerleave', function () {
|
|
card.style.transform = '';
|
|
});
|
|
});
|
|
}
|
|
|
|
function initMagnet() {
|
|
if (reduce || window.matchMedia('(pointer: coarse)').matches) return;
|
|
|
|
var buttons = document.querySelectorAll('[data-brand-magnet]');
|
|
buttons.forEach(function (btn) {
|
|
btn.addEventListener('pointermove', function (e) {
|
|
var r = btn.getBoundingClientRect();
|
|
var x = e.clientX - r.left - r.width / 2;
|
|
var y = e.clientY - r.top - r.height / 2;
|
|
btn.style.transform = 'translate(' + x * 0.18 + 'px, ' + y * 0.22 + 'px)';
|
|
});
|
|
btn.addEventListener('pointerleave', function () {
|
|
btn.style.transform = '';
|
|
});
|
|
});
|
|
}
|
|
|
|
function initCanvas() {
|
|
var canvas = document.querySelector('[data-brand-canvas]');
|
|
var hero = document.querySelector('[data-brand-hero]');
|
|
if (!canvas || !hero || reduce || !canvas.getContext) return;
|
|
|
|
var ctx = canvas.getContext('2d');
|
|
var dpr = Math.min(window.devicePixelRatio || 1, 2);
|
|
var particles = [];
|
|
var mouse = { x: 0.5, y: 0.4 };
|
|
var w = 0;
|
|
var h = 0;
|
|
|
|
var colors = [
|
|
'rgba(14,165,233,',
|
|
'rgba(56,189,248,',
|
|
'rgba(249,115,22,',
|
|
'rgba(248,250,252,',
|
|
'rgba(0,24,112,'
|
|
];
|
|
|
|
function resize() {
|
|
w = hero.clientWidth;
|
|
h = hero.clientHeight;
|
|
canvas.width = Math.floor(w * dpr);
|
|
canvas.height = Math.floor(h * dpr);
|
|
canvas.style.width = w + 'px';
|
|
canvas.style.height = h + 'px';
|
|
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
}
|
|
|
|
function spawn() {
|
|
particles = [];
|
|
var count = w < 700 ? 28 : 46;
|
|
for (var i = 0; i < count; i++) {
|
|
particles.push({
|
|
x: Math.random(),
|
|
y: Math.random(),
|
|
z: 0.2 + Math.random() * 0.8,
|
|
r: 1.2 + Math.random() * 3.4,
|
|
vx: (Math.random() - 0.5) * 0.00035,
|
|
vy: (Math.random() - 0.5) * 0.00028,
|
|
c: colors[i % colors.length],
|
|
a: 0.12 + Math.random() * 0.28
|
|
});
|
|
}
|
|
}
|
|
|
|
function draw() {
|
|
ctx.clearRect(0, 0, w, h);
|
|
var mx = (mouse.x - 0.5) * 28;
|
|
var my = (mouse.y - 0.5) * 18;
|
|
|
|
particles.forEach(function (p) {
|
|
p.x += p.vx;
|
|
p.y += p.vy;
|
|
if (p.x < -0.05) p.x = 1.05;
|
|
if (p.x > 1.05) p.x = -0.05;
|
|
if (p.y < -0.05) p.y = 1.05;
|
|
if (p.y > 1.05) p.y = -0.05;
|
|
|
|
var depth = p.z;
|
|
var x = p.x * w + mx * depth;
|
|
var y = p.y * h + my * depth;
|
|
var radius = p.r * (0.6 + depth);
|
|
var g = ctx.createRadialGradient(x - radius * 0.3, y - radius * 0.35, 0, x, y, radius * 2.2);
|
|
g.addColorStop(0, p.c + Math.min(0.85, p.a + 0.35) + ')');
|
|
g.addColorStop(0.45, p.c + p.a + ')');
|
|
g.addColorStop(1, p.c + '0)');
|
|
ctx.beginPath();
|
|
ctx.fillStyle = g;
|
|
ctx.arc(x, y, radius * 2.2, 0, Math.PI * 2);
|
|
ctx.fill();
|
|
});
|
|
|
|
window.requestAnimationFrame(draw);
|
|
}
|
|
|
|
hero.addEventListener('pointermove', function (e) {
|
|
var r = hero.getBoundingClientRect();
|
|
mouse.x = (e.clientX - r.left) / r.width;
|
|
mouse.y = (e.clientY - r.top) / r.height;
|
|
});
|
|
|
|
resize();
|
|
spawn();
|
|
draw();
|
|
window.addEventListener('resize', function () {
|
|
resize();
|
|
spawn();
|
|
});
|
|
}
|
|
|
|
function boot() {
|
|
initToc();
|
|
initCopy();
|
|
initReveal();
|
|
initStageTilt();
|
|
initCardTilt();
|
|
initMagnet();
|
|
initCanvas();
|
|
}
|
|
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', boot);
|
|
} else {
|
|
boot();
|
|
}
|
|
})();
|