Snippets and notes about actionscript.

Particles Hidden Picture

Posted: December 16th, 2008 | Author: | Filed under: Flash | Tags: , , | No Comments »

An experiment using particles. Loads one picture (Naota), then loads another picture (the laughing man) over it and turns each pixel into a particle. As the mouse hovers over the picture, each pixel of the laughing man picture is pushed away from where the mouse is, revealing the picture of Naota underneath.



Main.as

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package  {
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.*;
	import flash.geom.Point;
 
	public class Main extends Sprite {
 
		[Embed(source = "laughing_man.png")]
		private var Laughing_Man:Class;
		[Embed(source = "naota_ears.jpg")]
		private var Naota_Ears:Class;
 
		private var buffer:BitmapData;
		private var bmd:BitmapData;
		private var arr:Array;
		private var aPoint:Point;
 
		private var angle:Number;
		private var xVel:Number;
		private var yVel:Number;
		private var accel:int;
 
		private var circ:Sprite;
		private var pic:Bitmap;
		private var picBuffer:BitmapData;
		private var hiddenPic:Bitmap;
 
		public function Main() {
 
			circ = new Sprite();
			circ.graphics.drawCircle(mouseX, mouseY, 35);
			circ.startDrag();
 
			picBuffer = new BitmapData(stage.stageWidth, stage.stageHeight);
			pic = new Bitmap(picBuffer);
			picBuffer.draw(new Laughing_Man);
			hiddenPic = new Naota_Ears;
 
			accel = 3;
 
			aPoint = new Point(0, 0);
			bmd = new BitmapData( stage.stageWidth, stage.stageHeight, false, 0x000000);
			buffer = new BitmapData( stage.stageWidth, stage.stageHeight, false, 0x000000);
			arr = new Array();
 
			for ( var j:int = 0; j < pic.width; j++ )
			{
				for ( var k:int = 0; k < pic.height; k++ )
				{
					arr.push( new Particle( j, k, picBuffer.getPixel(j, k) ) );
				}
			}
 
			addChild( new Bitmap(buffer) );
 
			addEventListener(Event.ENTER_FRAME, render);
		}
 
		public function render( e:Event ):void
		{
			bmd.draw(hiddenPic);
 
			for ( var j:int = 0; j < arr.length; ++j )
			{
				var p:Particle = arr[j] as Particle;
				bmd.setPixel32( p.x, p.y, p.rgb );
 
				// Move away from mouse
				if ( circ.hitTestPoint(p.x,p.y) )//p.x<(mouseX+30) && p.x>(mouseX-30) && p.y<(mouseY+30) && p.y>(mouseY-30) )
				{
					angle = Math.atan2( p.y - mouseY, p.x - mouseX );
					xVel = Math.cos(angle) * p.accel;
					yVel = Math.sin(angle) * p.accel;
					p.offset(xVel, yVel);
				}
				else if( p.x<(p.origX-2) || p.x>(p.origX+2) || p.y<(p.origY-2) || p.y>(p.origY+2) ) // Move back to point of origin
				{
					angle = Math.atan2( p.origY - p.y, p.origX - p.x );
					xVel = Math.cos(angle) * p.accel;
					yVel = Math.sin(angle) * p.accel;
					p.offset(xVel, yVel);
				}
				else
				{
					p.x = p.origX;
					p.y = p.origY;
				}
 
 
			}
 
			buffer.copyPixels(bmd, bmd.rect, aPoint);
		}
	}
 
}

Particles.as

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
package  {
 
	public class Particle {
 
		public var x:Number;
		public var y:Number;
		public var origX:Number;
		public var origY:Number;
		public var rgb:uint;
		public var accel:int;
 
		public function Particle( newX:int, newY:int, color:uint ) {
			x = origX = newX;
			y = origY = newY;
			rgb = color;
			accel = 1 + Math.random() * 3;
		}
 
		public function offset( offsetX:Number, offsetY:Number ):void
		{
			x += offsetX;
			y += offsetY;
		}
 
	}
 
}


Leave a Reply