June 15th, 2010

Nervous Spot

This movie requires Flash Player 9

Playing around with AS3, perhaps somebody wants to take the code and make a proper Rorschach test ;-):

/*
Copyright (c) 2010 Ingmar Drewing

Permission is hereby granted, free of charge, to any person obtaining 
a copy of this software and associated documentation files (the “Software”), 
to deal in the Software without restriction, including without limitation 
the rights to use, copy, modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons to whom the 
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be 
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, 
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
OTHER DEALINGS IN THE SOFTWARE.
*/

package  {
  import flash.display.*;
  import flash.utils.*;
  import flash.events.Event;
  import flash.events.MouseEvent;
  import flash.filters.BlurFilter;
  import flash.geom.*;

  [SWF(backgroundColor="#FFFFFF",frameRate="60",width="460",height="460")]

  public class Spot extends Sprite {
  private var W:int;
  private var H:int;
  private var rendering:Boolean = false;
  private var dots:Array = new Array();
  private var canvas:Bitmap = new Bitmap();
  private var range:int = 80;
  private var bg_colour:Number = 0xFFFFFF;
  private var centerpoint:Point;
  private var sync_x:Number;
  private var sync_y:Number;
  private var check_interval:int;

  private var colour:Number = 0x000000;

  public function Spot () {
    init();
  }

  private function init():void{
    H = stage.stageHeight;
    W = stage.stageWidth;
    centerpoint = new Point( W/2, H/2 );
    sync_x = W/2;
    sync_y = H/2;

    for ( var dx:int = 1 ; dx < W; dx+=10 ){
      for ( var dy:int = 1 ; dy < H; dy+=20 ){
        var d:Point = new Point( dx, dy );
        dots.push(d);
      }
    }

    addChild( canvas );
    render();
    stage.addEventListener( MouseEvent.MOUSE_MOVE,
    start_following_mouse );
  }

  private function stop_following_mouse( e:Event ):void{
    removeEventListener( Event.ENTER_FRAME, mouse_sync );
    sync_x = W/2;
    sync_y = H/2;
    stage.addEventListener( MouseEvent.MOUSE_MOVE,
        start_following_mouse );
    check_interval = setInterval( check_render_necessity, 500 );
  }

  private function check_render_necessity():void{
    var dx:Number = sync_x - centerpoint.x;
    var dy:Number = sync_y - centerpoint.y;
    var distance:Number = Math.sqrt( dx*dx + dy*dy );
    if ( distance < 1 ){
      stop_rendering();
      clearInterval( check_interval );
    }
  }

  private function start_following_mouse( e:MouseEvent=null ):void{
    clearInterval( check_interval );
    start_rendering();
    stage.removeEventListener( MouseEvent.MOUSE_MOVE,
      start_following_mouse );
    stage.addEventListener( Event.MOUSE_LEAVE, stop_following_mouse );
    addEventListener( Event.ENTER_FRAME, mouse_sync );
  }

  private function mouse_sync( e:Event ):void{
    sync_x = mouseX;
    sync_y = mouseY;
  }

  private function start_rendering():void{
    if ( !rendering ){
      rendering = true ;
      addEventListener( Event.ENTER_FRAME , render );
    }
  }

  private function stop_rendering():void{
    if ( rendering ){
      rendering = false ;
      removeEventListener( Event.ENTER_FRAME , render );
    }
  }

  private function render( e:Event=null ):void{
    centerpoint.x = sync_x - ( sync_x - centerpoint.x )*5/6 ;
    centerpoint.y = sync_y - ( sync_y - centerpoint.y )*5/6  ;
    var bd:BitmapData = new BitmapData( W, H, false, bg_colour );
    bd.lock();

    for each ( var dot:Point in dots ){
      var dx:Number = dot.x - centerpoint.x ;
      var dy:Number = dot.y - centerpoint.y ;
      var distance:Number = Math.sqrt( dx*dx + dy*dy );

      if ( distance < range ){
        var r:int = (1-distance/range) *1000;

        var current_x:int = dot.x;
        var current_y:int = dot.y;
        for( var i:int = 0; i < r; i++){
          bd.setPixel(current_x, current_y, colour);
          var randX:Number =  Math.random();
          if ( randX < .33 ){                             
            current_x -=1;                         
          }                         
          else if ( randX >=.33 && randX < .66 ){
            current_x +=1;
          }
           var randY:Number =  Math.random();
           if ( randY < .33 ){                             
             current_y -=1;                         
          }                         
         else if ( randY >=.33 && randY < .66 ) {
           current_y +=1;
        }
      }
    }
  }

  bd.unlock();
  canvas.bitmapData = bd;
  }
 }
}