Skip to content

Instantly share code, notes, and snippets.

@quakeboy
Last active December 26, 2015 00:49
Show Gist options
  • Save quakeboy/7066901 to your computer and use it in GitHub Desktop.
Save quakeboy/7066901 to your computer and use it in GitHub Desktop.
Starling with AS3 Graphics class
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.CapsStyle;
import flash.display.Sprite;
import flash.filters.BitmapFilter;
import flash.filters.GlowFilter;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import starling.display.BlendMode;
import starling.display.Image;
import starling.display.Sprite;
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.events.TouchPhase;
import starling.textures.Texture;
public class StarlingScene extends starling.display.Sprite
{
private var img:Image = null;
public function StarlingScene()
{
var sprite:flash.display.Sprite = new flash.display.Sprite();
sprite.graphics.beginFill(0x003300);
sprite.graphics.lineStyle(1.0, 0x00FF00, 1, false, "normal", CapsStyle.ROUND);
sprite.graphics.drawRoundRect(0, 0, 110, 110, 5, 5);
sprite.graphics.endFill();
sprite.filters = new Array(new GlowFilter(0x00FF00, 1.0, 10, 10, 1));
var _rect:Rectangle;
if (sprite.filters)
_rect = getBoundingBox(sprite.getRect(sprite), sprite.filters);
else
_rect = sprite.getBounds(sprite);
var _matrix:Matrix = new Matrix();
_matrix.translate(-_rect.x, -_rect.y);
var ct:ColorTransform = sprite.transform.colorTransform; //copy the current colorTransform so you don't have to mess with colors
ct.alphaMultiplier = 1; //set it's alpha to .4;
var myColorTransform:ColorTransform = new ColorTransform(1, 1, 1, 1, 0, 0, 0, 0);
var _bitmapdata:BitmapData = new BitmapData(_rect.width, _rect.height, true, 0x00000000) //, false, 0x000000);
_bitmapdata.draw(sprite, _matrix);
var _texture:Texture = Texture.fromBitmapData(_bitmapdata);
var _image:Image = new Image(_texture);
//_image.blendMode = starling.display.BlendMode.NONE;
_image.x = 100; _image.y = 100;
addChild(_image);
}
/**
* * Compute the largest bounding box for a MovieClip that has several filters applied.
* * @param   pDORect
* * @param   pFilters
* * @return
* * http://www.refactored.fr/?p=163
* * Tony Broyez
* */
public static function getBoundingBox( pDORect:Rectangle, pFilters:Array ):Rectangle
{
// Get the first filter and compute his bounding box
var filter:BitmapFilter = pFilters[ 0 ];
if (filter == null ) throw new TypeError( "The filters Array must contains at least one filter" );
var b:BitmapData = new BitmapData( pDORect.width, pDORect.height, false );
var r:Rectangle  = b.generateFilterRect( pDORect, filter );
b.dispose();
 
// Helps to find the largest bounding box
var le:Number = r.left;
var to:Number = r.top;
var ri:Number = r.right;
var bo:Number = r.bottom;
// How many filters have been applied to the MovieClip ?
var ln:int = pFilters.length;
for (var i:int = 1; i < ln ; i++)
{
b              = new BitmapData( r.width, r.height, false );
filter         = pFilters[ i ];
// Find the next flash.geom.Rectangle object starting with the one we find in the previous iteration
r               = b.generateFilterRect( r, filter );
// Release the memory used.
// Notice that this BitmapData object is only necessary for computing the filtered bounding box
b.dispose();
// If the size of new Rectangle is larger that the previous one, we update the values for the bounding box
if ( r.left   < le ) le = r.left;
if ( r.top    < to ) to = r.top;
if ( r.right  > ri ) ri = r.right;
if ( r.bottom > bo ) bo = r.bottom;
}
b = null;
// Return the Rectangle object that corresponds to the largest bounding box
r = new Rectangle();
r.left   = le;
r.top    = to;
r.right  = ri;
r.bottom = bo;
return r;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment