Chapter 30. Drawing Vector Graphics Programmatically

Introduction

In this chapter

  • Using lines and line styles

  • Creating shapes

  • Using masks

  • Creating a drawing application

Drawing programmatically allows you much greater freedom in creating and modifying your graphics at runtime, enabling you to create rich and exciting interactivity. Whenever you're drawing programmatically you're creating vector graphics. You do this using lines and fills, where lines define a vector that has at least two points and no fill, and fills define colors bounded by lines. To anyone who has used any image manipulation tool, a vector graphic is probably familiar. Whenever you've used circle or rectangle drawing tools, you've created vector graphics.

If you used the Drawing API in previous versions of Flash then many of the methods and properties we explore here will be somewhat familiar to you. However, the object in which the graphics are drawn has changed, and several new methods have been added.

All DisplayObjects possess a graphics object and a series of methods, which you can use to draw vector graphics. This graphics object is, in fact, an instance of flash.display.Graphics and it is that class that we will investigate. Because any DisplayObject can be used for drawing, in our examples here we use Sprite objects or Shape objects, depending on whether we want to listen for mouse events on that object.

Lines and LineStyles

We'll start our graphics exploration with the line. I've always found it easy to think ...

Get ActionScript™ 3.0 Bible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.