D
D
Den_13372018-06-30 15:03:46
JavaScript
Den_1337, 2018-06-30 15:03:46

How to rotate an object towards the mouse?

var 
  		canvas = document.getElementById("canvas");
      canvas.width=window.innerWidth;
   		canvas.height=window.innerHeight;
var 
    ctx=canvas.getContext("2d");    

var mouse;
$(window).mousemove(function(e){
//	mouse =  угол куда должен поворачиватся объект
});

function drawRect (x,y,w,h,a){
  var dx = x+w/2;
  var dy = y+h/2;

if(a){
  a = a * (Math.PI / 180); // переводим радианы в градусы
  ctx.save();
  ctx.translate(dx,dy);
  ctx.rotate(a);//тут объект поворачивается
  ctx.translate(-dx,-dy);	
}

  ctx.strokeRect(x,y,w,h);

if(a){
  ctx.restore();
}

}


setInterval(function(){
  ctx.clearRect(0,0,innerWidth,innerHeight);
  drawRect(50,50,50,50,mouse);

},0);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-06-30
@Den_1337

So ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question