robin-dela/hover-effect

Javascript library to draw and animate images on hover

HTML

1,885

70 commits

updated Jun 27, 2023

See the code

README

Hover effect

Javascript library to draw and animate images on hover.

paypal

DEMO

ARTICLE

Example

example 1 example 2

Use locally

To load the images you'll need to view the demo via a web server, simply go to the demo's folder location and type python -m SimpleHTTPServer 8000 in your terminal. Then access the demo in your browser by typing: localhost:8000

Basic usage

This helper needs Three.js and gsap to do the transition, so you need to include it before this little helper.

Then you only need a div element in HTML to start animating things with a piece of code like this:

<!-- Div to draw the effect -->
<div class="my-div"></div>

<!-- library needed -->
<script src="three.min.js"></script>
<script src="gsap.min.js"></script>

<script src="dist/hover.umd.js"></script>
<script>
    var myAnimation = new hoverEffect({
        parent: document.querySelector('.my-div'),
        intensity: 0.3,
        image1: 'images/myImage1.jpg',
        image2: 'images/myImage2.jpg',
        displacementImage: 'images/myDistorsionImage.png'
    });
</script>

Node JS usage

This helper can also be used in node js environments. Three.js and TweenMax scripts are included as dependencies in the package so you don't need to manually include them.

Install

npm install hover-effect

Import

import hoverEffect from 'hover-effect'

Init

Initialize just as you would in the basic usage example above.

Options

Mandatory parameters

NameTypeDefaultDescription
parentDom elementnullThe DOM element where the animation will be injected. The images of the animation will take the parent's size.
image1ImagenullThe first Image of the animation.
image2ImagenullThe second Image of the animation.
displacementImageImagenullThe Image used to do the transition between the 2 main images.

Optional parameters

NameTypeDefaultDescription
intensityFloat1Used to determine the intensity of the distortion effect. 0 is no effect and 1 is full distortion.
intensity1FloatintensityOverrides the distortion intensity of the first image.
intensity2FloatintensityOverrides the distortion intensity of the second image.
angleFloatMath.PI / 4Angle of the distortion effect in Radians. Defaults to Pi / 4 (45 degrees).
angle1FloatangleOverrides the distortion angle for the first image.
angle2Float-angle * 3Overrides the distortion angle for the second image.
speedInFloat1.6Speed of the inbound animation (in seconds).
speedOutFloat1.2Speed of the outbound animation (in seconds).
hoverBooleantrueif set to false the animation will not be triggered on hover (see next and previous function to interact)
easingStringExpo.easeOutEasing of the transition, see greensock easing
videoBooleanfalseDefines if you want to use videos instead of images (note: you need 2 videos, it doesn't work with one image and one video.)
imagesRatioFloat1Specify a value if you want a background: cover type of behaviour, otherwise it will apply a square aspect ratio. usage: image height / image width example: 1080 / 1920

Methods

NameDescription
nextTransition to the second image.
previousTransition to the first image.

Credits

Thanks to :

Made with it

Alex Brown demo

robin-dela/hover-effect

Javascript library to draw and animate images on hover

HTML

1,885

70 commits

updated Jun 27, 2023

See the code

README

Hover effect

Javascript library to draw and animate images on hover.

paypal

DEMO

ARTICLE

Example

example 1 example 2

Use locally

To load the images you'll need to view the demo via a web server, simply go to the demo's folder location and type python -m SimpleHTTPServer 8000 in your terminal. Then access the demo in your browser by typing: localhost:8000

Basic usage

This helper needs Three.js and gsap to do the transition, so you need to include it before this little helper.

Then you only need a div element in HTML to start animating things with a piece of code like this:

<!-- Div to draw the effect -->
<div class="my-div"></div>

<!-- library needed -->
<script src="three.min.js"></script>
<script src="gsap.min.js"></script>

<script src="dist/hover.umd.js"></script>
<script>
    var myAnimation = new hoverEffect({
        parent: document.querySelector('.my-div'),
        intensity: 0.3,
        image1: 'images/myImage1.jpg',
        image2: 'images/myImage2.jpg',
        displacementImage: 'images/myDistorsionImage.png'
    });
</script>

Node JS usage

This helper can also be used in node js environments. Three.js and TweenMax scripts are included as dependencies in the package so you don't need to manually include them.

Install

npm install hover-effect

Import

import hoverEffect from 'hover-effect'

Init

Initialize just as you would in the basic usage example above.

Options

Mandatory parameters

NameTypeDefaultDescription
parentDom elementnullThe DOM element where the animation will be injected. The images of the animation will take the parent's size.
image1ImagenullThe first Image of the animation.
image2ImagenullThe second Image of the animation.
displacementImageImagenullThe Image used to do the transition between the 2 main images.

Optional parameters

NameTypeDefaultDescription
intensityFloat1Used to determine the intensity of the distortion effect. 0 is no effect and 1 is full distortion.
intensity1FloatintensityOverrides the distortion intensity of the first image.
intensity2FloatintensityOverrides the distortion intensity of the second image.
angleFloatMath.PI / 4Angle of the distortion effect in Radians. Defaults to Pi / 4 (45 degrees).
angle1FloatangleOverrides the distortion angle for the first image.
angle2Float-angle * 3Overrides the distortion angle for the second image.
speedInFloat1.6Speed of the inbound animation (in seconds).
speedOutFloat1.2Speed of the outbound animation (in seconds).
hoverBooleantrueif set to false the animation will not be triggered on hover (see next and previous function to interact)
easingStringExpo.easeOutEasing of the transition, see greensock easing
videoBooleanfalseDefines if you want to use videos instead of images (note: you need 2 videos, it doesn't work with one image and one video.)
imagesRatioFloat1Specify a value if you want a background: cover type of behaviour, otherwise it will apply a square aspect ratio. usage: image height / image width example: 1080 / 1920

Methods

NameDescription
nextTransition to the second image.
previousTransition to the first image.

Credits

Thanks to :

Made with it

Alex Brown demo

Project health

Overall

2.0/10

Maintained

0/10

Code review

0/10
OpenSSF Scorecard

Latest release

1.2.1 · Feb 15, 2023

Releases this year

0

Significant stargazers

Tim Baek

1,807 followers · starred Jul 2023

you06

109 followers · starred Mar 2019

Hugo Torzuoli

147 followers · starred Sep 2021

Kimmo Brunfeldt

513 followers · starred Aug 2018