Even if the following code snippet seems short, I struggled during days (shame on me!) to find a way to zoom on the point that is clicked using only CSS3 transform
. It works now:
var current = {x: 0, y: 0, zoom: 1}, c = document.getElementById('container');
window.onclick = function(e) {
wx = current.x + e.clientX / current.zoom;
wy = current.y + e.clientY / current.zoom;
var coef = e.ctrlKey ? 0.5 : 2;
current.zoom *= coef;
current.x = wx - e.clientX / current.zoom;
current.y = wy - e.clientY / current.zoom;
c.style.transform = 'scale(' + current.zoom +') translate(' + (-current.x) + 'px,' + (-current.y) + 'px)';
};
html, body { margin: 0; padding: 0; overflow: hidden; min-height: 100%; }
#container { position: absolute; transform-origin: 0 0; transition-duration: 3s;}
#item { position: absolute; left:0px; top:0px; }
<div id="container"><div id="item"><img src="http://fadili.users.greyc.fr/demos/WaveRestore/EMInpaint/parrot_original.png"></img></div></div>
The only problem is that the transition is weird, like if it first translates and then zooms ; it produces a weird zigzag effet. How to have a smooth CSS3 transition in this case?
See animated GIF here of the weird transition effect: http://gget.it/zf3fmwum/weirdtransition.gif
Note: the point which is clicked on is a fixed point of the scaling transform (example: click on the eye, the image is zoomed, and the cursor is still on the eye), like in GoogleMaps-doubleclick-zooming.
One thing to watch out for when using transforms is the order that you apply them. You'll find your example works rather differently if you switch the
scale
and thetranslate
around.Here is an interesting article on the matter:
https://staff.washington.edu/fmf/2011/07/15/css3-transform-attribute-order/
I wasn't able to repair your version, mainly because it misbehaves unexpectedly when you switch the order of the transforms. Basically it seems you are running into odd behaviour because the scale itself causes an automatic translation in position, and then you also translate... and it seems these different translations are occurring at a slightly different pace.
I did however re-implement a version that works, and allows you to translate before scaling. Keeping the transforms in this order seems to avoid the issue.
http://jsfiddle.net/fxpc5rao/32/
I've modified the version below to use
translate3D
just because it performs better for many systems.update
I've updated my answer (and the snippet above) to take into account your additional requirement, you just need to modify the calculation to include the difference in mouse pointer offset.
http://jsfiddle.net/fxpc5rao/33/
Now with every click the difference between the calculated unscaled position and
e.clientX, e.clientY
is added. This gives you the offset you need to keep the zoomed translation occurring around the mouse pointer. The key change is here:update 2
Good call @Basj, I wasn't aware that the transformations occurred in reverse order, I'll add the link in from your comment here:
CSS3 transform order matters: rightmost operation first
So as you say, you require the scale to occur before the translate in processing terms, but the translate to be written before the scale in the actual transform value — if that makes sense :) Still not exactly sure why doing one before the other results in the odd interpolation however.
Also, I've noticed there is a rather obvious optimisation — which I'm sure, as you are implementing this, you will have spotted — no point adding something only to subtract it later. I guess I'd just had too much festive cheer that day!
update 3
No problem @jdavies, it is just a matter of converting your mouse coordinates so they are relative to the container's top left. How you calculate this offset will depend entirely on your project (it is much easier to get a layer's offset — cross browser — using something like jQuery.offset). However I've updated the code in this answer to take into account a hard-coded/fixed offset away from 0,0 using position absolute — just to illustrate. Here is an updated fiddle too:
http://jsfiddle.net/fxpc5rao/5/
As we are using
clientX
andclientY
the mouse coordinates will always be calculated from the top left of the browser window, making them global to the page (disregarding scrolling). In order to localise them to the container, you just need to subtract the containers x and y position.The
#container
can also be contained within other elements, you just again have to take into account any positional offset these elements give to the#container
. In the following fiddle there is a#page-setting
element that is offsetting everything with margin, as long as theox, oy
variables are updated with the margin values everything should behave.http://jsfiddle.net/fxpc5rao/34/
Tough problem viewing / zooming and paning an image, right? :)
I finally succeeded in calibrating the zoom algorithm so I want to share it with the community. I created a viewer class to interact with underlying image. One important point in my solution is that it doesn't modify default transform-origin, which could be useful for some other transforms.
You could use click to zoom / ctrl + click to unzoom, or pinch in pinch out (uses Hammer JS). Warning, touch events are not enabled by default on Firefox.
I am sorry, I know it uses Hammer and home made Transform & Point classes but please focus on the zoomTo method which is framework agnostic and is the main point of this zoom problem.
(You will find the TypeScript version below if you prefer)
Try it in this snippet
TypeScript version