Flash
  Home arrow Flash arrow Page 3 - Flash Hacks: Simulate 3D and Add a Vector ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
FLASH

Flash Hacks: Simulate 3D and Add a Vector Edge to a Bitmap
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 13
    2004-08-10

    Table of Contents:
  • Flash Hacks: Simulate 3D and Add a Vector Edge to a Bitmap
  • Masking
  • Event Handlers
  • Hack 23: Add a Vector Edge to a Bitmap
  • Move the Vector Outline

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    bhangal 

    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.

    Buy the book!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.

    More Flash Articles
    More By O'Reilly Media


     

    FLASH ARTICLES

    - Decorator Pattern
    - Organizing Frames and Layers for Flash Anima...
    - Organizing Frames and Layers
    - Using XML and ActionScript with Flex Applica...
    - Interfaces and Events with ActionScript and ...
    - Manipulating Data with ActionScript in Flex ...
    - ActionScript Syntax for Flex Applications
    - ActionScript in Flex Applications
    - A Closer Look at Apollo`s File System API
    - Using the File System API
    - ActionScript 101
    - Flash Buttons
    - Advanced Flash Animation
    - Creating Your First Animated Movie with Flas...
    - Flash: Building Blocks






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT