PDA

View Full Version : scale image from center


mantra
07-25-2009, 06:37 AM
Hi i was just wondering if there was a way to scale an image from it's center point rather than from the top left corner.

I have 2 HSliders, one for width and one for height. I also have a checkbox to enable 'maintain aspect ratio'. With aspect ratio turned on obviously you can just use one slider to scale the image up or down. I'm in the process of adding rotation as well, and once i scale the image or rotate it it goes funny as the rotation and scaling are happening from the top left corner.

Can anyone recommend a way to set the scale and rotation positions to the center of the image ?

Thx

wvxvw
07-26-2009, 01:23 PM
If it's an image, just use it's bitmapdata, use drawing API to draw that bitmapdata into Sprite which would have it's center point in X=0, Y=0 (i.e. actual bounds would be:
left: width * -0.5
top: height * -0.5
right: width * 0.5
bottom: height * 0.5

Alternatively, you may use transformation matrix, and calculate those values before making any transformation, then apply all the transformations all at once. Regretfully, if you're going to apply each transformation consequently, you're going to run into number of problems like:
the order in which you perform the transformations matters (if you rotate and then move / stretch the object, it won't be the same as if you first moved / stretched it and then rotated).
There is an ambiguity, or maybe even a bug in AS, where after rotating an object by the value that scales with 90 degrees, when you will try to set it's width, you'll be actually setting it's height, and by setting it's height, you will, again, set it's height.