1k views
Check if a pixel is transparent in a DisplayObject
Very useful when detecting clicks on bitmaps with transparency.
ActionScript 3
- /**
- * Checks if a specific pixel is transparent in a <code>DisplayObject</code>
- *
- * @param point Global (stage) coordinates of the pixel to check
- *
- * @return True if the pixel is transparent
- */
- {
- // If object is a BitmapData we can use directly the hitTest function
- {
- }
- else
- {
- // If the "bounding boxes" don't collide, we can return false directly
- if(!object.hitTestPoint(point.x, point.y, true))
- {
- return false;
- }
- else
- {
- // object was not a BitmapData so we have to copy the pixels to
- // a new BitmapData and we use the hitTest function
- // This is a bit tricky because we can not assume that the
- // object regpoint is in (0, 0). So, we need to ensure we keep
- // the regpoint in the new BitmapData.
- matrix.translate(-rect.x, -rect.y); // This is what corrects the regpoint when copying pixels
- bmapData.draw(object, matrix);
- bmapData.dispose(); // Not sure if this is required...
- return isTransparent;
- }
- }
- }
Submitted by miguelSantirso about 1 year ago — last modified less than a minute ago