mardi 29 avril 2014

Gesture movement with hammerjs, css-transform and transform origin


Vote count:

0




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://ift.tt/1m5Twxn



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


asked 46 secs ago






Aucun commentaire:

Enregistrer un commentaire