Created
February 25, 2011 21:44
-
-
Save rafaelrinaldi/844547 to your computer and use it in GitHub Desktop.
Easy way to create a Bitmap instance based on a view.
This file contains 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
package tea.display | |
{ | |
import flash.display.Bitmap; | |
import flash.text.TextField; | |
import flash.display.Stage; | |
import flash.display.BitmapData; | |
import flash.display.BitmapDataChannel; | |
/** | |
* @param p_target Target to create Bitmap instance. | |
* @param p_removeTarget Remove target after create Bitmap instance? | |
* @return Bitmap instance drawn based on target. | |
*/ | |
public function toBitmap( p_target : *, p_removeTarget : Boolean = false ) : Bitmap | |
{ | |
var width : String = "width"; | |
var height : String = "height"; | |
var data : BitmapData; | |
if(p_target is TextField) { | |
width = "textWidth"; | |
height = "textHeight"; | |
} else if(p_target is Stage) { | |
width = "stageWidth"; | |
height = "stageHeight"; | |
} | |
if(!p_target.hasOwnProperty(width) || !p_target.hasOwnProperty(height)) return null; | |
data = new BitmapData(p_target[width], p_target[height], true, BitmapDataChannel.ALPHA); | |
data.draw(p_target, p_target.transform.matrix || null); | |
if(p_removeTarget) { | |
if(p_target.hasOwnProperty("dispose")) p_target.dispose(); | |
if(p_target.hasOwnProperty("kill")) p_target.kill(); | |
if(p_target.hasOwnProperty("clear")) p_target.clear(); | |
if(p_target.hasOwnProperty("destroy")) p_target.destroy(); | |
p_target = null; | |
} | |
return new Bitmap(data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment