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

Webcam Ghosts

code by rigardkruger (http://www.rigardkruger.com)
need a webcam
// code by rigardkruger (http://www.rigardkruger.com)
// need a webcam
package 
{
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.filters.BlurFilter;
	import flash.filters.GlowFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.utils.Timer;
	
	[SWF(backgroundColor = 0x000000, frameRate = 30)]
	
	/**
	 * ...
	 * @author Rigard Kruger
	 */
	public class Webcam extends Sprite 
	{
		
		private var _width:int;
		private var _height:int;
		
		private var _video:Video;
		private var _output:Bitmap;
		private var _olddata:BitmapData;
		private var _data:BitmapData;
		private var _rect:Rectangle;
		
		private var _pixelarray:Array;
		
		public function Webcam()
		{
			
			var camera:Camera = Camera.getCamera();
			if (camera != null)
			{
				_video = new Video(160, 120);
				_video.attachCamera(camera);
				
			}
			else
			{
				// no good
			}
			
			_width = _video.width;
			_height = _video.height;
			_rect = new Rectangle(0, 0, _width, _height);
			
			_data = new BitmapData(_width, _height, true, 0x00000000);
			_olddata = new BitmapData(_width, _height, true, 0x00000000);
			
			_pixelarray = [];
			
			_output = new Bitmap(_data);
			_output.smoothing = true;
			addChild(_output);
			
			_output.width = stage.stageWidth;
			_output.height = stage.stageHeight;
			
			var timer:Timer = new Timer(30);
			timer.addEventListener(TimerEvent.TIMER, onTimer);
			timer.start();
		}
		
		private function onTimer(e:TimerEvent) : void
		{
			
			var bd:BitmapData = new BitmapData(_width, _height, true, 0xffffffff);
			bd.draw(_video);
			bd.draw(_olddata, null, null, BlendMode.DIFFERENCE);
			
			bd.threshold(bd, _rect, new Point(), ">", 0xff222222, 0xff00ff00, 0x00ffffff, false);
			bd.threshold(bd, _rect, new Point(), "!=", 0xff00ff00, 0xff000000, 0x00ffffff, false);
			
			_data.lock();
			
			for (var i:int = 0; i < _width; i++)
			{
				for (var j:int = 0; j < _height; j++)
				{
					
					if (bd.getPixel32(i, j) == 0xff00ff00)
					{
						_pixelarray.push(new Pixel(i, j, 0xffffffff));
					}
					
				}
			}
			
			_pixelarray = _pixelarray.slice(0, 5000);
			var numPixels:int = _pixelarray.length;
			for (i = 0; i < numPixels; i++)
			{
				var p:Pixel = _pixelarray[i];
				p.x += Math.random() * 3 - 1;
				p.y += Math.random() * 3 - 1;
				_data.setPixel32(p.x, p.y, p.c);
				p.l++;
				
				if (p.l > 10)
				{
					_pixelarray.splice(i--, 1);
					numPixels--;
				}
				
			}
			
			var ct:ColorTransform = new ColorTransform();
			ct.alphaMultiplier = 0.96;
			_data.colorTransform(_rect, ct);
			
			_data.applyFilter(_data, _rect, new Point(), new GlowFilter(0x33ff33, 1, 6, 6, 0.1));
			_data.applyFilter(_data, _rect, new Point(), new BlurFilter(2, 2));
			
			_data.unlock();
			
			_output.width = stage.stageWidth;
			
			_olddata.draw(_video);
		}
		
	}
	
}

class Pixel
{
	public var x:int;
	public var y:int;
	public var c:uint;
	public var l:int;
	
	public function Pixel(x:int, y:int, c:uint, l:int = 0)
	{
		this.x = x;
		this.y = y;
		this.c = c;
		this.l = l;
	}
}