锤击,css变换和变换原点的手势运动

时间:2014-04-29 10:18:15

标签: javascript svg transform hammer.js css-transforms

我试图在SVG环境中创建多手势导航。我已经能够通过Hammer.js提供的示例成功实现拖放,并通过应用转换CSS3属性来缩放和旋转。

转换似乎有效,但是当我进行第二次转换/旋转并且原点似乎丢失并且两个运动之间存在跳跃时,问题会附加。

这是一个有问题实现的jsfiddle。您可以按住maj按钮尝试多点触控(旋转/缩放)。

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();
});

1 个答案:

答案 0 :(得分:0)

根据我的理解,您的问题是,您正在修改转换中心(transform-origin)。我理解你想要实现的目标,即让新的修改应用于夹点/旋转手势的中心。但是,通过更改转换中心,您还可以更改先前应用的所有转换的中心,从而产生跳跃行为。

要实现您想要的功能,您可能需要执行一些矩阵运算并使用css matrix属性。从理论上讲,我猜你是:

  • 需要保留当前的转换矩阵(TM)
  • 应用新转换时
    • 使用矩阵乘法
    • 将手势移动到手势的负变换中心
    • 矩阵将TM与手势的比例和位置相乘
    • 通过正转型中心移动TM
    • 将新TM应用于css矩阵属性

希望这有帮助

本杰明