motionverse-studio / script.js
f88fa's picture
اريد تاثيرات وانيميشن بالموقع
af9f1ed verified
Raw History Blame Contribute Delete
11.5 kB
// Wait for DOM to be ready
document.addEventListener('DOMContentLoaded', () => {
initCustomCursor();
initParticles();
initScrollReveal();
initCounterAnimation();
initMagneticButtons();
initTiltEffect();
initSmoothScroll();
initPageTransitions();
initMarqueeSpeed();
});
// Custom Cursor
function initCustomCursor() {
if (window.matchMedia("(max-width: 768px)").matches) return;
const cursorDot = document.getElementById('cursor-dot');
const cursorOutline = document.getElementById('cursor-outline');
let mouseX = 0, mouseY = 0;
let outlineX = 0, outlineY = 0;
document.addEventListener('mousemove', (e) => {
mouseX = e.clientX;
mouseY = e.clientY;
cursorDot.style.left = `${mouseX}px`;
cursorDot.style.top = `${mouseY}px`;
});
// Smooth follow for outline
function animate() {
outlineX += (mouseX - outlineX) * 0.15;
outlineY += (mouseY - outlineY) * 0.15;
cursorOutline.style.left = `${outlineX}px`;
cursorOutline.style.top = `${outlineY}px`;
requestAnimationFrame(animate);
}
animate();
// Hover effects
const interactiveElements = document.querySelectorAll('a, button, [data-tilt], input, textarea');
interactiveElements.forEach(el => {
el.addEventListener('mouseenter', () => {
cursorDot.classList.add('hovering');
cursorOutline.classList.add('hovering');
});
el.addEventListener('mouseleave', () => {
cursorDot.classList.remove('hovering');
cursorOutline.classList.remove('hovering');
});
});
}
// Particle System
function initParticles() {
const canvas = document.getElementById('particleCanvas');
if (!canvas) return;
const ctx = canvas.getContext('2d');
let particles = [];
function resize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
resize();
window.addEventListener('resize', resize);
class Particle {
constructor() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.size = Math.random() * 2 + 0.5;
this.speedX = Math.random() * 1 - 0.5;
this.speedY = Math.random() * 1 - 0.5;
this.opacity = Math.random() * 0.5 + 0.1;
}
update() {
this.x += this.speedX;
this.y += this.speedY;
if (this.x > canvas.width) this.x = 0;
if (this.x < 0) this.x = canvas.width;
if (this.y > canvas.height) this.y = 0;
if (this.y < 0) this.y = canvas.height;
}
draw() {
ctx.fillStyle = `rgba(139, 92, 246, ${this.opacity})`;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
}
}
// Create particles
for (let i = 0; i < 50; i++) {
particles.push(new Particle());
}
// Animation loop
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
particles.forEach(particle => {
particle.update();
particle.draw();
});
// Connect particles
particles.forEach((a, index) => {
particles.slice(index + 1).forEach(b => {
const dx = a.x - b.x;
const dy = a.y - b.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 100) {
ctx.strokeStyle = `rgba(139, 92, 246, ${0.1 * (1 - distance / 100)})`;
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(a.x, a.y);
ctx.lineTo(b.x, b.y);
ctx.stroke();
}
});
});
requestAnimationFrame(animate);
}
animate();
}
// Scroll Reveal
function initScrollReveal() {
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
// Add delay if specified
const delay = entry.target.dataset.delay || 0;
setTimeout(() => {
entry.target.classList.add('revealed');
}, delay);
}
});
}, observerOptions);
document.querySelectorAll('.reveal-on-scroll').forEach(el => {
observer.observe(el);
});
}
// Counter Animation
function initCounterAnimation() {
const counters = document.querySelectorAll('.counter');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const counter = entry.target;
const target = parseInt(counter.getAttribute('data-target'));
const duration = 2000;
const step = target / (duration / 16);
let current = 0;
const updateCounter = () => {
current += step;
if (current < target) {
counter.textContent = Math.floor(current);
requestAnimationFrame(updateCounter);
} else {
counter.textContent = target;
}
};
updateCounter();
observer.unobserve(counter);
}
});
}, { threshold: 0.5 });
counters.forEach(counter => observer.observe(counter));
}
// Magnetic Buttons
function initMagneticButtons() {
if (window.matchMedia("(max-width: 768px)").matches) return;
const buttons = document.querySelectorAll('.magnetic-btn');
buttons.forEach(btn => {
btn.addEventListener('mousemove', (e) => {
const rect = btn.getBoundingClientRect();
const x = e.clientX - rect.left - rect.width / 2;
const y = e.clientY - rect.top - rect.height / 2;
btn.style.transform = `translate(${x * 0.3}px, ${y * 0.3}px)`;
});
btn.addEventListener('mouseleave', () => {
btn.style.transform = 'translate(0, 0)';
});
});
}
// 3D Tilt Effect
function initTiltEffect() {
if (window.matchMedia("(max-width: 768px)").matches) return;
const cards = document.querySelectorAll('[data-tilt]');
cards.forEach(card => {
card.addEventListener('mousemove', (e) => {
const rect = card.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
const centerX = rect.width / 2;
const centerY = rect.height / 2;
const rotateX = (y - centerY) / 10;
const rotateY = (centerX - x) / 10;
card.style.transform = `perspective(1000px) rotateX(${rotateX}deg) rotateY(${rotateY}deg) scale3d(1.05, 1.05, 1.05)`;
});
card.addEventListener('mouseleave', () => {
card.style.transform = 'perspective(1000px) rotateX(0) rotateY(0) scale3d(1, 1, 1)';
});
});
}
// Smooth Scroll
function initSmoothScroll() {
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
}
// Page Transitions
function initPageTransitions() {
const transition = document.getElementById('pageTransition');
const links = document.querySelectorAll('a[href^="/"], a[href^="."]');
links.forEach(link => {
link.addEventListener('click', (e) => {
const href = link.getAttribute('href');
if (href && !href.startsWith('#')) {
e.preventDefault();
transition.classList.add('active');
setTimeout(() => {
window.location.href = href;
}, 500);
}
});
});
}
// Marquee Speed based on scroll
function initMarqueeSpeed() {
const marquee = document.querySelector('.marquee-content');
if (!marquee) return;
let scrollSpeed = 0;
let lastScrollY = window.scrollY;
window.addEventListener('scroll', () => {
const currentScrollY = window.scrollY;
scrollSpeed = Math.abs(currentScrollY - lastScrollY);
lastScrollY = currentScrollY;
const speed = Math.max(20 - scrollSpeed, 5);
marquee.style.animationDuration = `${speed}s`;
});
}
// Text Scramble Effect for Hero
class TextScramble {
constructor(el) {
this.el = el;
this.chars = '!<>-_\\/[]{}—=+*^?#________';
this.update = this.update.bind(this);
}
setText(newText) {
const oldText = this.el.innerText;
const length = Math.max(oldText.length, newText.length);
const promise = new Promise((resolve) => this.resolve = resolve);
this.queue = [];
for (let i = 0; i < length; i++) {
const from = oldText[i] || '';
const to = newText[i] || '';
const start = Math.floor(Math.random() * 40);
const end = start + Math.floor(Math.random() * 40);
this.queue.push({ from, to, start, end });
}
cancelAnimationFrame(this.frameRequest);
this.frame = 0;
this.update();
return promise;
}
update() {
let output = '';
let complete = 0;
for (let i = 0, n = this.queue.length; i < n; i++) {
let { from, to, start, end, char } = this.queue[i];
if (this.frame >= end) {
complete++;
output += to;
} else if (this.frame >= start) {
if (!char || Math.random() < 0.28) {
char = this.randomChar();
this.queue[i].char = char;
}
output += `<span class="dud">${char}</span>`;
} else {
output += from;
}
}
this.el.innerHTML = output;
if (complete === this.queue.length) {
this.resolve();
} else {
this.frameRequest = requestAnimationFrame(this.update);
this.frame++;
}
}
randomChar() {
return this.chars[Math.floor(Math.random() * this.chars.length)];
}
}
// Parallax Effect
window.addEventListener('scroll', () => {
const scrolled = window.pageYOffset;
const parallaxElements = document.querySelectorAll('.parallax');
parallaxElements.forEach(el => {
const speed = el.dataset.speed || 0.5;
el.style.transform = `translateY(${scrolled * speed}px)`;
});
});
// Form Interaction
document.querySelectorAll('input, textarea').forEach(input => {
input.addEventListener('focus', () => {
document.body.style.cursor = 'text';
});
input.addEventListener('blur', () => {
document.body.style.cursor = 'none';
});
});