Flash Hacks: Simulate 3D and Add a Vector Edge to a Bitmap - Event Handlers
(Page 3 of 5 )
The event handlers that control the movement per slice are shown next. The initClip( ) function is used as the onEnterFrame( ) event handler for each slice; it’s a simple initialization script that deletes itself after running once.
initClip = function () {
// Initialize this slice
this.startX = this._x;
this.startY = this._y; delete (this.onEnterFrame);
this.onMouseMove = mover;
};
mover = function () {
// Move this slice according to its position in the slice stack.
var distX = this.offset * (_xmouse - this._x) / 50;
var distY = this.offset * (_ymouse - this._y) / 50;
this._x = this.startX + distX;
this._y = this.startY + distY;
updateAfterEvent();
};
Following initialization, the mover( ) function runs whenever the mouse moves. This onMouseMove( ) event handler is almost identical to the earlier eye movement code implemented within an onEnterFrame( ) handler [Hack #4]. The major difference is that in this slice animation, the offset property gives a slightly different range of movement for each slice.
That’s all there is to the effect. It’s a surprisingly simple concept and simple code.
Why did we choose a different event than the one used in earlier eye movement animation? Flash issues onMouseMove events as frequently as it can while the mouse is moving. The onEnterFrame event runs at the frame rate. When the mouse is stationary, onEnterFrame events are still issued, but during mouse movement, onEnterFrame events occur less often than onMouseMove events. You can confirm this by running both event handlers at the same time with the onMouseMove( ) handler incrementing a counter variable each time it is called, and the onEnterFrame( ) handler decrementing the same counter. You will find that the counter climbs when the mouse is moving because, during that time, onMouseMove( ) is invoked more often than onEnterFrame( ).
So we choose an event to supply responsiveness in the user’s area of focus: we concentrate more processing power [Hack #71] on the 3D animation because we expect the user to look at it more intently. The earlier eye movement animation is too subtle to look any better if we throw more processing resources at it, so we use the less-processor-intensive onEnterFrame event instead.
You can also vary the width and height of the slices as they move. As an object rotates left or right, each slice’s width should change when viewed from the front. Likewise, when an object rotates up or down, each slice’s height should change. Adding the following lines (shown in bold) to the main animation script sometimes (but not always) helps the effect look more realistic:
mover = function () {
// Move this slice according to its position in the slice stack.
var distX = this.offset * (_xmouse - this._x) / 50;
var distY = this.offset * (_ymouse - this._y) / 50;
this._x = this.startX + distX;
this._y = this.startY + distY;
this._width = 100 - Math.abs(distX)
this._height = 100 - Math.abs(distY)
updateAfterEvent();
};
Final Thoughts
Our 2D-to-3D bitmap effect could use further refinement now that you grasp the basic technique. The basic technique effectively maps the image slices onto a cone. If you move the mouse cursor a great distance from the tarsier, the cone shape reveals itself, as seen in Figure 5-4.
Because the mask consists of a simple circle that gets bigger, the slices’ perimeters mark out a cone. By hand drawing each slice mask using perimeters that map to the true 3D object more closely, you can create an effect that holds up at severe angles, allowing rotation to almost 180 degrees. (You can’t rotate to a full 180 degrees because the thickness of the slices tends toward zero at that extreme. You’d have to switch to another set of slices created with the object viewed at a different angle.)
The more slices you use, the better the effect, which comes at the expense of performance. You can make your slices more numerous where the contour of the 3D shape curves the most. You must then vary the offset in a nonlinear way to emulate the fact that the slices are no longer equally spaced in depth.
 | If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!
Visit the O'Reilly Network http://www.oreillynet.com for more online content. |
Next: Hack 23: Add a Vector Edge to a Bitmap >>
More Flash Articles
More By O'Reilly Media