Create a Basic Drawing Application in Flash


Twice a month, we revisit some of our readers’ favorite posts from throughout the history of Activetuts+. This tutorial was first published in August, 2009.

In this tutorial, we'll create a Drawing Application with basic functions and an easy to use interface. Feeling artistic? Then let's go..

Step 1: Overview

This application will feature a series of tools such as pencil, eraser, text tool, and also the possibility to save your drawing in png format using the Adobe's PNG Encoder.

Step 2: Setting Up

Open Flash and create a new Flash File (ActionScript 3).

Set the stage size to 600x350px and the frame rate to 25fps.


Step 3: Board

Draw a 600x290px white rectangle and convert it to MovieClip. Set its instance name to "board" and align it to the top-left of the stage.

This will be the area on which we'll be able to draw.

Step 4: Tools Panel

Create a 600px wide 60px high gray (#DFDFDF) rectangle and align it to the bottom of the stage. This will be our background for the Tools Panel.

Step 5: Tool Icons

I'm not going to cover the creation of the tools icons since that isn't the intention of the turorial, still, you can see how they are made in the Fla file included in the Source.

As you can see in the image, we're going to use two icons, one in a gray tone and the other one in color.

Convert the gray pencil icon to a Button (F8) and name it "pencilTool", double click to edit it and add a KeyFrame (F6) in the "over" state. Here's where we're going to put the color version of the icon.

Lastly, add another KeyFrame in the "hit" state, and draw a 30x30px square to act as the button's hit area.

Repeat this process with all your tool icons, don't forget to give them the correct instance names (eraserTool, textTool, saveButton, clearTool).

In order to display the active tool icon, we're going to use the color version of the icon and place it in the same position of the button we created before. The instance names will be "pencil", "eraser" and "text".

We'll hide these icons later using ActionScript.

Step 6: Save Dialog

When we use the save option we're going to display a "save successful" message.

Create a 600x350px black rectangle with 50% alpha to use as a background.

Add a black rounded rectangle in the center with the save button icon and some text indicating that the save has been completed.

Create a close button and align it in the top-left of the rounded rectangle, name it "closeBtn". As you can imagine, this button will close the Save Dialog.

Convert all the elements to a single MovieClip and mark the "Export for ActionScript" checkbox. Set the Class textfield to "SaveDialog".

We'll instantiate this class when the user presses the SaveButton and the save is completed.

Step 7: Size Panel

The Size Panel is the area where you can change the size of the pencil, eraser and text tool. You can do that by clicking in the panel area or the oval inside it.

Select the Rectangle Primitive Tool, set the corner radius to 4 and create a #EFEFEF 80x50px rectangle. Convert it to a MovieClip and name it "sizePanel".

Open the Filters panel and add a Drop Shadow with the following values:

Now use the Oval Tool to create a 5x5px black circle and center it in the Size Panel, convert it to MovieClip and set its instance name to "shapeSize".

Step 8: Default Colors

We'll create a default color palette instead of using the Color Picker Component.

Create a 22x22px circle using the Oval Tool, set its color to #EEEEEE and convert it to MovieClip. Add the same Drop Shadow Filter we used in the Size Panel.

Draw a second circle of 16x16px and use black for the color this time. Center the circles and repeat this process changing the last circle color to the following:

Align them to end up with something like this:

Convert all the colors to a single MovieClip, name it "colors" and be sure to set the Registration Point to the Top-Left, since we're going to get pixel data using the Bitmap Class.

Step 9: Adobe PNG Encoder

In order to use the Save function we'll need the Adobe PNG Encoder wich is part of the as3corelib that you can find in Google Code.

To use this class outside of the package it comes with as default, we need to change one line. Open the file and change the line "package com.adobe..." to just "package". This will let us call the class in the directory where the Fla file is.

Step 10: Main Class

A single Class will handle this application. Open the Properties Panel in the Fla file and set the Document Class as "Main".

Create a new ActionScript Document and save it as "" in the same directory where the Fla file is.

Step 11: Import Required Classes

These are the classes we're going to need in this app. Remember to check the Flash Help (F1) if you're unsure of a specific class.

	import PNGEncoder;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.DisplayObject;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldType;
	import flash.text.TextFieldAutoSize;
	import flash.display.BitmapData;
	import flash.geom.ColorTransform;
	import flash.utils.ByteArray;

Step 12: Extending the Class

We're extending the MovieClip class since we use this class' specific properties and methods.

public class Main extends MovieClip

Step 13: Variables

These are the variables we'll use. They're all explained in the code comments.

/* Pencil Tool shape, everything drawn with this tool and the eraser tool is stored inside board.pencilDraw */

var pencilDraw:Shape = new Shape();

/* Text format */

var textformat:TextFormat = new TextFormat();

/* Colors */

var colorsBmd:BitmapData; //We'll use this Bitmap Data to get the pixel RGB Value when clicked
var pixelValue:uint;
var activeColor:uint = 0x000000; //This is the current color in use, displayed by the shapeSize MC

/* Save dialog instance */

var saveDialog:SaveDialog;

/* Active var, to check which tool is active */

var active:String;

/* Shape size color */

var ct:ColorTransform = new ColorTransform();

Step 14: Main Function

The main function will take care of setting the Text Format of the Text Tool, converting the Colors MovieClip to Bitmap Data so we can extract the Pixels RGB Value, adding the listeners and hiding the active icons.

public function Main():void
	textformat.font = "Quicksand Bold Regular"; // You can use any font you like
	textformat.bold = true;
	textformat.size = 16;

   /* We create these functions later */



	/* Hide tools highlights */

	pencil.visible = false;
	hideTools(eraser, txt);

Step 15: Tools Actions

The tools actions are each split into four functions.

The first one will set the tool to its Active state, the second and third ones will handle the Mouse Events (like Drawing or Erasing) and the fourth one will stop those Events.

Step 16: Pencil Tool

These functions handle the Pencil Tool, read the comments in the code for a better understanding.

The set to active function:

private function PencilTool(e:MouseEvent):void
	/* Quit active tool */

	quitActiveTool(); //This function will be created later

	/* Set to Active */

	active = "Pencil"; //Sets the active variable to "Pencil"

	/* Adds the listeners to the board MovieClip, to draw just in it */

	board.addEventListener(MouseEvent.MOUSE_DOWN, startPencilTool);
	board.addEventListener(MouseEvent.MOUSE_UP, stopPencilTool);

	/* Highlight, sets the Pencil Tool Icon to the color version, and hides any other tool */

	hideTools(eraser, txt);

   /* Sets the active color variable based on the Color Transform value and uses that color for the shapeSize MovieClip */

	ct.color = activeColor;
	shapeSize.transform.colorTransform = ct;

The Start function; this function is called when the Board MovieClip is pressed.

private function startPencilTool(e:MouseEvent):void
	pencilDraw = new Shape(); //We add a new shape to draw always in top (in case of text, or eraser drawings)
	board.addChild(pencilDraw); //Add that shape to the board MovieClip, mouseY); //Moves the Drawing Position to the Mouse Position, activeColor);//Sets the line thickness to the ShapeSize MovieClip size and sets its color to the current active color

	board.addEventListener(MouseEvent.MOUSE_MOVE, drawPencilTool); //Adds a listener to the next function

The Draw function; called when the user presses the Board MovieClip and moves the mouse.

private function drawPencilTool(e:MouseEvent):void
{, mouseY); //Draws a line from the current Mouse position to the moved Mouse position

Stop function, executed when the user releases the mouse.

private function stopPencilTool(e:MouseEvent):void
	board.removeEventListener(MouseEvent.MOUSE_MOVE, drawPencilTool); //Stops the drawing

Step 17: Eraser Tool

The Eraser Tool is pretty much the same as the Pencil Tool, except that we don't use any color other than White.

private function EraserTool(e:MouseEvent):void
	/* Quit active tool */


	/* Set to Active */

	active = "Eraser";

	/* Listeners */

	board.addEventListener(MouseEvent.MOUSE_DOWN, startEraserTool);
	board.addEventListener(MouseEvent.MOUSE_UP, stopEraserTool);

	/* Highlight */

	hideTools(pencil, txt);

   /* Use White Color */

	ct.color = 0x000000;

	shapeSize.transform.colorTransform = ct;

private function startEraserTool(e:MouseEvent):void
	pencilDraw = new Shape();
	board.addChild(pencilDraw);, mouseY);, 0xFFFFFF); //White Color

	board.addEventListener(MouseEvent.MOUSE_MOVE, drawEraserTool);

