forked from: Rainbow Circle
/**
* Copyright nondelion ( http://wonderfl.net/user/nondelion )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/b1D8
*/
// forked from nondelion's Rainbow Circle
package
{
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.BlurFilter;
import flash.geom.*;
import flash.utils.Dictionary;
import frocessing.color.ColorHSV;
import net.hires.debug.Stats;
[SWF(width=465, height=465, backgroundColor=0x000000, frameRate=30)]
public class RainbowCircle extends Sprite
{
protected static const PARTICLE_NUM:uint = 360;
private var canvas:BitmapData;
private var particles:Dictionary;
private var bf:BlurFilter = new BlurFilter(16,16,3);
private var ct:ColorTransform = new ColorTransform(1,1,1,1,-2,-1,-1,0);
private var _hsv:ColorHSV;
private var currentBlendMode:String = "add";
public function RainbowCircle()
{
_hsv = new ColorHSV(0, 1, 1, 1);
canvas = new BitmapData(465, 465, true, 0xff000000);
addChild( new Bitmap(canvas));
//this.addChild(new Stats());
particles = new Dictionary();
stage.addEventListener(MouseEvent.CLICK, clickHandler);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
addEventListener( Event.ENTER_FRAME, update);
}
private function setParticles():void
{
var o:Point = new Point(mouseX,mouseY);
var p:Particle;
for(var i:int=0; i<PARTICLE_NUM; i++){
p = new Particle();
p.color = 0xffffff;
p.x = o.x;
p.y = o.y;
var r:Number = 0;
var angle:Number = i%360+Math.random();
p.radian = angle2radian(angle);
p.vx = angle2x(angle,r);
p.vy = angle2y(angle,r);
particles[p] = true;
}
}
private function angle2x(a:Number, r:Number):Number {
return r * Math.cos(angle2radian(a));
}
private function angle2y(a:Number, r:Number):Number {
return r * Math.sin(angle2radian(a));
}
private function angle2radian(a:Number):Number {
return a * Math.PI / 180;
}
private function clickHandler(e:MouseEvent):void
{
if( currentBlendMode == "add" ) {
canvas.fillRect(canvas.rect, 0xff000000);
} else {
currentBlendMode = "add";
}
}
private function mouseMoveHandler(e:MouseEvent):void
{
setParticles();
}
private function update(e:Event):void
{
//setParticles();
canvas.lock();
canvas.applyFilter( canvas, canvas.rect, new Point(), bf);
canvas.colorTransform( canvas.rect, ct);
canvas.draw( canvas, null, null, currentBlendMode );
for(var key:* in particles){
var p:Particle = Particle(key);
_hsv.h = (_hsv.h+1) % 360;
canvas.setPixel32(p.x, p.y, _hsv.value + (0xff<<24));
p.x += p.vx;
p.y += p.vy;
p.vx = (Math.random()*30)*Math.cos(p.radian);
p.vy = (Math.random()*30)*Math.sin(p.radian);
if( p.x > 465 || p.y > 465 || p.x < 0 || p.y < 0){
delete particles[p];
}
}
canvas.unlock();
}
}
}
class Particle
{
public var color:uint;
public var x:Number;
public var y:Number;
public var vx:Number;
public var vy:Number;
public var radian:Number;
public function Particle()
{
}
}