First of all, this article has been written for pedagogical purposes so if you are waiting for the result only, do no hesitate to follow directly these links:
As explained in the title, in this tutorial I will explain you how I got a handsome light effect based on a scrolling gradient animation. To be more accurate, I want that the laptop appears gradually from the top right to the bottom left like if the lighting were expanding. And the animation is synchronized with scrollbar movement:
I recognize that it is totally inspired from Invision studio landing page. If you are interested in web design and you have not seen it, I can only recommend you to follow the link as soon as possible. It will blow your mind. Even if the message may not be very clear, the Invision team has raised web technologies as a master piece.
Whatever, I have decided to use this example because it gathers two new technologies I wanted to discover and share:
- SVG: svg technology is increasingly used in web development and may be very powerful to manage gradient and animation on images.
- animation based on scroll: a lot of landing pages are using animations following scroll movement to catch the reader look.
The example we will building in this tutorial is available here. I propose you to take a look to better understand what we are talking about.
Project initialization: Webpack 3.x + sass + babel
I suggest you to use:
- npm to manage dependencies
- webpack 3.0 to manage files: one of the most powerful module bundler of the moment
- sass for the style
- normalize.css to clear all exotic browser styles
This is the basic architecture now:
_colors.scss # to store all colors variables
_fonts.scss # to store all fonts mixins
style.scss # global style
.babelrc # to store babel config
webpack.config.js # webpack config
At this stage, the main files are index.html
index.js with only … 2 lines:
I assume that you already have a setup for babel, webpack, etc… For more information about this initialization, see the files webpack.config.js, package.json, .babelrc and of course README.md.
If you decompose the landing page of Studio, you can see that the main effect is the laptop appearing progressively. However, another discrete effect is present to support the apparition: a smooth linear gradient background. It is not obvious but this color transition improves the light effect.
Notice you could use a radial gradient but after trying, I think that linear gradient gives a better result.
To reproduce Studio landing page, I chosed the color
#151a22 and added them to the file _colors.scss:
In the main style file, style.scss:
If you are very careful about the orientation of the linear gradient, you can analyze that this orientation doesn’t depends on the size screen. So the gradient background is not adjusted from the top right corner to the bottom left corner, it is adjusted by a specific angle:
After creating the smooth gradient background, we need to manage the hard stuff: the laptop appearing progressively. For now we are going to focus on a static version: the laptop top right corner visible.
If you know a little about image processing, the best way to create this kind of effect is using mask with the raw image and a radial gradient:
At this stage, there is 2 ways to use a mask:
- using an image preprocessed with an image processing software like GIMP or Photoshop then displaying it with
- using SVG technology
The inconvenient with using a preprocessed image is that you will have to create an image for each step of the animation and load it at the good time: it is boring to create, hard to maintain and more significant, the loading process may slow your browser and so your animation. So SVG is clearly our best option.
Using SVG in web technologies is very easy when you use the good library. I suggest you SVG.js which is quite clear and efficient:
$ npm install --save svg.js
Next, add a new div with id drawing in index.html to include SVG:
<div class="drawing" id="drawing"></div>
And in file style.scss, add
And finally we:
- import an image in the SVG (in the following, I import 2 images grouped together: one for the computer border and one for the screen content)
- create a circle and color it with a radial gradient background
- create a mask from the circle
- apply the mask on the images grouped
The result in file index.js is:
I advise you to use an image with :
- a good resolution
- a transparent background
- a static light effect as if the laptop was enlighten from the top right. So when the laptop will be completely visible, the laptop will be in harmony with the gradient background. It may be generated with a filter from an image manipulating program like Gimp or Photoshop.
So at this moment, you should have this result:
Animation at scroll
The last thing you need to do is to synchronize the circle radius of the lighe effect with the scrollbar movement. To be more accurate, we want that:
- when the user begins to scroll: the laptop stays fixed in the center of the screen and appears progressively
- when the laptop is totally visible: it stops being fixed and lets the place of the content below (in my case, a simple white centered title “Handsome animation finished” ).
In order to create this animation, I suggest you to use the library ScrollMagic.
To be totally honest, I didn’t try other libraries to manage scroll animations. ScrollMagic does the job, it was the only thing that matters for me.
$ npm install --save scrollmagic
So we include ScrollMagic on the top of our index.js:
import ScrollMagic from 'scrollmagic'
ScrollMagic works with a controller and a scene to defined the scroll duration while the laptop must be fixed and animated. And at the line 34, we insert:
We use the
setPin to force the scene to be fixed during 3000px and the event
progress to manage custom animation, the circle radius in this case.
I wanted that the circle radius goes from 100px to 2600px to be sure that the circle would so I used the following formula:
100 + e.progress * 2500
// e.progress goes from 0 (the begin) to 1 (the end)
Now you should have this result and pay attention to the small right grey scrollar:
With this short example, you have got a first taste:
- how to manipulate images directly in the browser with SVG and create a radial smooth ligh effect with SVG.js
- how to create animation based on scrollbar movement with ScrollMagic
With these two technologies, you should be able to create handsome landing pages and catch user look as never before.
This tutorial is my first one so do not hesistate to propose improvements to make it more understandable.
And if this tutorial helped you, remember to click on the clap button to make this post more visible on medium.