Particle Glitch Text
Home
Snippets
Particle Glitch Text
HTML
CSS
JS
<canvas id="canvas"></canvas>
body { margin: 0; background: #333333; overflow: hidden; } canvas { display: block; }
const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d', { willReadFrequently: true }); let particles = []; const mouse = { x: 0, y: 0, radius: 60 }; function init() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; ctx.fillStyle = 'white'; ctx.textWrap= 'wrap'; ctx.font = 'bold 100px sans-serif'; ctx.textAlign = 'center'; ctx.fillText('AXIS', canvas.width / 2, canvas.height / 2); const data = ctx.getImageData(0, 0, canvas.width, canvas.height).data; particles = []; for (let y = 0; y < canvas.height; y += 4) { for (let x = 0; x < canvas.width; x += 4) { if (data[(y * canvas.width + x) * 4 + 3] > 128) { particles.push(new Particle(x, y)); } } } } class Particle { constructor(x, y) { this.x = Math.random() * canvas.width; this.y = Math.random() * canvas.height; this.baseX = x; this.baseY = y; this.size = 2; this.density = (Math.random() * 30) + 1; } draw() { ctx.fillStyle = '#00D155'; ctx.beginPath(); ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2); ctx.fill(); } update() { let dx = mouse.x - this.x; let dy = mouse.y - this.y; let distance = Math.sqrt(dx * dx + dy * dy); let forceDirectionX = dx / distance; let forceDirectionY = dy / distance; let maxDistance = mouse.radius; let force = (maxDistance - distance) / maxDistance; let directionX = forceDirectionX * force * this.density; let directionY = forceDirectionY * force * this.density; if (distance < mouse.radius) { this.x -= directionX; this.y -= directionY; } else { if (this.x !== this.baseX) { let dx = this.x - this.baseX; this.x -= dx / 10; } if (this.y !== this.baseY) { let dy = this.y - this.baseY; this.y -= dy / 10; } } } } function animate() { ctx.clearRect(0, 0, canvas.width, canvas.height); for (let i = 0; i < particles.length; i++) { particles[i].draw(); particles[i].update(); } requestAnimationFrame(animate); } window.addEventListener('mousemove', (e) => { mouse.x = e.x; mouse.y = e.y; }); init(); animate(); window.addEventListener('resize', init);
Ad #1
Ad #2
Scroll to Top