samedi 31 janvier 2015

Drag images smooth on mobile devices with javascript


Hi i want to create a javascript application where i have to drag images around. I've broken it down to the minimum here (works only for touch events, needs jquery):





var draggable = $("#draggable")[0];
draggable.x = 0;
draggable.y = 0;

$("#draggable").bind('touchstart', function(e) {
draggable.lastmouse = e.originalEvent.touches[0];

});
$("#draggable").bind('touchmove', function(f) {
f = f.originalEvent.touches[0];
var newx = draggable.x + (f.clientX - draggable.lastmouse.clientX);
var newy = draggable.y + (f.clientY - draggable.lastmouse.clientY);
draggable.x = newx;
draggable.y = newy;
$(draggable).css('transform', 'translate3d(' + newx + 'px,' + newy + 'px,0)');
draggable.lastmouse = f;
});



<div class="map" id="draggable" style="width: 90%; height: 90%; position: absolute;">
<img src="http://ift.tt/1KgDXJP" width="100%" height="100%">
</div>



On a Computer it runs smooth, but on my GalaxyS4 its stuttering when i drag the image around, although i used translate3d. I thought it would be hardware accellerated.


Is there a way to get a native-app-like performance?





Aucun commentaire:

Enregistrer un commentaire