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:
[sociallocker]
[/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
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> |
09 | <div id="main_object"> |
10 | <img id="source_img" src="image.jpg"> |
11 | <div id="wave_zone"></div> |
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} |
09 | #main_object #wave_zone img { |
17 | #main_object #wave_zone { |
21 | background-color:#000; |
Step 3. JS
Here are our main Javascript:
js/main.js
02 | function WSubObj(num, img, object, iwidth, iheight){ |
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"); |
11 | oI.style.left = "0px"; |
12 | oI.style.top = (-iheight + num) + "px"; |
13 | oI.style.width = iwidth + "px"; |
14 | oI.style.height = iheight + "px"; |
16 | document.getElementById("wave_zone").appendChild(o); |
21 | if (num > 0) this.prev = object[num - 1]; |
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"; |
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); |
45 | while (o = weff.object[i++]) o.main(weff.power); |
46 | setTimeout(weff.run, 40); |
50 | window.onload = function() { |
51 | weff.simg = document.getElementById("source_img"); |
52 | weff.iwidth = weff.simg.width; |
53 | weff.iheight = weff.simg.height; |
55 | var css = document.getElementById("wave_zone").style; |
56 | css.height = (weff.iheight/4 - 1) + "px"; |
57 | css.width = weff.iwidth + "px"; |
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:

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!