Photo grid with javascript. Today we continue JavaScript examples, and today we will create effect of splitting image to pieces (transforming image into grid), plus – our pieces will moving when we will move our mouse pointer. This will pretty interesting demonstration. As we using pure javascript, our result will quite crossbrowser. I already checked it in most major browsers.
Here are sample 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 our main page code of our sample.
index.html
04 | <title>Photo grid (random pieces) example | Script Tutorials (2011)</title> |
05 | <link rel="stylesheet" href="css/main.css" type="text/css" /> |
06 | <script src="js/script.js"></script> |
09 | <h3><a href="#">Photo grid (random pieces) example</a></h3> |
10 | <div class="example" id="example"> |
12 | <img id="src_img" src="data_images/01.jpg" style="visibility:hidden"> |
14 | <hr style="clear:both;" /> |
Step 2. CSS
Here are used CSS styles.
css/main.css
1 | body{background:#eee;margin:0;padding:0} |
3 | .example{background:#FFF;width:900px;height:600px;border:1px #000 solid;margin:20px auto;padding:15px;position:relative;-moz-border-radius:3px;-webkit-border-radius:3px;overflow: hidden} |
4 | #grid {position:relative;width:100%;height:100%} |
5 | #grid .span {overflow:hidden;font-size:1px;position:absolute;left:-9999px} |
Step 3. JS
Here are our main control JS file. Most inportant functionality here.
js/script.js
002 | this.obj = document.createElement('span'); |
003 | this.obj.className='span'; |
004 | this.img = document.createElement('img'); |
005 | this.img.style.position='absolute'; |
006 | this.img.src = rgrid.doot.src; |
007 | this.obj.appendChild(this.img); |
008 | rgrid.mobj.appendChild(this.obj); |
013 | this.xr = (this.x / 2) % 1 ? 1 : -1; |
014 | this.yr = (this.y / 2) % 1 ? 1 : -1; |
015 | this.xf = Math.random() * 4 + 1; |
016 | this.yf = Math.random() * 4 + 1; |
018 | this.init = function (){ |
019 | this.L = Math.round(this.x * rgrid.Nx); |
020 | this.T = Math.round(this.y * rgrid.Ny); |
021 | this.obj.style.width = Math.round(rgrid.Nx + 1) + 'px'; |
022 | this.obj.style.height = Math.round(rgrid.Ny + 1) + 'px'; |
023 | this.img.style.left = Math.round(-this.L) + 'px'; |
024 | this.img.style.top = Math.round(-this.T) + 'px'; |
027 | this.draw = function (){ |
028 | this.obj.style.left = Math.round(this.L + (-rgrid.nx / 4 + rgrid.xm) * (this.xr * this.xf)) + 'px'; |
029 | this.obj.style.top = Math.round(this.T + (-rgrid.ny / 4 + rgrid.ym) * (this.yr * this.yf)) + 'px'; |
039 | objects : new Array(), |
052 | this.mobj = document.getElementById('grid'); |
053 | this.doot = document.getElementById('src_img'); |
055 | for(var i=0; i<this.NY; i++) |
056 | for(var j=0;j<this.NX;j++) |
057 | this.objects[k++] = new CObj(i,j); |
058 | this.nxp = this.mobj.parentNode.offsetLeft / 2; |
059 | this.nyp = this.mobj.parentNode.offsetTop / 2; |
063 | this.nx = this.mobj.offsetWidth; |
064 | this.ny = this.mobj.offsetHeight; |
065 | this.nxp = this.mobj.parentNode.offsetLeft / 2; |
066 | this.nyp = this.mobj.parentNode.offsetTop / 2; |
067 | this.Ix = this.doot.width; |
068 | this.Iy = this.doot.height; |
069 | this.Nx = Math.round(this.Ix / this.NX); |
070 | this.Ny = Math.round(this.Iy / this.NY); |
071 | this.Ox = (this.nx - this.Ix) / 2; |
072 | this.Oy = (this.ny - this.Iy) / 2; |
073 | var css = this.mobj.style; |
074 | css.left = Math.round(this.Ox) + 'px'; |
075 | css.top = Math.round(this.Oy) + 'px'; |
076 | css.width = Math.round(this.Ix) + 'px'; |
077 | css.height = Math.round(this.Iy) + 'px'; |
079 | while (o = this.objects[i++]) |
085 | while (o = rgrid.objects[i++]) |
087 | setTimeout(rgrid.run, 20); |
091 | window.onload = function() { |
094 | rgrid.xm = rgrid.nx / 4 - Math.random() * 50; |
095 | rgrid.ym = rgrid.ny / 4 - Math.random() * 50; |
098 | window.onresize = rgrid.resize(); |
101 | document.onmousemove = function(e){ |
102 | if (window.event) e = window.event; |
103 | rgrid.xm = ((e.x || e.clientX) / 2) - rgrid.nxp; |
104 | rgrid.ym = ((e.y || e.clientY) / 2) - rgrid.nyp; |
105 | if (Math.abs(2 * rgrid.xm - rgrid.nx / 2) < 2 && Math.abs(2 * rgrid.ym - rgrid.ny / 2) < 2){ |
106 | rgrid.xm = rgrid.nx / 4; |
107 | rgrid.ym = rgrid.ny / 4; |
Step 4. Images
Our sample image located in ‘data_images’ folder.

Conclusion
Hope that you was happy to investigate and play with our javascript lessons. I hope that today`s example looks fine 🙂 If is you were wondering – do not forget to thank. Will glad to listen your interesting comments. Good luck!