Saturday, 8 February 2014

360 degree rotation

Leave a Comment

 Many times you would have seen 360 degree roation of some objects on web page to display its all around dimensions close to reality. Here, is the code snippet that can be used for achieving the same visual effect. We have to take enough number of images of same dimensions and keem them at same location. Name these images in an order say, from 1 to 35.

Use the HTML Code below:

  1. <link href="styles/mystyle.css" rel="stylesheet">  
  2. <script src="main.js"></script>  
  3.   
  4.   
  5. <div id="mydiv">  
  6.  <img src="images/1.jpg">  
  7. </div>  
Use the CSS Code below:
  1. body  
  2. {  
  3.  padding: 0px;  
  4.  margin: 0px;   
  5. }  
  6. div  
  7. {  
  8.    
  9.  width: 210px;  
  10.    
  11. }  
Create a "main.js" javascript file and Use the Code below:
  1. window.onload = init;  
  2.   
  3. var mydiv;  
  4. var down = false;  
  5. var selectedPoint = 0;  
  6. var file = 1;  
  7. function init(e)  
  8. {  
  9.  mydiv = document.getElementById("mydiv");  
  10.  mydiv.addEventListener("mousedown",onDown);  
  11.  mydiv.addEventListener("mouseup",onUp);  
  12.  mydiv.addEventListener("mousemove",onMove);  
  13.  mydiv.addEventListener("mouseout",onOut);  
  14. }  
  15.   
  16. function onOut(e)  
  17. {  
  18.  e.preventDefault();  
  19.  down = false;  
  20. }  
  21.   
  22. function onDown(e)  
  23. {  
  24.  selectedPoint = e.clientX;  
  25.  e.preventDefault();  
  26.  down = true;  
  27.    
  28. }  
  29. function onUp(e)  
  30. {  
  31.  e.preventDefault();  
  32.  down = false;  
  33. }  
  34.   
  35. function onMove(e)  
  36. {  
  37.  if(down)  
  38.  {  
  39.   if(selectedPoint > e.clientX)  
  40.   {  
  41.    //console.log(selectedPoint,e.clientX);  
  42.    nextImage();     
  43.   }else if(selectedPoint < e.clientX)  
  44.   {  
  45.    //console.log(selectedPoint,e.clientX);  
  46.    prevImage();  
  47.   }  
  48.   selectedPoint = e.clientX;  
  49.  }  
  50. }  
  51.   
  52. function nextImage()  
  53. {  
  54.  if(file==35) file = 1;  
  55.  mydiv.children[0].setAttribute("src","images/"+file+".jpg");  
  56.  file++;  
  57. }  
  58. function prevImage()  
  59. {  
  60.  if(file == 0) file = 35;  
  61.  mydiv.children[0].setAttribute("src","images/"+file+".jpg");  
  62.  file--;  
  63. }  
I hope you found this tutorial to be of help and useful. Please leave a message in the comment section below, should you need further assistance or advice.
 
 
      
      
      
      
    

0 comments:

Post a Comment