/**
* Copyright termat ( http://wonderfl.net/user/termat )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/jqUt
*/
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldType;
[SWF(width="480", height="480", backgroundColor="0xffffff", frameRate="60")];
public class Practice58 extends Sprite{
private var canvas:Canvas;
public function Practice58() {
canvas = new Canvas(480, 480);
addChild(canvas);
addEventListener(Event.ENTER_FRAME, update);
canvas.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
}
private function update(e:Event):void {
canvas.update();
}
private function onDown(e:MouseEvent):void {
canvas.next();
}
}
}
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.filters.BlurFilter;
import flash.filters.BitmapFilterQuality;
import flash.system.LoaderContext;
import flash.system.Security;
class Canvas extends MovieClip {
private var bmp:BitmapData;
private var tmp:BitmapData;
private var images:Array;
private var list:Vector.<Particle>;
private var filter:BlurFilter;
private var id:int = 0;
private var url:Array = ["http://farm4.static.flickr.com/3273/2824587969_f4a1cff8f8_m.jpg",
"http://farm1.static.flickr.com/190/508971226_cdcfd71c4d_m.jpg",
"http://farm2.static.flickr.com/1185/1464649124_49691c6057_m.jpg",
"http://farm3.static.flickr.com/2168/2224323796_193b9c58dc_m.jpg",
"http://farm1.static.flickr.com/216/508866526_152e9cf6ec_m.jpg",
"http://farm3.static.flickr.com/2379/2221908369_4c85ca67c1_m.jpg"];
public function Canvas(w:int,h:int) {
bmp = new BitmapData(w, h, false, 0xffffff);
addChild(new Bitmap(bmp));
filter = new BlurFilter(2, 2, BitmapFilterQuality.MEDIUM);
this.filters = [filter];
Particle.bmp = bmp;
images = [];
list = new Vector.<Particle>();
for (var i:int = 1; i < 5;i++) Security.loadPolicyFile("http://farm"+i.toString()+".static.flickr.com/crossdomain.xml");
for (i = 0; i < url.length; i++) {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleted);
loader.load(new URLRequest(url[i]), new LoaderContext(true));
}
}
private function loadCompleted(e:Event):void {
var loader:Loader = e.target.loader;
var t:BitmapData = new BitmapData(loader.width, loader.height);
t.draw(loader);
images.push(t);
if (images.length == 1) {
tmp = images[0];
createperticle();
jump(300);
}
}
private function createperticle():void {
while (list.length > 0) list.pop();
var mx:int = (bmp.width - tmp.width) / 2;
var my:int = (bmp.height - tmp.height) / 2;
for (var i:int = 0; i < tmp.width; i++) {
for (var j:int = 0; j < tmp.height; j++) {
var c:uint = tmp.getPixel(i, j);
var p:Particle = new Particle(i+mx, j+my, c);
list.push(p);
}
}
}
public function update():void {
bmp.fillRect(bmp.rect, 0xffffff);
for each(var p:Particle in list) {
if (p.canDraw()) bmp.setPixel(p.x, p.y, p.color);
p.update();
}
}
public function next():void {
id = (id + 1) % images.length;
tmp = images[id];
createperticle();
jump(300);
}
private function jump(r:Number):void {
for each(var p:Particle in list) {
var dir:Number = (360.0 * Math.random())/Math.PI;
var len:Number = r * Math.random();
p.x = p.bx +len * Math.sin(dir);
p.y = p.by +len * Math.cos(dir);
}
}
}
class Particle {
public var x:int;
public var y:int;
public var color:uint;
public var bx:Number;
public var by:Number;
public var vx:Number;
public var vy:Number;
public static var bmp:BitmapData;
private var sp:Number = 0.5;
private var fr:Number = 0.7;
public function Particle(_x:int, _y:int, _c:uint) {
x = _x, y = _y, color = _c;
bx = x, by = y;
vx = 0, vy = 0;
}
public function canDraw():Boolean {
if (x < 0) return false;
if (x > bmp.width) return false;
if (y < 0) return false;
if (y > bmp.height) return false;
return true;
}
public function update():void {
if (x == bx && y == by) return;
x += vx, y += vy;
var ax:Number = (bx - x) * sp;
var ay:Number = (by - y) * sp;
vx += ax, vy += ay;
vx *= fr, vy *= fr;
}
}