よくわからないもの
写真を粒子化し、粒子を爆発させたりばらばらにしたいと思い作ってみたものです。
こんな感じ
http://about-hiroppy.com/flash/work/explode/
そーす
//pushbuttonなぜつかわなかったのか? //A. Buttonかっこ良くしたかった(pushbuttonは面倒くさいイメージ) package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Loader; import flash.display.SimpleButton; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.filters.BlurFilter; import flash.geom.ColorTransform; import flash.geom.Matrix; import flash.net.FileFilter; import flash.net.FileReference; import flash.net.URLRequest; import flash.system.LoaderContext; [SWF(width="500",height="500",backgroundColor="0")] public class particle7 extends Sprite{ private var button:SimpleButton; private var button2:SimpleButton; private var url:String = "ex1.jpg"; private var fileReference:FileReference; private var loader:Loader=new Loader(); private var bmpd:BitmapData; private var canvas:BitmapData; private var bmp:Bitmap; private var ctf:ColorTransform = new ColorTransform(0.4,0.7,0.97,1); private var particles:Vector.<Particle> = new Vector.<Particle>; private var button_flag:Boolean = false; private var button2_flag:Boolean = false; private var radian:Number = 100; public function particle7():void{ //button //left var up:Button = new Button(0x00ffff,"Reload",100); var over:Button = new Button(0xFF4500,"Reload",100); button = new SimpleButton(up, over, over, over); button.x = 0; button.y = stage.stageHeight - button.height; button.addEventListener(MouseEvent.CLICK, onMouseClick); //right var up2:Button = new Button(0x00ffff,"select a photo",200); var over2:Button = new Button(0xff4500,"select a photo",200); button2 = new SimpleButton(up2,over2,over2,over2); button2.x = 300; button2.y = stage.stageHeight - button2.height; button2.addEventListener(MouseEvent.CLICK,onMouseClick_select); //file reference(uploading picture) fileReference = new FileReference(); fileReference.addEventListener(Event.SELECT, selectHandler); fileReference.addEventListener(Event.COMPLETE, completeHandler); //canvas canvas= new BitmapData(500,500,false,0); //loding image loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadComplete); loader.load(new URLRequest(url), new LoaderContext(true)); } private function loadComplete(e:Event):void{ var lo:Loader = Loader(e.target.loader); bmpd= new BitmapData(300,300,false,0); //画像の大きさ調整 var matrix:Matrix = new Matrix(); matrix.scale(300 /loader.width, 300 /loader.height); bmpd.draw(lo.content,matrix); bmp = new Bitmap(canvas); addChild(bmp); addChild(button); addChild(button2); Start(); stage.addEventListener(MouseEvent.CLICK,start_explode); } //image select private function selectHandler(event:Event):void{ fileReference.load(); } private function completeHandler(event:Event):void{ var loader:Loader = new Loader(); loader.loadBytes(fileReference.data); loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler); } private function initHandler(event:Event):void{ var loader2:Loader = event.currentTarget.loader; canvas.fillRect(canvas.rect,0); var matrix:Matrix = new Matrix(); matrix.scale(300 /loader2.width, 300 /loader2.height); bmpd.draw(loader2,matrix); Start(); } //start private function Start():void{ if(particles.length != 0) particles.splice(0,particles.length); addEventListener(Event.ENTER_FRAME,onEnterFrame2); Analysis(); make_canvas(); } private function Analysis():void{ for(var i:int=0;i<bmpd.width;i+=2){ for(var j:int=0;j<bmpd.height;j+=2){ var p:Particle = new Particle(i,j,bmpd.getPixel(i,j)); particles.push(p); } } } private function make_canvas():void{ canvas.lock(); var n:Number = particles.length; while(n--){ canvas.setPixel(particles[n].xx,particles[n].yy,particles[n].color); } canvas.unlock(); } //mouse event //button private function onMouseClick(e:MouseEvent):void{ button_flag = true; addEventListener(Event.ENTER_FRAME,onEnterFrame2); removeEventListener(Event.ENTER_FRAME,onEnterFrame); canvas.fillRect(canvas.rect,0); var len:Number = particles.length; particles.splice(0,len); Start(); } private function start_explode(e:MouseEvent):void{ if(!button_flag && !button2_flag){ removeEventListener(Event.ENTER_FRAME,onEnterFrame2); addEventListener(Event.ENTER_FRAME,onEnterFrame); } else{ button_flag = false; button2_flag = false; } } //image select private function onMouseClick_select(event:MouseEvent):void{ button2_flag = true; removeEventListener(Event.ENTER_FRAME,onEnterFrame); removeEventListener(Event.ENTER_FRAME,onEnterFrame2); var fileFilter:FileFilter = new FileFilter("Images(JPEG,PNG)", "*.jpg;*.png"); fileReference.browse([fileFilter]); } //EnterFrame //explode private function onEnterFrame(e:Event):void{ canvas.colorTransform(canvas.rect,ctf); canvas.applyFilter(canvas,canvas.rect,canvas.rect.topLeft,new BlurFilter(50,50,1)); canvas.lock(); var n:Number = particles.length; while(n--){ particles[n].xx += particles[n].vx; particles[n].yy += particles[n].vy; canvas.setPixel(particles[n].xx,particles[n].yy,particles[n].color); // if(particles[n].xx > 500) particles[n].xx = -50; // else if(particles[n].xx < 0) particles[n].xx = 550; // // if(particles[n].yy > 500) particles[n].yy = -50; // else if(particles[n].yy < 0) particles[n].yy = 550; } canvas.unlock(); } //mouse move animation private function onEnterFrame2(e:Event):void{ var n:Number = particles.length; canvas.lock(); while(n--){ var angle:Number = Math.atan2(particles[n].yy - mouseY,particles[n].xx - mouseX); var distance:Number = Math.sqrt(Math.pow(mouseX-particles[n].xx,2)+Math.pow(mouseY-particles[n].yy,2)); var circle:Number = radian / distance; if((mouseX == 0 || mouseY == 0) || (mouseX <= 100 && mouseY >= 450) || (mouseX >= 300 && mouseY >= 450)) { continue; } if(distance < 50 || (mouseX != 0 && mouseY != 0 )){ canvas.setPixel(particles[n].xx,particles[n].yy,0); particles[n].xx += circle*Math.cos(angle); particles[n].yy += circle*Math.sin(angle); canvas.setPixel(particles[n].xx,particles[n].yy,particles[n].color); } else{ if(canvas.getPixel(particles[n].fx,particles[n].fy) == 0){ canvas.setPixel(particles[n].xx,particles[n].yy,0); canvas.setPixel(particles[n].fx,particles[n].fy,particles[n].color); } } } canvas.unlock(); } } } //particle class Particle{ //position //now public var xx:Number; public var yy:Number; //first public var fx:Number; public var fy:Number; public var color:uint; public var vx:Number; public var vy:Number; public function Particle(xx,yy,color){ this.fx = xx; this.fy = yy; this.xx = xx; this.yy = yy; this.vx = Math.random()*60-40; this.vy = Math.random()*60-40; this.color = color; } } //Button import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFormat; class Button extends Sprite { public function Button(color:int,text:String,w:Number) { graphics.lineStyle(2.0, color,0.2); graphics.drawRect(0, 0, w, 50); var tf:TextField = new TextField(); tf.defaultTextFormat = new TextFormat("_typeWriter", 20, color, true); tf.text = text; tf.autoSize = "left"; tf.x = (this.width - tf.width) / 2; tf.y = (this.height - tf.height) / 2; tf.selectable = false; addChild(tf); } }