18 June 2010

Resize a Movie Clip containing a mask by getting dimensions of visible bounds/area and compensating width/height

This was a head wrecker and took me most of the day today. Actionscript does not see Movie Clips as we do and considers invisible content to be part of the MC. As such, when you try to resize an MC that contains masked content which extends beyond what we consider to be the edges of the MovieClip, flash makes a balls of it.

To counteract this I've written and rewritten a double function which resizes your clip based on the visible area rather than what flash considers the bounds. Here it comes...


//Returns width or height you need to use to resize an MC containing a mask.
//include visibleOrigSize if you know it to cut down on processing power
function maskCompensate(mc:MovieClip, prop:String, desiredSize:Number, visibleOrigSize:Number){
 var oX=mc._xscale;
 var oY=mc._yscale;
 mc._xscale=mc._yscale=100
 if(visibleOrigSize==undefined){
  var visibleOrigSize=(prop=="_width" ? getVisibleBounds(mc).width : getVisibleBounds(mc).height)
 }
 var diff = mc[prop]-visibleOrigSize
 var percentIncrease = desiredSize/visibleOrigSize
 mc._xscale=oX
 mc._yscale=oY
 return(desiredSize+diff*percentIncrease)
}
import flash.display.BitmapData;
function getVisibleBounds(mc){
 var myBitmapData:BitmapData = new BitmapData(mc._width,mc._height);
 myBitmapData.draw(mc);
 bounds = myBitmapData.getColorBoundsRect( 0xFFFFFFFF, 0xFFFFFFFF, false );
 bitmapData.dispose();
 return(bounds)
}

//sample usage:
this._width = maskCompensate(this,"_width",500) 
this._height = maskCompensate(this,"_height",300) 


No comments:

Post a Comment

Cron Design Studio: Dublin based web design & software development