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

パーティクル

Get Adobe Flash player
by akira_odohira 21 May 2009
    Embed
/**
 * Copyright akira_odohira ( http://wonderfl.net/user/akira_odohira )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/2q8k
 */

package{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
		
	public class Particle1 extends Sprite{
		private var _cnt:uint = 150000;
		private var _bmStage:BitmapStage;
		private var _timer:Timer;
		private var _time:uint = 6000;
		
		public function Particle1() {
			init();
		}
		
		private function init():void{
			_bmStage = new BitmapStage(_cnt, stage.stageWidth, stage.stageHeight);
			addChild(_bmStage.bm);
		//	addChild(_bmStage.perlinBitmap);
			addEventListener(Event.ENTER_FRAME, rendering, false, 0, true);
			setTimer();
		}

		private function rendering(e:Event):void {
			//rendering
			_bmStage.rendering();
		}
		
		private function setTimer():void{
			_timer = new Timer(_time, 1);
			_timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComp, false, 0, true);
			_timer.start();
		}
		
		private function timerComp(e:TimerEvent):void {
			_bmStage.createPerlinNoise();
			var time:uint = _time >> 1;
			_timer.delay = Math.random() * time + time;
			_timer.reset();
			_timer.start();
		}
		
	}
}

import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.geom.ColorTransform;
import flash.geom.Rectangle;

class BitmapStage extends Sprite{
	private var _cnt:uint;
	private var _W:uint;
	private var _H:uint;
	private var _bmd:BitmapData;
	private var _bm:Bitmap;
	private var _perlinBitmap:Bitmap;
	private var _particle:Particle;
	private var _colorTransform:ColorTransform;
	private var _rect:Rectangle;
	private var _perlinBitmapData:BitmapData;
	private var _random:int = Math.random() * 0xff;
	//
	public function get bm():Bitmap { return _bm; }
	public function get perlinBitmap():Bitmap { return _perlinBitmap; }
	
	
	public function BitmapStage(cnt:uint, W:uint, H:uint) {
		_cnt = cnt;
		_W = W;
		_H = H;
		init();
	}
	
	private function init():void{
		_rect = new Rectangle(0, 0, _W, _H);
		
		_bmd = new BitmapData(_W, _H, false, 0);
		_bm = new Bitmap(_bmd);
		
		_perlinBitmapData = new BitmapData(_W >> 1, _H >> 1, false, 0);
		_perlinBitmap = new Bitmap(_perlinBitmapData);
		createPerlinNoise();
	//	_colorTransform = new ColorTransform(1, 1, 1, 1, 0.95, 0.95, 0.95, 0.95);
		
		var i:uint;
		var prevParticle:Particle = _particle = new Particle();
		var particle:Particle;
		while (++i <= _cnt) {
			particle = new Particle();
			particle.x = Math.random() * _W;
			particle.y = Math.random() * _H;
			prevParticle.next = particle;
			prevParticle = particle;
		}
	}
	
	public function createPerlinNoise():void{
		_perlinBitmapData.perlinNoise(_W, _H, 3, _random, true, true, 3);
		_random = Math.random() * 0xff;
	}
	
	public function rendering():void{
		var particle:Particle = _particle;
		_bm.bitmapData.lock();
		_bm.bitmapData.fillRect(_rect, 0);
	//	_bm.bitmapData.colorTransform(_rect, _colorTransform);
		while ((particle = particle.next) != null){
			var col:int = _perlinBitmapData.getPixel(particle.x >> 1, particle.y >> 1);
			particle.x += particle.vx = (particle.vx * 0.98) + ((col >> 16 & 0xff) -128) * 0.004;
			particle.y += particle.vy = (particle.vx * 0.98) + ((col >> 8 * 0xff) -128) * 0.004;
			if (particle.x > _W) particle.x = 0; 
			else if (particle.x < 0) particle.x += _W; 
			if (particle.y > _H) particle.y = 0; 
			else if (particle.y < 0) particle.y += _H; 
			_bm.bitmapData.setPixel(particle.x, particle.y, 0xffffff);
		}
		_bm.bitmapData.unlock();
	}
	
}

class Particle {
	public var x:Number = 0;
	public var y:Number = 0;
	public var vx:Number = 0;
	public var vy:Number = 0;
	public var next:Particle;
	public function Particle () {}	
}