simpledistortion.as

来自「Flex三维特效 examples - see DistortionExamp」· AS 代码 · 共 699 行 · 第 1/2 页

AS
699
字号
package com.adobe.ac.mxeffects
{
	import com.adobe.ac.util.DisplayObjectBounds;
	import com.adobe.ac.util.SimpleDisplayObjectBoundsUtil;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import sandy.util.DistortImage;
	import flash.geom.Matrix;
	
	public class SimpleDistortion
	{
		public var buildMode : String;			
		public var smooth : Boolean;
		public var offsetRect : Rectangle;
		public var target : DisplayObject;
		public var container : DisplayObject;
		public var liveUpdate : Boolean = false;
		public var liveUpdateInterval : int = 0;
		public var isDistorted : Boolean = false;
		public var targetContainer : DisplayObjectContainer;
		public var concatenatedMatrix : Matrix;
		
		public var topLeftX : Number;
		public var topLeftY : Number;
		public var topRightX : Number;
		public var topRightY : Number;		
		public var bottomRightX : Number;
		public var bottomRightY : Number;
		public var bottomLeftX : Number;
		public var bottomLeftY : Number;
		
		private var liveUpdateIntervalCounter : int;
		private var distort : DistortImage;
		private var justAddChild : Boolean;
		
		public function SimpleDistortion( target : DisplayObject, offsetRect : Rectangle = null )
		{
			buildMode = DistortionConstants.REPLACE;
			smooth = false;
			this.target = target;			
			this.offsetRect = offsetRect;
			container = new Shape();
		}
		
		public function flipFront( percentage : Number, direction : String, 
											distortion : Number = NaN, exceedBounds : Boolean = false ) : void
		{
			distortion = getDistortion( distortion );
			
			var leftPercentageDistance : Number;
			var topPercentageDistance : Number;
			var rightPercentageDistance : Number;
			var bottomPercentageDistance : Number;
			
			var inversedPercentage : Number = 100 - percentage;
			var distortionPercentage : Number = getDistortedPercentage( percentage, distortion );
			var inversedDistortionPercentage : Number = 100 - distortionPercentage;
			
			if( direction == DistortionConstants.LEFT )
			{
				leftPercentageDistance = distortionPercentage;
				topPercentageDistance = inversedPercentage;
				rightPercentageDistance = ( exceedBounds ) ? 100 + inversedDistortionPercentage : 100;
				bottomPercentageDistance = inversedPercentage;
			}
			else if( direction == DistortionConstants.TOP )
			{
				leftPercentageDistance = inversedPercentage;
				topPercentageDistance = distortionPercentage;
				rightPercentageDistance = inversedPercentage;
				bottomPercentageDistance = ( exceedBounds ) ? 100 + inversedDistortionPercentage : 100;
			}
			else if( direction == DistortionConstants.RIGHT )
			{
				leftPercentageDistance = ( exceedBounds ) ? 100 + inversedDistortionPercentage : 100;
				topPercentageDistance = inversedPercentage;
				rightPercentageDistance = distortionPercentage;
				bottomPercentageDistance = inversedPercentage;				
			}			
			else if( direction == DistortionConstants.BOTTOM )
			{
				leftPercentageDistance = inversedPercentage;
				topPercentageDistance = ( exceedBounds ) ? 100 + inversedDistortionPercentage : 100;
				rightPercentageDistance = inversedPercentage;
				bottomPercentageDistance = distortionPercentage;
			}
			else
			{
				throw new Error( "Invalid direction " + direction );
			}
			renderSides( leftPercentageDistance, topPercentageDistance, 
							rightPercentageDistance, bottomPercentageDistance );
		}
		
		public function flipBack( percentage : Number, direction : String, 
										distortion : Number = NaN, exceedBounds : Boolean = false ) : void
		{
			var reversedDirection : String = reverseDirection( direction );
			flipFront( 100 - percentage, reversedDirection, distortion, exceedBounds );
		}
		
		public function reverseDirection( direction : String ) : String
		{
			var reversedDirection : String;
			if( direction == DistortionConstants.LEFT )
			{
				reversedDirection = DistortionConstants.RIGHT;
			}
			else if( direction == DistortionConstants.TOP )
			{
				reversedDirection = DistortionConstants.BOTTOM;
			}
			else if( direction == DistortionConstants.RIGHT )
			{
				reversedDirection = DistortionConstants.LEFT;		
			}
			else if( direction == DistortionConstants.BOTTOM )
			{
				reversedDirection = DistortionConstants.TOP;
			}
			else
			{
				throw new Error( "Invalid direction " + direction );
			}
			return reversedDirection;
		}
			
		public function push( percentage : Number, direction : String, distortion : Number = NaN ) : void
		{
			distortion = getDistortion( distortion );
			
			var topLeft : Point;
			var topRight : Point;
			var bottomRight : Point;
			var bottomLeft : Point;
			var inversedDistortionPercentage : Number = getInversedDistortedPercentage( percentage, distortion );
			if( direction == DistortionConstants.LEFT )
			{
				topLeft = new Point( percentage, 100 );
				topRight = new Point( 100, inversedDistortionPercentage );
				bottomRight = new Point( 100, inversedDistortionPercentage );
				bottomLeft = new Point( percentage, 100 );
			}
			else if( direction == DistortionConstants.TOP )
			{
				topLeft = new Point( 100, percentage );
				topRight = new Point( 100, percentage );
				bottomRight = new Point(inversedDistortionPercentage, 100 );
				bottomLeft = new Point( inversedDistortionPercentage, 100 );
			}
			else if( direction == DistortionConstants.RIGHT )
			{
				topLeft = new Point( 100, inversedDistortionPercentage );
				topRight = new Point( percentage, 100 );
				bottomRight = new Point( percentage, 100 );
				bottomLeft = new Point( 100, inversedDistortionPercentage );
			}
			else if( direction == DistortionConstants.BOTTOM )
			{
				topLeft = new Point( inversedDistortionPercentage, 100 );
				topRight = new Point( inversedDistortionPercentage, 100 );
				bottomRight = new Point( 100, percentage );
				bottomLeft = new Point( 100, percentage );
			}
			else
			{
				throw new Error( "Invalid direction " + direction );
			}
			renderCorners( topLeft, topRight, bottomRight, bottomLeft );
		}
		
		public function pop( percentage : Number, direction : String, distortion : Number = NaN ) : void
		{
			distortion = getDistortion( distortion );
			
			var topLeft : Point;
			var topRight : Point;
			var bottomRight : Point;
			var bottomLeft : Point;
			
			var inversedPercentage : Number = 100 - percentage;
			var distortionPercentage : Number = getDistortedPercentage( percentage, distortion );
			if( direction == DistortionConstants.LEFT )
			{
				topLeft = new Point( 100, distortionPercentage );
				topRight = new Point( inversedPercentage, 100 );
				bottomRight = new Point( inversedPercentage, 100 );
				bottomLeft = new Point( 100, distortionPercentage );
			}
			else if( direction == DistortionConstants.TOP )
			{
				topLeft = new Point( distortionPercentage, 100 );
				topRight = new Point( distortionPercentage, 100 );
				bottomRight = new Point( 100, inversedPercentage );
				bottomLeft = new Point( 100, inversedPercentage );
			}
			else if( direction == DistortionConstants.RIGHT )
			{
				topLeft = new Point( inversedPercentage, 100 );
				topRight = new Point( 100, distortionPercentage );
				bottomRight = new Point( 100, distortionPercentage );
				bottomLeft = new Point( inversedPercentage, 100 );
			}
			else if( direction == DistortionConstants.BOTTOM )
			{
				topLeft = new Point( 100, inversedPercentage );
				topRight = new Point( 100, inversedPercentage );
				bottomRight = new Point( distortionPercentage, 100 );
				bottomLeft = new Point( distortionPercentage, 100 );
			}
			else
			{
				throw new Error( "Invalid direction " + direction );
			}
			renderCorners( topLeft, topRight, bottomRight, bottomLeft );
		}
		
		public function popUp( percentage : Number, direction : String, distortion : Number = NaN ) : void
		{			
			distortion = getDistortion( distortion );
			distortion *= 4;
			
			var topLeft : Point;
			var topRight : Point;
			var bottomRight : Point;
			var bottomLeft : Point;
			
			var inversedPercentage : Number = 100 - percentage;			
			var distortionPercentage : Number = getDistortedPercentage( percentage, distortion );						
			
			container.alpha = inversedPercentage / 100;
			
			var doubledDistortionPercentage : Number = getDoubledDistortedPercentage( percentage, distortion );
			var expandedDistortion : Number = 100 + ( 100 - doubledDistortionPercentage );
			
			var halfedDistortionPercentage : Number = getHalfedDistortedPercentage( percentage, distortion );
			var expandedSlowerDistortion : Number = 100 + ( 100 - halfedDistortionPercentage );			
			
			var expandedYDistortion : Number = getExpandedYDistortion( percentage );
			
			if( direction == DistortionConstants.LEFT )
			{
				topLeft = new Point( expandedSlowerDistortion, expandedSlowerDistortion );
				topRight = new Point( expandedYDistortion, expandedDistortion );
				bottomRight = new Point( expandedYDistortion, expandedDistortion );
				bottomLeft = new Point( expandedSlowerDistortion, expandedSlowerDistortion );
			}
			else if( direction == DistortionConstants.TOP )
			{
				topLeft = new Point( expandedSlowerDistortion, expandedSlowerDistortion );
				topRight = new Point( expandedSlowerDistortion, expandedSlowerDistortion );
				bottomRight = new Point( expandedDistortion, expandedYDistortion );
				bottomLeft = new Point( expandedDistortion, expandedYDistortion );
			}
			else if( direction == DistortionConstants.RIGHT )
			{
				topLeft = new Point( expandedYDistortion, expandedDistortion );
				topRight = new Point( expandedSlowerDistortion, expandedSlowerDistortion );
				bottomRight = new Point( expandedSlowerDistortion, expandedSlowerDistortion );
				bottomLeft = new Point( expandedYDistortion, expandedDistortion );
			}
			else if( direction == DistortionConstants.BOTTOM )
			{
				topLeft = new Point( expandedDistortion, expandedYDistortion );
				topRight = new Point( expandedDistortion, expandedYDistortion );
				bottomRight = new Point( expandedSlowerDistortion, expandedSlowerDistortion );
				bottomLeft = new Point( expandedSlowerDistortion, expandedSlowerDistortion );
			}
			else
			{
				throw new Error( "Invalid direction " + direction );
			}
			renderCorners( topLeft, topRight, bottomRight, bottomLeft );
		}
		
		private function getExpandedYDistortion( percentage : Number ) : Number
		{
			var degrees : Number = percentage / 100 * 225;
			var radians : Number = degrees * Math.PI/180
			var expandedYDistortion : Number = ( Math.sin( radians * .75 ) * 100 );
			expandedYDistortion += 100;
			return expandedYDistortion;
		}
		
		private function getDoubledDistortedPercentage( percentage : Number, distortion : Number ) : Number
		{
			return 100 - ( percentage / 100 * ( distortion * 1.5 ) );
		}		
		
		private function getHalfedDistortedPercentage( percentage : Number, distortion : Number ) : Number
		{
			return 100 - ( percentage / 100 * ( distortion / 2 ) );
		}				
		
		public function openDoor( percentage : Number, direction : String, distortion : Number = NaN ) : void
		{
			distortion = getDistortion( distortion );
			
			var topLeft : Point;
			var topRight : Point;
			var bottomRight : Point;
			var bottomLeft : Point;
			
			var inversedPercentage : Number = 100 - percentage;
			var distortionPercentage : Number = getDistortedPercentage( percentage, distortion );
			if( direction == DistortionConstants.LEFT )
			{
				topLeft = new Point( 100, 100 );
				topRight = new Point( inversedPercentage, distortionPercentage );
				bottomRight = new Point( inversedPercentage, distortionPercentage );
				bottomLeft = new Point( 100, 100 );
			}
			else if( direction == DistortionConstants.TOP )
			{
				topLeft = new Point( 100 , 100 );
				topRight = new Point( 100 , 100 );
				bottomRight = new Point( distortionPercentage, inversedPercentage );
				bottomLeft = new Point( distortionPercentage, inversedPercentage );
			}
			else if( direction == DistortionConstants.RIGHT )
			{
				topLeft = new Point( inversedPercentage, distortionPercentage );
				topRight = new Point( 100, 100 );
				bottomRight = new Point( 100, 100 );
				bottomLeft = new Point( inversedPercentage, distortionPercentage );
			}
			else if( direction == DistortionConstants.BOTTOM )
			{
				topLeft = new Point( distortionPercentage, inversedPercentage );
				topRight = new Point( distortionPercentage, inversedPercentage );
				bottomRight = new Point( 100, 100 );
				bottomLeft = new Point( 100, 100 );
			}
			else
			{
				throw new Error( "Invalid direction " + direction );
			}
			renderCorners( topLeft, topRight, bottomRight, bottomLeft );
		}
		
		public function closeDoor( percentage : Number, direction : String, distortion : Number = NaN ) : void
		{
			openDoor( 100 - percentage, direction, distortion );
		}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?