forked from: meh
// forked from etheo's meh
package {
import flash.display.Sprite;
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.system.*;
public class FlashTest extends Sprite {
private var grid:MyGrid;
public function FlashTest() {
inittrace(stage);
grid = new MyGrid(40, 40, 10);
addChild(grid);
grid.bitmapUrls.push("http://sandbox.dev.studioseptember.nl/img/1.jpg");
grid.bitmapUrls.push("http://sandbox.dev.studioseptember.nl/img/2.jpg");
grid.bitmapUrls.push("http://sandbox.dev.studioseptember.nl/img/3.jpg");
grid.bitmapUrls.push("http://sandbox.dev.studioseptember.nl/img/4.jpg");
grid.bitmapUrls.push("http://sandbox.dev.studioseptember.nl/img/5.jpg");
grid.bitmapUrls.push("http://sandbox.dev.studioseptember.nl/img/6.jpg");
grid.bitmapUrls.push("http://sandbox.dev.studioseptember.nl/img/7.jpg");
grid.bitmapUrls.push("http://sandbox.dev.studioseptember.nl/img/8.jpg");
grid.bitmapUrls.push("http://sandbox.dev.studioseptember.nl/img/9.jpg");
grid.bitmapUrls.push("http://sandbox.dev.studioseptember.nl/img/10.jpg");
grid.bitmapUrls.push("http://sandbox.dev.studioseptember.nl/img/11.jpg");
grid.loadNextBitmap();
}
}
}
import flash.utils.Timer;
import flash.display.*;
import flash.geom.*;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.events.*;
import flash.net.*;
import flash.system.*;
class MyGrid extends Sprite {
private var spacing:Number = 10;
private var gridWidth:int = 0;
private var gridHeight:int = 0;
private var grid:Array = [];
private var tinyBitmap:Bitmap;
public var bitmapUrls:Array = [];
public function loadNextBitmap():void{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE , morphToBitmap);
var url:String = bitmapUrls.shift();
//trace("Loading "+url);
loader.load(new URLRequest(url), new LoaderContext(true));
}
public function setBitmap(bitmap:Bitmap):void{
var bd:BitmapData = bitmap.bitmapData;
var colours:Array = averageColours(bd, gridWidth, gridHeight);
for(var a:int=0;a<gridWidth;a++){
for(var b:int=0;b<gridHeight;b++){
var colour:uint = colours[a][b];
var pixel:MyPixel = grid[a][b] as MyPixel;
pixel.setColour(colour);
}
}
}
public function morphToBitmap(e:Event):void{
var loaderInfo:LoaderInfo = e.target as LoaderInfo;
var bitmap:Bitmap = loaderInfo.content as Bitmap;
var bd:BitmapData = bitmap.bitmapData;
if(tinyBitmap!=null){
removeChild(tinyBitmap);
}
var tinyBd:BitmapData = new BitmapData(gridWidth, gridHeight);
var colours:Array = averageColours(bd, gridWidth, gridHeight);
for(var a:int=0;a<gridWidth;a++){
for(var b:int=0;b<gridHeight;b++){
var colour:uint = colours[a][b];
tinyBd.setPixel(a,b,colour);
var pixel:MyPixel = grid[a][b] as MyPixel;
pixel.targetColor = colour;
}
}
tinyBitmap = new Bitmap(tinyBd);
addChild(tinyBitmap);
var timer:Timer = new Timer(50, 100);
timer.addEventListener(TimerEvent.TIMER, timerEvent);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
timer.start();
currentTick = 0;
totalTicks = 100;
}
private var totalTicks:int;
private var currentTick:int;
public function timerEvent(te:TimerEvent):void{
tick(currentTick / totalTicks);
currentTick++;
}
public function timerComplete(e:TimerEvent):void{
for(var a:int=0;a<gridWidth;a++){
for(var b:int=0;b<gridHeight;b++){
var pixel:MyPixel = grid[a][b] as MyPixel;
pixel.commit();
}
}
loadNextBitmap();
}
public function tick(progress:Number):void{
for(var a:int=0;a<gridWidth;a++){
for(var b:int=0;b<gridHeight;b++){
grid[a][b].tick(progress);
}
}
}
public static function averageColour( source:BitmapData ):uint{
var red:Number = 0;
var green:Number = 0;
var blue:Number = 0;
var count:Number = 0;
var pixel:Number;
for (var x:int = 0; x < source.width; x++)
{
for (var y:int = 0; y < source.height; y++)
{
pixel = source.getPixel(x, y);
red += pixel >> 16 & 0xFF;
green += pixel >> 8 & 0xFF;
blue += pixel & 0xFF;
count++
}
}
red /= count;
green /= count;
blue /= count;
return red << 16 | green << 8 | blue;
}
public static function averageColours( source:BitmapData, rows:int, cols:int ):Array{
var averages:Array = new Array();
var x:int = 0;
var y:int = 0;
var w:int = Math.round( source.width / rows );
var h:int = Math.round( source.height / cols );
var box:BitmapData = new BitmapData( w, h, false );
for(var row:int = 0; row<rows; row++){
averages[row] = new Array();
for(var col:int = 0; col<cols; col++){
x = w * (row);
y = h * (col + 0.5);
var rect:Rectangle = new Rectangle( x, y, w, h );
box.copyPixels( source, rect, new Point() );
var colour:uint = averageColour( box );
averages[row][col] = colour;
}
}
box.dispose();
return averages;
}
public function MyGrid(gridWidth:int, gridHeight:int, spacing:Number){
this.gridWidth = gridWidth;
this.gridHeight = gridHeight;
this.spacing = spacing;
for(var a:int=0;a<gridWidth;a++){
this.grid[a] = [];
for(var b:int=0;b<gridHeight;b++){
var pixel:MyPixel = new MyPixel((a+1)*spacing + ((b%2)*(spacing/2)), (b+1)*spacing, spacing/2 );
grid[a][b] = pixel;
addChild(pixel);
pixel.setGreyScale(220);
}
}
}
}
class MyPixel extends Shape{
public static var delay:Number = 0.5;
private var size:Number;
private var startScale:Number;
private var targetScale:Number;
public var startColor:uint;
public var targetColor:uint;
public function scale(_scale:Number):void{
var scale:Number = Math.min(1, Math.max(0, _scale));
new Tween(this, "scaleX", None.easeOut, this.scaleX, scale, delay, true);
new Tween(this, "scaleY", None.easeOut, this.scaleY, scale, delay, true);
}
public static function mixColours(start:uint, end:uint, _progress:Number):uint{
var progress:Number = Math.min(1,Math.max(0,_progress));
var startRed:int = start >> 16 & 0xFF;
var startGreen:int = start >> 8 & 0xFF;
var startBlue:int = start & 0xFF;
var targetRed:int = end >> 16 & 0xFF;
var targetGreen:int = end >> 8 & 0xFF;
var targetBlue:int = end & 0xFF;
var newRed:int = transition(startRed, targetRed, progress);
var newGreen:int = transition(startGreen, targetGreen, progress);
var newBlue:int = transition(startBlue, targetBlue, progress);
return (newRed << 16) + (newGreen << 8) + (newBlue);
}
private static function transition(start:Number, end:Number, progress:Number):Number{
return start + progress * (end-start);
}
public function tick(progress:Number):void{
this.setColourScale(mixColours(startColor, targetColor, progress));
}
public function setGreyScale(_greyScale:Number):void{
var greyScale:Number = Math.min(255,Math.max(_greyScale,0));
this.scale(1 - Math.sqrt(Math.pow(greyScale, 2)/Math.pow(256,2)));
//this.scale(1 - Math.pow(Math.pow(greyScale, 1/2)/Math.pow(256, 1/2), 2));
}
public function MyPixel(x:Number, y:Number, size:Number){
this.graphics.beginFill(0x0, 1);
this.graphics.drawCircle(0,0,size);
this.x = x;
this.y = y;
this.size = size;
}
public function setColour(colour:int):void{
this.graphics.clear();
this.graphics.beginFill(colour,1);
this.graphics.drawCircle(0,0,this.size);
this.startColor = colour;
}
public function setColourScale(colour:uint):void{
setColour(colour);
var r:uint = (colour & 0xFF0000) >>16;
var g:uint = (colour & 0x00FF00) >> 8;
var b:uint = (colour & 0x0000FF);
setGreyScale(Math.round((r+g+b) / 3));
}
public function commit():void{
this.startColor = this.targetColor;
}
}
//SHOW TRACES
import flash.display.Sprite;
import flash.display.Stage;
import flash.text.TextField;
import flash.text.TextFormat;
function inittrace(s:Stage):void
{
WTrace.initTrace(s);
}
//global trace function
var trace:Function;
//wtreace class
class WTrace
{
private static var FONT:String = "Fixedsys";
private static var SIZE:Number = 12;
private static var TextFields:Array = [];
private static var trace_stage:Stage;
public static function initTrace(stg:Stage):void
{
trace_stage = stg;
trace = wtrace;
}
private static function scrollup():void
{
// maximum number of lines: 100
if (TextFields.length > 100)
{
var removeme:TextField = TextFields.shift();
trace_stage.removeChild(removeme);
removeme = null;
}
for(var x:Number=0;x<TextFields.length;x++)
{
(TextFields[x] as TextField).y -= SIZE*1.2;
}
}
public static function wtrace(... args):void
{
var s:String="";
var tracefield:TextField;
for (var i:int;i < args.length;i++)
{
// imitating flash:
// putting a space between the parameters
if (i != 0) s+=" ";
s+=args[i].toString();
}
tracefield= new TextField();
tracefield.autoSize = "left";
tracefield.text = s;
tracefield.y = trace_stage.stageHeight - 20;
var tf:TextFormat = new TextFormat(FONT, SIZE);
tracefield.setTextFormat(tf);
trace_stage.addChild(tracefield);
scrollup();
TextFields.push(tracefield);
}
}