flash on 2012-8-19
/**
* Copyright leichtgewicht ( http://wonderfl.net/user/leichtgewicht )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/cdsx
*/
package {
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>;
public function FlashTest() {
const width: int = SIZE.width;
const height: int = SIZE.height;
_move = createRadientMovement(width, height, 0.1);
_moveData = _move.getVector(SIZE);
_out = new BitmapData(width, height, true, 0);
_outData = _out.getVector(SIZE);
_particles = createParticles();
addEventListener(Event.ENTER_FRAME, animate);
addChild(new Bitmap(_move));
addChild(new Bitmap(_out));
graphics.beginFill(0);
graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
}
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] = 0xFFFFFFFF;
}
}
_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 outLen: int = _outData.length;
for( var i: int = 0; i<size; ++i) {
var part: Particle = _particles[i];
var x: int = int(part.x);
var x1: Number = (part.x - x);
var x2: Number = 1-x1;
var y: int = int(part.y);
var y1: Number = (part.y - y);
var y2: Number = 1-y1;
var p: int;
var speedX: Number = 0.0;
var speedY: Number = 0.0;
var speed: Number = 0.0;
var c: uint, fx: Number, fy: Number;
const fs: Number = 40.0/4;
var fxy: Number;
fx = x2*y2;
fy = fx;
p = x+y*width;
if( p < outLen && p >= 0 ) {
c = _moveData[p];
speed += (((c & 0xFF0000) >> 16) - 128.0) / 255.0 * fs;
speedX += (((c & 0xFF00) >> 8) - 128.0) / 255.0 * fx;
speedY += (((c & 0xFF)) - 128.0) / 255.0 * fy;
}
++x;
fx = x1*y2;
fy = fx;
p = x+y*width;
if( p < outLen && p >= 0 ) {
c = _moveData[p];
speed += (((c & 0xFF0000) >> 16) - 128.0) / 255.0 * fs;
speedX += (((c & 0xFF00) >> 8) - 128.0) / 255.0 * fx;
speedY += (((c & 0xFF)) - 128.0) / 255.0 * fy;
}
++y;
fx = x1*y1;
fy = fx;
p = x+y*width;
if( p < outLen && p >= 0 ) {
c = _moveData[p];
speed += (((c & 0xFF0000) >> 16) - 128.0) / 255.0 * fs;
speedX += (((c & 0xFF00) >> 8) - 128.0) / 255.0 * fx;
speedY += (((c & 0xFF)) - 128.0) / 255.0 * fy;
}
--x;
fx = x2*y1;
fy = fx;
p = x+y*width;
if( p < outLen && p >= 0 ) {
c = _moveData[p];
speed += (((c & 0xFF0000) >> 16) - 128.0) / 255.0 * fs;
speedX += (((c & 0xFF00) >> 8) - 128.0) / 255.0 * fx;
speedY += (((c & 0xFF)) - 128.0) / 255.0 * fy;
}
part.x += speedX*speed;
part.y += speedY*speed;
}
}
private function animate(e:*): void {
calcParticles();
drawParticles();
}
private function createParticles(amount: int = 600): 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;
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 xSpeed: Number;
public var ySpeed: Number;
}
var trace: Function = Wonderfl.log;