纯AS3实现Flash炫动背景

纯AS3实现Flash炫动背景,多种可选形状,随机大小(一定范围内),随机位置。背景随鼠标移动滑动。

先看效果截图:

 

效果示例(全屏浏览):

示例代码:

import Dots;

var dot:Dots=new Dots(10,60);

stage.addChild(dot);
类代码:
package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.GradientType;
	import flash.display.JointStyle;
	import flash.display.CapsStyle;
	import flash.filters.BlurFilter;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.display.MovieClip;
	import flash.display.DisplayObject;
	import flash.geom.Matrix;

	public class Dots extends Sprite
	{
		//层数
		public var layers:int;
		//每层数量
		public var numbers:int;
		//大小
		public var r:int=50;
		public var mr:int=20;
		public var bgColor:int;
		//大小,不透明度随机范围
		public var t:Number=.5;
		//颜色
		public var c:Array;
		//不透明度
		public var a:Number=.7;
		//倍率
		public var m:int= 10;
		//缓冲量
		public var b=.02;
		
		//当前使用的形状
		public var shapFun:String='Heart';
		//所有支持的形状
		public var shapes:Array=null;
		
		//所有层
		private var l:Array=[];
		
		//鼠标位置
		private var m_x:Number=0;
		private var m_y:Number=0;
		
		//
		private var s_w:int=0;
		private var s_h:int=0;
		
		private var arrStar:Array=null;
		
		public function Dots(ls:int=10,ns:int=50,bg:int=0x6e0000,cs:Array=null):void
		{
			layers = ls;
			numbers = ns;
			c	= cs?cs:[0xff0000,0x00ffff,0xff00ff,0x00ff00,0x0000ff,0xffff00];
			bgColor = bg;
			shapes = ['Circle','Huan','Star','Curen','Heart'];
			addEventListener(Event.ADDED_TO_STAGE,init);
			addEventListener(Event.REMOVED_FROM_STAGE,term);
		}
		
		public function init(e:Event=null):void
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode=StageScaleMode.NO_SCALE;
			x = 0;
			y = 0;
			refresh();
			stage.addEventListener(MouseEvent.MOUSE_MOVE,startMove);
			stage.addEventListener(Event.RESIZE,refresh);
		}
		
		//画示例
		public function exam(obj:Sprite,type:String,c:uint=0xffffff,w:int=30,h:int=30):void
		{
			this['draw'+type](obj,Math.min(w,h)*.5,w*.5,h*.5,c,1);
		}
		
		private function create():void
		{
			var cr:int,ca:Number,cc:int,cx:int,cy:int;
			var sw=s_w*(m-1),sh=s_h*(m-1);
			m_x = .5;
			m_y = .5;
			
			graphics.clear();
			//画背景
			var maxr:Number=(s_w+s_h)*.5;
			var mtx:Matrix=new Matrix();
			mtx.createGradientBox(maxr,maxr,0,(s_w-maxr)*.5,(s_h-maxr)*.5);
			//graphics.beginFill(0xffffff,.3)
			graphics.beginGradientFill(GradientType.RADIAL,[bgColor,bgColor,bgColor,bgColor],[1,.9,.3,0],[0,30,180,255],mtx);
			graphics.drawCircle(s_w*.5,s_h*.5,maxr*.5);
			graphics.endFill();
			
			//画层
			for(var i:int=0;i<layers;i++)
			{
				var tLayer:MovieClip=new MovieClip(),cp=(i+1)/layers,
					cr_b:int=mr + (r-mr) * cp,sw_b:int = sw*cp, sh_b:int=sh*cp;
				//加模糊滤镜吃内存
				//var blurFiler:BlurFilter=new BlurFilter((1-cp)*10,(1-cp)*10,1);
				//tLayer.filters=[blurFiler];
				for(var j:int=0;j<numbers;j++)
				{
					if(Math.random()>=.5)
					{
						cr = cr_b * (1+t*Math.random());
						ca = a * (1+t*Math.random());
					}else{
						cr = cr_b * (1-t*Math.random());
						ca = a * (1-t*Math.random());
					}
					cc = c[Math.floor(Math.random()*c.length)];
					cx = (sw_b+s_w)* Math.random();
					cy = (sh_b+s_h)* Math.random();
					this['draw'+shapFun](tLayer, cr, cx, cy, cc, ca);
				}
				addChild(tLayer);
				tLayer.addEventListener(Event.ENTER_FRAME,moves);
				l[i]=tLayer;
			}
		}
		
		public function term(e:Event=null):void
		{
			destroy();
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,startMove);
			stage.removeEventListener(Event.RESIZE,refresh);
		}
		
		private function drawCircle(obj, cr, cx, cy, cc, ca):void
		{
			obj.graphics.lineStyle(1,cc);
			obj.graphics.beginFill(cc,ca);
			obj.graphics.drawCircle(cx,cy,cr);
			obj.graphics.endFill();
		}
		
		private function drawHuan(obj, cr, cx, cy, cc, ca):void
		{
			obj.graphics.lineStyle(cr*.2,cc,ca);
			obj.graphics.drawCircle(cx,cy,cr);
			obj.graphics.lineStyle(cr*.4,cc,ca*.7);
			obj.graphics.drawCircle(cx,cy,cr*.4);
		}
		
		//计算五角星的八个长度与外圈半径比
		//内圈半径,外圈2,5点x,y 外圈3,4点x,y 内圈1,5点x,y 内圈2,4点x,y
		private function calcStar():Array
		{
			var dgr:Number = 360 * .1 * Math.PI / 180;
			var tanDgr:Number=Math.tan(dgr),
				sinDgr:Number=Math.sin(dgr),cosDgr:Number=Math.cos(dgr),
				cos2Dgr:Number=Math.cos(dgr*2),sin2Dgr:Number=Math.sin(dgr*2);
			var ir:Number = cos2Dgr / cosDgr;
			var arrReturn:Array=[];
			arrReturn[0] = ir;
			arrReturn[1] = sin2Dgr;
			arrReturn[2] = cos2Dgr;
			arrReturn[3] = sinDgr;
			arrReturn[4] = cosDgr;
			arrReturn[5] = sinDgr * ir;
			arrReturn[6] = cosDgr * ir;
			arrReturn[7] = sin2Dgr * ir;
			arrReturn[8] = cos2Dgr * ir;
			return arrReturn;
		}
		
		private function drawStar(obj:Sprite, cr:Number, cx:Number, cy:Number, cc:uint, ca:Number):void
		{
			if(!arrStar)arrStar = calcStar();
			obj.graphics.lineStyle(cr*.2,cc,ca,true,null,null,JointStyle.MITER);
			//1-1
			obj.graphics.moveTo(cx,cy-cr);
			//2-1
			obj.graphics.lineTo(cx + cr * arrStar[5],cy - cr * arrStar[6]);
			//1-2
			obj.graphics.lineTo(cx + cr * arrStar[1],cy - cr * arrStar[2]);
			//2-2
			obj.graphics.lineTo(cx + cr * arrStar[7],cy + cr * arrStar[8]);
			//1-3
			obj.graphics.lineTo(cx + cr * arrStar[3],cy + cr * arrStar[4]);
			//2-3
			obj.graphics.lineTo(cx,cy + cr * arrStar[0]);
			//1-4
			obj.graphics.lineTo(cx - cr * arrStar[3],cy + cr * arrStar[4]);
			//2-4
			obj.graphics.lineTo(cx - cr * arrStar[7],cy + cr * arrStar[8]);
			//1-5
			obj.graphics.lineTo(cx - cr * arrStar[1],cy - cr * arrStar[2]);
			//2-5
			obj.graphics.lineTo(cx - cr * arrStar[5],cy - cr * arrStar[6]);
			
			//回到原点
			obj.graphics.lineTo(cx,cy-cr);
		}
		
		private function drawCuren(obj:Sprite, cr:Number, cx:Number, cy:Number, cc:uint, ca:Number):void
		{
			var pr:Number=cr * .2;
			obj.graphics.lineStyle(pr,cc,ca);
			obj.graphics.drawCircle(cx,cy,cr);
			pr = cr * .4;
			obj.graphics.lineStyle(pr,cc,ca*.7,false,null,null,JointStyle.MITER);
			obj.graphics.drawRect(cx - pr,cy - pr,pr*2, pr*2);
		}
		
		private function drawHeart(obj:Sprite, cr:Number, cx:Number, cy:Number, cc:uint, ca:Number):void
		{
			//画空心
			//obj.graphics.lineStyle(cr*.3,cc,ca,true,null,null,JointStyle.MITER);
			//画实心
			obj.graphics.lineStyle(1,cc)
			obj.graphics.beginFill(cc,ca);
			obj.graphics.moveTo(cx, cy-cr*.46);
			obj.graphics.curveTo(cx + cr*.2,cy - cr*.84,cx + cr*.52,cy - cr*.84);
			obj.graphics.curveTo(cx + cr,cy - cr*.84,cx + cr,cy - cr*.22);
			obj.graphics.curveTo(cx + cr*.96,cy+cr*.16,cx + cr*.42,cy + cr*.46);
			obj.graphics.curveTo(cx + cr*.11,cy+cr*.65,cx,cy + cr*.9);	//底部中心点
			obj.graphics.curveTo(cx - cr*.11,cy+cr*.65,cx - cr*.42,cy + cr*.46);
			obj.graphics.curveTo(cx - cr*.96,cy+cr*.16,cx - cr,cy - cr*.22);
			obj.graphics.curveTo(cx - cr,cy - cr*.84,cx - cr*.52,cy - cr*.84);
			obj.graphics.curveTo(cx - cr*.2,cy - cr*.84,cx, cy-cr*.46);
			//画实心
			obj.graphics.endFill();
		}
		
		public function moves(e:Event):void
		{
			var fmx=0,fmy=0,mv=e.target;
			var cw:Number = (s_w-mv.width) * m_x;
			fmx = mv.x;
			mv.x=fmx + (cw - fmx)*b;
			var ch:Number = (s_h-mv.height) * m_y;
			fmy = mv.y;
			mv.y=fmy + (ch - fmy)*b;
		}
		
		public function startMove(e:MouseEvent):void
		{
			//trace('start!');
			m_x = e.stageX/s_w;
			m_y = e.stageY/s_h;
		}
		
		private function destroy():void
		{
			var cc:DisplayObject=null;
			while(numChildren>0)
			{
				cc=getChildAt(0);
				if(cc.hasEventListener(Event.ENTER_FRAME)){
					cc.removeEventListener(Event.ENTER_FRAME,moves);
				}
				removeChildAt(0);
			}
		}
		
		public function refresh(e:Event=null):void
		{
			s_w = stage.stageWidth;
			s_h = stage.stageHeight;
			destroy();
			create();
		}
	}
}