make everything one function

This commit is contained in:
Asif Bacchus 2020-04-08 05:00:04 -06:00
parent 89263b1495
commit c0e4424257

View File

@ -1,36 +1,37 @@
/* create particles animation
function particles() {
/* create particles animation
based on the amazing tutorial by 'Franks Labratory'
https://youtu.be/d620nV6bp0A
*/
*/
const canvas = document.getElementById('particles');
const canvasContext = canvas.getContext('2d');
const canvas = document.getElementById('particles');
const canvasContext = canvas.getContext('2d');
// read css colour variables from root element
const particleColour = getComputedStyle(document.documentElement).getPropertyValue('--col-particle');
const strokeColour = getComputedStyle(document.documentElement).getPropertyValue('--col-particle-stroke');
const strokeHoverColour = getComputedStyle(document.documentElement).getPropertyValue('--col-particle-stroke-hover');
// read css colour variables from root element
const particleColour = getComputedStyle(document.documentElement).getPropertyValue('--col-particle');
const strokeColour = getComputedStyle(document.documentElement).getPropertyValue('--col-particle-stroke');
const strokeHoverColour = getComputedStyle(document.documentElement).getPropertyValue('--col-particle-stroke-hover');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let particlesArray;
let particlesArray;
// get mouse position
let mousePosition ={
// get mouse position
let mousePosition ={
x: undefined,
y: undefined,
radius: (canvas.height / 80) * (canvas.width / 80)
};
};
// add mouse position event listener
window.addEventListener('mousemove', function(event){
// add mouse position event listener
window.addEventListener('mousemove', function(event){
mousePosition.x = event.x;
mousePosition.y = event.y;
});
});
// create particle class
class Particle{
// create particle class
class Particle{
constructor(x, y, directionX, directionY, size, colour) {
this.x = x;
this.y = y;
@ -55,7 +56,7 @@ class Particle{
if (this.y > canvas.height || this.y < 0){
this.directionY = -this.directionY;
}
/*
// collision detection between mouse and particles
let dx = mousePosition.x - this.x;
let dy = mousePosition.y - this.y;
@ -74,7 +75,7 @@ class Particle{
this.y -= 10;
}
}
*/
// move particle
this.x += this.directionX;
this.y += this.directionY;
@ -82,10 +83,10 @@ class Particle{
// draw particle
this.draw();
}
}
}
// create particle array
function init(){
// create particle array
function init(){
particlesArray = [];
let numberOfParticles = (canvas.height * canvas.width) / 9000;
for (let i = 0; i < numberOfParticles * 2; i++){
@ -98,10 +99,10 @@ function init(){
particlesArray.push(new Particle(x, y, directionX, directionY, size, colour));
}
}
}
// check if particles are close enough to connect to eachother
function connect(){
// check if particles are close enough to connect to eachother
function connect(){
let opacityValue = 1;
for (let a = 0; a < particlesArray.length; a++){
for (let b = a; b < particlesArray.length; b++){
@ -128,10 +129,10 @@ function connect(){
}
}
}
}
}
// animation loop
function animate(){
// animation loop
function animate(){
requestAnimationFrame(animate);
canvasContext.clearRect(0, 0, innerWidth, innerHeight);
@ -139,21 +140,22 @@ function animate(){
particlesArray[i].update();
}
connect();
}
}
// resize event
window.addEventListener('resize', function(){
// resize event
window.addEventListener('resize', function(){
canvas.width = innerWidth;
canvas.height = innerHeight;
mousePosition.radius = ((canvas.height / 80) * (canvas.width / 80));
init();
});
});
// mouse-out event (mouse leaving the window)
window.addEventListener('mouseout', function(){
// mouse-out event (mouse leaving the window)
window.addEventListener('mouseout', function(){
mousePosition.x = undefined;
mousePosition.y = undefined;
});
});
init();
animate();
init();
animate();
}