Products
Technologies

Developer resources

Determine if the mouse is over a transparent area of an image.

Avg. Rating 3.7

Problem

I have an image which is square but has transparent area on it. I need to determine if the mouse is over the transparent area of the image or over the opaque areas.

Solution

We can use the BitmapData.getPixel32 function to determine if the pixel under the mouse is transparent or not.

Detailed explanation

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
                                                        import flash.events.MouseEvent;
                        import mx.controls.Alert;
private function onOver(event:MouseEvent):void
{
var bmp:Bitmap = img.content as Bitmap;
var color:uint = bmp.bitmapData.getPixel32(event.localX, event.localY);
if (color < 0xff000000)
Alert.show("Mouse is over transparent area!");
else
Alert.show("Mouse is not over transparent area!");
}
]]>
</mx:Script>
<mx:Image id="img" source="path/to/image" width="400" height="300" mouseOver="onOver(event)" />
</mx:Application>
Report abuse

Related recipes