Today’s lesson pretty entertaining, we’ll learn how to make a flare lens effect. I hope you know what it is, but if not – just look our online demo. To achieve this effect, we need several images – lenses. These lenses will in 3 different colors (for a more nice result.) One of the lenses will simulate the brightest object of our composition (star). But it could also be the sun (for example). And, for greater interactivity – we will change the lens when moving the mouse.
Here are samples and downloadable package:
Ok, download the example files and lets start coding !
Step 1. HTML
As usual, we start with the HTML. This is source code of our sample:
index.html
03 | <title>Flare lens effect</title> |
04 | <link rel="stylesheet" type="text/css" href="css/main.css" /> |
05 | <script type="text/javascript" src="js/main.js"></script> |
08 | <img id="bg" src="images/background.jpg" /> |
10 | <img id="l1" class="transp" src="images/l1.png" /> |
11 | <img id="l2" class="transp" src="images/l2.png" /> |
12 | <img id="l3" class="transp" src="images/l3.png" /> |
13 | <img id="l4" class="transp" src="images/l2.png" /> |
14 | <img id="l5" class="transp" src="images/l1.png" /> |
15 | <img id="l6" class="transp" src="images/l2.png" /> |
16 | <img id="l7" class="transp" src="images/l3.png" /> |
17 | <img id="l8" src="images/flare1.png" style="position:absolute" /> |
Here, we will have some image on our background (space), 7 transparent lens, and one most bright flare image (flare1.png).
Step 2. CSS
Here are single CSS file with all necessary styles:
css/main.css
01 | html {overflow:hidden} |
02 | body{margin:0;padding:0} |
14 | filter:alpha(opacity=20); |
Step 3. JS
Here are our main Javascript:
js/main.js
15 | this.mobj = document.getElementById('main_area'); |
17 | this.lx = this.cx / 2; |
18 | this.ly = this.cy / 2; |
22 | mousemove : function(e) { |
23 | if (window.event) e = window.event; |
24 | this.lx = (e.x || e.clientX); |
25 | this.ly = (e.y || e.clientY); |
30 | lfeff.cx = lfeff.mobj.offsetWidth * 0.5; |
31 | lfeff.cy = lfeff.mobj.offsetHeight * 0.5; |
36 | lfeff.px -= (lfeff.px - lfeff.lx) * .1; |
37 | lfeff.py -= (lfeff.py - lfeff.ly) * .1; |
39 | lfeff.drawLens('l1', 0.7, 1, 0, 0); |
40 | lfeff.drawLens('l2', 0.5, 2, 0, 0); |
41 | lfeff.drawLens('l3', 0.3, 3, 0, 0); |
42 | lfeff.drawLens('l4', 0.2, 10, 0, 0); |
43 | lfeff.drawLens('l5', 0.7, -1, 0, 0); |
44 | lfeff.drawLens('l6', 0.5, -2, 0, 0); |
45 | lfeff.drawLens('l7', 0.3, -3, 0, 0); |
46 | lfeff.drawLens('l8', 1.0, -0.7, 0, 0); |
49 | setTimeout(lfeff.draw, 24); |
53 | drawLens : function(id, scale, distance, x, y) { |
54 | var vx = (this.cx - this.px) / distance; |
55 | var vy = (this.cy - this.py) / distance; |
56 | var d = this.max * scale; |
57 | css = document.getElementById(id).style; |
58 | css.top = Math.round(vy - (d * 0.5) + this.cy + y) + 'px'; |
59 | css.left = Math.round(vx - (d * 0.5) + this.cx + x) + 'px' |
60 | css.width = Math.round(d) + 'px' |
61 | css.height = Math.round(d) + 'px' |
65 | window.onload = function() { |
73 | document.onmousemove = function(e) { |
74 | if (window.event) e = window.event; |
79 | window.onresize = lfeff.resize(); |
It is rather simple. When the page loads – I initialize our main object, and link all the necessary events. Then, after initialization, I loop our main ‘draw’ function, which changing positions of our transparent lens while we moving our mouse.
Step 4. Images
For our demo I used only one image:





Conclusion
Today I told you how to create easy flare lens effect to images. Commonly – you can try to play with our JS file to get different results. Hope our javascript lessons still interesting for you. Good luck!