forked from: flash on 2012-8-19
forked from flash on 2012-8-19 (diff: 113)
ActionScript3 source code
/**
* Copyright leichtgewicht ( http://wonderfl.net/user/leichtgewicht )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/c7GV
*/
// forked from leichtgewicht's flash on 2012-8-19
package {
import flash.display.LoaderInfo;
import flash.display.Loader;
import flash.events.MouseEvent;
import flash.utils.setTimeout;
import flash.net.FileReference;
import flash.events.Event;
import flash.display.Bitmap;
import flash.geom.Rectangle;
import flash.display.BitmapData;
import flash.display.Sprite;
public class FlashTest extends Sprite {
private const SIZE: Rectangle = new Rectangle(0, 0, 400, 400);
private var _move: BitmapData;
private var _moveData: Vector.<uint>;
private var _out: BitmapData;
private var _outData: Vector.<uint>;
private var _particles: Vector.<Particle>;
private var _ref: FileReference = new FileReference();
public function FlashTest() {
const width: int = SIZE.width;
const height: int = SIZE.height;
_out = new BitmapData(width, height, true, 0);
_outData = _out.getVector(SIZE);
_particles = createParticles();
addChild(new Bitmap(_move));
addChild(new Bitmap(_out));
graphics.beginFill(0);
graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
_ref.addEventListener(Event.COMPLETE, handleCustomFile);
_ref.addEventListener(Event.SELECT, loadCustomFile);
stage.addEventListener(MouseEvent.CLICK, function(e:Event): void { _ref.browse() });
start(createRadientMovement(SIZE.width, SIZE.height, 0.1));
addEventListener(Event.ENTER_FRAME, animate);
}
private function loadCustomFile(e: Event): void {
_ref.load();
}
private function handleCustomFile(e: Event): void {
var l: Loader = new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE, customFileLoaded);
l.loadBytes(_ref.data);
}
private function customFileLoaded(e: Event): void {
var l: Loader = (e.target as LoaderInfo).loader;
trace("Custom File Loaded");
start((l.getChildAt(0) as Bitmap).bitmapData);
}
private function handleDefaultFile(e:Event): void {
}
private function start(bmp: BitmapData): void {
_move = bmp;
_moveData = _move.getVector(SIZE);
}
private function drawParticles(): void {
const size:int = _particles.length;
const width:int = SIZE.width;
const outLen: int = _outData.length;
clear();
for( var i: int = 0; i<size; ++i) {
var part: Particle = _particles[i];
var p: int = int(part.x+0.5)+int(part.y+0.5)*width;
if( p < outLen && p >= 0 ) {
_outData[p] = _outData[p] | part.color;
}
}
_out.setVector(SIZE, _outData);
}
private function clear(): void {
const size: int = _outData.length;
for( var i: int = 0; i<size; ++i) {
_outData[i] = 0;
}
}
private function calcParticles(): void {
const size:int = _particles.length;
const width:int = SIZE.width;
const height:int = SIZE.height;
const outLen: int = _outData.length;
const fs: Number = 40.0;
for( var i: int = 0; i<size; ++i) {
var part: Particle = _particles[i];
var x: int = int(part.x+.5);
var y: int = int(part.y+.5);
var p: int;
var speedX: Number = 0.0;
var speedY: Number = 0.0;
var speed: Number = 0.0;
if( x >= width ) x = width-1;
if( y >= height ) y = height-1;
if( x < 0.0 ) x = 0.0;
if( y < 0.0 ) y = 0.0;
p = x+y*width;
var c: uint = _moveData[p];
speed = (((c & 0xFF0000) >> 16) - 128.0) / 255.0 * fs;
speedX = (((c & 0xFF00) >> 8) - 128.0) / 255.0;
speedY = (((c & 0xFF)) - 128.0) / 255.0 ;
part.x += speedX*speed;
part.y += speedY*speed;
}
}
private function animate(e:*): void {
calcParticles();
drawParticles();
}
private function createParticles(amount: int = 2000): Vector.<Particle> {
const result: Vector.<Particle> = new Vector.<Particle>(amount);
for(var i: int= 0; i<amount;++i) {
result[i] = createParticle();
}
return result;
}
private function createParticle(): Particle {
const particle: Particle = new Particle;
particle.x = 100+Math.random()*200;
particle.y = 100+Math.random()*200;
particle.color = (((Math.random() * 0xFF) & 0xFF) << 24) | 0xFFFFFF;
return particle;
}
public function createRadientMovement(width: int, height: int, strength: Number): BitmapData {
var bmp: BitmapData = new BitmapData(width, height, false, 0);
var rect: Rectangle = new Rectangle(0, 0, width, height);
const v: Vector.<uint> = bmp.getVector(rect);
const centerX: int = 200;
const centerY: int = 200;
const s: int = int((strength*0xff)+0.5) << 16;
var i: int = 0;
for( var x: int = 0; x<width; x++) {
var xd: int = x - centerX;
for( var y: int = 0;y<height; y++) {
var yd: int = y - centerY;
var angle: Number = Math.atan2(xd, yd);
var speedX: Number = -Math.sin(angle);;
var speedY: Number = Math.cos(angle);
v[i] = s | int(((speedX+1.0)*0x7f)+.5) << 8 | int(((speedY+1.0)*0x7f)+.5);
++i;
}
}
bmp.setVector(rect, v);
return bmp;
}
}
}
class Particle {
public var x: Number;
public var y: Number;
public var color: uint;
public var xSpeed: Number;
public var ySpeed: Number;
}
var trace: Function = Wonderfl.log;