Created
January 10, 2013 19:07
-
-
Save davestewart/4504834 to your computer and use it in GitHub Desktop.
Function to optimize filters of elements on stage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flash.display.DisplayObject; | |
import flash.utils.describeType; | |
function optimize(element:DisplayObject):void | |
{ | |
function getFilterType(filter:*):String | |
{ | |
var xml:XML = flash.utils.describeType(filter); | |
return String(xml.@name).replace('flash.filters::', ''); | |
} | |
if(element.filters) | |
{ | |
var props:Array = ['blurX', 'blurY']; | |
var filters:Array = []; | |
element.filters.map(function(filter, index){ | |
props.map(function(property){ | |
if(filter.hasOwnProperty(property)){ | |
var oldValue:int = filter[property]; | |
var newValue:int = Math.pow( 2, Math.round( Math.log( oldValue ) / Math.log( 2 ) ) ); | |
if(oldValue != newValue) | |
{ | |
var name = getFilterType(filter); | |
filter[property] = newValue; | |
trace('Updating "' +element.name+ ':' + name +'.' +property+ '": ' +oldValue+ ' -> ' +newValue); | |
} | |
} | |
}); | |
filters.push(filter); | |
}); | |
element.filters = filters; | |
} | |
} | |
for(var i:int = 0; i < numChildren; i++)optimize(getChildAt(i)); | |
/* | |
Updating "alien1:BlurFilter.blurX": 7 -> 8 | |
Updating "alien1:BlurFilter.blurY": 7 -> 8 | |
Updating "alien1:DropShadowFilter.blurX": 71 -> 64 | |
Updating "alien1:DropShadowFilter.blurY": 71 -> 64 | |
Updating "alien2:BlurFilter.blurX": 7 -> 8 | |
Updating "alien2:BlurFilter.blurY": 7 -> 8 | |
Updating "alien2:DropShadowFilter.blurX": 71 -> 64 | |
Updating "alien2:DropShadowFilter.blurY": 71 -> 64 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment