Home can i set touch event on playing card when i use canvas on surface view?
Reply: 1

can i set touch event on playing card when i use canvas on surface view?

Sumit Saxena
1#
Sumit Saxena Published in 2018-02-14 09:36:55Z

I have a question related to android platform.Can i set touch event on playing cards while using canvas and surface view.I upload a image.card is work but not set touch event on card.

if you know about lib and another technology, please share your experience with me.

mansim
2#
mansim Reply to 2018-02-14 10:23:12Z

Just put an extra invisible canvas element on top of all other elements. make it to be the last. It will take all click/touch events. For example:

<canvas id="main" style="position:absolute;" width="500" height="500" >Canvas is not supported</canvas>
<canvas id="game" style="position:absolute;" width="500" height="500" >Canvas is not supported</canvas>
<canvas id="click_touch_events" style="position:absolute;" width="500" height="500" >Canvas is not supported</canvas>

Then get context for this canvas

var canvasM  = document.getElementById("main");
var canvasG  = document.getElementById("game");
var canvasT  = document.getElementById("click_touch_events");
var ctxM = canvasM.getContext("2d");
var ctxG = canvasG.getContext("2d");
var ctxT = canvasT.getContext("2d");


canvasT.addEventListener("touchstart", function (e) {
  e.preventDefault();
  var mousePos = getTouchPos(canvasM, e);
  var touch = e.touches[0];
  do_mouse_click_logic(mousePos.x, mousePos.y, touch.clientX, touch.clientY);
}, false);

canvasT.addEventListener("touchend", function (e) {
  e.preventDefault();
  var mousePos = getTouchPos(canvasM, e);
  do_mouse_up_logic(mousePos.x, mousePos.y);
}, false);

canvasT.addEventListener("touchmove", function (e) {
  e.preventDefault();
  var mousePos = getTouchPos(canvasM, e);
  var touch = e.touches[0];
  do_mouse_move_logic(mousePos.x, mousePos.y, touch.clientX, touch.clientY);
}, false);

And now you can set the card coordinates, and when player clicks on the screen, he clicks on the canvasT. Then your canvasT event listener is triggered, it gives you coordinates where the player clicked.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.293693 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO