I
I
Inter Carpenter2016-01-10 21:08:08
JavaScript
Inter Carpenter, 2016-01-10 21:08:08

How to track mouse movement how?

There is a playing field drawn on a Canvas using drawImage
How to track the movement of the mouse when clicking on the blocks for further actions in the game?
Moreover, the movements can be in any direction
. Similar to the swype application
dba6d32918ab81a09ff37549d74e366c.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Inter Plotnik, 2016-01-10
@Byrger

Here I have implemented both a mouse and a touch jsfiddle.net/4n4s8ccs/3
Based on the codes from the post ru.stackoverflow.com/a/418906

I
Ivanq, 2016-01-10
@Ivanq

Take for example onclick:

canvas.onclick = function(e) {
  var ev = e || event; // берем событие
  var x = ev.clientX - canvas.getBoundingClientRect().left; // Можно это и не считать в Firefox, там есть ev.layerX и ev.layerY
  var y = ev.clientY - canvas.getBoundingClientRect().top;
  if(x > 100 && x < 200 &&
    y > 100 && y < 200) {
    // Выполнить действие для кнопки с X = 100, Y = 100, width = 100, height = 100
  }
}

R
Robot, 2016-01-10
@iam_not_a_robot

document.addEventListener("mousemove", function() 
{ 
var mouseX = event.clientX+'px';
var mouseY = event.clientY+'px';
});

Pixels can be removed for the time being for css so that the block can be dragged right away ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question