Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TrackballControls - Touch Support #1822

Closed
JGeerWM opened this issue Apr 26, 2012 · 8 comments
Closed

TrackballControls - Touch Support #1822

JGeerWM opened this issue Apr 26, 2012 · 8 comments
Labels

Comments

@JGeerWM
Copy link

JGeerWM commented Apr 26, 2012

Are there any plans to add Touch Support to TrackballControls.js? I am looking to use the pinch gesture in IE 10 to zoom, and a swipe to twist the camera.

@mrdoob
Copy link
Owner

mrdoob commented Apr 26, 2012

Are you aware of any javascript lib that implements touch gestures?

@JGeerWM
Copy link
Author

JGeerWM commented Apr 26, 2012

@alteredq
Copy link
Contributor

I have found this:

http://eightmedia.github.com/hammer.js/

@JGeerWM
Copy link
Author

JGeerWM commented Apr 26, 2012

http://eightmedia.github.com/hammer.js/ doesn't appear to recognize IE 10 touch events

@mrdoob
Copy link
Owner

mrdoob commented Apr 26, 2012

I'm only aware of IE 10 gestures
http://msdn.microsoft.com/en-us/library/ie/hh673557(v=vs.85).aspx#Gesture_events

I can't see the pinch gesture there...

@JGeerWM
Copy link
Author

JGeerWM commented Apr 27, 2012

http://bing.maps.com handles the pinch well. I'll try to understand how they are doing it.

@mrdoob
Copy link
Owner

mrdoob commented Dec 13, 2012

Implemented in r53.

@mrdoob mrdoob closed this as completed Dec 13, 2012
@ericnoble
Copy link

As of r54, the touch implementation in TrackballControls.js could still use some improvement, particularly with the zoom. The current zoom implementation is activated when two fingers touch the screen, but only uses the first touch position in its calculations. This results in unpredictable behavior when a user attempts a pinch-zoom, and is particularly sensitive to which finger touched the display first. A better implementation would use the distance between the two touch points, and then compare the initial distance at touchstart to the final or current distance during a touchmove or touchend. See code below. Tested in Google Chrome and Firefox 18 on Windows 8 touch display, as well as on Firefox 18 for Android (Jelly Bean) on a Samsung Galaxy S3.

function touchstart( event ) {
    if ( ! _this.enabled ) return;

        switch ( event.touches.length ) {
            case 1:
                _state = STATE.TOUCH_ROTATE;
                _rotateStart = _rotateEnd = _this.getMouseProjectionOnBall( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
                break;
            case 2:
                _state = STATE.TOUCH_ZOOM;
                var p1 = new THREE.Vector2(event.touches[0].pageX, event.touches[0].pageY);
                var p2 = new THREE.Vector2(event.touches[1].pageX, event.touches[1].pageY);
                _touchZoomDistanceCurrent = _touchZoomDistancePrevious = p1.distanceTo(p2);
                break;
            case 3:
                _state = STATE.TOUCH_PAN;
                _panStart = _panEnd = _this.getMouseOnScreen(event.touches[0].pageX, event.touches[0].pageY);
                break;
            default:
                _state = STATE.NONE;
    }
}

function touchmove( event ) {
    if ( ! _this.enabled ) return;
    event.preventDefault();
    event.stopPropagation();
        switch ( event.touches.length ) {
        case 1:
            _rotateEnd = _this.getMouseProjectionOnBall( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
            break;
        case 2:
            var p1 = new THREE.Vector2(event.touches[0].pageX, event.touches[0].pageY);
            var p2 = new THREE.Vector2(event.touches[1].pageX, event.touches[1].pageY);
            _touchZoomDistanceCurrent = p1.distanceTo(p2);
            break;
        case 3:
            _panEnd = _this.getMouseOnScreen(event.touches[0].pageX, event.touches[0].pageY);
            break;
        default:
            _state = STATE.NONE;
    }
}

this.zoomCamera = function () {
    if (_state === STATE.TOUCH_ZOOM) {
        var factor = _touchZoomDistancePrevious / _touchZoomDistanceCurrent;
        _touchZoomDistancePrevious = _touchZoomDistanceCurrent;
        _eye.multiplyScalar(factor);
    } else {
        var factor = 1.0 + (_zoomEnd.y - _zoomStart.y) * _this.zoomSpeed;
        if (factor !== 1.0 && factor > 0.0) {
            _eye.multiplyScalar(factor);
            if (_this.staticMoving) {
                _zoomStart.copy(_zoomEnd);
            } else {
                _zoomStart.y += (_zoomEnd.y - _zoomStart.y) * this.dynamicDampingFactor;
            }
        }
    }
};

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

No branches or pull requests

4 participants