Friday, July 4, 2008

Flash Player: Text Around A Circle

I received a rare and very welcome comment about my most recent postings about the new Flash Player 10 (FP10) and my 1st demo, FontWheels.  In his comment, OLMSTJ, mentioned he was currently working on a project where he was placing text around a circle and therefore curious how I approached doing it in my demo.

Unlike my previous published code, for example my sort function for XML, this code is not at the point where it is generic.  It was actually more generic when I first built it for Flash Player 9 (FP9) when I was rotating text first converted to bitmaps.

 So I’m going to release the FP9 code, a single static function wrapped in a simple demo.  The framework (oh, I hate that word) for the old and new functions are similar but there are differences in how I place each character.  If at some point, I reduce the FP10 code to a similar scope, I’ll probably publish that code.  The FP10 code has a lot more functionality due to the demo’s requirement to stuff an entire circle with different font fields and other minor details that wouldn’t be needed outside the demo.

I did stripped a little code from the FP9 function.  It converted the complex Sprite to a single centered bitmap in a Sprite.  Each text character is implemented with a Bitmap inside its own Sprite.  Since I tend to make my pages very detailed, I like to strip away the complexity by converting content to a Bitmap whenever I can.  After reviewing the code, due to writing this blog, I decided building the conversion to a centered Bitmap in a Sprite into this static function was not a good design.

Code Narrative

The function has 3 arguments: 1) the radius; 2) a TextField containing the desired text and the formatting information; and 3) a Boolean where true means to place the text centered on the top and false means to center on the bottom.

I use a TextField object to specify the input data.  I wanted to be able vary the format of each character.  Therefore I build the multi-formatted text outside of the function.  However it turns out the results of doing this are varied.  It is somewhat dependent of the actual font.  For example changing the 1st character font size or setting it to bold sometimes does not look that great.  Of course, it might not look that good when displayed in a straight line either.  On the other hand changing the color works well (despite potential bad taste).

Don’t set the background color unless you are looking for some special effect.  The underline I created in my link to this blog, in the picture to the right, was drawn by hand.  This was a feature I tacked on at the end which contributes to the process not being as generic as I would like.  In order to know where to begin and end the underline circle you need internal information that is known at the time it was drawn.

As I said providing the underlined circular text was a last minute requirement for the FP10 project.  I needed a way to change the color when the mouse hovers over the link text.  I chose to not convert the text and underline to bitmapData, but instead I left it as a complex sprite.  I drill down to each TextField and change each of their colors.  I also redraw the underline to change the color.

Now that I’m done, I’ve had a chance to 2nd guess the approach.  My feelings at the moment would be to convert it to bitmapData and use its class functions to change the color.  This, of course, conflicts with the ability to restore different colors on the each character when the mouse rolls off the link.  A link shouldn’t be multi-colored but it just shows the complexity when you try to make one solution for all (like frameworks end up doing).

I hope this much detail in the design is useful to someone and doesn’t put everyone else to sleep.

The first thing I do is convert the input TextField to a bitmapData object.  I will use this object to extract each character into a bitmap.

Next I compute how many degrees the text will consume.  Using that number I figure where I need to start placing the text based on the fact that I want the text centered at the top or bottom.

Then, I loop through each character of the argument TextField.  I ignore spaces.  For each character, I create a Sprite and add a Bitmap containing that character to that Sprite.  Now the code is different depending on whether you want characters at the top or bottom.  Notice that the when the characters are at the top you are placing the bottom of the character around the circumference of the circle.  Then look at the pictures above and notice my name at the bottom of the circle.  Here, the top of the each character is drawn around circumference of the circle.  It took a while to get my head straight on this concept.

Each character is then extracted into a new little bitmap.  These bitmaps are each inserted into their own Sprite.  I rotate each of these Sprites to the correct location.  I compute this location by getting the location from the input argument TextField.  Earlier I computed the number of degrees the whole field needs so that I could compute where to put the 1st character after centering.  I took the easy way out and simply apply the same fractional character positioning of the flat text field and apply that to the total number of degrees the field consumes on the curve to find the rotation to apply to the sprite.  I’m sure most of you have taken math more recently than the 60’s and would have done it a different way, but this way I only had to look up how to compute the circumference of a circle.

But first where do you put the Bitmap character in the Sprite.  Imagine each Sprite to be like a spoke in a wheel with a character attached to the end and the other end at the center of the wheel.  As I mentioned earlier it depends on whether you are drawing the text around the top or on the bottom.  If on the top, the spoke connects to the bottom of the character and to the top of the character if the text goes on the bottom.

There is a short coming in using the spoke in a wheel to describe this.  The hardest part of writing this code was placing the text on the bottom of the circle.  I’m sure I won’t explain this properly but if you need to dive into the code maybe this will give you a head start.  I kept trying to calculate the rotation of the Sprite’s when the text is on the bottom by computing the rotation at the bottom.  But that is wrong.

Imagine the spoke for characters at the bottom to extend across the entire wheel instead of just to the center like the spokes for the top.  They still rotate at the center of the wheel but you control them by moving it at the top which is at the opposite end of where the Bitmap character is.  When you rotate the spoke at the top to the right the Bitmap at the bottom moves to the left.

Obviously, you can tell I’m not a mathematician by describing it that way.  But that’s they way I figured it out.

That’s basically it.  It isn’t a large function.

Friday, June 27, 2008

More on: Flash Player 10 Beta Development Without Installing Anything


