Happy Birthday for naruto baby
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/pAoK
*/
////////////////////////////////////////////////////////////////////////////////
// Happy Birthday for naruto baby
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
[SWF(backgroundColor="#000000", width="465", height="465", frameRate="60")]
public class Main extends Sprite {
private var detection:DetectPixels;
private static var accuracy:uint = 2;
private var threshold:uint = 0xFF808080;
private var twinkle:TwinkleEffect;
private var visualizer:SoundVisualizer;
public function Main() {
//Wonderfl.capture_delay(1);
init();
}
private function init():void {
graphics.beginFill(0x000000);
graphics.drawRect(0, 0, 465, 465);
graphics.endFill();
var label1:Label = new Label(400, 100, 80, Label.CENTER);
addChild(label1);
label1.x = 32;
label1.y = 72;
label1.textColor = 0xFFFFFF;
label1.alpha = 0.4;
label1.text = "Happy";
var label2:Label = new Label(400, 100, 80, Label.CENTER);
addChild(label2);
label2.x = 32;
label2.y = 172;
label2.textColor = 0xFFFFFF;
label2.alpha = 0.4;
label2.text = "Birthday";
var label3:Label = new Label(400, 100, 40, Label.CENTER);
addChild(label3);
label3.x = 32;
label3.y = 292;
label3.textColor = 0xFFFFFF;
label3.alpha = 0.4;
label3.text = "for naruto baby";
var label4:Label = new Label(400, 100, 40, Label.CENTER);
addChild(label4);
label4.x = 32;
label4.y = 332;
label4.textColor = 0xFFFFFF;
label4.alpha = 0.4;
label4.text = "2011.2.9.";
//
detection = new DetectPixels(accuracy);
var area1:Rectangle = new Rectangle(0, 10, 400, 100);
var offset1:Object = {x: 0, y: 0};
detection.search(label1, area1, threshold, offset1);
var map1:Array = detection.pixels();
var area2:Rectangle = new Rectangle(0, 10, 400, 100);
var offset2:Object = {x: 0, y: 100};
detection.search(label2, area2, threshold, offset2);
var map2:Array = detection.pixels();
var area3:Rectangle = new Rectangle(0, 10, 400, 100);
var offset3:Object = {x: 0, y: 210};
detection.search(label3, area3, threshold, offset3);
var map3:Array = detection.pixels();
var area4:Rectangle = new Rectangle(0, 10, 400, 100);
var offset4:Object = {x: 0, y: 250};
detection.search(label4, area4, threshold, offset4);
var map4:Array = detection.pixels();
//
var map:Array = map2.concat(map1).concat(map3).concat(map4);
//
var rect:Rectangle = new Rectangle(0, 0, 400, 400);
twinkle = new TwinkleEffect(rect, map);
addChild(twinkle);
twinkle.x = 32;
twinkle.y = 72;
twinkle.setup(100, 20);
twinkle.addEventListener(Event.INIT, initialize, false, 0, true);
//
visualizer = new SoundVisualizer(5, 0xFFFFFF);
addChild(visualizer);
visualizer.x = 445;
visualizer.y = 450;
}
private function initialize(evt:Event):void {
visualizer.start();
}
}
}
//////////////////////////////////////////////////
// TwinkleEffectクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundLoaderContext;
import flash.media.SoundMixer;
import flash.utils.ByteArray;
import flash.net.URLRequest;
import flash.system.Security;
class TwinkleEffect extends Sprite {
private var rect:Rectangle;
private var map:Array;
private var bitmapData:BitmapData;
private var bitmap:Bitmap;
private static var radius:uint = 10;
private var interval:uint = 100;
private var life:uint = 10;
private var particles:Array;
private static var white:uint = 0xFFFFFF;
private static var gold:uint = 0xFFCC33;
private static var pink:uint = 0xFF0099;
private static var green:uint = 0x99FF00;
private static var aqua:uint = 0x00FFFF;
private static var colors:Array = [white, white, gold, gold, pink, green, aqua];
private var units:Array = [30, 30, 45, 45, 45, 60];
private static var radian:Number = Math.PI/180;
private var timer:Timer;
private static var policyPath:String = "http://www.project-nya.jp/crossdomain.xml";
private var sound:Sound;
private var channel:SoundChannel;
private static var soundPath:String = "http://www.project-nya.jp/images/bgm/bgm_spring.mp3";
private var byteArray:ByteArray;
private static var channels:uint = 256;
private var factors:uint = 0;
public function TwinkleEffect(r:Rectangle, m:Array) {
rect = r;
map = m;
init();
}
private function init():void {
bitmapData = new BitmapData(rect.width, rect.height, true, 0x00000000);
bitmap = new Bitmap(bitmapData);
addChild(bitmap);
particles = new Array();
//
Security.loadPolicyFile(policyPath);
sound = new Sound();
sound.addEventListener(Event.COMPLETE, complete, false, 0, true);
sound.load(new URLRequest(soundPath), new SoundLoaderContext(10, true));
byteArray = new ByteArray();
}
public function setup(i:uint = 100, l:uint = 10):void {
interval = i;
life = l;
}
private function complete(evt:Event):void {
evt.target.removeEventListener(Event.COMPLETE, complete);
dispatchEvent(new Event(Event.INIT));
start();
}
private function start():void {
timer = new Timer(interval);
timer.addEventListener(TimerEvent.TIMER, tick, false, 0, true);
timer.start();
channel = sound.play(0, 1000);
addEventListener(Event.ENTER_FRAME, update, false, 0, true);
}
private function stop():void {
timer.stop();
removeEventListener(Event.ENTER_FRAME, update);
}
private function tick(evt:TimerEvent):void {
SoundMixer.computeSpectrum(byteArray, true, factors);
for (var c:uint = 0; c < 2; c++) {
for (var t:uint = 0; t < channels; t++) {
var p:Number = byteArray.readFloat();
if (p > 0) create(p);
}
}
}
private function create(p:Number):void {
//var id:uint = uint(Math.random()*map.length);
var id:uint = uint(map.length*p)%map.length;
var px:Number = map[id].x;
var py:Number = map[id].y;
plot(px, py);
}
private function plot(px:Number, py:Number):void {
var matrix:Matrix = new Matrix();
matrix.translate(px - radius, py - radius);
var color:uint = colors[uint(Math.random()*colors.length)];
var particle:TwinkleParticle = new TwinkleParticle();
particle.x = px;
particle.y = py;
particle.life = (Math.random()*0.5 + 0.5)*life;
particle.matrix = matrix;
particle.color = color;
particle.unit = units[uint(Math.random()*units.length)];
particles.push(particle);
bitmapData.lock();
draw(px, py, 0xFFFFFF, particle.life/life, particle.unit);
bitmapData.unlock();
}
private function update(evt:Event):void {
bitmapData.lock();
bitmapData.fillRect(rect, 0x00000000);
for (var n:uint = 0; n < particles.length; n++) {
var particle:TwinkleParticle = particles[n];
draw(particle.x, particle.y, particle.color, particle.life/life, particle.unit);
particle.life --;
if (particle.life < 0) {
particles.splice(n, 1);
particle = null;
}
}
bitmapData.unlock();
}
private function draw(px:uint, py:uint, color:uint, percent:Number, unit:uint):void {
var length:uint = uint(radius*percent);
var argb:uint = 0xFF << 24 | color;
for (var n:uint = 0; n < length; n++) {
for (var a:uint = 0; a < 360; a+=unit) {
var dx:Number = px + n*Math.cos(a*radian);
var dy:Number = py + n*Math.sin(a*radian);
bitmapData.setPixel32(dx, dy, argb);
}
}
}
}
//////////////////////////////////////////////////
// TwinkleParticleクラス
//////////////////////////////////////////////////
import flash.geom.Matrix;
class TwinkleParticle {
public var x:uint;
public var y:uint;
public var life:int;
public var matrix:Matrix;
public var color:uint;
public var unit:uint;
public function TwinkleParticle() {
}
}
//////////////////////////////////////////////////
// DetectPixelsクラス
//////////////////////////////////////////////////
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.geom.Matrix;
import flash.display.IBitmapDrawable;
class DetectPixels {
private var bd:IBitmapDrawable;
private var rect:Rectangle;
private var map:BitmapData;
private var mapList:Array;
private var accuracy:uint;
private var threshold:uint = 0x80FFFFFF;
private var offset:Object = {x: 0, y: 0};
public function DetectPixels(a:uint = 1) {
accuracy = a;
}
public function search(t:IBitmapDrawable, r:Rectangle, th:uint = 0x80FFFFFF, o:Object = null):void {
bd = t;
rect = r;
threshold = th;
if (o) offset = o;
var w:uint = rect.width/accuracy;
var h:uint = rect.height/accuracy;
detect(w, h);
}
private function detect(w:uint, h:uint):void {
map = new BitmapData(w, h, true, 0x00000000);
var matrix:Matrix = new Matrix();
matrix.translate(-rect.x, -rect.y);
matrix.scale(1/accuracy, 1/accuracy);
map.lock();
map.draw(bd, matrix);
map.unlock();
mapList = new Array();
for (var y:uint = 0; y < h; y++) {
for (var x:uint = 0; x < w; x++) {
var color:uint = map.getPixel32(x, y);
if (color >= threshold) {
var px:int = x*accuracy + rect.x + offset.x;
var py:int = y*accuracy + rect.y + offset.y;
var point:Point = new Point(px, py);
mapList.push(point);
}
}
}
}
public function pixels():Array {
return mapList;
}
}
//////////////////////////////////////////////////
// Labelクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFieldAutoSize;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
class Label extends Sprite {
private var txt:TextField;
private static var fontType:String = "_ゴシック";
private var _width:uint = 20;
private var _height:uint = 20;
private var size:uint = 12;
public static const LEFT:String = TextFormatAlign.LEFT;
public static const CENTER:String = TextFormatAlign.CENTER;
public static const RIGHT:String = TextFormatAlign.RIGHT;
public function Label(w:uint, h:uint, s:uint = 12, align:String = LEFT) {
_width = w;
_height = h;
size = s;
draw(align);
}
private function draw(align:String):void {
txt = new TextField();
addChild(txt);
txt.width = _width;
txt.height = _height;
txt.autoSize = align;
txt.type = TextFieldType.DYNAMIC;
txt.selectable = false;
//txt.embedFonts = true;
//txt.antiAliasType = AntiAliasType.ADVANCED;
var tf:TextFormat = new TextFormat();
tf.font = fontType;
tf.size = size;
tf.align = align;
txt.defaultTextFormat = tf;
textColor = 0x000000;
}
public function set text(param:String):void {
txt.text = param;
}
public function set textColor(param:uint):void {
txt.textColor = param;
}
}
//////////////////////////////////////////////////
// SoundVisualizerクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.TimerEvent;
class SoundVisualizer extends Sprite {
private var max:uint;
private var color:uint;
private static var bw:uint = 4;
private static var bh:uint = 16;
private static var xoffset:uint = 1;
private static var yoffset:uint = 2;
private var indicators:Array;
private var timer:Timer;
private static var interval:uint = 25;
public function SoundVisualizer(m:uint, c:uint = 0x000000) {
max = m;
color = c;
draw();
}
private function draw():void {
indicators = new Array();
for (var n:uint = 0; n < max; n++) {
var indicator:SoundIndicator = new SoundIndicator(bw, bh, color);
addChild(indicator);
indicator.x = - uint((bw + xoffset)*max/2) + (bw + xoffset)*n;
indicator.y = uint(bh/2);
indicators.push(indicator);
}
}
public function start():void {
timer = new Timer(interval);
timer.addEventListener(TimerEvent.TIMER, update, false, 0, true);
timer.start();
}
public function stop():void {
if (timer) {
timer.stop();
timer.removeEventListener(TimerEvent.TIMER, update);
}
reset();
}
public function update(evt:TimerEvent):void {
for (var n:uint = 0; n < max; n++) {
var indicator:SoundIndicator = indicators[n];
indicator.update();
}
}
public function reset():void {
for (var n:uint = 0; n < max; n++) {
var indicator:SoundIndicator = indicators[n];
indicator.reset();
}
}
}
//////////////////////////////////////////////////
// SoundIndicatorクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.Shape;
class SoundIndicator extends Sprite {
private var bar:Shape;
private var color:uint;
private var _width:uint;
private var _height:uint;
public function SoundIndicator(w:uint, h:uint, c:uint = 0x000000) {
_width = w;
_height = h;
color = c;
draw();
reset();
}
private function draw():void {
var base:Shape = new Shape();
addChild(base);
base.graphics.beginFill(color);
base.graphics.drawRect(0, 0, _width, 1);
base.graphics.endFill();
//
bar = new Shape();
addChild(bar);
bar.graphics.beginFill(color);
bar.graphics.drawRect(0, 0, _width, - _height);
bar.graphics.endFill();
}
public function update():void {
bar.scaleY = Math.random();
bar.height = uint(bar.height);
}
public function reset():void {
bar.scaleY = 0;
}
}