JSFiddle - React, Tailwind, and code Playground
HTML
<div id="container">
<img src="http://i.imgur.com/8jV4DEn.png" id="player" alt="hero"/>
</div>
CSS
body {
margin: 0;
padding: 0;
}
#container {
position: absolute;
top: 70px;
left: 200px;
width: 40px;
height: 60px;
border:1px solid black;
}
#player{
width: 40px;
height: 60px;
}
JavaScript
var angles = [];
function calculateAngle(){
var player = $('#player');
var container = $('#container');
//Checks to see which key is pressed down
$(window).on('mousemove', function (e) {
//Current position
var p1 = {
x: container.offset().left + container.width()/2,
y: container.offset().top + container.height()/2
};
//Future position
var p2 = {
x: e.clientX,
y: e.clientY
};
console.log(p1, p2);
//Angle between them in degrees
var angleDeg = Math.atan2(p2.y - p1.y, p2.x - p1.x) * 180 / Math.PI;
angleDeg -= 90;
//Animate the whole thing
player.css('-webkit-transform', 'rotate(' + angleDeg + 'deg)');
});
}
$(document).ready(function(){
calculateAngle();
});