Hmm constructing an array of vectors from a bitmap sounds intensive.
Here is how I solved the problem.
Application:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:cmps="Components.*" layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
[Embed(source="data/timezones.png")]
internal var timezones:Class;
internal function clicked(event:MouseEvent):void
{
if(timeZoneImage.hitAlphaPoint(event.localX,
event.localY))
{
Alert.show("Clicked");
}
}
]]>
</mx:Script>
<cmps:CroppedImage id="timeZoneImage" source="{timezones}"
width="100%" height="100%" click="clicked(event)"/>
</mx:Application>
CroppedImage.xml (bad name I know):
<?xml version="1.0" encoding="utf-8"?>
<mx:Image xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="initBuffer()" resize="initBuffer()">
<mx:Script>
<![CDATA[
internal var pixelBuffer:BitmapData;
public function hitAlphaPoint(x:Number, y:Number):Boolean
{
if(pixelBuffer)
{
pixelBuffer.draw(this);
var hitPoint:Point = this.globalToContent(new
Point(x, y));
var pixel:uint =
pixelBuffer.getPixel32(hitPoint.x, hitPoint.y);
var alphaChannel:uint = (pixel >> 24 & 0xFF);
var alpha:Boolean = Boolean(alphaChannel);
return alpha;
}
return true;
}
internal function initBuffer():void
{
if(!pixelBuffer || pixelBuffer.width != this.width ||
pixelBuffer.height != this.height)
{
pixelBuffer = new BitmapData(this.width,
this.height, true,
0x00000000);
}
}
]]>
</mx:Script>
</mx:Image>
Seems to support resizing. Basically I create a BitmapData object,
and have the image draw itself onto the BitmapData. I get the pixel at
the x,y location. I check the alpha value and return the result.
Unfortunately I couldn't figure out a way to do this before the event
handler, so each handler is going to have to call the hitAlphaPoint
and act on the result.
Mike Power
--- In [email protected], "Alex Harui" <[EMAIL PROTECTED]> wrote:
>
> Flash Authoring is a huge application and part of its size is due to a
> plethora of useful bitmap manipulation tools. There aren't any built
> into Flex, maybe some third-parties have one.
>
> You will need to describe in vectors and curves (preferably just
> vectors) the outer border of the image.
>
> ________________________________
>
> From: [email protected] [mailto:[EMAIL PROTECTED] On
> Behalf Of dodtsair
> Sent: Tuesday, October 02, 2007 10:19 AM
> To: [email protected]
> Subject: [flexcoders] Re: actionscript generation of mask/hitArea from
> bitmapdata...
>
>
>
> --- In [email protected] <mailto:flexcoders%40yahoogroups.com>
> , "unaflux" <unaflux@> wrote:
> >
> > How can I create an arbitrarily complex Shape
> > starting from alphachannel data of a dynamically loaded image?
> >
> > I'd like to use the shape mainly for two things:
> > - to set a complex hitArea of a Sprite
> > - to mask a DisplayObject that has MouseEvents on it
> >
> > The problem are somehow related:
> > 1) If I apply directly the loaded image
> > (wrapped in a Sprite) as the hitArea
> > mousevents gets triggered in the rect area
> > that has the dimension of the loaded image.
> >
> > 2) If I apply directly the loaded image as a mask
> > I've no problems to obtain the visual masking effect
> > but mousevents still gets triggered in the rect area
> > that has the dimension of the loaded image, not only
> > on the visible areas...
> >
> >
> >
> > In the authoring environment solution is simple...
> > - take the image
> > - do a trace bitmap
> > - use the traced bitmap non transparent areas
> > as hitArea in a SimpleButton.
> >
> > In Actionscript, if the masking/hitArea Shape is simple...
> > - draw the masking/hitArea Shape
> > - apply it as a mask
> >
> >
> > In ActionScript, if the masking/hitArea is complex
> > it starts becoming a problem.
> > I think that's because I'm still working
> > with the BitmapData of the image (that is rectangular).
> > Even in the authoring environment if you use a Bitmap,
> > instead of a Shape, you get a rectangular mouseSensible area.
> > But how can I obtain a Shape from the alphachannel data
> > of the image I loaded?
> > The idea is to use this arbitrarily complex Shape
> > (wrapped in a Sprite) as a dynamically generated mask/hitArea,
> > that gives me an arbitrarily complex mouseSensible area.
> >
> > Have you ever tried something like that?
> > Is that even possible?
> > Using Actionscript shouldn't I have access to all that
> > could be done in the authoring environment?
> > Stated in another way...
> > how can I actually do something similar to the
> > trace bitmap operation...
> >
> > Any suggestions
> > Thanks
> > Jo
> >
>
> I am trying to do the same thing. I wanted to build a widget that
> would allow the user to click on a world map to select his timezone.
>
> At this point like you I have failed to use the hitArea to filter the
> mouse events. The next thing I will try is grabing the x,y of the
> mouse event and trying to pull out the pixel data of the image at that
> point. We'll see if I succeed.
>
> If anyone knows a better more straight forward solution I think we
> would both appreciate it.
>
> Mike Power
>