How to Create Cross Browser Compatible Flare Lens Effect using Javascript

Tutorials

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:

Live Demo
download in 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

01 <html>
02 <head>
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>
06 </head>
07  <body>
08     <img id="bg" src="images/background.jpg" />
09     <div id="main_area">
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" />
18     </div>
19  </body>
20 </html>

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}
03 #bg{
04     width:100%;
05     position:absolute;
06     top:0;
07     left:0;
08 }
09 #main_area {
10     position:relative;
11     height:100%;
12 }
13 .transp {
14     filter:alpha(opacity=20);
15     opacity:0.2;
16     left:-1000px;
17     position:absolute;
18 }

Step 3. JS

Here are our main Javascript:

js/main.js

01 var lfeff = {
02
03     // variables
04     cx:0,
05     cy:0,
06     lx:0,
07     ly:0,
08     px:0,
09     py:0,
10     mobj:0,
11     max:400,
12
13     // initialization
14     init : function() {
15         this.mobj = document.getElementById('main_area');
16         this.resize();
17         this.lx = this.cx / 2;
18         this.ly = this.cy / 2;
19     },
20
21     // refreshing mouse positions
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);
26     },
27
28     // window resizing
29     resize : function() {
30         lfeff.cx = lfeff.mobj.offsetWidth  * 0.5;
31         lfeff.cy = lfeff.mobj.offsetHeight  * 0.5;
32     },
33
34     // main draw lens function
35     draw : function() {
36         lfeff.px -= (lfeff.px - lfeff.lx) * .1;
37         lfeff.py -= (lfeff.py - lfeff.ly) * .1;
38
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);
47
48         // looping current function
49         setTimeout(lfeff.draw, 24);
50     },
51
52     // draw each lens function
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'
62     }
63 }
64
65 window.onload = function() {
66     // initialization
67     lfeff.init();
68
69     // start
70     lfeff.draw();
71
72     // binding onmousemove event
73     document.onmousemove = function(e) {
74         if (window.event) e = window.event; // for IE
75         lfeff.mousemove(e);
76     }
77
78     // binding onresize event
79     window.onresize = lfeff.resize();
80 }

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:

flare
red circle
yellow circle
purple circle
space background


Live Demo
download in package

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!

Rate article