tags:

views:

199

answers:

1

I'm creating an image editing program in GTK\C, and I can't figure out how to make zoom work; I need to zoom my Cairo-based canvas to the mouse cursor's position, but Cairo only zooms to center of the image it displays. The normal way to get around this is to transform, zoom, and then transform back; but that messes up the scrollbars surrounding my canvas.

My idea was to slide the scrollbars back to compensate for the movement caused by zooming. Moving the scrollbars by mouse_position * (new_zoom - old_zoom) ought to work, but it doesn't; it under-corrects at low zoom levels, then over-corrects at high levels.

How can I zoom correctly?

My code is visible here, if it helps: ScreenSnap on Github

A: 

It looks like you need to scale your shift to your current zoom level in the code. So this line:

shift_x = self->zoom_point_x * (self->zoom_level / .9 - self->zoom_level);

Should be:
shift_x = self->zoom_point_x * (self->zoom_level / .9 - self->zoom_level)/(self->zoom_level);

Note that your approximation worked at a zoom level of one but as you move away you either under or overcorrect.

dave
Huh? If I replace that line, then self->zoom_point_x doesn't have any data, and I need that for the shift_x = line. Do you mean to replace the line below it?
snostorm
Sorry, I copy and pasted the wrong line!! Take a look now.
dave
Hm... that doesn't seem to work. It now consistently undercorrects by a large amount.
snostorm