Last active
August 29, 2015 14:23
-
-
Save chichilatte/436f203bb25a90715680 to your computer and use it in GitHub Desktop.
Converting Starling texture to Bitmap
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
/** Note: Only tested with Starling 1.5.1 */ | |
import flash.display.Bitmap; | |
import flash.display.BitmapData; | |
import starling.core.RenderSupport; | |
import starling.core.Starling; | |
import starling.display.*; | |
import flash.geom.Rectangle; | |
// 'sprite' is a Starling sprite you want to grab the bitmap from | |
var bitmapdata:BitmapData = copyAsBitmapData(sprite); | |
var bitmap:Bitmap = new Bitmap(bitmapdata); | |
bitmap.smoothing = false; | |
/** http://forum.starling-framework.org/topic/taking-a-screenshot-and-save-it-to-the-camera-roll | |
*/ | |
function copyAsBitmapData(displayObject:starling.display.DisplayObject, transparentBackground:Boolean = true, backgroundColor:uint = 0xcccccc):BitmapData | |
{ | |
if (displayObject == null || isNaN(displayObject.width)|| isNaN(displayObject.height)) | |
return null; | |
var resultRect:Rectangle = new Rectangle(); | |
displayObject.getBounds(displayObject, resultRect); | |
var result:BitmapData = new BitmapData(displayObject.width, displayObject.height, transparentBackground, backgroundColor); | |
var context:Context3D = Starling.context; | |
var support:RenderSupport = new RenderSupport(); | |
RenderSupport.clear(); | |
support.setOrthographicProjection(0,0, Starling.current.stage.stageWidth, Starling.current.stage.stageHeight); | |
support.applyBlendMode(true); | |
support.translateMatrix( -resultRect.x, -resultRect.y); | |
support.pushMatrix(); | |
//support.pushBlendMode(); | |
support.blendMode = displayObject.blendMode; | |
displayObject.render(support, 1.0); | |
support.popMatrix(); | |
//support.popBlendMode(); | |
support.finishQuadBatch(); | |
context.drawToBitmapData(result); | |
return result; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment