[1日1Wonderfl]1日目:TextParticle プリレンダリング演習
1日1Wonderfl 1/30日目
テキストをプリレンダリングしてみるテスト
書き込むときにMatrixとか使わない高速な方法があるんだろうか?
package {
// 1日1Wonderfl 1/30日目
// テキストをプリレンダリングしてみるテスト
// 書き込むときにMatrixとか使わない高速な方法があるんだろうか?
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
import flash.utils.*;
import com.flashdynamix.utils.SWFProfiler;
public class FlashTest extends Sprite {
private var _canvas:BitmapData;
private var _textfield:TextField;
private var _preRenderMap:Vector.<BitmapData> = new Vector.<BitmapData>();
private var _particles:Vector.<Particle>= new Vector.<Particle>();
private var _cleanupTimer:Timer = new Timer( 1000 );
private var _statusField:TextField;
public function FlashTest() {
_canvas = new BitmapData( stage.stageWidth, stage.stageHeight, true, 0xFFFFFFFF );
addChild( new Bitmap(_canvas) );
createPreRenderMap();
stage.addEventListener( MouseEvent.CLICK, onClick );
addEventListener( Event.ENTER_FRAME, enterFrame );
_cleanupTimer.addEventListener( TimerEvent.TIMER, onCleanUp );
_cleanupTimer.start();
_statusField = new TextField();
addChild( _statusField );
_statusField.x = 200;
_statusField.y = 200;
_statusField.width= stage.stageWidth;
_statusField.selectable = false;
_statusField.mouseEnabled= false;
_statusField.defaultTextFormat = new TextFormat( "_sans", 12, 0xFF0000 );
_statusField.text = "Please Click!";
SWFProfiler.init( this );
}
public function onCleanUp( e:TimerEvent ):void {
var l:int = _particles.length;
var p:Particle
while( l-->0 ) {
p = _particles[l];
if( p.life <= 0 ) {
_particles.splice( l, 1 );
p.bitmapData = null;
}
}
_statusField.text= String(_particles.length) + " particles";
}
public function enterFrame( e:Event ):void {
_canvas.lock();
_canvas.fillRect( _canvas.rect, 0xFFFFFFFF );
for each( var p:Particle in _particles ) {
if( p.life < 0 ) continue;
p.x = p.x + ( p.vx = p.vx * 0.98 );
p.y = p.y + ( p.vy = p.vy * 0.98 );
p.rot = p.rot + ( p.vrot = p.vrot * 0.98 );
var mat:Matrix = new Matrix();
mat.rotate( p.rot/180*Math.PI );
mat.translate( p.x, p.y );
_canvas.draw( p.bitmapData, mat );
p.life--;
}
_canvas.unlock();
}
public function onClick ( e:MouseEvent ):void {
var clickX:Number = e.localX;
var clickY:Number = e.localY;
var vLength:Number= 10;
// trace(clickX,clickY);
for( var i:int =0; i< 100; ++i ) {
var char:BitmapData = _preRenderMap[ Math.floor( _preRenderMap.length * Math.random() ) ];
var angle:Number = Math.random()*Math.PI*2;
var p:Particle = new Particle( char, clickX+Math.random()*100-50, clickY+Math.random()*100-50, angle/Math.PI*180,
Math.cos(angle)*vLength, Math.sin(angle)*vLength, Math.random()*30-15);
_particles.push( p );
}
}
public function createPreRenderMap():void{
var i:int = 0;
var textfield:TextField = new TextField();
textfield.autoSize =TextFieldAutoSize.LEFT;
textfield.height =20;
var originText:String ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-+/*";
while( originText.length ) {
var char:String = originText.substr( 0, 1 );
textfield.text = char;
var tmp:BitmapData = new BitmapData( 16, 16, false, 0xFFFFFF );
tmp.draw( textfield );
_preRenderMap.push( tmp );
originText = originText.substr( 1, originText.length );
}
}
}
}
import flash.display.*;
class Particle {
public var x:Number;
public var y:Number;
public var vx:Number;
public var vy:Number;
public var rot:Number;
public var vrot:Number;
public var bitmapData:BitmapData;
public var life:int = 100;
public function Particle( bmd:BitmapData, x:Number=0, y:Number=0, rot:Number= 0, vx:Number=0, vy:Number=0, vrot:Number=0 ){
this.x = x;
this.y = y;
this.rot = rot;
this.vx = vx;
this.vy = vy;
this.vrot = vrot;
this.bitmapData= bmd;
}
}