forked from: [1日1Wonderfl]9日目:メッシュ的なもの
// forked from yd_niku's [1日1Wonderfl]9日目:メッシュ的なもの
package {
import flash.display.*;
import flash.events.*;
import flash.geom.ColorTransform;
public class FlashTest extends Sprite {
private const W_LENGTH:int = 200;
private const H_LENGTH:int = 200;
private const offsetX:Number = (stage.stageWidth - W_LENGTH) / W_LENGTH;
private const offsetY:Number = (stage.stageHeight - H_LENGTH) / H_LENGTH;
private var particles:Vector.<Particle> = new Vector.<Particle>(W_LENGTH * H_LENGTH, true);
private var canvas:BitmapData;
private const colorTrans:ColorTransform = new ColorTransform(1,1,1,1,55,50,60);
public function FlashTest()
{
init();
createObject();
addEventListener(Event.ENTER_FRAME,loopHandler);
}
private function init():void {
canvas = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0xff000000);
addChild(new Bitmap(canvas));
}
private function createObject():void {
var k:int = 0;
for (var i:int = 0; i < W_LENGTH; i++) {
for (var j:int = 0; j < H_LENGTH; j++) {
var p:Particle = new Particle(offsetX * i + i, offsetY * j + j);
particles[k++] = p;
}
}
}
private function loopHandler(e:Event):void {
canvas.lock();
canvas.colorTransform(canvas.rect, colorTrans);
for each(var p:Particle in particles) {
var dx:Number = mouseX - p.x;
var dy:Number = mouseY - p.y;
var dis:Number = Math.sqrt(dx * dx + dy * dy);
var idx:Number = dx / dis * 100 / dis;
var idy:Number = dy / dis * 100 / dis;
p.vx += idx;
p.vy += idy;
p.vx *= 0.95;
p.vy *= 0.95;
p.x += (p.ax - (p.x + p.vx)) * 0.01;
p.y += (p.ay - (p.y + p.vy)) * 0.01;
canvas.setPixel32(p.x,p.y,0xccff0000);
}
canvas.unlock();
}
}
}
internal class Particle {
public var x:Number = 0;
public var y:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
public var ax:Number = 0;
public var ay:Number = 0;
public function Particle( x:Number, y:Number ){
this.x = this.ax = x;
this.y = this.ay = y;
}
}