Close and Go BackBack to Viget

Creating Unique Shapes Using Actionscript

Erik Olson
Erik Olson, ON THE TOPIC OF General
Aug18 8

Every so often, projects come along that are so unique that it's difficult to find others like them on the net. When building a project, take a video player for example; I often search for others like it to see how other developers have handled the same problems and make any attempt to add improvements wherever possible.

Such a project came along recently which needed to give users the ability to create their own floor plans. Since floor plans come in infinite shapes and sizes, I needed to give every user almost limitless control over their shapes. How does one go about doing this?

My solution is to start with a square and let the users edit from there. The user is initially presented with a square shape. In each corner is a dragable indicator that enables the user to expand the shape to their desired size. Drag the black indicators below to see it in action:

Please upgrade your Flash player.

The way I accomplished this was to dynamically place the indicators on the stage and create an array of their instances. Then I draw a shape based on their x and y positions. As the indicators move, the shape redraws based on the indicators' new locations. The shape will keep redrawing as long as the indicator is moving.

Done? Nope. Unless you grew up in a shoe box you know that floor layouts vary like snowflakes. The next challenge was to add the ability to add new walls and angles -- in effect, to enable the user to click on the area between the walls to split them up to create new angles. I achieved this by drawing a line from each indicator, which enabled clicking.

Please upgrade your Flash player.

When clicking on a wall, a new indicator was added to the floor plan. This was done on the backend by inserting the instance of the indicator into the array. The first indicator is on the top left and the second is on the bottom right and so on. If I were to click between the top left indicator and the bottom right, I would be inserting an indicator to the second position of the array and the indicator on the bottom left would move from second position to third.

Please upgrade your Flash player.

At this point, you have the ability to create any dynamic shape with straight lines you need to. But what if you make a mistake? What if you add an extra angle; how do you remove it? The solution is simple. All we need to do is subtract the errant indicator from the array. To do this, I used the double click function in AS (don't you just love AS3?!). When you double-click the indicator, it will be removed from the array.

Please upgrade your Flash player.

At this point, we could be done ... but why not take it one step further? For the sake of usability, the last feature I added is a readout of the wall lengths and angles. The data used here is from the length and angle of the now invisible lines. For this example, I am using a dummy scale of (lineLengthInPixels/10).

Please upgrade your Flash player.

I apologize for not getting into the nitty gritty of the code more. For all of those curious about the code you can download all the source files here. Feedback is greatly appreciated!

Christopher said on 08/24 at 09:36 PM

This is pretty darn, kudos to you for working this all out. I think I would be driven crazy. Speaking of which, everything was great until the last section with the dimensions. The dots (and yes, I realize this is a very tiny example) cover up the dimensions on short segments. Something to work out, as I’m sure there will be a situation where this comes up. Perhaps the dimension date isn’t visible at all times—not sure of AS could work that, really at all, I have zero idea—just throwing out a suggestion.

Erik Olson said on 08/25 at 10:16 AM

@Christopher. Thanks for the feedback! I see what you mean about the dots. For the final project I’ll look into a better format for displaying the dimensions readout.

Shaun Inman said on 08/25 at 11:41 AM

How about adding a shift-key modifier to maintain 90° angles when dragging a corner or the ability to drag an entire wall? As it is, transforming the default square to a more common rectangle is more difficult than it needs to be; dragging each corner the same distance and manually maintaining the 90° angle. Limitless control is good as long as it doesn’t get in the way of common tasks.

Erik Olson said on 08/26 at 11:04 PM

@Shaun, good idea. Since most walls will results in a 90º angle it would make sense to make that as easy as possible. Or perhaps have it snap in 15 or 45º intervals.

Haya said on 08/27 at 06:57 AM

The capcha code was Ill and so is your work. ill....very good

jive said on 08/29 at 01:10 PM

Another good idea would to make a grid behind it all and make the dots have to snap to that grid.

Trackback: hu jintao on 09/11 at 09:32 AM barbara wilson barbara wilson
Trackback: miranda richardson on 09/11 at 10:03 AM marco pantani marco pantani
Trackback: leslie winston on 09/11 at 10:35 AM honor blackman honor blackman
sonal said on 09/17 at 08:26 AM

no

Veera said on 09/29 at 07:03 AM

Very nice demonstrations.

Name:

Email:

URL:

Not a robot? Prove it by entering the word below.


Remember my personal information

Notify me of follow-up comments?