Skip to content
This repository has been archived by the owner on Aug 25, 2022. It is now read-only.

Error work on mobile devices #54

Open
lydie88 opened this issue Apr 11, 2017 · 1 comment
Open

Error work on mobile devices #54

lydie88 opened this issue Apr 11, 2017 · 1 comment

Comments

@lydie88
Copy link

lydie88 commented Apr 11, 2017

wScratchPad.js:318 Uncaught TypeError: Cannot read property 'screenX' of undefined
at HTMLCanvasElement. (wScratchPad.js:318)
at HTMLCanvasElement.dispatch (jquery.1.11.0.min.js:3)
at HTMLCanvasElement.r.handle (jquery.1.11.0.min.js:3)

@ghost
Copy link

ghost commented May 3, 2017

This is due to the touchend event not getting a touch object.

I've modified my wScratchPad.js L:288
and replaced $.fn.bindMobileEvents function with below.

  $.fn.bindMobileEvents = function () {
    var touchScreenX = 0.0
    var touchScreenY = 0.0
    var touchClientX = 0.0
    var touchClientY = 0.0
    var target = null;
  
    $(this).on('touchstart touchmove touchend touchcancel', function (event) {
      var touches = (event.changedTouches || event.originalEvent.targetTouches),
          first = touches[0],
          type = '';

      switch (event.type) {
      case 'touchstart':
        type = 'mousedown';
        break;
      case 'touchmove':
        type = 'mousemove';
        event.preventDefault();
        break;
      case 'touchend':
        type = 'mouseup';
        break;
      default:
        return;
      }
      
      if ( first ) {
        touchScreenX = first.screenX;
        touchScreenY = first.screenY;
        touchClientX = first.clientX;
        touchClientY = first.clientY;
        target = first.target;
      }

      var simulatedEvent = document.createEvent('MouseEvent'); 

      simulatedEvent.initMouseEvent(
        type, true, true, window, 1, 
        touchScreenX, touchScreenY, touchClientX, touchClientY, 
        false, false, false, false, 0/*left*/, null
      );

      target.dispatchEvent(simulatedEvent);
    });

Simply caching the parameters solves the issue, but this isn't a permanent solution.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant