James B
James B

Reputation: 51

GWT 2.5 Canvas toDataURL() works in MouseUp handler but not TouchEnd Handler

I have implemented a digital signature widget using the GWT 2.5 Canvas which works great with mouse events. Unfortunately it fails miserably for touch events and I have no idea why. On a touch screen the signature is drawn but canvas.toDataURL() alway return nothing ("data:," actually). Code snippets are:

  _canvas.addMouseMoveHandler(new MouseMoveHandler() {
  public void onMouseMove(MouseMoveEvent event) {
      int thisX = event.getRelativeX(_canvas.getElement());
      int thisY = event.getRelativeY(_canvas.getElement());
      if (_lastX >= 0 && _lastY >= 0) {
    _exists = true;
    _context.moveTo(_lastX, _lastY);
    _context.lineTo(thisX, thisY);
    _context.stroke();
      }
      _lastX = thisX;
      _lastY = thisY;
  }
});
  _canvas.addMouseUpHandler(new MouseUpHandler() {
  public void onMouseUp(MouseUpEvent event) { 
     _lastX = -1;
    _lastY = -1;
   _drawing = false; 
    if (_exists) _handler.sigChanged(_canvas.toDataUrl());
  }
});

  _canvas.addTouchMoveHandler(new TouchMoveHandler() {
  public void onTouchMove(TouchMoveEvent event) {
    if (event.getTouches().length() > 0) {
      Touch touch = event.getTouches().get(0);
      int thisX = touch.getRelativeX(_canvas.getElement());
      int thisY = touch.getRelativeY(_canvas.getElement());
      if (_lastX >= 0 && _lastY >= 0) {
    _context.moveTo(_lastX, _lastY);
    _context.lineTo(thisX, thisY);
    _context.stroke();
    //_handler.sigChanged(_canvas.toDataUrl());
      }
      _lastX = thisX;
      _lastY = thisY;
    }
    event.preventDefault();
    event.stopPropagation();
  }
});
  _canvas.addTouchEndHandler(new TouchEndHandler() {
  public void onTouchEnd(TouchEndEvent event) {
    _handler.sigChanged(_canvas.toDataUrl());
    com.google.gwt.user.client.Window.alert("onTouchEnd1: " + _canvas.toDataUrl());
  }
  event.preventDefault();
});

Again, toDataURL() works fine for the mouse events but always return nothing in the touch events, even if you call it from the touch move handler. Any help or suggestions would be greatly appreciated.

Upvotes: 0

Views: 160

Answers (1)

James B
James B

Reputation: 51

I borrowed a friends android phone that is running a newer version (4.1 vs 2.3) and it works. I don't understand it but I assume it's a bug with GWT 2.5 and I'm moving on.

James

Upvotes: 1

Related Questions