Question

I'm trying to create a multi-gesture navigation in a SVG environment. I've been able, with the example provided by Hammer.js to implement successfully the drag/drop and the pinch to zoom and rotate by applying transformation the the CSS3 attributes.

The transformation seems to work, but the issue appends when I do a second transformation / rotation and the origin point seems to be lost and there is a jump between the two movements.

Here is a jsfiddle with an implementation of the issue. You can try the multitouch (for rotation / zoom) by holding the maj button.

http://jsfiddle.net/TdCcW/

canvas = {
    posX: 0,
    posY: 0,
    lastPosX: 0,
    lastPosY: 0,
    xImage: 0,
    yImage: 0,
    xLast: 0,
    yLast: 0,
    newPosX: 0,
    newPosY: 0,
    anchorX: 0,
    anchorY: 0,
    lastAnchorX: 0,
    lastAnchorY: 0,
    bufferX: 0,
    bufferY: 0,
    scale: 1,
    lastScale: undefined,
    rotation: 0,
    last_rotation: undefined,
    dragReady: 0,
    transformOrigin: '',
    initTouch: function () {
        Hammer.plugins.showTouches();
        Hammer.plugins.fakeMultitouch();

        var hammertime = Hammer(document.getElementById('container'), {
            transform_always_block: true,
            transform_min_scale: 1,
            drag_block_horizontal: true,
            drag_block_vertical: true,
            drag_min_distance: 0
        });

        var posX = 0,
            posY = 0,
            lastPosX = 0,
            lastPosY = 0,
            bufferX = 0,
            bufferY = 0,
            scale = 1,
            last_scale,
            rotation = 1,
            last_rotation, 
            dragReady = 0;

        hammertime.on('touch drag dragend transform', function (ev) {
            elemRect = document.getElementById('viewport');
            canvas.manageMultitouch(ev);
        });
    },
    manageMultitouch: function (ev) {

        switch (ev.type) {
            case 'touch':
                canvas.last_scale = canvas.scale;
                canvas.last_rotation = canvas.rotation;
                break;

            case 'drag':
                canvas.posX = ev.gesture.deltaX + canvas.lastPosX;
                canvas.posY = ev.gesture.deltaY + canvas.lastPosY;
                break;

            case 'transform':
                canvas.rotation = canvas.last_rotation + ev.gesture.rotation;
                canvas.scale = Math.max(1, Math.min(canvas.last_scale * ev.gesture.scale, 10));

                canvas.anchorX = (ev.gesture.center.pageX - canvas.lastPosX);
                canvas.anchorY = (ev.gesture.center.pageY - canvas.lastPosY);

                canvas.transformOrigin = canvas.anchorX + " " + canvas.anchorY;

                break;

            case 'dragend':
                canvas.lastPosX = canvas.posX;
                canvas.lastPosY = canvas.posY;

                break;
        }



        var transform =
            "translate3d(" + canvas.posX + "px," + canvas.posY + "px, 0) " +
            "scale3d(" + canvas.scale + "," + canvas.scale + ", 0) " +
            "rotate(" + canvas.rotation + "deg) ";

        elemRect.style.transform = transform;
        elemRect.style.oTransform = transform;
        elemRect.style.msTransform = transform;
        elemRect.style.mozTransform = transform;
        elemRect.style.webkitTransform = transform;

        elemRect.style.webkitTransformOrigin = canvas.transformOrigin;



    }
}

$(document).ready(function () {
    canvas.initTouch();
});
Was it helpful?

Solution

From my understanding, your problem is, that you are modifying the transformation center (transform-origin). I understand what you are trying to achieve, i.e. let the new modification apply centered at the center of the pinch/rotate gesture. However, by changing the transformation center, you also change the center for all the transformations you did apply previously, resulting in the jumpy-behaviour.

To realize what you want, you probably need to do some matrix operations and use the css matrix attribute. Theoretically I would guess that you:

  • need to keep the current transformation matrix (TM)
  • when a new transformation is applied
    • move the TM by the negative transformation center of the gesture using matrix multiplication
    • matrix multiply the TM with the scale and position of the gesture
    • move the TM by the positive transformation center
    • apply the new TM to the css matrix attribute

Hope this helps

Benjamin

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top