Cross-browser Supported Image Skew effect using Javascript

Tutorials

Image shaking effect with javascript. Today’s lesson quite entertaining, we’ll learn how to construct a shaking effect. For clarity, we will apply this effect to the picture. You will need to hold down by mouse a certain area in the image and move it to another location (drag). Now, I sure that better to see our online demonstration. How did it achieve? In principle, simply enough, the entire ‘image’ divided into 4 sectors. Place, where we will begin to drag by mouse – is the boundary separating of our four pictures. And shaking effect itself – will only change the sizes of our sectors and pictures in them. Read more.

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>Image shaking effect 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             <div class="tl"><img alt="" src="image.jpg"></div>
11             <div class="tr"><img alt="" src="image.jpg"></div>
12             <div class="bl"><img alt="" src="image.jpg"></div>
13             <div class="br"><img alt="" src="image.jpg"></div>
14         </div>
15     </div>
16  </body>
17 </html>

Here are main object with 4 images inside

Step 2. CSS

Here are single CSS file with all necessary styles:

css/main.css

1 body{background:#eee;margin:0;padding:0}
2 .example{background:#FFF;width:900px;border:1px #000 solid;margin:20px auto;padding:15px;-moz-border-radius: 3px;-webkit-border-radius: 3px}
3 #main_object{position:relative;width:900px;height:675px;overflow:hidden;cursor:pointer}
4 #main_object div{position:absolute;overflow:hidden}
5 #main_object img{position:absolute;-ms-interpolation-mode:nearest-neighbor}
6 #main_object .tl,#main_object .tl img{top:0;left:0}
7 #main_object .tr,#main_object .tr img{top:0;right:0}
8 #main_object .bl,#main_object .bl img{bottom:0;left:0}
9 #main_object .br,#main_object .br img{right:0;bottom:0}

Step 3. JS

Here are our main Javascript:

js/main.js

001 var ishk = {
002     // variables
003     speedD : .98, // degree of attenuation
004     speedX : 50, // speed by X
005     speedY : 60, // speed by X
006     // inner variables
007     xm   : 0,
008     ym   : 0,
009     sx   : 1,
010     sy   : 1,
011     svx  : 0,
012     svy  : 0,
013     xd   : 1,
014     yd   : 1,
015     drag : false,
016     // initialization
017     init : function() {
018         // firstly we will pass through all inner div`s and its images and collect styles
019         this.mobj = document.getElementById('main_object');
020         var div = this.mobj.getElementsByTagName('div');
021         this.d = [];
022         this.i = [];
023         for (var i = 0; i<4; i++) {
024             this.d[i] = div[i].style;
025             this.i[i] = div[i].getElementsByTagName('img')[0].style;
026         }
027         // set initial params of our generated result
028         this.resize();
029         // prevent text selection in IE
030         document.onselectstart = function() {
031             return false;
032         }
033         // prevent IE from trying to drag an image
034         this.mobj.ondrag = function() {
035             return false;
036         }
037         // mouse down event, we will set 'drag flag to true, change cursor and positions
038         this.mobj.onmousedown = function() {
039             ishk.drag = true;
040             ishk.mobj.style.cursor = 'move';
041             ishk.xd = ishk.xm - ishk.nx;
042             ishk.yd = ishk.ym - ishk.ny;
043             return false;
044         }
045         // on mouse up - release 'drag' flag, change pointer
046         document.onmouseup = function() {
047             ishk.drag = false;
048             ishk.mobj.style.cursor = 'pointer';
049             return false;
050         }
051         // set max sizes to last image
052         this.i[3].width =  ishk.nw;
053         this.i[3].height = ishk.nh;
054         // and initial positions too
055         ishk.sx = ishk.nx;
056         ishk.sy = ishk.ny;
057         // start looping
058         this.shake();
059     },
060     // refreshing mouse positions
061     mousemove : function(e) {
062         this.xm = e.clientX;
063         this.ym = e.clientY;
064     },
065     // sub init of initial positions, plus, in case of window resizing - repeat too
066     resize : function() {
067         var o = ishk.mobj;
068         for (ishk.nx = 0, ishk.ny = 0; o != null; o = o.offsetParent)
069             ishk.nx += o.offsetLeft, ishk.ny += o.offsetTop;
070         ishk.nw = ishk.mobj.offsetWidth;
071         ishk.nh = ishk.mobj.offsetHeight;
072     },
073     // main shaking function
074     shake : function() {
075         // in case of mouse move and dragging - change center position
076         if (ishk.drag) {
077             ishk.sx = ishk.xm;
078             ishk.sy = ishk.ym;
079         else // otherwise - attenuation
080             ishk.svx = ishk.speedD * ishk.svx - (ishk.sx - ishk.xd - ishk.nx) / ishk.speedX;
081             ishk.svy = ishk.speedD * ishk.svy - (ishk.sy - ishk.yd - ishk.ny) / ishk.speedY;
082             ishk.sx += ishk.svx;
083             ishk.sy += ishk.svy;
084         }
085         // calculation new widths and heights of our div`s and images
086         var w0 = Math.max(0, Math.round(ishk.sx) - ishk.nx);
087         var h0 = Math.max(0, Math.round(ishk.sy) - ishk.ny);
088         var w1 = Math.max(0, ishk.nw - (Math.round(ishk.sx) - ishk.nx));
089         var h1 = Math.max(0, ishk.nh - (Math.round(ishk.sy) - ishk.ny));
090         var w2 = Math.max(0, Math.round((w0 * ishk.nw) / ishk.xd));
091         var h2 = Math.max(0, Math.round((h0 * ishk.nh) / ishk.yd));
092         var w3 = Math.max(0, Math.round((w1 * ishk.nw) / (ishk.nw - ishk.xd)));
093         var h3 = Math.max(0, Math.round((h1 * ishk.nh) / (ishk.nh - ishk.yd)));
094         // apply new widths
095         ishk.d[0].width = w0 + 'px';
096         ishk.d[1].width = w1 + 'px';
097         ishk.d[2].width = w0 + 'px';
098         ishk.d[3].width = w1 + 'px';
099         ishk.d[0].height = h0 + 'px';
100         ishk.d[1].height = h0 + 'px';
101         ishk.d[2].height = h1 + 'px';
102         ishk.d[3].height = h1 + 'px';
103         ishk.i[0].width = w2 + 'px';
104         ishk.i[1].width = w3 + 'px';
105         ishk.i[2].width = w2 + 'px';
106         ishk.i[3].width = w3 + 'px';
107         ishk.i[0].height = h2 + 'px';
108         ishk.i[1].height = h2 + 'px';
109         ishk.i[2].height = h3 + 'px';
110         ishk.i[3].height = h3 + 'px';
111         // looping current function
112         setTimeout(ishk.shake, 20); // 1/50*1000 = 20ms (for 50 fps) :)
113     }
114 };
115 window.onload = function() {
116     ishk.init(); // first initialization
117     // binding mouse move event
118     document.onmousemove = function(e) {
119         if (window.event) e = window.event; // for IE
120         ishk.mousemove(e);
121     }
122     // binding onresize event
123     window.onresize = ishk.resize;
124 }

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 ‘shake’ function, which changing sizes of all our 4 images (sectors).

Step 4. Images

For our demo I used only one image:

img_01


Live Demo

Conclusion

Today I told you how to create easy shaking effect to images. Commonly – you can try to play with any another objects too. Hope our javascript lessons still interesting for you. Good luck!

Rate article