Flash Components      Flash Menu      Flash Gallery      Flash Slideshow      FLV Player      Flash Form      MP3 Player      PhotoFlow      Flash CMS      3D Wall      Flash Scroller

Flash / AS



ASP.net / PHP






  Design & Effects
  Audio & Video
  User Interface





The Drawing API
         by ilyas usal

Using the drawing API is not the easiest thing in the world, but once you get the hang of it, you can produce very easily lots of cool all-code movies.
The main functions of the drawing API are:

  1. moveTo
  2. lineTo
  3. curveTo
  4. beginFill/endFill
  5. clear

I talked about some of them in the drawing board tutorial, so you can read that afterwards to see what you can do with all this.

This article is divided into 2 parts: in the first part we'll see how the functions I mentioned work, and in the second part I'll show you how to build basic shapes.

Functions Overview

  1. moveTo(x,y)
    This is the function you're going to have to use all the time. It moves your imaginary pen to the point (x,y) so that you can draw from there. If you don't do that, Flash will start drawing from (0,0).
  2. lineTo(x,y)
    Once you've defined the starting point, you can define the ending point: this function will draw a line between the position of the pen and the point (x,y).

    There are 2 things to notice here:
    1. nothing will come up on the screen if you haven't defined a lineStyle
    2. at the end of the lineTo function, the pen is moved to the end of the line, so that you don't have to constantly moveTo.

    Here's a short example of how it works. Put this in the first frame of the _root:

    // First I create a clip that will hold
    // my line (it could be the _root)

    // I define the style of the line
    // 1: thickness
    // 0x000000: color (white)
    // 100: alpha
    // I put the pen at (150,200)
    // I draw a line to the point (300,200)


  3. curveTo(controlX,controlY,anchorX,anchorY)
    Now curveTo is a strange animal: it needs 4 parameters for 2 point: the anchor, which is the ending point of the curve, and the control point, which is the point towards which the curve will curve.



    // To check where (150,150) is:

    As you can see, the control point is NOT a point that the line will reach. In order to define a point that the line will actually reach, you have to twist it a bit. Imagine that you want to curveTo the position of the mouse.

    /* Thanks to Robert Penner for this piece of code */

    // We define the starting and ending points


      // We calculate the control point
      // We erase the previous line

    Notice that you have to define the lineStyle after the clear because otherwise it will be erased.

  4. beginFill(rgb,alpha)/endFill()
    This is used to color the inside of the shape you are building. You put the beginFill just before the moveTo and the endFill just after you have finished to draw. Note also that if the shape is not closed, Flash will do it for you.


  5. clear()
    This clears the movie clip you're targetting. In our examples, as we draw in the clip holder, you have to do holder.clear();

There. We have all the necessary tools in our hands. Make sure that you have understood everything before going further, because it gets a bit more difficult.

Draw basic shapes

  1. A square
    We are going see how to build a square, and (since it is AS tricks...) then how to optimize that code. Basically, we have to draw 4 lines for a square, so we can hard code everything like that:

    // This is half the width of the square
    // The center of the square is (150,150)

  2. A polygon
    Time for optimization now. Because if you have 10 sides, it's painful to write lineTo 10 times. That's why we are going to put our points in an array and then get them in a loop. Don't panic, I'm here :)

    There isn't much to explain. It's the same as before, except I put the points in a 2-dimensions array, and then I get the values from there. Just study how it is structured, and I'm sure there won't be problems. The good thing is: you can put as many values as you want in that array, it will be the same. You're encouraged to try it.

  3. A circle
    The technique to make a circle is pretty straightforward: I draw a very short line with a huge pen. And it works! Here is the code:




Hope this tutorial helped. If you have any questions, please post them on the forums at //www.kirupa.com/forum/


kirupa.com's fast and reliable hosting provided by Media Temple. flash components
The Text Animation Component for Flash CS3
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.
Check out our high quality vector-based design packs! Flash Effect Components
flash menus, buttons and components Digicrafts Components
The best flash components ever! Entheos Flash Website Templates
Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com Purchase & Download Flash Components
flash components Free Website | Make a Website
Streamsolutions Content Delivery Networks Learn how to advertise on kirupa.com