var field = document.getElementById('field');
var player = document.getElementById('player');
field.oncontextmenu = (e) => {
var xposition = (e.clientX - player.offsetLeft - player.offsetWidth/2);
var yposition = (e.clientY - player.offsetTop - player.offsetHeight/2);
player.style.transform = "translate("+ xposition + "px," + yposition + "px)";
e.preventDefault();
}
#field{
width: 500px;
height: 500px;
background-color: #999;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
#player{
background-color: #f30f4f;
width: 50px;
height: 50px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border-radius: 50%;
transform-origin: 25px 25px;
transition: all 0.3s linear;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="layout.css">
<title>TESTING</title>
</head>
<body>
<p> Click on the gray box anywhere and the dot will move to the click location</p>
<div id="field"></div>
<div id="player"></div>
<script src="layout.js"></script>
</body>
</html>
Instead of transition: all 0.3s linear;
is there a way to make it move f.e. 2px per 0.3s?
Right now if the player moves a longer distance it moves faster than moving a short distance (since it has to move further in 0.3s): I would want that to be the same consistent speed.