package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.MovieClip; import flash.display.Sprite; import flash.events.Event; import flash.filters.BlurFilter; import flash.geom.Point; import flash.geom.Rectangle; [SWF( backgroundColor='0x000000', frameRate='50', width='800', height='600')] public class BlobsInteractive extends Sprite { private var bl :Array; private var r :Bitmap; private var c :Sprite; private var b :Sprite; private var s :Number = 0; private var p :Number = .9; private var container:Sprite; public function BlobsInteractive() { stage.align = "TL"; stage.scaleMode = "noScale"; container = new Sprite(); bl = new Array(); b = new Sprite(); b.graphics.beginFill(0x000000,1); b.graphics.lineTo(stage.stageWidth,0); b.graphics.lineTo(stage.stageWidth,stage.stageHeight); b.graphics.lineTo(0,stage.stageHeight); b.graphics.lineTo(0,0); b.graphics.endFill(); container.addChild(b); c = new Sprite(); c.filters = [new BlurFilter(20,20,2)]; container.addChild(c); r = new Bitmap( new BitmapData( stage.stageWidth, stage.stageHeight, false, 0x00FFFFFF ) ); container.addChild(b); container.addChild(c); container.addChild(r); //container.width = stage.stageWidth * 2; //container.height = stage.stageHeight * 2; buttonMode = true; // local coordinate in the container b.x = - b.width/2; b.y = - b.height/2; c.x = - c.width/2; c.y = - c.height/2; r.x = - r.width/2; r.y = - r.height/2; // add the container to the display list this.addChild(container); // at the middle of the stage container.x = stage.stageWidth/2; container.y = stage.stageHeight/2; this.addEventListener( "enterFrame", u ); this.addEventListener( "mouseUp", mu ); this.addEventListener( "mouseDown", md ); //this.addEventListener( "mouseDown", shakeObject ); } private function u(e:Event):void { for (var i:Number = 0; i < 100; i ++) { if (!bl[i]) { var rndmAlpha:Number = Math.random(); bl[i] = new MovieClip(); if (i % 2 == 0) { bl[i].graphics.beginFill(0xFF7004A, 1); //(bl[i] as MovieClip).graphics.drawRect(0,0,widthHeight, widthHeight); } else { bl[i].graphics.beginFill(0x0470FFA, 1); //bl[i].graphics.drawCircle(0,0,Math.random() * 20+5); } //bl[i].graphics.drawCircle(0,0,Math.random()*40+5); bl[i].graphics.drawCircle(0,0,Math.random()*20+5); bl[i].x = stage.stageWidth*.5 + Math.random()*1000-500; bl[i].y = stage.stageHeight*.5 + Math.random()*1000-500; bl[i].xspeed = bl[i].yspeed = 0; bl[i].cycle = Math.random() * .01 + .0005; bl[i].offsetx = Math.random() * 200-100; bl[i].offsety = Math.random() * 200-100; c.addChild(bl[i]); } bl[i].x += bl[i].xspeed = (( mouseX - bl[i].x + bl[i].offsetx) * bl[i].cycle ) + ( bl[i].xspeed * p); bl[i].y += bl[i].yspeed = (( mouseY - bl[i].y + bl[i].offsety) * bl[i].cycle ) + ( bl[i].yspeed * p); } r.bitmapData.fillRect(new Rectangle(0,0,stage.stageWidth, stage.stageHeight),0x00FFFFFF); r.bitmapData.draw(c); r.bitmapData.threshold(r.bitmapData, new Rectangle(0,0,stage.stageWidth, stage.stageHeight), new Point(0,0), ">", 0x00800000, 0x000000FF, 0x00808080, true); //md(null); //this.container.rotation += 2; } private function md(e:Event):void { for (var i:Number = 0; i < bl.length; i ++) { bl[i].offsetx = Math.random()*1000-500; bl[i].offsety = Math.random()*1000-500; } } private function mu(e:Event):void { for (var i:Number = 0; i < bl.length; i ++) { bl[i].offsetx = Math.random()*200-100; bl[i].offsety = Math.random()*200-100; } } // //This function is responsible for animating the shake // private function shakeObject (e:Event):void { // // for (var i:Number = 0; i < bl.length; i ++) { // //bl[i].x += Math.random() * 5; // //bl[i].y += Math.random() * 5; // bl[i].scaleX = Math.random() * 1.5; // bl[i].scaleY = (bl[i] as MovieClip).scaleX; // } // } } } forked from: Interactive blobs