Make Cloud
/**
* Copyright greentec ( http://wonderfl.net/user/greentec )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ch53
*/
// forked from greentec's Make Wind Grid
package {
import com.bit101.components.Label;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.PixelSnapping;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.BlurFilter;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
public class FlashTest extends Sprite {
public var gridSize:int = 15;
public var gridWidth:int = 465 / gridSize;
public var windScaleMax:Number = 1;
public var windGrid:Vector.<Vector.<Number>> = new Vector.<Vector.<Number>>(gridSize);
public var windScale:Vector.<Vector.<Number>> = new Vector.<Vector.<Number>>(gridSize);
public var tempWindGrid:Vector.<Vector.<Number>> = new Vector.<Vector.<Number>>(gridSize);
public var tempWindScale:Vector.<Vector.<Number>> = new Vector.<Vector.<Number>>(gridSize);
public var windBitmapData:BitmapData;
public var windColorTransform:ColorTransform;
public var countMax:int = 10;
public var count:int = 0;
public var label:Label;
public var particleNum:int = 30000;
public var particleArray:Array;
public var particleShape:Shape;
public var particleLifeTime:int = 200;
public var indent:int = 30;
public var particleLifeTimeCount:int = 0;
public var particleLifeTimeMax:int = particleLifeTime;
public function FlashTest() {
// write as3 code here..
Wonderfl.disable_capture();
var back:BitmapData = new BitmapData(465, 465, false, 0x292929);
addChild(new Bitmap(back));
windBitmapData = new BitmapData(465, 465, true, 0xff3333b2);
addChild(new Bitmap(windBitmapData));
windColorTransform = new ColorTransform(.8, .8, .9, 1.0);
label = new Label(this, 370, 0, "");
label.transform.colorTransform = new ColorTransform(0, 0, 0, 1, 255, 255, 255, 0);
particleShape = new Shape();
particleShape.graphics.beginFill(0xffffff, 0.1);
particleShape.graphics.drawRect(0, 0, 1, 1);
particleShape.graphics.endFill();
initWind();
interpolateWind();
initParticle();
addEventListener(Event.ENTER_FRAME, onLoop);
stage.addEventListener(MouseEvent.CLICK, onReset);
}
private function initParticle():void
{
var i:int;
var particle:Particle;
particleArray = [];
for (i = 0; i < particleNum; i += 1)
{
particle = new Particle(Math.random() * (465 - indent) + indent / 2, Math.random() * (465 - indent) + indent / 2, particleLifeTime);
particleArray.push(particle);
}
}
private function resetParticle():void
{
var i:int;
var particle:Particle;
for (i = 0; i < particleNum; i += 1)
{
particle = particleArray[i];
particle._x = Math.random() * (465 - indent) + indent / 2;
particle._y = Math.random() * (465 - indent) + indent / 2;
particle._vx = 0;
particle._vy = 0;
particle.lifeTime = particleLifeTime;
}
}
private function moveParticle():void
{
var i:int;
var particle:Particle;
var len:int = particleArray.length;
var angle:Number;
var scale:Number;
for (i = 0; i < len; i += 1)
{
particle = particleArray[i];
if (particle.lifeTime <= 0 ||
particle._x < indent || particle._x + indent > 465 ||
particle._y < indent || particle._y + indent > 465)
{
particle.lifeTime = 0;
//particle._x = Math.random() * 300 + 165 / 2;
//particle._y = Math.random() * 300 + 165 / 2;
//particle.lifeTime = particleLifeTime;
continue;
}
angle = windGrid[int(particle._x / gridWidth)][int(particle._y / gridWidth)];
scale = windScale[int(particle._x / gridWidth)][int(particle._y / gridWidth)];
particle._vx += Math.cos(angle) * scale;
particle._vy += Math.sin(angle) * scale;
particle._vx *= 0.88;
particle._vy *= 0.88;
particle._x += particle._vx;
particle._y += particle._vy;
particle.lifeTime -= 1;
}
}
private function drawParticle():void
{
var i:int;
var particle:Particle;
var len:int = particleArray.length;
windBitmapData.lock();
//if (particleLifeTimeCount % 40 == 0 && particleLifeTimeCount < particleLifeTimeMax / 2)
//{
//windBitmapData.applyFilter(windBitmapData, windBitmapData.rect, new Point(), new BlurFilter(2, 2));
//}
for (i = 0; i < len; i += 1)
{
particle = particleArray[i];
if (particle.lifeTime <= 0)
{
continue;
}
particle._x += Math.random() * 2 - 1;
particle._y += Math.random() * 2 - 1;
windBitmapData.draw(particleShape,
new Matrix(1, 0, 0, 1, int(particle._x), int(particle._y)),
null,
BlendMode.OVERLAY);
}
windBitmapData.unlock();
}
private function onReset(e:Event):void
{
count = 0;
countMax = Math.random() * 20 + 5;
resetWind();
interpolateWind();
particleLifeTimeCount = 0;
resetParticle();
windBitmapData.fillRect(windBitmapData.rect, 0xff3333b2);
}
private function onLoop(e:Event):void
{
if (particleLifeTimeCount < particleLifeTimeMax)
{
moveParticle();
drawParticle();
particleLifeTimeCount += 1;
label.text = "LifeTime:\t" + String(particleLifeTimeCount) + "/" + String(particleLifeTimeMax);
}
else if (particleLifeTimeCount == particleLifeTimeMax)
{
particleLifeTimeCount += 1;
windBitmapData.applyFilter(windBitmapData, windBitmapData.rect, new Point(), new BlurFilter(3, 3));
}
}
private function drawWind():void
{
var shape:Shape = new Shape();
windBitmapData.lock();
windBitmapData.applyFilter(windBitmapData, windBitmapData.rect, new Point(), new BlurFilter(1, 1));
windBitmapData.colorTransform(windBitmapData.rect, windColorTransform);
var i:int;
var j:int;
var gridWidth:int = 465 / gridSize;
var _x:Number;
var _y:Number;
shape.graphics.clear();
shape.graphics.lineStyle(0, 0xffffff);
for (i = 0; i < gridSize; i += 1)
{
for (j = 0; j < gridSize; j += 1)
{
_x = i * gridWidth + gridWidth / 2;
_y = j * gridWidth + gridWidth / 2;
shape.graphics.drawCircle(_x, _y, 2);
shape.graphics.moveTo(_x, _y);
shape.graphics.lineTo(_x + Math.cos(windGrid[i][j]) * windScale[i][j] * gridWidth / 2, _y + Math.sin(windGrid[i][j]) * windScale[i][j] * gridWidth / 2);
}
}
windBitmapData.draw(shape);
windBitmapData.unlock();
}
private function interpolateWind():void
{
var i:int;
var j:int;
//interpolate
var k:int;
for (k = 0; k < countMax; k += 1)
{
var count:int;
for (i = 0; i < gridSize; i += 1)
{
for (j = 0; j < gridSize; j += 1)
{
tempWindGrid[i][j] = windGrid[i][j];
tempWindScale[i][j] = windScale[i][j];
count = 1;
if (i > 0)
{
tempWindGrid[i][j] += windGrid[i - 1][j];
tempWindScale[i][j] += windScale[i - 1][j];
count += 1;
}
if (i < gridSize - 1)
{
tempWindGrid[i][j] += windGrid[i + 1][j];
tempWindScale[i][j] += windScale[i + 1][j];
count += 1;
}
if (j > 0)
{
tempWindGrid[i][j] += windGrid[i][j - 1];
tempWindScale[i][j] += windScale[i][j - 1];
count += 1;
}
if (j < gridSize - 1)
{
tempWindGrid[i][j] += windGrid[i][j + 1];
tempWindScale[i][j] += windScale[i][j + 1];
count += 1;
}
tempWindGrid[i][j] /= count;
tempWindScale[i][j] /= count;
}
}
}
for (i = 0; i < gridSize; i += 1)
{
for (j = 0; j < gridSize; j += 1)
{
windGrid[i][j] = tempWindGrid[i][j];
windScale[i][j] = tempWindScale[i][j];
}
}
}
private function initWind():void
{
var i:int;
var j:int;
for (i = 0; i < gridSize; i += 1)
{
windGrid[i] = new Vector.<Number>(gridSize);
windScale[i] = new Vector.<Number>(gridSize);
tempWindGrid[i] = new Vector.<Number>(gridSize);
tempWindScale[i] = new Vector.<Number>(gridSize);
for (j = 0; j < gridSize; j += 1)
{
windGrid[i][j] = Math.random() * Math.PI * 2;
windScale[i][j] = Math.random() * windScaleMax * 3 / 4 + windScaleMax / 4;
}
}
}
private function resetWind():void
{
var i:int;
var j:int;
for (i = 0; i < gridSize; i += 1)
{
for (j = 0; j < gridSize; j += 1)
{
windGrid[i][j] = Math.random() * Math.PI * 2;
windScale[i][j] = Math.random() * windScaleMax * 3 / 4 + windScaleMax / 4;
}
}
}
}
}
Class
{
/**
* ...
* @author ypc
*/
class Particle
{
public var _x:Number;
public var _y:Number;
public var _vx:Number = 0;
public var _vy:Number = 0;
public var lifeTime:int;
public function Particle(_x:Number, _y:Number, lifeTimeMax:int)
{
this._x = _x;
this._y = _y;
this.lifeTime = lifeTimeMax;
}
}
}