The Drawing API
by ilyas usal
Introduction
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:
- moveTo
- lineTo
- curveTo
- beginFill/endFill
- 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
- 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).
- 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:
- nothing will come up on the screen if you haven't defined a lineStyle
- 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)
_root.createEmptyMovieClip("holder",1);
// I define the style of the line
// 1: thickness
// 0x000000: color (white)
// 100: alpha
holder.lineStyle(1,0x000000,100);
// I put the pen at (150,200)
holder.moveTo(150,200);
// I draw a line to the point (300,200)
holder.lineTo(300,200);
-
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.
_root.createEmptyMovieClip("holder",1);
holder.lineStyle(1,0,100);
holder.moveTo(100,100)
holder.curveTo(150,150,100,200);
// To check where (150,150) is:
holder.lineTo(150,150)
holder.lineTo(100,100)
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
x1=100;
y1=200;
x2=450;
y2=200;
this.onEnterFrame=function(){
// We calculate the control point
controlX=_xmouse*2-(x1+x2)/2;
controlY=_ymouse*2-(y1+y2)/2;
// We erase the previous line
this.clear();
this.lineStyle(1,0,100);
this.moveTo(x1,y1);
this.curveTo(controlX,controlY,x2,y2);
}
Notice that you have to define the
lineStyle after
the clear because otherwise it will be erased.
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.
_root.createEmptyMovieClip("holder",1);
holder.lineStyle(1,0,100);
holder.beginFill(0xABCDEF,50);
holder.moveTo(100,100)
holder.lineTo(150,150);
holder.lineTo(150,200);
holder.endFill();
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
- 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:
_root.createEmptyMovieClip("holder",1);
holder.lineStyle(1,0,100);
// This is half the width of the square
width=30;
// The center of the square is (150,150)
holder.moveTo(150-width,150-width);
holder.lineTo(150+width,150-width);
holder.lineTo(150+width,150+width);
holder.lineTo(150-width,150+width);
holder.lineTo(150-width,150-width);
- 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.
- 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:
_root.createEmptyMovieClip("rond",1);
rond.lineStyle(30,0,100);
rond.lineTo(.15,.45);
Hope this tutorial helped. If you have any
questions, please post them on the forums at
//www.kirupa.com/forum/
|