Zooming to Center

Ken Ray kray at sonsothunder.com
Sun May 18 16:08:52 EDT 2008


> I have an image that is much larger than the stack.  I have two
> buttons, ZoomIn and ZoomOut.  They do their jobs as far as zooming in
> and out by resizing the image, but I would like the part of the image
> that is in the center of the screen to stay there during the zoom
> (when possible).  Right now it moves around and I can't figure out
> the math to make it seem to zoom to (or from) the part of the picture
> that is in the center of the card.

Here's what I did - measure the distance from the clickH to the left of the
object, divide by the current zoom  factor, then multiply by the new zoom
factor. Then after scaling the image, set the left of the image to the
center of the card, minus the new distance. Do the same for the top.

Something like this:

  -- First, note the click location relative to the image
  put pClickH - (the left of pImgObj) into tDistFromLeft
  put pClickV - (the top of pImgObj) into tDistFromTop
  put the uScale of pImgObj into tCurrZoom
  put round((tDistFromLeft / tCurrZoom) * pNewZoom) into tNewDistFromLeft
  put round((tDistFromTop / tCurrZoom) * pNewZoom) into tNewDistFromTop

where "pImgObj" is the image being scaled, "the uScale" is the custom
property scoring the current scale factor before zooming, and "pNewZoom" is
the new scale factor. Then apply it like this:

  put the loc of this card into tCardLoc
  if pNewZoom <> 1 then
    set the left of pImgObj to ((item 1 of tCardLoc) - tNewDistFromLeft)
    set the top of pImgObj to ((item 2 of tCardLoc) - tNewDistFromTop)
  else
    set the loc of pImgObj to tCardLoc
  end if

If you want the whole script that handles everything (I had to do this to
figure out what worked), let me know and I'll post it.

Ken Ray
Sons of Thunder Software, Inc.
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/





More information about the use-livecode mailing list