In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Water Surface demo

flash version of http://lib.ivank.net/?p=demos&d=water
Get Adobe Flash player
by Ivan_Kuckir 09 Sep 2012
    Embed
/**
 * Copyright Ivan_Kuckir ( http://wonderfl.net/user/Ivan_Kuckir )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/cVTZ
 */

package 
{
    import flash.display.Sprite;
    import flash.display.Loader;
    
    import flash.net.URLRequest;
    
    import flash.events.Event;
    import flash.events.MouseEvent;
    
    public class FlashTest extends Sprite 
    {
        var h = [];  // buffers for heights
        var v = [];  // buffers for velocities
        var vrt = new Vector.<Number>();  // vertices
        var ind = new Vector.<int>(); // indices
        
        var s, n=40, clicked = false, calm = 360;
        var rad = Math.round(n/12);

        public function FlashTest ()
        {
            stage.frameRate = 60;
            
            var bg = new Loader();
            bg.load(new URLRequest("http://lib.ivank.net/demos/winter2.jpg"));
            bg.scaleX = bg.scaleY = stage.stageHeight/512; 
            stage.addChild(bg);
            
            s = new Sprite();
            stage.addChild(s);
            stage.addEventListener(Event.ENTER_FRAME, onEF);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, onMD);
            stage.addEventListener(MouseEvent.MOUSE_UP  , onMU);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, onMM);
            initWaves(stage.stageWidth, stage.stageHeight);
        }
        
        function initWaves(wi, hi)
        {
            var step = wi/(n-1);
            for(var i=0; i < n; i++) {h.push(calm); v.push(0);}
            for(var i=0; i < n  ; i++) vrt.push(i*step,calm,   i*step,hi); 
            for(var i=0; i < n-1; i++) ind.push(2*i,2*i+1,2*i+2,  2*i+1,2*i+2,2*i+3);
        }
        
        function onEF(e)
        {
            for(var i=0; i<n; i++)
            {
                // computing velocity from neighbouring heights
                v[i] += ((he(i-1)+he(i+1)) + calm) /3 - h[i];
                v[i] *= 0.98;    // damping
                h[i] += v[i] * 0.05;
                vrt[i*4+1] = h[i];
            }
            s.graphics.clear();
            s.graphics.beginFill(0x4466aa, 0.5);
            s.graphics.drawTriangles(vrt, ind);
        }
      
        function he(i)   { return h[(i+n)%n]; }    // "cycled" access to array 'h'
        
        function onMD(e) { clicked = true; onMM(0); }
        function onMU(e) { clicked = false; }
        function onMM(e)
        {
            var i = Math.round(n*stage.mouseX/stage.stageWidth);
            if(clicked) if(i>rad && i<n-rad) pushAt(i);
        }
       
        function pushAt(i)
        {
            for(var j=-rad; j<rad; j++)
                h[i+j] += Math.cos(j*Math.PI*0.5/rad)*15;
        }
    }
}