Drag Image in Div and Find Coordinates From Where It's Cropped Like in Facebook

Introduction

 
This article explains how to drag an image in a Div and determine the coordinates of where it's cropped like in Facebook.
 
In this article, you will see how to use JavaScript along with MooTools to drag an image in a Div and determine its coordinates instead of cropping it. In Facebook you crop your Profile Picture by adjusting it. The same thing is generated by the sample code in this article, the only difference is that instead of cropping you can just determine the coordinates of the repositioned image.
 
Step 1
 
First of all, add a Web page to your Visual Studio.
 
drag1.jpg
 
Now you need to add MooTools to your Application, which can be downloaded from the Zip File available at the beginning of this article.
 
drag2.jpg
 
Step 2
 
Add these MooTools to the Head section of your application.
 
Now you need to add some Div, hidden fields, Button and an image to our application. To do that you can add this code:
  1. <div id="div1">  
  2.        <div id="div2">  
  3.               <div id="div3">  
  4.                      <img id="imgPhoto" src="image2.jpg"  />  
  5.               </div>  
  6.        </div>  
  7.    
  8.        <input id="hdn1" type="hidden" />  
  9.        <input id="hdn2" type="hidden" />  
  10.        <input type="submit" value="Cut Image" onclick="javascript:alert('Cut from Top/Left: ' + $('hdnInpTop').get('value') + ' / '+ $('hdnInpLeft').get('value'))"/>  
  11. </div> 
Step 3
 
Now we will add some CSS code to our apllication.
  1. <style>    
  2.        *{    
  3.               margin:0;    
  4.               padding:0;    
  5.        }    
  6.           
  7.        body{    
  8.               background-color:Grey;    
  9.               color:White;    
  10.               font-familyHelvetica, Calibri, Arialsans-serif;    
  11.               font-size:14px;    
  12.               line-height:1.3em;    
  13.               padding:10px;    
  14.        }    
  15.     
  16.        #div1{    
  17.               width:200px;    
  18.               margin:10px auto;    
  19.               background-color:#5C5C5C;    
  20.               padding:10px;    
  21.               border:1px dashed #2C2C2C;    
  22.               text-align:center;    
  23.        }         
  24.          
  25.        #div2 {    
  26.               width:68px;    
  27.               margin:10px auto;    
  28.               border:1px #c0c0c0 solid;    
  29.        }    
  30.          
  31.        #div3 {    
  32.               height:83px;    
  33.               width:68px;    
  34.               overflow:hidden;    
  35.               cursor:move;    
  36.        }    
  37.      
  38.        input {    
  39.               padding:5px;    
  40.               font-size:1em;    
  41.        }    
  42.           
  43. </style>   
Step 4
 
Now for the main part of the JavaScript code for this article.
  1. <script type="text/javascript">  
  2.     var cropImage = function (target, container, coordinateContainer, topCoord, leftCoord) {  
  3.         var img = $(target);  
  4.         var imgSizeRatio = img.getSize().x / img.getSize().y;  
  5.         var desiredRatio = 1 / 1;  
  6.         if (imgSizeRatio > desiredRatio) {  
  7.             img.setStyle('height''170px');  
  8.         } else {  
  9.             img.setStyle('width''180px');  
  10.         }  
  11.         var myDragScroller = new Drag(container, {  
  12.             snap: 0,  
  13.             style: false,  
  14.             invert: true,  
  15.             modifiers: { x: 'scrollLeft', y: 'scrollTop' },  
  16.             onComplete: function (el) {  
  17.                 var cropCutFromTop = img.getCoordinates($(coordinateContainer)).top;  
  18.                 var cropCutFromLeft = img.getCoordinates($(coordinateContainer)).left;  
  19.                 $(topCoord).set('value', cropCutFromTop);  
  20.                 $(leftCoord).set('value', cropCutFromLeft);  
  21.    
  22.             }  
  23.         });  
  24.     }  
  25.    
  26.     window.addEvent('load'function () {  
  27.         document.ondragstart = function () { return false; };  
  28.         cropImage('imgPhoto''div3''div2''hdn1''hdn2');  
  29.     });  
  30. </script> 
What this code will do is, it will set the Aspect Ratio, Height and Width of an image up to which it can be dragged.
 
At the end of the code, you can see that I added an "ondragstart" function to help in running the application in IE, without this code your application will not run in IE.
 
Output
 
First of all this is the type of image that will be shown to you.
 
drag3.jpg
 
Now you can reposition this image by dragging it.
 
Animation3.gif
 
Now if you click on the Button it will show you the position of the image from where it will be cropped at this instance of time.
 
drag8.jpg


Similar Articles