private function drawEraserTool(e:MouseEvent):void
{, mouseY);

function stopEraserTool(e:MouseEvent):void
	board.removeEventListener(MouseEvent.MOUSE_MOVE, drawEraserTool);


As you can see, the code is the same except for the White color changes.

Step 18: Text Tool

The Text Tool has only two functions; one in charge of setting it to active, and the other one for handling the text writing. Let's take a look:

private function TextTool(e:MouseEvent):void
	/* Quit active tool */


	/* Set to Active */

	active = "Text";

	/* Listener */

	board.addEventListener(MouseEvent.MOUSE_UP, writeText);

	/* Highlight */

	hideTools(pencil, eraser);

private function writeText(e:MouseEvent):void
   /* Create a new TextField Object, this way we won't replace older instances */

	var textfield = new TextField();

	/* Set Formats, position, and focus */

	textfield.type = TextFieldType.INPUT;
	textfield.autoSize = TextFieldAutoSize.LEFT;
	textfield.defaultTextFormat = textformat;
	textfield.textColor = activeColor;
	textfield.x = mouseX;
	textfield.y = mouseY;
	stage.focus = textfield;

	/* Add text to Board */


This one was easy, remember that you can change the size and color using the shapeSize and the Colors MovieClips.

Step 19: Save Option

The save option handled by the saveButton will make use of the Adobe's PNGEnconder Class to save the artwork as a PNG file.

