forked from: asahi.com花火
package {
import flash.display.*;
import flash.events.Event;
import flash.geom.*;
import flash.filters.*;
import flash.text.*;
import flash.net.*;
[SWF(backgroundColor=0x000000)]
public class StringParticle2 extends Sprite{
private var URL:String = "http://qzone.net.cn/rss.php?auth=4ca0y%2FATDbJZK52Yn%2FQWd1pkJ%2BO0qTiOAfoaz1ssb0wSCuEE1ZvE5g";
private static const WIDTH:Number = 475;
private static const HEIGHT:Number = 475;
private static const SPEED:Number = 3;
private static var ptZero:Point = new Point();
private var texts:Array = [];
public function StringParticle2()
{
stage.scaleMode = "noScale";
stage.align = "TL";
var req:URLRequest = new URLRequest(URL);
var loader:URLLoader = new URLLoader();
loader.addEventListener("complete", function(event:Event):void{
for each (var x:XML in XML(loader.data)..item){
texts.push(x.title.text().toString());
}
start();
});
loader.load(req);
}
private function start():void{
var filter1:BitmapFilter = new ColorMatrixFilter([
.98, 0, 0, 0, 0,
0, .95, 0, 0, 0,
0, 0, .95, 0, 0,
0, 0, 0, 1, 0]);
var filter2:BitmapFilter = new BlurFilter(3, 3);
var index:int = 0;
var bmd:BitmapData = createBitmapData(texts[index++]);
var particles:Array = initParticles(bmd);
var canvas:BitmapData = new BitmapData(WIDTH, HEIGHT, false, 0x000000);
addChild(new Bitmap(canvas));
addEventListener("enterFrame", function(event:Event):void{
if (Math.random() < .01 && particles.length < 30000){
var bmd:BitmapData = createBitmapData(texts[index]);
particles = particles.concat(initParticles(bmd));
index = (index + 1) % texts.length;
}
canvas.lock();
canvas.applyFilter(canvas, canvas.rect, ptZero, filter1);
canvas.applyFilter(canvas, canvas.rect, ptZero, filter2);
for (var i:int = particles.length - 1; i > 0; i--){
var p:Particle = particles[i];
if(p.counter > 0){
p.counter--;
}else{
p.x += p.vx;
p.y += p.vy;
p.vy += .01 * SPEED;
p.vx -= p.vx * .001 * SPEED;
p.vy -= p.vy * .001 * SPEED;
if(p.x < 0 || p.x > WIDTH || p.y < 0 || p.y > HEIGHT){
particles.splice(i, 1);
}
}
canvas.setPixel(p.x, p.y, p.c);
}
canvas.unlock();
});
}
private static function createBitmapData(letters:String):BitmapData{
var fmt:TextFormat = new TextFormat();
fmt.size = 20 + Math.random() * 40;
var tf:TextField = new TextField();
tf.defaultTextFormat = fmt;
tf.width = WIDTH * (.5 + Math.random() * .3);
tf.height = HEIGHT;
tf.wordWrap = true;
tf.multiline = true;
tf.textColor = Math.random() * 0x808080 + 0x808080;
tf.text = letters;
var bmd:BitmapData = new BitmapData(WIDTH, HEIGHT, true, 0x000000);
var mtx:Matrix = new Matrix();
mtx.translate((WIDTH - tf.textWidth) * Math.random(), (HEIGHT - tf.textHeight) * Math.random());
bmd.draw(tf, mtx);
bmd.applyFilter(bmd, bmd.rect, ptZero, new GlowFilter(0xffffff, 1, 2, 2));
return bmd;
}
private static function initParticles(bmd:BitmapData):Array{
var particles:Array = [];
for (var yy:int = 0; yy < bmd.height; yy++){
for (var xx:int = 0; xx < bmd.width; xx++){
var c:uint = bmd.getPixel(xx, yy);
if (c != 0){
var p:Particle = new Particle();
p.x = xx; p.y = yy; p.c = c;
var theta:Number = Math.random() * SPEED * Math.PI;
var f:Number = Math.random() * 1 * SPEED;
p.vx = Math.cos(theta) * f;
p.vy = Math.sin(theta) * f * .7;
p.counter = 50;
particles.push(p);
}
}
}
return particles;
}
}
}
class Particle{
public var x:Number;
public var y:Number;
public var vx:Number;
public var vy:Number;
public var c:int;
public var counter:int;
public function Particle(){
x = 0;
y = 0;
c = 0;
}
}