Skip to content

Instantly share code, notes, and snippets.

@th3d0g
Created November 18, 2014 00:11
Show Gist options
  • Select an option

  • Save th3d0g/43350eca4dde1108e567 to your computer and use it in GitHub Desktop.

Select an option

Save th3d0g/43350eca4dde1108e567 to your computer and use it in GitHub Desktop.
Flambe - Alpha Hit Test
// Checks if a point on a texture hits a pixel with 0 alpha
// @th3d0g
// ImageUtils.hx
import flambe.display.Texture;
import haxe.io.Bytes;
import haxe.io.BytesData;
class ImageUtils
{
public static function hitTestAlpha( x:Float, y:Float, texture:Texture ):Bool {
var bytes:Bytes = texture.readPixels( Math.round( x ), Math.round( y ), 1, 1 );
var bytesData:BytesData = bytes.getData();
var colourHex:String = bytes.toHex();
var colourInt:Int = Std.parseInt( "0x" + colourHex );
var hasHitAlpha:Bool = ( colourInt >> 24 == 0 );
return hasHitAlpha;
}
}
// Example
imageSprite.pointerDown.connect(function(event : PointerEvent){
var imageSprite:ImageSprite = cast( event.hit, ImageSprite );
var hasHitAlpha = ImageUtils.hitTestAlpha( event.viewX - imageSprite.x._,
event.viewY - imageSprite.y._,
imageSprite.texture );
trace( hasHitAlpha );
});
@th3d0g

th3d0g commented Nov 19, 2014

Copy link
Copy Markdown
Author

Needs some optimisation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment