Xmas 2009
package {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.filters.BlurFilter;
import flash.text.*;
[SWF(backgroundColor="#000000",frameRate = 120,width="465",height="465")]
public class Xmas2009 extends Sprite {
private var px:Vector.<Shape> = new Vector.<Shape>();
private var container:Sprite = new Sprite();
private var bmd:BitmapData;
private var rect:Rectangle;
private var filter:BlurFilter;
private var bm:Bitmap;
private var _txbm:Bitmap;
public function Xmas2009():void {
var comment:TextField = new TextField();
comment.text = "マウスクリックで粒子散布します。";
comment.autoSize = TextFieldAutoSize.LEFT;
comment.textColor = 0xCCCCCC;
addChild(comment);
for (var i:uint = 0; i<2000; i++) {
var sp:Shape = new Shape();
sp.graphics.beginFill(rand(0xFFFFFF));
sp.graphics.drawCircle(0,0,1);
sp.x=rand(stage.stageWidth);
sp.y=rand(stage.stageHeight);
container.addChild(sp);
px.push(sp);
}
bmd=new BitmapData(stage.stageWidth,stage.stageHeight,true,0x000000);
rect=new Rectangle(0,0,bmd.width,bmd.height);
filter=new BlurFilter(2,2);
bm=new Bitmap(bmd);
addChild(bm);
_txbm = messageText();
this.addChild(_txbm);
stage.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler);
this.addEventListener(Event.ENTER_FRAME , loop);
}
private function loop(e:Event):void {
var txbmd:BitmapData = _txbm.bitmapData;
for (var i:uint = 0; i<px.length; i++) {
if(txbmd.hitTest(new Point(_txbm.x,_txbm.y),0x00,new Point(px[i].x,px[i].y))) {
px[i].x+=rand(1)-1;
px[i].y+=rand(1)-1;
}else{
px[i].x+=(mouseX-px[i].x)*(i*0.001/4);
px[i].y+=(mouseY-px[i].y)*(i*0.001/4);
}
}
bitmapDraw();
}
private function clickHandler(e:MouseEvent):void {
for (var t:uint = 0; t<px.length; t++) {
px[t].x=rand(stage.stageWidth);
px[t].y=rand(stage.stageHeight);
}
bitmapDraw();
}
private function bitmapDraw():void {
bmd.draw(container);
bmd.scroll(rand(1),rand(1));
bmd.applyFilter(bmd,rect,new Point(0,0),filter);
}
private function rand(num:Number):Number {
return Math.random()*num;
}
private function messageText():Bitmap {
var txtBlur:BlurFilter = new BlurFilter(2,2,4);
var txFormat:TextFormat = new TextFormat(null,36,0x000000,true);
var txt:TextField = new TextField();
txt.filters = [txtBlur];
txt.autoSize = TextFieldAutoSize.LEFT;
txt.defaultTextFormat = txFormat;
txt.text = "2009のX'masは独りきり…\nごめん、大好きなひと\n全部オレが悪いんだ。";
txt.selectable = false;
txt.x=stage.stageWidth/2-txt.width/2;
txt.y=stage.stageHeight/2-txt.height/2;
var bmd:BitmapData = new BitmapData(txt.width,txt.height,true,0x000000);
bmd.draw(txt);
var txbm:Bitmap = new Bitmap(bmd);
txbm.x=stage.stageWidth/2-txbm.width/2;
txbm.y=stage.stageHeight/2-txbm.height/2;
return txbm;
}
}
}