Search code examples
actionscript-3zoominggesture

AS3 - zoom gesture get zoom direction?


I've been searching for a way to do what seems like a very simple task. I have a mousewheel zoom function which works perfectly because the delta value is reset to 0 after each function. The zoom gesture it seems is not so simple? My issue is that I'm trying to create a dynamic scale limit (max zoom will be different for each level). and the zoom is getting stuck at the max scale that I'm setting and can't be zoomed out. I'm trying to use the e.scaleX property to identify the direction so that you can zoom out, but it's not working properly.

Thanks for any help! the code:

Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener(TransformGestureEvent.GESTURE_ZOOM , onZoom); 
function onZoom (e:TransformGestureEvent):void{ 
    var zoomAmountGesture:Number = 0;
    zoomAmountGesture = e.scaleX;

    if (zoomAmountGesture <= 0){
container.scaleX *= e.scaleX;
container.scaleY *= e.scaleY;
        if (container.scaleX < 1){
            container.scaleX = 1;
            container.scaleY = 1
        }
    }
    if (zoomAmountGesture > 0){
    container.scaleX *= e.scaleX;
    container.scaleY *= e.scaleY;
        if(4* BlockSize * container.scaleX > StageWidth){
            trace("zoom too big");
            var newBlockScale:Number = StageWidth / 3;
            var newBoardScale:Number = newBlockScale / BlockSize;
            trace("newBoardScale = " + newBoardScale);
            container.scaleX = newBoardScale;
            container.scaleY = newBoardScale;
        }
    }
}

Solution

  • I had a quick read of TransformGestureEvent.

    The next block has no meaning, because the event scaleX and scaleY properties are 1-based:

    if (zoomAmountGesture <= 0){
    

    Then, you should probably start with the following:

    function onZoom (e:TransformGestureEvent):void
    {
        var aScale:Number = container.scaleX * e.scaleX;
        if (aScale < 1) aScale = 1;
        if (aScale > PredefinedMaxScale) aScale = PredefinedMaxScale;
    
        container.scaleX = aScale;
        container.scaleY = aScale;
    }