forked from: test[朝わん]
朝ワンのネタがないからちょっとずつ高速化を目指す。
* まず、bitmapData を lock した。
// forked from bkzen's test
/**
* 朝ワンのネタがないからちょっとずつ高速化を目指す。
* まず、bitmapData を lock した。
*/
// write as3 code here..
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.BlurFilter;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.text.TextField;
import net.hires.debug.Stats
[SWF(width="465", height="465", backgroundColor="0x000000", frameRate="30")]
public class Test extends Sprite
{
public static var color: uint = 0xFFFFFF;
private const num: int = 500;
private const rect: Rectangle = new Rectangle(0, 0, 465, 465);
private const bf: BlurFilter = new BlurFilter(1.5, 1.5);
private const bfPoint: Point = new Point();
private var particles: Array = [];
private var bmp: Bitmap;
private var sp: Sprite;
public function Test()
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e: Event = null): void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
//
bmp = new Bitmap(new BitmapData(rect.width, rect.height, false, 0x00000000));
addChild(bmp);
sp = new Sprite();
var tf: TextField = new TextField();
tf.textColor = color;
tf.text = "click start!";
tf.width = 55;
tf.height = 20;
bmp.bitmapData.draw(tf, new Matrix(1, 0, 0, 1, (rect.width / 2) - (tf.width / 2), (rect.height / 2) - (tf.width / 2)));
for (var i: int = 0; i < num; i++)
{
var r: Number = Math.PI * i / num;
particles.push(new Particle(r, 232.5, bmp.bitmapData, sp));
}
stage.addEventListener(MouseEvent.CLICK, click);
addChild(new Stats());
}
private function draw(e: Event): void
{
bmp.bitmapData.lock();
for (var i: int = 0; i < num; i++)
{
var p: Particle = particles[i];
p.move();
}
bmp.bitmapData.applyFilter(bmp.bitmapData, rect, new Point(), bf);
bmp.bitmapData.unlock();
color = (Math.random() * 10000 > 9950) ? randomColor() : color;
}
private function click(e: MouseEvent): void
{
stage.removeEventListener(MouseEvent.CLICK, click);
addEventListener(Event.ENTER_FRAME, draw);
stage.addEventListener(MouseEvent.CLICK, changeColor);
}
private function changeColor(e: MouseEvent): void
{
color = randomColor();
}
private function randomColor(): uint
{
var r: int = Math.random() * 128 + 128;
var g: int = Math.random() * 128 + 128;
var b: int = Math.random() * 128 + 128;
return (r << 16) | (g << 8) | b;
}
}
}
import flash.display.Graphics;
import flash.geom.Point;
import flash.display.BitmapData;
import flash.display.Sprite;
class Particle
{
private var o: Point; // ox, oy
private var p: Point; // x, y
private var b: Point; // xx, yy
private var v: Point; //
private var age: int = (Math.random() * 200);
private var color: uint;
private var canvas: BitmapData;
private var sp: Sprite;
public function Particle(r: Number, o: int, canvas: BitmapData, sp: Sprite)
{
this.o = new Point(o, o);
p = new Point(30 * Math.sin(r), 30 * Math.cos(r));
b = new Point();
v = new Point(2 * Math.cos(r), 2 * Math.sin(r));
color = Test.color;
this.canvas = canvas;
this.sp = sp;
}
public function move(): void
{
b.x = p.x;
b.y = p.y;
p = p.add(v);
v.x += ((Math.random() * 100) - (Math.random() * 100)) * 0.005;
v.y += ((Math.random() * 100) - (Math.random() * 100)) * 0.005;
draw();
age++;
if (age > 200)
{
var t: Number = Math.random() * Math.PI * 2;
p.x = 30 * Math.sin(t);
p.y = 30 * Math.cos(t);
b.x = 0;
b.y = 0;
v.x = 0;
v.y = 0;
color = Test.color;
age = 0;
}
}
private function draw(): void
{
var g: Graphics = sp.graphics;
g.clear();
g.lineStyle(1, color, 0.3);
g.moveTo(o.x + b.x, o.y + b.y);
g.lineTo(o.x + p.x, o.y + p.y);
g.moveTo(o.x - b.x, o.x + b.y);
g.lineTo(o.x - p.x, o.x + p.y);
g.moveTo(o.x + b.x, o.y - b.y);
g.lineTo(o.x + p.x, o.y - p.y);
g.moveTo(o.x - b.x, o.x - b.y);
g.lineTo(o.x - p.x, o.x - p.y);
canvas.draw(sp);
}
}