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

Particles

Processingライクに描画するメインクラス
package {
  
  import flash.display.*;
  import flash.events.*;
  import flash.geom.*;
  import flash.utils.*;
  
  /**
  * Processingライクに描画するメインクラス
  */
  public class Particles extends Sprite {
    var canvas:BitmapData;
    var particleClasses:Array = [Particle,Particle2];
    var particleClass:Class;
    var particles:Array;
    const numParticles:uint = 100;
    
    /**
    * コンストラクタ
    */
    public function Particles() {
      setUp();
    }
    
    /**
    * 初期化
    */
    public function setUp():void {
      canvas = new BitmapData( stage.stageWidth, stage.stageHeight, true, 0xFFFFFFFF );
      addChild( new Bitmap( canvas ) );
      particles = new Array();
      for( var i:int=0; i<numParticles; i++ ){
        particleClass = particleClasses[ i % 2 ];
        var _:Particle = new particleClass( stage ) as Particle;
        _.p.x = Math.random() * stage.stageWidth;
        _.p.y = Math.random() * stage.stageHeight;
        _.v.x = Math.random() * 2 - 1;
        _.v.y = Math.random() * 2 - 1;
        particles.push(_);
      }
      addEventListener( Event.ENTER_FRAME, draw );
      stage.addEventListener( MouseEvent.MOUSE_DOWN, mouseHandle );
      stage.addEventListener( MouseEvent.MOUSE_UP, mouseHandle );
    }
    
    /**
    * 描画処理
    * @param	event
    */
    public function draw( event:Event ):void {
      canvas.draw( canvas, null, new ColorTransform( 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF * .2 ),null,null,false);
      particles.forEach(
        function( particle:Particle, index:int, array:Array ){
          particle.move();
          particle.render( canvas );
        }
      );
    }
    
    /**
    * マウスイベントハンドル
    * @param	event
    */
    public function mouseHandle( event:MouseEvent ):void {
      switch( event.type ){
        case MouseEvent.MOUSE_DOWN:
          particles.forEach(
            function( particle:*, index:int, array:Array ){
              particle.isPressed = true;
            }
          );
        break;
        case MouseEvent.MOUSE_UP:
          particles.forEach(
            function( particle:*, index:int, array:Array ){
              particle.isPressed = false;
            }
          );
        break;
      }
    }
    
  }
  
  
}

import flash.display.*;
import flash.geom.*;

  /**
  * 粒子の実装定義インターフェイス
  */
  interface IParticle {
    function move():void;
    function render( canvas:BitmapData ):void;
  }
  
  /**
  * 粒子クラス
  */
  class Particle implements IParticle {
    
    var subject:DisplayObject;
    
    var stage:Stage;
    var isPressed:Boolean;
    
    /**
    * 座標
    */
    var p:Point = new Point();
    /**
    * 移動ベクトル
    */
    var v:Point = new Point();
    /**
    * 色
    */
    var col:ColorTransform;
    /**
    * 拡大率
    */
    var scale:Number = 1;
    /**
    * 回転角(ラジアン)
    */
    var rotation:Number = 0;
    
    /**
    * コンストラクタ
    * @param	_stage
    */
    public function Particle( _stage:Stage ) {
      if( !subject )
        subject = getSubject();
      stage = _stage;
      col = new ColorTransform( 0, 0, 0, 0, Math.random() * 0xFF, 0x00, 0x64, Math.random() * 0x66 );
    }
    
    /**
    * 移動処理
    */
    public function move():void {
      
      v.x += Math.random() * 2 - 1;
      v.y += Math.random() * 2 - 1;
      v.x *= 0.99;
      v.y *= 0.99;
      
      if( isPressed ){
        var t:Point = new Point( stage.mouseX, stage.mouseY );
        t = t.subtract(p);
        t.normalize(v.length);
        v = t;
      }
      
      p = p.add(v);
      rotation = Math.atan2( v.y, v.x );
      modifyPositoin();
      
      var distance:Number = Point.distance( new Point( stage.mouseX, stage.mouseY ), p );
      var maxDistance:Number = Point.distance( new Point(), new Point( stage.stageWidth, stage.stageHeight ) );
      scale = 10 * distance / maxDistance + .1;
   }
    
    /**
    * 描画処理
    * @param	canvas
    */
    public function render( canvas:BitmapData ):void{
      var matrix:Matrix = new Matrix();
      matrix.rotate( rotation );
      matrix.scale( scale, scale );
      matrix.translate( p.x, p.y );
      canvas.draw( subject, matrix, col);
    };
    
    /**
    * 画面をはみ出た場合の処理
    */
    private function modifyPositoin():void {
      //  画面をはみ出たら跳ね返る処理
      p.x = ( p.x < 0 ) ? 0 : ( p.x > stage.stageWidth ) ? stage.stageWidth : p.x;
      p.y = ( p.y < 0 ) ? 0 : ( p.y > stage.stageHeight ) ? stage.stageHeight : p.y;
      v.x *= ( p.x == 0 || p.x == stage.stageWidth ) ? -1 : 1;
      v.y *= ( p.y == 0 || p.y == stage.stageHeight ) ? -1 : 1;
    }
    
    /**
    * 描画する主題を生成
    * @return
    */
    protected function getSubject():DisplayObject {
      var sp:Shape = new Shape();
      sp.graphics.beginFill( 0xFFFFFF );
      sp.graphics.drawRect( -1, 0, 3, 1 );
      sp.graphics.drawRect( 0, -1, 1, 3 );
      sp.graphics.drawRect( 0, 0, 1, 1 );
      return sp as DisplayObject;
    }
    
  }
  
  /**
  * 粒子クラス2
  */
  class Particle2 extends Particle {
    /**
    * コンストラクタ
    * @param	_stage
    */
    public function Particle2( _stage:Stage ){
      super( _stage );
    }
    /**
    * 描画する主題を生成
    * @return
    */
    override protected function getSubject():DisplayObject {
      var sp:Shape = new Shape();
      sp.graphics.beginFill( 0xFFFFFF );
      sp.graphics.drawCircle(0,0,1.5);
      return sp as DisplayObject;
    }
  }