2020-04-08 06:06:12 -06:00
|
|
|
function particles(){
|
2020-04-08 05:00:04 -06:00
|
|
|
/* create particles animation
|
|
|
|
based on the amazing tutorial by 'Franks Labratory'
|
|
|
|
https://youtu.be/d620nV6bp0A
|
|
|
|
*/
|
|
|
|
|
2020-04-08 06:06:12 -06:00
|
|
|
// handle parameters
|
|
|
|
particles = (
|
|
|
|
typeof particles !== 'undefined'
|
|
|
|
&& Number.isFinite(particles)
|
|
|
|
) ? particles : 1;
|
|
|
|
sizeMultiplier = (
|
|
|
|
typeof sizeMultiplier !== 'undefined'
|
|
|
|
&& Number.isFinite(sizeMultiplier)
|
|
|
|
) ? sizeMultiplier : 3;
|
|
|
|
speed = (
|
|
|
|
typeof speed !== 'undefined'
|
|
|
|
&& Number.isFinite(speed)
|
|
|
|
&& (1 <= speed && speed <= 1000)
|
2020-04-08 06:15:54 -06:00
|
|
|
) ? (speed / 100) : 50;
|
2020-04-08 06:06:12 -06:00
|
|
|
avoidMouse = (
|
|
|
|
typeof avoidMouse !== 'undefined'
|
|
|
|
&& typeof avoidMouse === 'boolean'
|
|
|
|
) ? avoidMouse : true;
|
|
|
|
hover = (
|
|
|
|
typeof hover !== 'undefined'
|
|
|
|
&& typeof hover === 'boolean'
|
|
|
|
) ? hover : true;
|
2020-04-08 05:26:04 -06:00
|
|
|
|
2020-04-08 05:00:04 -06:00
|
|
|
// 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');
|
|
|
|
|
2020-04-08 06:06:12 -06:00
|
|
|
// get canvas element, set context and size
|
|
|
|
const canvas = document.getElementById('particles');
|
|
|
|
const canvasContext = canvas.getContext('2d');
|
2020-04-08 05:00:04 -06:00
|
|
|
canvas.width = window.innerWidth;
|
|
|
|
canvas.height = window.innerHeight;
|
|
|
|
|
|
|
|
let particlesArray;
|
2020-04-08 06:15:54 -06:00
|
|
|
speed = speed / 100; // index speed to base 100 = 1
|
2020-04-08 05:00:04 -06:00
|
|
|
|
|
|
|
// 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){
|
|
|
|
mousePosition.x = event.x;
|
|
|
|
mousePosition.y = event.y;
|
|
|
|
});
|
|
|
|
|
|
|
|
// create particle class
|
|
|
|
class Particle{
|
|
|
|
constructor(x, y, directionX, directionY, size, colour) {
|
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
this.directionX = directionX;
|
|
|
|
this.directionY = directionY;
|
|
|
|
this.size = size;
|
|
|
|
this.colour = colour;
|
2020-04-08 04:08:51 -06:00
|
|
|
}
|
2020-04-08 05:00:04 -06:00
|
|
|
// method to draw individual particles
|
|
|
|
draw(){
|
|
|
|
canvasContext.beginPath();
|
|
|
|
canvasContext.arc(this.x, this.y, this.size, 0, Math.PI * 2, false);
|
|
|
|
canvasContext.fillStyle = particleColour;
|
|
|
|
canvasContext.fill();
|
2020-04-08 04:08:51 -06:00
|
|
|
}
|
2020-04-08 05:00:04 -06:00
|
|
|
// check particle position, mouse position, move particle and draw it
|
|
|
|
update(){
|
|
|
|
// check if particle is still within canvas
|
|
|
|
if (this.x > canvas.width || this.x < 0){
|
|
|
|
this.directionX = -this.directionX;
|
2020-04-08 04:08:51 -06:00
|
|
|
}
|
2020-04-08 05:00:04 -06:00
|
|
|
if (this.y > canvas.height || this.y < 0){
|
|
|
|
this.directionY = -this.directionY;
|
2020-04-08 04:08:51 -06:00
|
|
|
}
|
2020-04-08 05:02:34 -06:00
|
|
|
|
|
|
|
// avoid the mouse if avoidMouse = true (default)
|
|
|
|
if (avoidMouse){
|
|
|
|
let dx = mousePosition.x - this.x;
|
|
|
|
let dy = mousePosition.y - this.y;
|
|
|
|
let distance = Math.sqrt((dx * dx) + (dy * dy));
|
|
|
|
if (distance < mousePosition.radius + this.size){
|
|
|
|
if (mousePosition.x < this.x && this.x < canvas.width - this.size * 10){
|
|
|
|
this.x += 10;
|
|
|
|
}
|
|
|
|
if (mousePosition.x > this.x && this.x > this.size * 10){
|
|
|
|
this.x -= 10;
|
|
|
|
}
|
|
|
|
if (mousePosition.y < this.y && this.y < canvas.height - this.size * 10){
|
|
|
|
this.y += 10;
|
|
|
|
}
|
|
|
|
if (mousePosition.y > this.y && this.y > this.size * 10){
|
|
|
|
this.y -= 10;
|
|
|
|
}
|
2020-04-08 05:00:04 -06:00
|
|
|
}
|
2020-04-08 04:08:51 -06:00
|
|
|
}
|
2020-04-08 05:10:24 -06:00
|
|
|
|
2020-04-08 05:00:04 -06:00
|
|
|
// move particle
|
2020-04-08 05:23:28 -06:00
|
|
|
this.x += this.directionX * speed;
|
|
|
|
this.y += this.directionY * speed;
|
2020-04-08 04:08:51 -06:00
|
|
|
|
2020-04-08 05:00:04 -06:00
|
|
|
// draw particle
|
|
|
|
this.draw();
|
|
|
|
}
|
2020-04-08 04:08:51 -06:00
|
|
|
}
|
2020-04-08 05:00:04 -06:00
|
|
|
|
|
|
|
// create particle array
|
|
|
|
function init(){
|
|
|
|
particlesArray = [];
|
|
|
|
let numberOfParticles = (canvas.height * canvas.width) / 9000;
|
2020-04-08 06:31:19 -06:00
|
|
|
for (let i = 0; i < numberOfParticles * numParticles; i++){
|
2020-04-08 06:06:12 -06:00
|
|
|
let size = (Math.random() * sizeMultiplier) + 1;
|
2020-04-08 05:00:04 -06:00
|
|
|
let x = (Math.random() * ((innerWidth - size * 2) - (size * 2)) + size * 2);
|
|
|
|
let y = (Math.random() * ((innerHeight - size * 2) - (size * 2)) + size * 2);
|
|
|
|
let directionX = (Math.random() * 5) - 2.5;
|
|
|
|
let directionY = (Math.random() * 5) - 2.5;
|
|
|
|
let colour = '#8c5523';
|
|
|
|
|
|
|
|
particlesArray.push(new Particle(x, y, directionX, directionY, size, colour));
|
|
|
|
}
|
2020-04-08 04:08:51 -06:00
|
|
|
}
|
2020-04-08 05:00:04 -06:00
|
|
|
|
|
|
|
// 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++){
|
|
|
|
let distance = ((particlesArray[a].x - particlesArray[b].x) * (particlesArray[a].x - particlesArray[b].x)) + ((particlesArray[a].y - particlesArray[b].y) * (particlesArray[a].y - particlesArray[b].y));
|
2020-04-08 06:06:12 -06:00
|
|
|
if (distance < (canvas.width / 10) * (canvas.height / 10)){
|
2020-04-08 05:00:04 -06:00
|
|
|
opacityValue = 1 - (distance / 20000);
|
2020-04-08 05:10:24 -06:00
|
|
|
// change colour on 'hover' if hover = true (default)
|
|
|
|
if(hover){
|
|
|
|
let dx = mousePosition.x - particlesArray[a].x;
|
|
|
|
let dy = mousePosition.y - particlesArray[a].y;
|
|
|
|
let mouseDistance = Math.sqrt((dx * dx) + (dy * dy));
|
|
|
|
if (mouseDistance < 200) {
|
|
|
|
// change colour if mouse is close
|
|
|
|
canvasContext.globalAlpha = opacityValue;
|
|
|
|
canvasContext.strokeStyle = strokeHoverColour;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// use regular stroke colour
|
|
|
|
canvasContext.globalAlpha = opacityValue;
|
|
|
|
canvasContext.strokeStyle = strokeColour;
|
|
|
|
}
|
2020-04-08 05:00:04 -06:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
canvasContext.globalAlpha = opacityValue;
|
|
|
|
canvasContext.strokeStyle = strokeColour;
|
|
|
|
}
|
|
|
|
canvasContext.lineWidth = 1;
|
|
|
|
canvasContext.beginPath();
|
|
|
|
canvasContext.moveTo(particlesArray[a].x, particlesArray[a].y);
|
|
|
|
canvasContext.lineTo(particlesArray[b].x, particlesArray[b].y);
|
|
|
|
canvasContext.stroke();
|
2020-04-08 04:18:55 -06:00
|
|
|
}
|
2020-04-08 04:08:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 05:00:04 -06:00
|
|
|
// animation loop
|
|
|
|
function animate(){
|
|
|
|
requestAnimationFrame(animate);
|
|
|
|
canvasContext.clearRect(0, 0, innerWidth, innerHeight);
|
2020-04-08 04:08:51 -06:00
|
|
|
|
2020-04-08 05:00:04 -06:00
|
|
|
for (let i = 0; i < particlesArray.length; i++){
|
|
|
|
particlesArray[i].update();
|
|
|
|
}
|
|
|
|
connect();
|
2020-04-08 04:08:51 -06:00
|
|
|
}
|
|
|
|
|
2020-04-08 05:00:04 -06:00
|
|
|
// resize event
|
|
|
|
window.addEventListener('resize', function(){
|
|
|
|
canvas.width = innerWidth;
|
|
|
|
canvas.height = innerHeight;
|
|
|
|
mousePosition.radius = ((canvas.height / 80) * (canvas.width / 80));
|
|
|
|
init();
|
|
|
|
});
|
2020-04-08 04:08:51 -06:00
|
|
|
|
2020-04-08 05:00:04 -06:00
|
|
|
// mouse-out event (mouse leaving the window)
|
|
|
|
window.addEventListener('mouseout', function(){
|
|
|
|
mousePosition.x = undefined;
|
|
|
|
mousePosition.y = undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
init();
|
|
|
|
animate();
|
|
|
|
}
|