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

Threshold + BlurFilter

Threshold + BlurFilter
Get Adobe Flash player
by alumican_net 09 Jan 2009
    Embed
/**
 * Copyright alumican_net ( http://wonderfl.net/user/alumican_net )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/zoPq
 */

/**
 * Threshold + BlurFilter
 */
package {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.geom.Point;
	import net.hires.debug.Stats;
        
	public class Main extends Sprite {
		
		private var _bmd:BitmapData;
		private var _sp:Sprite;
		private var _p:Vector.<Particle>;
		private var _n:uint;
		private var _pt:Point;
		private var _blur:BlurFilter;
		
		public function Main() {
			_bmd = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x000000);
			addChild(new Bitmap(_bmd));
			_sp = new Sprite();
			_pt = new Point();
			_blur = new BlurFilter(40, 40, 2);
			_n = 30;
			_p = new Vector.<Particle>(_n);
			for (var i:uint = 0; i < _n; ++i ) {
				var p:Particle = new Particle(_bmd.width, _bmd.height, 360 * i / _n);
				_sp.addChild(p);
				_p[i] = p;
			}
			addEventListener(Event.ENTER_FRAME, _update);
			//addChild(new Stats());
		}
		
		private function _update(e:Event=null):void {
			for (var i:uint = 0; i < _n; ++i ) _p[i].update();
			_bmd.lock();
			_bmd.fillRect(_bmd.rect, 0x000000);
			_bmd.draw(_sp);
			_bmd.applyFilter(_bmd, _bmd.rect, _pt, _blur);
			_bmd.threshold(_bmd, _bmd.rect, _pt, "<", 0x99999900, 0x00000000, 0x99999900, false);
			_bmd.unlock();
		}
	}
}

import flash.display.BlendMode;
import flash.display.Sprite;
class Particle extends Sprite {
	
	private var _vx:int, _vy:int, _r:uint, _w:uint, _h:uint;
	
	public function Particle(w:uint, h:uint, hue:uint):void {
		_w = w;
		_h = h;
		_vx = (Math.random() - 0.5) * 10;
		_vy = (Math.random() - 0.5) * 10;
		_r = 75 + Math.random() * 50;
		x = _r + Math.random() * _w - 2 * _r;
		y = _r + Math.random() * _h - 2 * _r;
		graphics.beginFill(_hsv2rgb(hue, 0.8, 0.5));
		graphics.drawCircle(0, 0, _r);
		blendMode = BlendMode.ADD;
	}
	
	public function update():void {
		x += _vx;
		y += _vy;
		if (x < _r) {
			x = _r;
			_vx *= -1;
		}
		if (x > _w - _r) {
			x = _w - _r;
			_vx *= -1;
		}
		if (y < _r) {
			y = _r;
			_vy *= -1;
		}
		if (y > _h - _r) {
			y = _h - _r;
			_vy *= -1;
		}
	}
	
	private function _hsv2rgb(h:uint/*[0,∞]*/, s:Number/*[0.0,1.0]*/, v:Number/*[0.0,1.0]*/):uint {
		if (s == 0) return v << 16 + v << 8 + v;
		var i:uint   = Math.floor(h % 360 / 60);
		var f:Number = h - i;
		var m:Number = v * (1 - s);
		var n:Number = v * (1 - s * f);
		var k:Number = v * (1 - s * (1 - f));
		var r:Number, g:Number, b:Number;
		switch(i) {
			case 0:
				r = v;
				g = k;
				b = m;
				break;
			case 1:
				r = n;
				g = v;
				b = m;
				break;
			case 2:
				r = m;
				g = v;
				b = k;
				break;
			case 3:
				r = m;
				g = n;
				b = v;
				break;
			case 4:
				r = k;
				g = m;
				b = v;
				break;
			case 5:
				r = v;
				g = m;
				b = n;
				break;
		}
		return Math.floor(r * 255) << 16 + Math.floor(g * 255) << 8 + Math.floor(b * 255);
	}
}