set mouse avoidance via parameter, default enabled

This commit is contained in:
Asif Bacchus 2020-04-08 05:02:34 -06:00
parent 6b165f24da
commit bf5321eec5
2 changed files with 22 additions and 20 deletions

View File

@ -8,7 +8,7 @@
href="css/index.min.css" href="css/index.min.css"
crossorigin="anonymous"/> crossorigin="anonymous"/>
</head> </head>
<body onload='particles()'> <body onload='particles(avoidMouse = false)'>
<canvas id="particles"></canvas> <canvas id="particles"></canvas>
<!-- javascript --> <!-- javascript -->
<script <script

View File

@ -1,4 +1,4 @@
function particles() { function particles(avoidMouse = true) {
/* create particles animation /* create particles animation
based on the amazing tutorial by 'Franks Labratory' based on the amazing tutorial by 'Franks Labratory'
https://youtu.be/d620nV6bp0A https://youtu.be/d620nV6bp0A
@ -56,8 +56,9 @@ function particles() {
if (this.y > canvas.height || this.y < 0){ if (this.y > canvas.height || this.y < 0){
this.directionY = -this.directionY; this.directionY = -this.directionY;
} }
/*
// collision detection between mouse and particles // avoid the mouse if avoidMouse = true (default)
if (avoidMouse){
let dx = mousePosition.x - this.x; let dx = mousePosition.x - this.x;
let dy = mousePosition.y - this.y; let dy = mousePosition.y - this.y;
let distance = Math.sqrt((dx * dx) + (dy * dy)); let distance = Math.sqrt((dx * dx) + (dy * dy));
@ -75,7 +76,8 @@ function particles() {
this.y -= 10; this.y -= 10;
} }
} }
*/ }
// move particle // move particle
this.x += this.directionX; this.x += this.directionX;
this.y += this.directionY; this.y += this.directionY;