Creating Cross-browser Compatible Wave Effects Using Javascript

Tutorials

Wave effect with Javascript. Today’s lesson quite entertaining, we’ll learn how to construct a wave effect. For clarity, we will apply that effect to the picture. I will explain how it works directly in our tutorial, now, its time to see our online demonstration.

Here are samples and downloadable package:

Live Demo

[sociallocker]

download in package

[/sociallocker]


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>Effect of waves using pure JavaScript</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     <div class="example">
09         <div id="main_object">
10             <img id="source_img" src="image.jpg">
11             <div id="wave_zone"></div>
12         </div>
13     </div>
14  </body>
15 </html>

Here are prepared object, with 2 elements inside – image itself and bottom area for waving effect

Step 2. CSS

Here are single CSS file with all necessary styles:

css/main.css

01 body{background:#eee;margin:0;padding:0}
02 .example{background:#FFF;width:800px;border:1px #000 solid;margin:20px auto;padding:15px;-moz-border-radius: 3px;-webkit-border-radius: 3px}
03 #main_object {
04     positionrelative;
05 }
06 #main_object img {
07     displayblock;
08 }
09 #main_object #wave_zone img {
10     position:absolute;
11     left:-9999px;
12 }
13 #main_object span {
14     position:absolute;
15     font-size:0px;
16 }
17 #main_object #wave_zone {
18     position:relative;
19     display:block;
20     overflow:hidden;
21     background-color:#000;
22 }

Step 3. JS

Here are our main Javascript:

js/main.js

01 // wave sub objects
02 function WSubObj(num, img, object, iwidth, iheight){
03     this.S = num;
04     var o = document.createElement("span");
05     o.style.overflow = "hidden";
06     o.style.width = iwidth + "px";
07     o.style.height = iheight + "px";
08     o.style.top = (num-1) + "px";
09     var oI = document.createElement("img");
10     oI.src = img.src;
11     oI.style.left = "0px";
12     oI.style.top = (-iheight + num) + "px";
13     oI.style.width = iwidth + "px";
14     oI.style.height = iheight + "px";
15     o.appendChild(oI);
16     document.getElementById("wave_zone").appendChild(o);
17     this.spa = o.style;
18     this.ddx = 0
19     this.PX  = 0
20     this.x   = 0
21     if (num > 0) this.prev = object[num - 1];
22 }
23 WSubObj.prototype.main = function(power){
24     var x0 = (this.S == 0) ? Math.random() * power : this.prev.x;
25     this.x = this.PX += (this.ddx += ( (x0 - this.PX - this.ddx) * 2 ) / 2.8) / 1.4;
26     this.spa.left = Math.round(this.x) + "px";
27 }
28 // wave effect object
29 var weff = {
30     // variables
31     power : 2.2,
32     // inner variables
33     object : new Array(),
34     simg : null,
35     iwidth  : 0,
36     iheight  : 0,
37     // initialization function
38     init : function() {
39         for (var i = 0; i < this.iheight/4; i++)
40             this.object[i] = new WSubObj(i, this.simg, this.object, this.iwidth, this.iheight);
41     },
42     // main loop function of water effect
43     run : function() {
44         var i = 0, o;
45         while (o = weff.object[i++]) o.main(weff.power);
46         setTimeout(weff.run, 40);
47     }
48 };
49 // on page load
50 window.onload = function() {
51     weff.simg = document.getElementById("source_img");
52     weff.iwidth = weff.simg.width;
53     weff.iheight = weff.simg.height;
54     // set necessary width and height for wave zone element
55     var css = document.getElementById("wave_zone").style;
56     css.height = (weff.iheight/4 - 1) + "px";
57     css.width  = weff.iwidth + "px";
58     weff.init();
59     weff.run();
60 }

It is rather simple. When the page loads – I initialize our main object, and create multiple subimages (in spans) for each row of second half of our sample, then, periodically gently increase the amplitude of oscillations. So, in result – we can see effect of waves.

Step 4. Images

For our demo I used only one image:

img_01


Live Demo

Conclusion

Today I told you how to create easy wave effect to images. You always can play with different variables of this demo ot archive different funny results. Hope our javascript lessons still interesting for you. Good luck!

Rate article