Creating a Photo Scrambler grid in javascript

Tutorials

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:

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 our main page code of our sample.

index.html

01 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
02 <html>
03 <head>
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>
07 </head>
08 <body>
09     <h3><a href="#">Photo grid (random pieces) example</a></h3>
10     <div class="example" id="example">
11         <div id="grid"></div>
12         <img id="src_img" src="data_images/01.jpg" style="visibility:hidden">
13     </div>
14     <hr style="clear:both;" />
15     <h4><a href="https://www.script-tutorials.com/photo-grid-in-javascript/">back to original article page</a></h4>
16 </body>
17 </html>

Step 2. CSS

Here are used CSS styles.

css/main.css

1 body{background:#eee;margin:0;padding:0}
2 h3 {text-align:center}
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;overflowhidden}
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

001 function CObj(y,x){
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);
009     this.x = x;
010     this.y = y;
011     this.L = 0;
012     this.T = 0;
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;
017     //initialization function
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';
025     }
026     // update object positions
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';
030     }
031  }
032 var rgrid = {
033     // external variables
034     NX  : 5, // amount X elements
035     NY  : 5, // amount Y elements
036     // internal variables
037     mobj : null,
038     doot : null,
039     objects : new Array(),
040     nx  : 0,
041     ny  : 0,
042     nxp  : 0,
043     nyp  : 0,
044     Ix  : 0,
045     Iy  : 0,
046     Ox  : 0,
047     Oy  : 0,
048     xm  : 0,
049     ym  : 0,
050     // main initialization function
051     init : function(){
052         this.mobj  = document.getElementById('grid');
053         this.doot = document.getElementById('src_img');
054         var k = 0;
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;
060     },
061     // resize function
062     resize : function(){
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';
078         var i = 0, o;
079         while (o = this.objects[i++])
080             o.init();
081     },
082     // main loop drawing function
083     run : function(){
084         var i = 0, o;
085         while (o = rgrid.objects[i++])
086             o.draw();
087         setTimeout(rgrid.run, 20);
088     }
089 }
090 // page onload
091 window.onload = function() {
092     rgrid.init(); // perform initialization
093     rgrid.resize(); // perform resizing
094     rgrid.xm = rgrid.nx / 4 - Math.random() * 50;
095     rgrid.ym = rgrid.ny / 4 - Math.random() * 50;
096     rgrid.run();
097     // binding onresize event
098     window.onresize = rgrid.resize();
099 }
100 // binding onmousemove event
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;
108     }
109 }

Step 4. Images

Our sample image located in ‘data_images’ folder.

sunset


Live Demo

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!

Rate article