The mini project I picked as my first dip into AS3 was a short example that I had been planning for this blog, so here it is in AS3, instead of AS2 as I had originally planned.
The South Park Chin balls Flash app required that the user drag and zoom a loaded photo. Anyone who’s ever done this will know that you can’t just scale the picture around it’s registration point when you zoom. Why? because the centre point changes as you pan the image around. So the requirement in a nutshell is – to be able to scale a MovieClip around an arbitrary centre.
Sure, you could take the Russian doll approach with multiple clips inside clips, but that just ain’t cool! I thought I’d share the way I did it as it’s pretty concise.
First of all here’s the principal in action. Scaling and rotation, both performed around an arbitrary centre point that you can alter by dragging that little marker around. Go on, have a go.
[ Update: due to a nasty server crash some of these files are missing.. sorry. This source was rescued though ]
Loading example SWF…
Download the source files for this example
Most of the source files just serve to create the example interface. The only class we need to discuss is the one with the scale and rotating logic. The class VirtualCentreSprite
is an extension of the Sprite
class. In AS2 it would have to be an extension of the MovieClip
class, but we don’t need a timeline. You could use this as a base class for sprites across your project to ensure they all have this capability.
It has two methods as follows:
void scaleAround( offsetX:Number, offsetY:Number, absScaleX:Number, absScaleY:Number )
void rotateAround( offsetX:Number, offsetY:Number, toDegrees:Number )
The scaling method is the simplest. It’s basically GCSE vector maths. We calculate the position that the clip should be at after scaling, do the scaling (which will move the clip according to the centre that Flash knows about) and then we simply reposition it to where it should be.
Click to see source of method scaleAround
The rotating method is a bit more complex, because it uses a transformation matrix. But thanks to the flash.geom
package this is short and sweet. Again the principal is that we let Flash rotate the sprite and then adjust the position.
Click to see source of method rotateAround
Good idea, I’ve been looking for some help on this topic. I don’t know if you remember, but is there a way to continuously scale an object. I placed your function inside of a zoom function that scales the image every time a key is pressed, but it only scales it (up or down) once. I cannot get the object to scale again after the first time.
This is a very old article, the only link that survived my lost server was this one:
http://cf.timwhitlock.info/wp-content/uploads/2008/04/virtualcentresprite.html
broken link for source code :(
Nice! here is another article that I found to be helpful in getting the scaling part done. There is a cool little demo at the end of the article showing how it works.
http://www.actionscript-flash-guru.com/blog/33-scale-around-a-point-transform-multiple-objects-actionscript-3-as3
OOPS
Sorry guys, I’ve had a major hardware crash and I’ve lost the source code for this demo… I’m working to restore it / find it / rebuild it [!]
I have spent a number of hours getting close to, but not fully being able to create what you have here. Thanks for sharing, this really helped me
Thank you very much!
Nice demo Man.
All what the math the newbies want.