I have now built an HTML page containing my FontWheels Flash Player 10 Beta Demo which is only useful if you have installed Flash Player 10 beta.

However, to summarize my previous post, you may view Flash Player 10 Beta content without installing anything if you fear impacting your system with beta installables.  This can be done by downloading the external Flash Player 10 beta: FlashPlayer.exe.

This is not an install executable.  It is the stand-alone external player and requires no installation.  To view content on the player, execute FlashPlayer.exe and enter the URL from the File/Open menu.  The URL to my swf demo is:

Thursday, June 26, 2008

Flash Player 10 Beta Development Without Installing Anything

I wrote some ActionScript 3 code to try some of the new features in the Adobe Flash Player 10 beta and became so excited about it I can't put it down.

I have a demo, FontWheels, with some explanation below but first let me tell you how I build the swf.

I had some problems getting started and still haven't work out some basic things. Fortunately, by accident, I found a way to do beta develop without having to install anything on my Windows XP machine.  At least to the best of my knowledge I didn't install anything and I can still use my Flex Builder 2 and Flash Debugger 9 with no conflicts.

I initially tried following the instructions from Adobe about installing Flash Player 10 and the SDK. I was able to get it installed and ran through their examples and tutorials just fine. However I wasn't able to get it to work with Flex Builder 2 and then I started having problems trying to resume developing for Flash Player 9 with the debugger.

I ended up removing everything (except the new SDK) and reinstalling Flex Builder 2 with the current debug player. I'm not trying to discourage anyone from doing this. I'm sure I goofed somewhere.

But I thought I'd give it one more shot and by accident came up with something acceptable and as I said nothing needs to be installed.

I ended up using the FlashDevelop version for FP10 from which for some reason is distributed as a compressed file with the rar extension. This gave me some problems coming up with something that worked with rar files instead of zip. However I eventually decompressed it. I created a shortcut on my desktop to the FlashDevelop.exe in the decompressed files and double clicked on the shortcut. Sure enough, FlashDevelop ran without any install.

However I still need Flash Player 10 but found out I could use the external Flash Player 10 beta instead of the versions for embedding into a browser. In the support page for FP10 at there is a link to the standalone content debugger ("External player"):

The external player requires no installation. (Note this is for Windows.)

Next in the FlashDevelop Project\Properties menu I had to change the Target to Flash Player 10 and Test Player to “Play in external player”.

Now I'm not sure how FlashDevelop knew where to find the standalone Flash Player and I'm not sure how it found the Flash Player 10 SDK. I was unable to follow their instructions to set up the SDK path but it ran anyway. I previously placed the SDK on my C: drive as C:\FLEX_SDK\ and edited C:\FLEX_SDK\frameworks\flex-config.xml according to the alternative directions from Adobe as follows:













FlashDevelop works quite well with the external player and handles the trace command just fine.  I had been using FlashDevelop until FlexBuilder came along but it has changed a lot since then.  It handles the new vector syntax of Flash 10.  The behavior of the code completion is different from Flex Builder.  I think Flex Builder is smoother but FlexDevelop tries to do more with code completion.  It seems to fail to “code complete” more frequently than Flex Builder and I find it mildly annoying because I have to use the escape key to get out of the code completion mode much too often.  In all fairness I haven't tried much customization to get rid of the annoyances and overall I like FlexDevelop.  It has a bunch of useful feature many of which I haven't mastered.  If I ever figure out how to make a living out of this I definitely plan on donating something for this product.

So here is my demo but you might have go through some hoops to see it.  Since I am using the external Flash 10 Player I don't have a way of testing a SWF within a web page. So if you want to see my demo, download the External Play Player 10 from the link mentioned above. Then in the menu File open enter the following:

Of course, if you are able to view Flash 10 content from your browser, you should be able to view the swf file by entering the above URL.  I’ve never seen it that way so I can only hope it runs.


FontWheels, the Flash 10 Beta Demo

I was working on  an ActionScript 3  for Player 9 project where I wanted to display characters around a circle dynamically.  I don’t like having to embed fonts so I thought I would see how bad it would be to first convert characters to a bitmap and then rotate them.  The result was far from perfect but since the size of the characters were fairly small it didn’t look as bad as I expected.  Certain letters, like “W” looked pretty bad but I decided I could live with it.  Since I first heard about Flash Player 10 beta while I was working on rotating the fonts I used that code as the starting place for testing the new player.

Using the exact code, that is first converting to bitmaps and the rotating, there was amazing improvement.  This wasn’t something I was anticipating.  I hadn’t read anything about the new player improving the results of rotating a bitmap, but there is no doubt it is a lot better.

Well, that created a lot of interest on my part so I decided to try rotating a TextField instead of a bitmap but still not embedding a font.  We let me tell you it looks a whole lot better than the bitmap.

Next I wondered what would it look like if I rotated the characters around the circle live.  My initial app was to build a static medallion with characters around a curve but I couldn’t resist seeing them rotate on the screen.  Then I wondered if I could do this will all of my device fonts.  Yes.  The player could handle displaying all of my approximately 110 device fonts on the screen rotating simultaneously.  Later on after trying it on my granddaughter’s older computer I decide it would be best to limit the total number of fonts.

Then I wanted to play with the 3D  capability.  I discovered that rotating each letter on another axis and the rotating the font wheel on another axis I could get what appears to be a circular marquee.

So there it is.  I hope you take the time to see it run.  I’ve rush this post out because I’m anxious to get back and try some other new things.  I hope I’m not leaving in too many misspellings and grammar errors but I’m in a hurry!