redefine vertical and horizontal bouncing
Set option to remove bounce in either/both directions. Replace old speed based logic. Allow for combinations of directional speeds. Rename speed variables for consistency.
This commit is contained in:
parent
818c82ad6c
commit
81e41b9fd3
@ -58,12 +58,12 @@ 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.x is still within canvas
|
// check if particle.x is still within canvas
|
||||||
if (vSpeed === 0 && hSpeed > 0){
|
if (noBounceH === true && speedH > 0){
|
||||||
if (this.x > canvas.width){
|
if (this.x > canvas.width){
|
||||||
this.x = 0;
|
this.x = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (vSpeed === 0 && hSpeed < 0){
|
else if (noBounceH === true && speedH < 0){
|
||||||
if (this.x < 0){
|
if (this.x < 0){
|
||||||
this.x = canvas.width;
|
this.x = canvas.width;
|
||||||
}
|
}
|
||||||
@ -75,12 +75,12 @@ function particles(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// check if particle.y is still within canvas
|
// check if particle.y is still within canvas
|
||||||
if (hSpeed === 0 && vSpeed > 0){
|
if (noBounceV === true && speedV > 0){
|
||||||
if (this.y > canvas.height) {
|
if (this.y > canvas.height) {
|
||||||
this.y = 0;
|
this.y = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (hSpeed === 0 && vSpeed < 0){
|
else if (noBounceV === true && speedV < 0){
|
||||||
if (this.y < 0){
|
if (this.y < 0){
|
||||||
this.y = canvas.height;
|
this.y = canvas.height;
|
||||||
}
|
}
|
||||||
@ -114,8 +114,8 @@ function particles(){
|
|||||||
|
|
||||||
// move particle
|
// move particle
|
||||||
if (speed !== 0) {
|
if (speed !== 0) {
|
||||||
this.x += this.directionX * speed * hSpeed;
|
this.x += this.directionX * speed * speedH;
|
||||||
this.y += this.directionY * speed * vSpeed;
|
this.y += this.directionY * speed * speedV;
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw particle
|
// draw particle
|
||||||
@ -129,24 +129,12 @@ function particles(){
|
|||||||
let numberOfParticles = canvas.width * 0.01;
|
let numberOfParticles = canvas.width * 0.01;
|
||||||
for (let i = 0; i < numberOfParticles * numParticles; i++){
|
for (let i = 0; i < numberOfParticles * numParticles; i++){
|
||||||
let size = (Math.random() * sizeMultiplier) + 1;
|
let size = (Math.random() * sizeMultiplier) + 1;
|
||||||
|
// set X
|
||||||
let x = (Math.random() * ((innerWidth - size * 2) - (size * 2)) + size * 2);
|
let x = (Math.random() * ((innerWidth - size * 2) - (size * 2)) + size * 2);
|
||||||
|
directionX = Math.random() * 5;
|
||||||
|
// set Y
|
||||||
let y = (Math.random() * ((innerHeight - size * 2) - (size * 2)) + size * 2);
|
let y = (Math.random() * ((innerHeight - size * 2) - (size * 2)) + size * 2);
|
||||||
// set directionX
|
directionY = Math.random() * 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
particlesArray.push(new Particle(x, y, directionX, directionY, size));
|
particlesArray.push(new Particle(x, y, directionX, directionY, size));
|
||||||
}
|
}
|
||||||
@ -287,6 +275,28 @@ function particles(){
|
|||||||
console.log("'connectionDensity' must be a finite number greater than 1. Using default of '15'.");
|
console.log("'connectionDensity' must be a finite number greater than 1. Using default of '15'.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof noBounceH == 'undefined'){
|
||||||
|
noBounceH = false;
|
||||||
|
}
|
||||||
|
else if (typeof noBounceH === 'boolean'){
|
||||||
|
noBounceH;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
noBounceH = false;
|
||||||
|
console.log("'noBounceH' must be either 'true' or 'false'. Using default of 'false'.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof noBounceV == 'undefined'){
|
||||||
|
noBounceV = false;
|
||||||
|
}
|
||||||
|
else if (typeof noBounceV === 'boolean'){
|
||||||
|
noBounceV;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
noBounceV = false;
|
||||||
|
console.log("'noBounceV' must be either 'true' or 'false'. Using default of 'false'.");
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof speed == 'undefined'){
|
if (typeof speed == 'undefined'){
|
||||||
speed = 50;
|
speed = 50;
|
||||||
}
|
}
|
||||||
@ -298,28 +308,28 @@ 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'){
|
if (typeof speedH == 'undefined'){
|
||||||
hSpeed = 1;
|
speedH = 1;
|
||||||
}
|
}
|
||||||
else if (Number.isInteger(hSpeed) && (-1000 <= hSpeed && hSpeed <= 1000)){
|
else if (Number.isInteger(speedH) && (-1000 <= speedH && speedH <= 1000)){
|
||||||
hSpeed;
|
speedH;
|
||||||
speed = 1;
|
speed = 1;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
hSpeed = 1;
|
speedH = 1;
|
||||||
console.log("'hSpeed' must be an integer between -1000 and 1000. Using default of '1'.");
|
console.log("'speedH' must be an integer between -1000 and 1000. Using default of '1'.");
|
||||||
};
|
};
|
||||||
|
|
||||||
if (typeof vSpeed == 'undefined'){
|
if (typeof speedV == 'undefined'){
|
||||||
vSpeed = 1;
|
speedV = 1;
|
||||||
}
|
}
|
||||||
else if (Number.isInteger(vSpeed) && (-1000 <= vSpeed && vSpeed <= 1000)){
|
else if (Number.isInteger(speedV) && (-1000 <= speedV && speedV <= 1000)){
|
||||||
vSpeed;
|
speedV;
|
||||||
speed = 1;
|
speed = 1;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
vSpeed = 1;
|
speedV = 1;
|
||||||
console.log("'vSpeed' must be an integer between -1000 and 1000. Using default of '1'.");
|
console.log("'speedV' must be an integer between -1000 and 1000. Using default of '1'.");
|
||||||
};
|
};
|
||||||
|
|
||||||
if (typeof avoidMouse == 'undefined'){
|
if (typeof avoidMouse == 'undefined'){
|
||||||
|
Loading…
Reference in New Issue
Block a user