private function export():void
	var bmd:BitmapData = new BitmapData(600, 290);//Creates a new BitmapData with the board size

	bmd.draw(board);//Draws the board MovieClip into a BitmapImage in the BitmapData

	var ba:ByteArray = PNGEncoder.encode(bmd); //Creates a ByteArray of the BitmapData, encoded as PNG

	var file:FileReference = new FileReference(); // Instantiates a new File Reference Object to handle the save

	file.addEventListener(Event.COMPLETE, saveSuccessful); //Adds a new listener to listen when the save is complete, "MyDrawing.png"); //Saves the ByteArray as a PNG

private function saveSuccessful(e:Event):void
	saveDialog = new SaveDialog();// Instantiates a new SaveDialog Class

	addChild(saveDialog); //Adds the SaveDialog MovieClip to the Stage

	saveDialog.closeBtn.addEventListener(MouseEvent.MOUSE_UP, closeSaveDialog);//Adds a listener to the close button of the dialog

private function closeSaveDialog(e:MouseEvent):void
	removeChild(saveDialog); //Removes the dialog of the Stage

private function save(e:MouseEvent):void
	export(); //Calls the export function to begin the saving process

Step 20: Clear Tool

The Clear Tool will add a white screen in front of all the elements in order to leave the board ready to draw again.

private function clearBoard(e:MouseEvent):void
	/* Create a white rectangle on top of everything */

	var blank:Shape = new Shape();;, 0, board.width, board.height);;


Step 21: Get Colors Value

To get the value of the colors we're using in the Colors MovieClip, we convert it to a BitmapData Object and use the getPixel method to obtain the RGB value of the pixel clicked.

private function convertToBMD():void
	colorsBmd = new BitmapData(colors.width,colors.height);

private function chooseColor(e:MouseEvent):void
	pixelValue = colorsBmd.getPixel(colors.mouseX,colors.mouseY);//Gets the cliked pixel RGB value
	activeColor = pixelValue;

  /* Use a ColorTransform object to change the shapeSize MovieClip color */

	ct.color = activeColor;
	shapeSize.transform.colorTransform = ct;

We'll add the chooseColor listener in the addListeners function later in the code.

Step 22: Active Tool

Earlier, we declared a variable to set the active or current tool in use, and we call this function to remove the corresponding listeners in order to have just one active tool.

Basically, the function checks the "active" variable in a switch loop, then depending on its value removes the listeners it has.

private function quitActiveTool():void
	switch (active)
		case "Pencil" :
			board.removeEventListener(MouseEvent.MOUSE_DOWN, startPencilTool);
			board.removeEventListener(MouseEvent.MOUSE_UP, stopPencilTool);
		case "Eraser" :
			board.removeEventListener(MouseEvent.MOUSE_DOWN, startEraserTool);
			board.removeEventListener(MouseEvent.MOUSE_UP, stopEraserTool);
		case "Text" :
			board.removeEventListener(MouseEvent.MOUSE_UP, writeText);
		default :

We also need to highlight the active tool and hide the other ones:

private function highlightTool(tool:DisplayObject):void
	tool.visible=true; //Highlights tool in the parameter

/* Hides the tools in the parameters */

private function hideTools(tool1:DisplayObject, tool2:DisplayObject):void

Step 23: Shape Size

We click the Size Panel or the ShapeSize MovieClip to change the size of the Pencil, Eraser and Text Tool. The size is changed in intervals of 5, and is reset when the size is bigger or equal to 50. Take a look at the code:

private function changeShapeSize(e:MouseEvent):void
	if (shapeSize.width >= 50)
		shapeSize.width = 1;
		shapeSize.height = 1;

		/* TextFormat */

		textformat.size = 16;
		shapeSize.width += 5;

		/* TextFormat */


Step 24: Add Listeners

A function to add all the listeners.

private function addListeners():void
	pencilTool.addEventListener(MouseEvent.MOUSE_UP, PencilTool);
	eraserTool.addEventListener(MouseEvent.MOUSE_UP, EraserTool);
	textTool.addEventListener(MouseEvent.MOUSE_UP, TextTool);
	saveButton.addEventListener(MouseEvent.MOUSE_UP, save);
	clearTool.addEventListener(MouseEvent.MOUSE_UP, clearBoard);
	colors.addEventListener(MouseEvent.MOUSE_UP, chooseColor);
	sizePanel.addEventListener(MouseEvent.MOUSE_UP, changeShapeSize);
	shapeSize.addEventListener(MouseEvent.MOUSE_UP, changeShapeSize);

Step 25: Test

Test your app by pressing cmd+return and check if everything is working as expected.

Use all the tools, colors and functions and start drawing your scene!


This Flash Movie can be easily adapted as a kids drawing application, it has a friendly user interface and basic tools that can teach how the mouse works whilst having some colorful fun in the process.

You can also explore the rest of the as3corelib and read its documentation to learn some new ActionScript features.

Thanks for reading!

Related Posts