Skip to content

Instantly share code, notes, and snippets.

@ghiden
Created August 20, 2010 14:26
Show Gist options
  • Save ghiden/540426 to your computer and use it in GitHub Desktop.
Save ghiden/540426 to your computer and use it in GitHub Desktop.
// Original source from here
// http://d.hatena.ne.jp/matsu4512/20090424/1240541319
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.filters.ColorMatrixFilter;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.utils.Timer;
[SWF(backgroundColor=0x000000)]
public class StringParticles extends Sprite
{
//BitmapData for drawing particles
private var canvas:BitmapData;
//BitmapData for text
private var textCanvas:BitmapData;
//Arrays for particles
private var particles:Array;
//Arrays for shuffled particles
private var s_particles:Array;
//number of particles
private var p_num:int
private var textAry:Array;
private var index:uint=0;
//max moving distance for each particle
private var maxDis:int = 500;
private var timer:Timer;
public function StringParticles()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
canvas = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x000000);
addChild(new Bitmap(canvas));
textAry = new Array;
for(var i:uint = 0; i < 2; i++){
textAry[i] = new Array(2);
}
textAry[0][0] = "Hello World";
textAry[0][1] = 60;
textAry[1][0] = "Testing Particles";
textAry[1][1] = 60;
init(textAry[0][0], textAry[0][1]);
stage.addEventListener(MouseEvent.CLICK, onClick);
timer = new Timer(33);
timer.addEventListener(TimerEvent.TIMER, loop);
timer.start();
}
private function init(str:String, size:uint):void{
var tf:TextField = new TextField;
tf.defaultTextFormat = new TextFormat("Swis721 BdRndBT", size, 0xFFFFFF );
tf.autoSize = TextFieldAutoSize.LEFT;
tf.text = str;
//drawing text
textCanvas = new BitmapData(tf.textWidth, tf.textHeight, false, 0x000000);
textCanvas.draw(tf);
particles = new Array;
createText();
s_particles = new Array;
shuffleParticles();
}
//create particles based on text
private function createText():void{
//center it
var ofx:Number = stage.stageWidth / 2 - textCanvas.width / 2;
var ofy:Number = stage.stageHeight / 2 - textCanvas.height / 2;
//checking textCanvas by 1px at at time
for(var ex:uint = 0; ex < textCanvas.width; ex++){
for(var ey:uint = 0; ey < textCanvas.height; ey++){
//get the color of textCanvas at(ex, ey)
var c:uint = textCanvas.getPixel(ex, ey);
if(c != 0x000000){
createParticle(ex + ofx, ey + ofy, c);
}
}
}
}
private function shuffleParticles():void {
trace(particles.length);
for (var i:int = particles.length - 1; i >= 0; i--) {
var ri:uint = Math.random() * i;
s_particles.push(particles.splice(ri, 1)[0]);
}
trace(s_particles.length);
trace(particles.length);
trace("shuffle done!");
}
public function createParticle(ex:Number, ey:Number, c:Number):void{
var p:Particle = new Particle;
p.ex = ex;
p.ey = ey;
// set random color to make it interesting
c = (Math.random() * 255 << 16) + (Math.random() * 255 << 8) + (Math.random() * 255);
p.c = c;
initParticle(p);
particles.push(p);
p_num = 0;
}
public function initParticle(p:Particle):void{
var rad:Number = Math.random() * Math.PI * 2;
var dis:Number = Math.random() * maxDis;
p.x = p.ex + dis * Math.cos(rad);
p.y = p.ey + dis * Math.sin(rad);
}
private function onClick(event:MouseEvent):void{
index = (index+1)%textAry.length;
init(textAry[index][0], textAry[index][1]);
// var n:int = particles.length;
// while(n--){
// var p:Particle = particles[n];
// initParticle(p);
// }
// p_num = 0;
}
private function loop(event:TimerEvent):void{
canvas.lock();
//clear
canvas.fillRect(canvas.rect, 0x000000);
//draw particles
for(var i:uint = 0; i < p_num; i++){
var p:Particle = s_particles[i];
if(Math.abs(p.ex - p.x) < 0.5 && Math.abs(p.ey - p.y) < 0.5){
//snap processing?
p.x = p.ex;
p.y = p.ey;
}
else{
p.x += (p.ex - p.x) / 4;
p.y += (p.ey - p.y) / 4;
}
canvas.setPixel(p.x, p.y, p.c);
}
//add more particles
var n:uint = s_particles.length;
p_num = (p_num + 100 < n) ? p_num + 100 : n;
canvas.unlock();
}
}
}
class Particle{
public var x:Number;
public var y:Number;
public var ex:Number;
public var ey:Number;
public var c:int;
public function Particle(){
x = 0;
y = 0;
ex = 0;
ey = 0;
c = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment