2

I'm basically using this to make my image draggable in a canvas. Make image drawn on canvas draggable with JavaScript

I'm trying to add touch events and all of the touch events work except touchleave. The touchleave event doesn't seem to be firing when the user attempts to drag the image outside of the canvas. Is there a different touch event that works for this purpose?

Community
  • 1
  • 1
Guy Lee
  • 193
  • 1
  • 10

1 Answers1

0

Please see this. Here they are actually triggering mouse events for touch evnts. this may help you.

touchstart – to toggle drawing mode “on”
touchend – to toggle drawing mode “off”
touchmove – to track finger position, used in drawing


// Set up touch events for mobile, etc
canvas.addEventListener("touchstart", function (e) {
        mousePos = getTouchPos(canvas, e);
  var touch = e.touches[0];
  var mouseEvent = new MouseEvent("mousedown", {
    clientX: touch.clientX,
    clientY: touch.clientY
  });
  canvas.dispatchEvent(mouseEvent);
}, false);
canvas.addEventListener("touchend", function (e) {
  var mouseEvent = new MouseEvent("mouseup", {});
  canvas.dispatchEvent(mouseEvent);
}, false);
canvas.addEventListener("touchmove", function (e) {
  var touch = e.touches[0];
  var mouseEvent = new MouseEvent("mousemove", {
    clientX: touch.clientX,
    clientY: touch.clientY
  });
  canvas.dispatchEvent(mouseEvent);
}, false);

// Get the position of a touch relative to the canvas
function getTouchPos(canvasDom, touchEvent) {
  var rect = canvasDom.getBoundingClientRect();
  return {
    x: touchEvent.touches[0].clientX - rect.left,
    y: touchEvent.touches[0].clientY - rect.top
  };
}
Rohith K P
  • 3,233
  • 22
  • 28
  • I already have these touch events mapped. I just don't have the touch equivalent of mouseout working – Guy Lee Apr 29 '17 at 07:27