2

I have come up with the following script (based on answers here and around the web) to pinch/zoom and move zoomed image. It will do the zooming in/out but will not move the resized image. The document.write script never executes even though the code looks perfectly fine. Am I missing something obvious?

$(image).hammer().on('pinch', function(event) {
    var photo = $(this);

    newWidth = photo.width() * event.gesture.scale;
    newHeight = photo.height() * event.gesture.scale;

    // Convert from screen to image coordinates
    var x;
    var y;
    x -= offset.left + newX;
    y -= offset.top + newY;                                     

    newX += -x * (newWidth - width) / newWidth;
    newY += -y * (newHeight - height) / newHeight;

    photo.css('transform', "scale3d("+event.gesture.scale+", "+event.gesture.scale+", 1)");      
    wrap.css('transform', "translate3d("+newX+"px, "+newY+"px, 0)");

    width = newWidth;
    height = newHeight;
// Reset pinch
}).on('pinchout',function(event){
    photo.css('transform', "translate3d(0,0,0)");
// Start of moving zooming
}).on('pan panstart', function(event){
    photo.css('transform', "translate3d("+event.gesture.deltaX+"px, "+event.gesture.deltaY+"px, 0) ");                  
    document.write('ending panning 1');
// End of moving
}).on('pan panend', function(event){
    photo.css('transform', "translate3d(0,0,0)");
    panX += event.gesture.deltaX/zoom;
    panY += event.gesture.deltaY/zoom;
    wrap.css({left: panX,top: panY});
    document.write('ending panning 2');
})                          

1 Answer 1

1

var hammer = new Hammer(/** DOM Element **/); hammer.on(/** Your Script **/);

Edit: Ya instantiated Hammer.js wrong. Notice that I wrote DOM Element, it doesn't seem to work quite right with JQuery referenced elements.

2
  • 3
    Please augment your code-only answer with some explanation in order to help fighting the misconception that StackOverflow is a free coding service.
    – Yunnosch
    Commented Aug 2, 2019 at 22:11
  • I thought the code block was short enough that it didn't need an explanation but ok.
    – Dauas
    Commented Aug 9, 2019 at 22:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.