change speed calculations

Allow for independent horizontal and vertical speeds.
Allow for negative speeds to reverse directions.
Directional speed resets general speed to 1.
This commit is contained in:
Asif Bacchus 2020-04-10 11:26:31 -06:00
parent 4ba30a0a35
commit 95d99ff803

View File

@ -58,13 +58,39 @@ function particles(){
} }
// check particle position, mouse position, move particle and draw it // check particle position, mouse position, move particle and draw it
update(){ update(){
// check if particle is still within canvas // check if particle.x is still within canvas
if (vSpeed === 0 && hSpeed > 0){
if (this.x > canvas.width){
this.x = 0;
}
}
else if (vSpeed === 0 && hSpeed < 0){
if (this.x < 0){
this.x = canvas.width;
}
}
else{
if (this.x > canvas.width || this.x < 0){ if (this.x > canvas.width || this.x < 0){
this.directionX = -this.directionX; this.directionX = -this.directionX;
} }
}
// check if particle.y is still within canvas
if (hSpeed === 0 && vSpeed > 0){
if (this.y > canvas.height) {
this.y = 0;
}
}
else if (hSpeed === 0 && vSpeed < 0){
if (this.y < 0){
this.y = canvas.height;
}
}
else{
if (this.y > canvas.height || this.y < 0){ if (this.y > canvas.height || this.y < 0){
this.directionY = -this.directionY; this.directionY = -this.directionY;
} }
}
// avoid the mouse if avoidMouse = true (default) // avoid the mouse if avoidMouse = true (default)
if (avoidMouse){ if (avoidMouse){
@ -89,8 +115,8 @@ function particles(){
// move particle // move particle
if (speed !== 0) { if (speed !== 0) {
this.x += this.directionX * speed; this.x += this.directionX * speed * hSpeed;
this.y += this.directionY * speed; this.y += this.directionY * speed * vSpeed;
} }
// draw particle // draw particle
@ -106,8 +132,22 @@ function particles(){
let size = (Math.random() * sizeMultiplier) + 1; let size = (Math.random() * sizeMultiplier) + 1;
let x = (Math.random() * ((innerWidth - size * 2) - (size * 2)) + size * 2); let x = (Math.random() * ((innerWidth - size * 2) - (size * 2)) + size * 2);
let y = (Math.random() * ((innerHeight - size * 2) - (size * 2)) + size * 2); let y = (Math.random() * ((innerHeight - size * 2) - (size * 2)) + size * 2);
let directionX = (Math.random() * 5) - 2.5; // set directionX
let directionY = (Math.random() * 5) - 2.5; let directionX = 0
if (vSpeed === 0){
directionX = (Math.random() * 5);
}
else {
directionX = (Math.random() * 5) - 2.5;
}
// set directionY
let directionY = 0;
if (hSpeed === 0){
directionY = (Math.random() * 5);
}
else{
directionY = (Math.random() * 5) - 2.5;
}
let colour = '#8c5523'; let colour = '#8c5523';
particlesArray.push(new Particle(x, y, directionX, directionY, size, colour)); particlesArray.push(new Particle(x, y, directionX, directionY, size, colour));
@ -260,6 +300,30 @@ function particles(){
console.log("'speed' must be an integer between 1-1000. Using default of '50'."); console.log("'speed' must be an integer between 1-1000. Using default of '50'.");
}; };
if (typeof hSpeed == 'undefined'){
hSpeed = 1;
}
else if (Number.isInteger(hSpeed) && (-1000 <= hSpeed && hSpeed <= 1000)){
hSpeed;
speed = 1;
}
else{
hSpeed = 1;
console.log("'hSpeed' must be an integer between -1000 and 1000. Using default of '1'.");
};
if (typeof vSpeed == 'undefined'){
vSpeed = 1;
}
else if (Number.isInteger(vSpeed) && (-1000 <= vSpeed && vSpeed <= 1000)){
vSpeed;
speed = 1;
}
else{
vSpeed = 1;
console.log("'vSpeed' must be an integer between -1000 and 1000. Using default of '1'.");
};
if (typeof avoidMouse == 'undefined'){ if (typeof avoidMouse == 'undefined'){
avoidMouse = true; avoidMouse = true;
} }