Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Second click on canvas is not being fired on mobile devices

I have a candy-crush like game and use this four listeners to swap items

canvas.addEventListener('mousemove', onMouseMove);
canvas.addEventListener('mousedown', onMouseDown); // also tried pointerup and pointerdown
canvas.addEventListener('mouseup', onMouseUp);
canvas.addEventListener('mouseout', onMouseOut);

It works perfectly on desktop, but sometimes second click (mousedown) (on second tile is not being fired at all). I mean listener function is not being called.
I have

cursor: pointer

in my CSS and I don’t know what to do.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Note: If you click next time somewhere on canvas, event is being triggered as if it was clicked in the old spot.
So if I what to change places of tiles x: 0, y: 0 and x: 0, y: 1:

  1. I select first tile (x: 0, y: 0)
  2. I try to select second tile (x:0, y:1). Event is not being triggered
  3. If I click on any random tile those two will swap places somehow

Help please! Thanks!

>Solution :

Use Touch Events Docs in conjunction with your existing code

canvas.addEventListener('touchmove', onMouseMove);
canvas.addEventListener('touchstart', onMouseDown);
canvas.addEventListener('touchend', onMouseUp);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading