cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div id="tank">
  <div id="cannon"></div>
</div>



            
          
!
            
              body{
  margin:0px;
  padding:0px;
  background-image:url('http://i221.photobucket.com/albums/dd22/djmid71/desert1_zps2de2be66.png');
}

#tank{
  left:50%;
  width:100px;
  height:50px;
  top:100px;
  margin-left:-50px;
  position:relative;
  overflow:visible;
}

#cannon{
   left:10px;
   top:13px;
}
            
          
!
            
              var tankImg = "http://i221.photobucket.com/albums/dd22/djmid71/tank2b-shadow_zps83fb730c.png";

var cannonImg = "http://i221.photobucket.com/albums/dd22/djmid71/tank2c_zpsbef20208.png";

var targetImg = "http://i221.photobucket.com/albums/dd22/djmid71/tank3damaged_zpse8155303.png";

var bulletImg = "http://i221.photobucket.com/albums/dd22/djmid71/bullet_zps08a7ecb6.png";

var loadedBullet;

$(document).ready(function(){
  
  var tank = new Image();
  tank.style.left = "0px";
  tank.style.top  = "0px";
  tank.onload = function(){
    
    $("#tank").append(tank);
    
  };
  
  tank.src = tankImg;
  
  //////////////
  
  var cannon = new Image();
  cannon.style.left = "10px";
  cannon.style.top  = "13px";
  cannon.style.position = "absolute";
  cannon.style.marginRight = "100%";
  cannon.style.overflow    = "visible";
  cannon.onload = function(){
    
    $("#cannon").append(cannon);
    
  };
  
  cannon.src = cannonImg;
  
  ////////////////
  
  var target = new Image();
  target.style.left = getRandom($(document).width()-50,50)+"px";
  target.style.top  = getRandom(300,50)+"px";
  target.style.position = "absolute";
  target.id = "target";
  target.onload = function(){
    
    $("body").append(target);
    var angle =  checkAngle();
    TweenMax.to("#cannon>img",5,{rotation:360,transformOrigin:9+"px "+7+'px',onUpdate:function(){
      
      var element = this.target;
      var _angle = Math.round(element[0]._gsTransform.rotation);
      
      console.log(_angle,"----",angle);
      if(_angle === angle || (angle<=_angle+5 && angle>=_angle-5) ){
        
        this.pause();
      }
      
    }});
  };
  
  target.src = targetImg;
  
  ///////////////////////
  
  var bullet = new Image();
  bullet.style.position = "absolute";
  bullet.onload = function(){
    loadedBullet = bullet;
  };
  

  
});


function checkAngle(){
 
 var targetX = $("#target").offset().left+25;
 var targetY = $("#target").offset().top+25;
 var targetW = $("#target").width();
 var targetH = $("#target").height();
  
  var angleY = (targetY-(targetH/2)) - $("#tank").offset().top;
	var angleX = (targetX-(targetW/2)) - $("#tank").offset().left;
	var angle = Math.round(Math.atan2(angleY, angleX) * 180 / Math.PI) + 90;// in degrees
		
	angle = angle - 90; // minus the tank angle, otherwise the cannon aligns to the front
  
  if(angle < 0){
    angle = 360 - Math.abs(angle); // converting negative angle to its counterpart
  }
  
  console.log(angle);
  
  return angle;
}

function getRandom(max, min) {
    return Math.floor(Math.random() * (1 + max - min) + min);
}
            
          
!
999px
Loading ..................

Console