pop.input.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * polls input and updates POP.m object
  3. *
  4. * @access public
  5. * @return null
  6. *
  7. */
  8. POP.input = function() {
  9. var target = $("#SF_Game canvas")[0];
  10. var $target = $(target);
  11. $target.on('click', function(e) {
  12. if (POP.state === 'play') {
  13. e.preventDefault();
  14. }
  15. var width = POP.canvas.width, height = POP.canvas.height;
  16. var rwidth = $target.width(), rheight = $target.height();
  17. POP.m.set(
  18. e.offsetX * width / rwidth,
  19. e.offsetY * height / rheight
  20. );
  21. });
  22. $target.on('touchstart', function(e) {
  23. if (POP.state === 'play') {
  24. e.preventDefault();
  25. }
  26. var touch = e.touches[0], offset = $target.offset();
  27. var width = POP.canvas.width, height = POP.canvas.height;
  28. var rwidth = $target.width(), rheight = $target.height();
  29. var x = (touch.pageX - offset.left) * width / rwidth;
  30. var y = (touch.pageY - offset.top) * height / rheight;
  31. POP.m.set(x, y);
  32. });
  33. target.addEventListener('touchmove', function(e) {
  34. window.scrollTo(0, 1);
  35. e.preventDefault();
  36. return false;
  37. }, false);
  38. target.addEventListener('touchend', function(e) {
  39. window.scrollTo(0, 1);
  40. e.preventDefault();
  41. return false;
  42. }, false);
  43. };