| |
| |
| |
|
|
|
|
| const canvas = document.getElementById('bg-canvas');
|
| const ctx = canvas.getContext('2d');
|
|
|
| let particlesArray;
|
|
|
|
|
| let mouse = {
|
| x: null,
|
| y: null,
|
| radius: 150
|
| }
|
|
|
| window.addEventListener('mousemove', function (event) {
|
| mouse.x = event.x;
|
| mouse.y = event.y;
|
| });
|
|
|
|
|
| class Particle {
|
| constructor(x, y, loadingSpeed, size, color) {
|
| this.x = x;
|
| this.y = y;
|
| this.baseX = x;
|
| this.baseY = y;
|
| this.size = size;
|
| this.color = color;
|
| this.density = (Math.random() * 30) + 1;
|
| }
|
|
|
| draw() {
|
| ctx.beginPath();
|
| ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2, false);
|
| ctx.fillStyle = this.color;
|
| 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 init() {
|
| particlesArray = [];
|
|
|
| const numberOfParticles = (canvas.width * canvas.height) / 9000;
|
|
|
|
|
| for (let i = 0; i < numberOfParticles * 2; i++) {
|
| let size = (Math.random() * 2) + 0.5;
|
| let x = Math.random() * innerWidth;
|
| let y = Math.random() * innerHeight;
|
| let color = '#00DC82';
|
|
|
| particlesArray.push(new Particle(x, y, 1, size, color));
|
| }
|
| }
|
|
|
| function animate() {
|
| requestAnimationFrame(animate);
|
| ctx.clearRect(0, 0, innerWidth, innerHeight);
|
|
|
| for (let i = 0; i < particlesArray.length; i++) {
|
| particlesArray[i].draw();
|
| particlesArray[i].update();
|
| }
|
|
|
|
|
| }
|
|
|
|
|
| window.addEventListener('resize', function () {
|
| canvas.width = innerWidth;
|
| canvas.height = innerHeight;
|
| mouse.radius = ((canvas.height / 80) * (canvas.height / 80));
|
| init();
|
| });
|
|
|
|
|
| canvas.width = innerWidth;
|
| canvas.height = innerHeight;
|
| init();
|
| animate();
|
|
|