hover effect based on parameter (default enabled)
This commit is contained in:
parent
bf5321eec5
commit
8d08d145c4
@ -8,7 +8,7 @@
|
|||||||
href="css/index.min.css"
|
href="css/index.min.css"
|
||||||
crossorigin="anonymous"/>
|
crossorigin="anonymous"/>
|
||||||
</head>
|
</head>
|
||||||
<body onload='particles(avoidMouse = false)'>
|
<body onload='particles(avoidMouse = false, hover = false)'>
|
||||||
<canvas id="particles"></canvas>
|
<canvas id="particles"></canvas>
|
||||||
<!-- javascript -->
|
<!-- javascript -->
|
||||||
<script
|
<script
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
function particles(avoidMouse = true) {
|
function particles(avoidMouse = true, hover = 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
|
||||||
@ -111,13 +111,21 @@ function particles(avoidMouse = true) {
|
|||||||
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));
|
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));
|
||||||
if (distance < (canvas.width / 7) * (canvas.height / 7)){
|
if (distance < (canvas.width / 7) * (canvas.height / 7)){
|
||||||
opacityValue = 1 - (distance / 20000);
|
opacityValue = 1 - (distance / 20000);
|
||||||
// if the mouse is close to particles, change the line colour
|
// change colour on 'hover' if hover = true (default)
|
||||||
let dx = mousePosition.x - particlesArray[a].x;
|
if(hover){
|
||||||
let dy = mousePosition.y - particlesArray[a].y;
|
let dx = mousePosition.x - particlesArray[a].x;
|
||||||
let mouseDistance = Math.sqrt((dx * dx) + (dy * dy));
|
let dy = mousePosition.y - particlesArray[a].y;
|
||||||
if (mouseDistance < 200) {
|
let mouseDistance = Math.sqrt((dx * dx) + (dy * dy));
|
||||||
canvasContext.globalAlpha = opacityValue;
|
if (mouseDistance < 200) {
|
||||||
canvasContext.strokeStyle = strokeHoverColour;
|
// change colour if mouse is close
|
||||||
|
canvasContext.globalAlpha = opacityValue;
|
||||||
|
canvasContext.strokeStyle = strokeHoverColour;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// use regular stroke colour
|
||||||
|
canvasContext.globalAlpha = opacityValue;
|
||||||
|
canvasContext.strokeStyle = strokeColour;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
canvasContext.globalAlpha = opacityValue;
|
canvasContext.globalAlpha = opacityValue;
|
||||||
|
Loading…
Reference in New Issue
Block a user