?? simpledistortion.as
字號:
public function renderSides( leftPercentageDistance : Number,
topPercentageDistance : Number,
rightPercentageDistance : Number,
bottomPercentageDistance : Number ) : void
{
initDistortImage();
var corners : DisplayObjectBounds = getSimpleBounds();
renderSidesWithBounds( corners, leftPercentageDistance,
topPercentageDistance,
rightPercentageDistance,
bottomPercentageDistance );
}
public function renderSidesInPixels( leftValueDistance : Number,
topValueDistance : Number,
rightValueDistance : Number,
bottomValueDistance : Number ) : void
{
initDistortImage();
var corners : DisplayObjectBounds = getSimpleBounds();
renderSidesInPixelsWithBounds( corners,
leftValueDistance,
topValueDistance,
rightValueDistance,
bottomValueDistance );
}
public function renderCorners( topLeft : Point,
topRight : Point,
bottomRight : Point,
bottomLeft : Point ) : void
{
initDistortImage();
var corners : DisplayObjectBounds = getSimpleBounds();
renderCornersWithBounds( corners,
topLeft,
topRight,
bottomRight,
bottomLeft );
}
public function renderCornersInPixels( topLeft : Point, topRight : Point,
bottomRight : Point, bottomLeft : Point ) : void
{
initDistortImage();
setTransform( topLeft.x, topLeft.y,
topRight.x, topRight.y,
bottomRight.x, bottomRight.y,
bottomLeft.x, bottomLeft.y );
}
public function destroy( restoreTarget : Boolean = true ) : void
{
if( !isDistorted ) return;
restore( restoreTarget );
if( distort != null )
{
if( distort.texture != null ) distort.texture.dispose();
}
distort = null;
isDistorted = false;
}
protected function initialize() : void
{
if( buildMode == DistortionConstants.REPLACE )
{
findParent();
target.addEventListener( Event.ADDED, catchAddedEvent );
target.addEventListener( Event.REMOVED, catchRemovedEvent );
if( targetContainer.contains( target ) )
{
justAddChild = false;
replaceTarget( DisplayObject( target ), DisplayObject( container ) );
}
else
{
justAddChild = true;
addContainer( DisplayObject( container ) );
}
target.removeEventListener( Event.ADDED, catchAddedEvent );
target.removeEventListener( Event.REMOVED, catchRemovedEvent );
}
else if( buildMode == DistortionConstants.ADD )
{
findParent();
target.addEventListener( Event.ADDED, catchAddedEvent );
target.addEventListener( Event.REMOVED, catchRemovedEvent );
addContainer( DisplayObject( container ) );
target.removeEventListener( Event.ADDED, catchAddedEvent );
target.removeEventListener( Event.REMOVED, catchRemovedEvent );
}
else if( buildMode == DistortionConstants.OVERWRITE )
{
container = target;
}
}
protected function restore( restoreTarget : Boolean ) : void
{
if( buildMode == DistortionConstants.REPLACE )
{
if( justAddChild )
{
removeContainer( DisplayObject( container ) );
}
else
{
if( restoreTarget ) replaceTarget( container, DisplayObject( target ) );
}
}
else if( buildMode == DistortionConstants.ADD )
{
removeContainer( DisplayObject( container ) );
}
}
protected function initDistortImage() : void
{
if( distort == null )
{
isDistorted = true;
liveUpdateIntervalCounter = 0;
initialize();
getBounds();
if( buildMode != DistortionConstants.OVERWRITE )
{
container.width = offsetRect.width;
container.height = offsetRect.height;
}
container.x += offsetRect.x;
container.y += offsetRect.y;
distort = new DistortImage();
distort.smooth = smooth;
distort.container = container;
distort.target = target;
distort.initialize( 2, 2, offsetRect );
distort.render();
}
else
{
liveUpdateIntervalCounter++;
if( liveUpdate && liveUpdateIntervalCounter >= liveUpdateInterval )
{
liveUpdateIntervalCounter = 0;
distort.initialize( 2, 2, offsetRect );
distort.render();
}
}
}
protected function getBounds() : void
{
offsetRect = new SimpleDisplayObjectBoundsUtil().getBoundsForOffsetRect( target, offsetRect );
}
protected function getSimpleBounds() : DisplayObjectBounds
{
var corners : DisplayObjectBounds = new DisplayObjectBounds();
if( concatenatedMatrix == null )
{
if( isMatrixInitialized( target ) )
{
concatenatedMatrix = target.transform.concatenatedMatrix;
}
}
corners.getActualBounds( target, offsetRect, concatenatedMatrix );
return corners;
}
protected function isMatrixInitialized( target : DisplayObject ) : Boolean
{
return ( target.parent != null );
}
protected function findParent() : void
{
if( targetContainer == null )
{
if( target.parent == null )
{
throw new Error( "target " + target + " needs to have a valid parent property in buildMode " + buildMode );
}
targetContainer = target.parent;
}
}
private function replaceTarget( oldTarget : DisplayObject, newTarget : DisplayObject ) : void
{
removeContainer( oldTarget );
targetContainer.addChild( newTarget );
}
private function addContainer( container : DisplayObject ) : void
{
targetContainer.addChild( container );
}
private function removeContainer( container : DisplayObject ) : void
{
if( targetContainer.contains( container ) ) targetContainer.removeChild( container );
}
private function catchAddedEvent( event : Event ) : void
{
event.stopImmediatePropagation();
}
private function catchRemovedEvent( event : Event ) : void
{
event.stopImmediatePropagation();
}
private function renderSidesWithBounds( corners : DisplayObjectBounds,
leftPercentageDistance : Number,
topPercentageDistance : Number,
rightPercentageDistance : Number,
bottomPercentageDistance : Number ) : void
{
var leftDistance : Number = corners.bottomLeft.y - corners.topLeft.y;
var topDistance : Number = corners.topRight.x - corners.topLeft.x;
var rightDistance : Number = corners.bottomRight.y - corners.topRight.y;
var bottomDistance : Number = corners.bottomRight.x - corners.bottomLeft.x;
var leftDelta : Number = getDelta( getValue( leftPercentageDistance, leftDistance ), leftDistance );
var topDelta : Number = getDelta( getValue( topPercentageDistance, topDistance ), topDistance );
var rightDelta : Number = getDelta( getValue( rightPercentageDistance, rightDistance ), rightDistance );
var bottomDelta : Number = getDelta( getValue( bottomPercentageDistance, bottomDistance ), bottomDistance );
setTransform( corners.topLeft.x + topDelta, corners.topLeft.y + leftDelta,
corners.topRight.x - topDelta, corners.topRight.y + rightDelta,
corners.bottomRight.x - bottomDelta, corners.bottomRight.y - rightDelta,
corners.bottomLeft.x + bottomDelta, corners.bottomLeft.y - leftDelta );
}
private function renderSidesInPixelsWithBounds( corners : DisplayObjectBounds,
leftValueDistance : Number,
topValueDistance : Number,
rightValueDistance : Number,
bottomValueDistance : Number ) : void
{
var leftDistance : Number = corners.bottomLeft.y - corners.topLeft.y;
var topDistance : Number = corners.topRight.x - corners.topLeft.x;
var rightDistance : Number = corners.bottomRight.y - corners.topRight.y;
var bottomDistance : Number = corners.bottomRight.x - corners.bottomLeft.x;
var leftDelta : Number = getDelta( leftValueDistance, leftDistance );
var topDelta : Number = getDelta( topValueDistance, topDistance );
var rightDelta : Number = getDelta( rightValueDistance, rightDistance );
var bottomDelta : Number = getDelta( bottomValueDistance, bottomDistance );
setTransform( corners.topLeft.x + topDelta, corners.topLeft.y + leftDelta,
corners.topRight.x - topDelta, corners.topRight.y + rightDelta,
corners.bottomRight.x - bottomDelta, corners.bottomRight.y - rightDelta,
corners.bottomLeft.x + bottomDelta, corners.bottomLeft.y - leftDelta );
}
private function renderCornersWithBounds( corners : DisplayObjectBounds,
topLeft : Point,
topRight : Point,
bottomRight : Point,
bottomLeft : Point ) : void
{
var leftDistance : Number = corners.bottomLeft.y - corners.topLeft.y;
var topDistance : Number = corners.topRight.x - corners.topLeft.x;
var rightDistance : Number = corners.bottomRight.y - corners.topRight.y;
var bottomDistance : Number = corners.bottomRight.x - corners.bottomLeft.x;
var topLeftX : Number = getValue( topLeft.x, topDistance );
var topLeftY : Number = getValue( topLeft.y, leftDistance );
var topRightX : Number = getValue( topRight.x, topDistance );
var topRightY : Number = getValue( topRight.y, rightDistance );
var bottomRightX : Number = getValue( bottomRight.x, bottomDistance );
var bottomRightY : Number = getValue( bottomRight.y, rightDistance );
var bottomLeftX : Number = getValue( bottomLeft.x, bottomDistance );
var bottomLeftY : Number = getValue( bottomLeft.y, leftDistance );
setTransform( topDistance - topLeftX, leftDistance - topLeftY,
topRightX, rightDistance - topRightY,
bottomRightX, bottomRightY,
bottomDistance - bottomLeftX, bottomLeftY );
}
private function setTransform( topLeftX : Number, topLeftY : Number,
topRightX : Number, topRightY : Number,
bottomRightX : Number, bottomRightY : Number,
bottomLeftX : Number, bottomLeftY : Number ) : void
{
this.topLeftX = topLeftX;
this.topLeftY = topLeftY;
this.topRightX = topRightX;
this.topRightY = topRightY;
this.bottomRightX = bottomRightX;
this.bottomRightY = bottomRightY;
this.bottomLeftX = bottomLeftX;
this.bottomLeftY = bottomLeftY;
distort.setTransform(
topLeftX, topLeftY,
topRightX, topRightY,
bottomRightX, bottomRightY,
bottomLeftX, bottomLeftY );
}
private function getDistortion( distortion : Number ) : Number
{
if( isNaN( distortion ) ) distortion = 20;
return distortion;
}
private function getDistortedPercentage( percentage : Number, distortion : Number ) : Number
{
return 100 - ( percentage / 100 * distortion );
}
private function getInversedDistortedPercentage( percentage : Number, distortion : Number ) : Number
{
return 100 - ( distortion - ( percentage / 100 * distortion ) );
}
private function getDelta( value : Number, total : Number ) : Number
{
var delta : Number = ( total - value ) / 2;
return delta;
}
private function getValue( percentage : Number, total : Number ) : Number
{
var value : Number = percentage / 100 * total;
return value;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -