summaryrefslogtreecommitdiff
path: root/webcam/com/neave/webcam/effects/delay/TrailEffect.as
blob: 725b0acc0a454e7ba49ccee4320541a6ccb48fa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
 * Neave Webcam // Trail Effect
 * 
 * Copyright (C) 2008 Paul Neave
 * http://www.neave.com/
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation at http://www.gnu.org/licenses/gpl.html
 */

package com.neave.webcam.effects.delay
{
	import flash.display.*;
	import com.neave.webcam.effects.*;
	
	public class TrailEffect extends AbstractEffect
	{
		private var fade:uint;
		private var whiteBitmap:BitmapData;
		
		/**
		 * Creates a trail effect by smearing and fading out movement
		 * 
		 * @param	source			The source object to use for the effect
		 * @param	targetBitmap	The target bitmap data to draw the resulting effect into
		 * @param	fade			The amount to fade out the trail by each frame, the higher the faster
		 */
		public function TrailEffect(source:IBitmapDrawable, targetBitmap:BitmapData, fade:uint = 8)
		{
			super(source, targetBitmap, "Trail");
			this.fade = fade > 0xFF ? 0xFF: fade;
			
			createTrail();
		}
		
		/**
		 * Sets up the trail effect
		 */
		private function createTrail():void
		{
			whiteBitmap = new BitmapData(rect.width, rect.height, false, 0xFFFFFFFF); // White bitmap for fading out the trail
			targetBitmap.draw(source, null, color, null, null, true); // Start with the current source image
		}
		
		/**
		 * Draws the trail effect
		 */
		override public function draw():void
		{
			super.draw();
			targetBitmap.lock();
			targetBitmap.merge(whiteBitmap, rect, point, fade, fade, fade, 0); // Repeatedly fade out the image
			targetBitmap.draw(sourceBitmap, null, null, BlendMode.DARKEN); // Blend the new source image with the old one
			targetBitmap.unlock();
		}
		
		/**
		 * Removes the divide effect and all other referenced objects
		 */
		override public function destroy():void
		{
			super.destroy();
			whiteBitmap.dispose();
			whiteBitmap = null;
		}
	}
}