Dynamic 3d space | Floating Sketches Tutorial

    parallax_thumbI’ve had quite a few questions about how to make depth in flash. Earlier (like, 2 years ago) I put up an experiment file to give some interactive depth to some sketchbook sketches, see Floating Sketches. I’ve finally gotten around to translating that into as3. It’s still the same basic idea, Create layers of levels, and have each one respond to the mouse a little differently. The ‘closer’ depths will move faster while the farther away depths will be slower. A simple technique called Parallax.

    1. Seperate the scene into layers
    2. Place the layers in the correct depth
    3. Make closer layers react fast and farther layers slower

    Example

    Get Adobe Flash player

    parallax_thumb

    Actionscript

    //define number of layer.
    var numLayers:uint = 15;
    //number of items in a layer
    var numBallsPerLayer:uint = 100;
    var defaultBallSize:uint = 25;

    var stageWidth3d:uint = 800;
    var stageHeight3d:uint = 800;

    var layers:Array = new Array();
    //init
    makeMatrix();
    //3d created by layers and placing objects on each layer - the layer has it's own distance, simulated by movement and alpha

    function makeMatrix():void {
        //walk through desired number of layers
        for (var layerNum:uint=0; layerNum<numLayers; layerNum++){
            //make layer object to add balls to
            var thisLayer:MovieClip = new MovieClip();
            addChild(thisLayer);
            layers.push(thisLayer);
            //create desired number of balls per layer
            for (var ballNum:uint=0; ballNum<numBallsPerLayer; ballNum++){
                //trace("layer: "+layerNum +"; ball: "+ballNum);
                var c1:Number = randomColor();
                var c2:Number = randomColor();

                //create ball
                var thisBall:MovieClip = new MovieClip();
                thisBall.graphics.beginFill(c1, .9);
                thisBall.graphics.lineStyle(defaultBallSize/3, c2, .9);
                thisBall.graphics.drawCircle(defaultBallSize, defaultBallSize, defaultBallSize);
                thisBall.graphics.endFill();

                //apply filter to ball
                var myGradientGlowFilter = new GradientGlowFilter(0,
                                                      0,
                                                      [c1, c2, c1],
                                                      [0, .9, 1],
                                                      [0, 63, 255],
                                                      20,
                                                      20,
                                                      .9,
                                                      BitmapFilterQuality.HIGH,
                                                      BitmapFilterType.OUTER,
                                                      false);
                var myBlurFilter = new BlurFilter((numLayers-layerNum)/numLayers*5, (numLayers-layerNum)/numLayers*5, 1);
                var filtersArray:Array = new Array(myGradientGlowFilter, myBlurFilter);
                thisBall.filters = filtersArray;

                thisLayer.addChild(thisBall);

                //coordinates
                thisBall.x = Math.random() * stageWidth3d * layerNum;
                thisBall.y = Math.random() * stageHeight3d * layerNum;
                //size
                thisBall.scaleY = thisBall.scaleX = layerNum/numLayers * (Math.random() + 1);

                //ball animation
                thisBall.layer = layerNum;
                thisBall.addEventListener(Event.ENTER_FRAME, animateBall);
            }

            //layer depth/alpha
            thisLayer.alpha = layerNum/numLayers;

        }
        //listeners to animate from mouse movement
        addEventListener(Event.ENTER_FRAME, positionLayer);

    }

    var easingStrength:Number = 10;

    function positionLayer(e:Event):void{
        for (var layerNum:uint=0; layerNum<numLayers; layerNum++){
            //move layers according to mouse poosition
            var xv:Number = (stage.stageWidth-layers[layerNum].width)/stage.stageWidth;
            layers[layerNum].x -= (layers[layerNum].x - xv * mouseX) / easingStrength;
            var yv:Number = (stage.stageHeight-layers[layerNum].height)/stage.stageHeight;
            layers[layerNum].y -= (layers[layerNum].y - yv * mouseY) / easingStrength;
        }
    }

    function animateBall(e:Event):void{
        e.target.x += (Math.random() - .5) * 2 * e.target.layer/numLayers;
        e.target.y += (Math.random() - .5) * 2 * e.target.layer/numLayers;
    }

    function randomColor():Number{
        return Math.floor(Math.random() * 16777215);
    }

    Source

    depth.fla

    This entry was posted in tutorial and tagged , , , , , , , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

    4 Comments

    1. Charlie Tlatenchi
      Posted August 21, 2009 at 5:59 am | Permalink

      Wow this is great! thanks for the post.

    2. Posted August 31, 2009 at 4:18 pm | Permalink

      Hi, very awesome work ! i build my website in as2 but i want to rebuild in as3

      i have a similar source for the float background controlled by the mouse in as2

      I am very interesting by your code but i am a beginner in as3 i am a graphic designer not a developer and it’s very hard for me :/

      it’s possible to have an help to modify your source for obtain the similar effect with white stars ?

      i am waiting for your answer
      Kind regards, Yann

    3. DaveH
      Posted September 6, 2009 at 2:15 pm | Permalink

      Great post. I’ve been trying to research this technique for manipulating layers in photographs(check out http://82.ufc.com/ ) and have found a few tutorials that use as2 but this is the first I’ve seen that is using as3. I’m quite new at all this and was hoping you c0uld show a simple as3 script to give this kind of functionality…much the same as your original floating sketches but in as3.

    4. Radi
      Posted July 21, 2010 at 1:53 am | Permalink

      Great effect :)

      Anyway, I also made Parallax Effect in AS3, it’s pure AS3 class. And I think it can be used for many purposes.

      You can check it out here http://as3gameprogrammer.wordpress.com/2010/07/20/parallax-scrolling-effect-in-as3/

      Hope this helps people that in need of this simple but beautiful effect ;)

    Post a Comment

    Your email is never published nor shared. Required fields are marked *

    *
    *

    You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    • Recent Posts

      WordPress updates plugin directory

      New additions to the plugin directory include: favorites, incorporating support forums into it's own tab for each plugin as well as support stats being displayed! Great! I think we also need the ability to give plugins ratings and reviews (bonus points if it can be done from within a wordpress admin dashboard when installing plugins). [...]

      Short Head

      Use zipf's short head to tune your website rather than redesign the whole thing. To make a website successful it needs to meet the needs of the users. Find out what those needs are by using the short head philosophy to equate most searched things as the biggest needs of the users. Use personas to [...]

      Img Set?

      Great article at a list apart discusing the state of the industry regarding responsive images. This picks apart the set attribute of the img element from a surprisingly objective view coming from someone so close to the picture element. Insightful discussion about the principle behind the proposals than the actual solution too. If the working [...]

      Triudo

      A mesmerizing animated triangle-ish shape form. Embedded Link triduo triduo Tweet

      Git – the paradigm shift

      A great developer story about the differences on what Git is vs other version control and what Git is not. This is how we should learn it. I heard over and over that it was distributed, but never grasped what that meant, so here are a few links and explanations that will help unlearn version [...]

      Tweening Lib comes to Javascript!

      I'm very excited to share the news that the tween library from GreenSock (hands down the best tweening library I used in flash) is not ported for use in javascript! This will be great! I missed that simple syntax from as3 when animating javascript, and now I can have my cake and eat it too. [...]

      Responsive CSS Tricks

      Here are a few useful css tricks to remember when building responsive design sites from web designer wall Embedded Link 5 Useful CSS Tricks for Responsive Design Making the design to be responsive is very easy as shown in my Responsive Design in 3 Steps tutorial, but maintaining the elements to look aesthetically balanced on [...]

      Picture element of srcset attribute?

      Bruce details the reasons and story behind the srcset attribute which is now introduced as an alternative to the picture element. Some aspects of the attribute are nice (like the fact that it's an attribute and not a new element, so it's creating up new elements with for problems. It's adapting currently used elements to [...]

      SVG Preloader with Raphael JS

      Here's a very creative use of using a newly available technology. Using svg graphics which are very lightweight, for a website preloader. I like the animation used as well. Embedded Link Make a stylish preloader with SVG | Tutorial | .net magazine Many sites neglect users with slow connections. Ian Culshaw explains how to use [...]

      CSS3 Button/Icon set

      I've been secretly hoping to see a few of these pop up once the whole icon font idea spread through the nets. I really like this idea and it's a very nice implementation too! I only see some quality issues on a couple of the icons (such as youtube), but it's awesome and I hope [...]

    • Recent Comments

      Bruce Brownlee

      Bruce Brownlee

      Ah IE6. I'd have 2 more years of sleep without IE6. Margin doubling, no properties,...
      versaena

      versaena

      how to give color at runtime…… thank you
      Mobile Websites

      Mobile Websites

      I disagree, mobile websites are the future – desktop websites and mobile websites...
      Matt Fasick

      Matt Fasick

      That's cool. I like the ripple effect as well.
      Nico

      Nico

      hi! really great job guy! very impressive.. just a question… do u have a solution to do a refresh...
      Evan Mullins

      Evan Mullins

      Agreed! I've just seen some people get pretty heated about separating all functionality...