Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question