Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Image Pan / Zoom

By combining some CSS and Jquery UI / draggable I have created the ability to pan an image and with a little extra JS you can now zoom the image.

The problem I am having is that, if you zoom in the image's top left corner is fixed, as you would expect. What I would like is for the image to stay central (based on the current pan) so that the middle of the image stays in the middle of the container whilst getting larger.

I have written some code for this but doesn't work, I expect my maths is wrong. Could anyone help?

I want it to work like this does. When you scroll into an image it keeps the image centered based on the current pan rather than zooming out from the corner.

HTML:

<div id="creator_container" style="position: relative; width: 300px; height: 400px; overflow: hidden;">
    <img src="/images/test.gif" class="user_image" width="300" style="cursor: move; position: absolute; left: 0; top: 0;">
</div>

Javascript:

$("#_popup_creator .user_image").bind('mousewheel', function(event, delta) {
    zoomPercentage += delta;
    $(this).css('width',zoomPercentage+'%');
    $(this).css('height',zoomPercentage+'%');

    var widthOffset = (($(this).width() - $(this).parent().width()) / 2);
    $(this).css('left', $(this).position().left - widthOffset);
});
like image 947
Chris Avatar asked Jun 07 '11 18:06

Chris


People also ask

How to pan and zoom JavaScript?

To change the zoom of the workspace area (dotted border), you have to use a mouse wheel or touchpad with the “Ctrl” key pressed. You can change scale sensitivity by passing the scaleSensivity property to the renderer instance. To change the pan area you have to move your mouse/touchpad with the “Shift” key pressed.

How do you zoom in and zoom out in JavaScript?

JavaScript can be used to manipulate an HTML element's style property to create a zoom-in and zoom-out effect. To do this, simply access the width attribute using the style property and increase it or decrease it according to the requirement.

What is Panzoom?

panzoom is a simple jQuery & Vanilla JavaScript plugin that allows you to drag, pan, zoom in/out any elements of your webpage using CSS3 transforms. Fast, performant and mobile-friendly. It supports panning and zooming images, text, videos, divs and many other html elements.

How do you zoom in on a picture?

Zoom in or out on a picture , and then click and drag or tap and drag to center it where you want.


1 Answers

Long story short, you need to make a transform matrix to scale by the same amount as the image and then transform the image's position using that matrix. If that explanation is complete greek to you, look up "image transforms" and "matrix math".

The beginning of this page is a pretty good resource to start with even though it's a different programming language: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/geom/Matrix.html


Anyway, I've implemented those methods in some projects of my own. Here's the zoom in function from something I wrote that functions the way you want:

function zoomIn(event) {
    var prevS = scale;
    scale += .1;
    $(map).css({width: (baseSizeHor * scale) + "px", height: (baseSizeVer * scale) + "px"});

    //scale from middle of screen
    var point = new Vector.create([posX - $(viewer).width() / 2, posY - $(viewer).height() / 2, 1]);
    var mat = Matrix.I(3);
    mat = scaleMatrix(mat, scale / prevS, scale / prevS);
    point = transformPoint(mat, point);

    //http://stackoverflow.com/questions/1248081/get-the-browser-viewport-dimensions-with-javascript
    posX = point.e(1) + $(viewer).width() / 2;
    posY = point.e(2) + $(viewer).height() / 2;
    $(map).css({left: posX, top: posY});

    return false;//prevent drag image out of browser
}

Note the commands "new Vector.create()" and "Matrix.I(3)". Those come from the JavaScript vector/matrix math library http://sylvester.jcoglan.com/

Then note "transformPoint()". That's one of the functions from that ActionScript link (plus hints on http://wxs.ca/js3d/) that I implemented using sylvester.js

For the full set of functions I wrote:

function translateMatrix(mat, dx, dy) {
    var m = Matrix.create([
        [1,0,dx],
        [0,1,dy],
        [0,0,1]
    ]);
    return m.multiply(mat);
}
function rotateMatrix(mat, rad) {
    var c = Math.cos(rad);
    var s = Math.sin(rad);
    var m = Matrix.create([
        [c,-s,0],
        [s,c,0],
        [0,0,1]
    ]);
    return m.multiply(mat);
}
function scaleMatrix(mat, sx, sy) {
    var m = Matrix.create([
        [sx,0,0],
        [0,sy,0],
        [0,0,1]
    ]);
    return m.multiply(mat);
}
function transformPoint(mat, vec) {
    return mat.multiply(vec);
}
like image 73
jhocking Avatar answered Sep 25 '22 21:09

jhocking