Drawing to Your Flash Movie

To draw to your Flash piece you need to create an instance of the SWFShape class. You do this the same way you created an instance of the SWFMovie class. Once an instance of SWFShape is created you have access to several functions to draw lines and curves.

$shape = new SWFShape();

Now that you have an instance of the SWFShape class, you can draw to the movie that you created earlier. To draw a line, SWFShape gives you two member functions to actually draw the line and one member function to define the style of the line.

To set the style of the line you can use the setLine() function. The setLine() function takes five arguments. The first is the width of the line in pixels; the next three arguments are the RGB values for the color of the line; the fifth and final argument is the alpha value for the line. Let’s say you wanted to draw a 5-pixel wide, yellow line onto your canvas. You would set the style like this:

$shape->setLine(5, 255, 255, 0. 255);

The two functions to draw a line to the canvas are drawLine() and drawLineTo().Each of these functions take two arguments: an x coordinate and a y coordinate. The drawLine() function draws a line from the current pen position to a point (x, y) pixels away from the current position. The drawLineTo() function draws a line from the current pen position to the (x, y) coordinate specified.

The pen position is where the tip of the pen is currently on the canvas. You can move the position of the pen by calling the movePenTo() member function. The movePenTo() member function takes two arguments: an x coordinate and a y coordinate. Take a look at the fol-lowing code example to see the difference between the two draw line functions:

<?php
$myMovie = new SWFMovie();
$myMovie->setDimension(400, 300);
$myMovie->setRate(30);
$myMovie->setBackground(200, 200, 200);

// Draw a line to the canvas using drawLine()
$line1 = new SWFShape();
$line1->setLine(5, 0, 0, 0, 255);
$line1->movePenTo(40, 20);
$line1->drawLine(100, 100);

$line2 = new SWFShape();
$line2->setLine(5, 0, 0, 0, 255);
$line2->movePenTo(80, 20);
$line2->drawLineTo(200, 100);

// Now add the shapes to the movie
$myMovie->add($line1);
$myMovie->add($line2);

// Now output the movie
header(“Content-type:application/x-shockwave-flash”);
$myMovie->output();
?>

First you create a movie that is 400 × 300 pixels with a light gray background. After you have initialized the movie you create two lines, both 5 pixels wide with a color of black. Then you move the pen to a specific point on the canvas and draw a line. After you have drawn a line to both of your SWFShape objects you need to add them to the movie. To do this you use the add() member function of the SWFMovie class. The add() function takes one argument of a mixed type. The results of the above example look like Figure 12.2.

Figure 12.2. Drawing lines with SWFShape.


Now that you know how to draw lines to the stage of your Flash piece, take a look at how you draw curves to the stage. Ming provides two functions to draw curves just like it provides you with two functions to draw lines. The first of the two functions is drawCurve(). The drawCurve() function draws a curve relative to the current pen position. The drawCurve() function takes four arguments. The first two arguments are the x and y coordinates of the control point of the curve. The last two arguments are the x and y coordinates of what is called the anchor point.

When Ming draws a curve, it starts at the current position of the pen, which is called the source point. It then draws a curve to the anchor point, first passing through the control point. Take a look at Figure 12.3 to see a visual representation of this.

Figure 12.3. The Ming curve drawing method.


The second function used to draw a curve is called drawCurveTo().Just like the drawLineTo() function, it starts at the current pen position and draws its point to the anchor point, using the control point to define the severity of the curve. The drawCurve() function also takes four arguments. The first two arguments are the x and y coordinates of the control point. The final two arguments are the x and y coordinates of the anchor point.

To actually draw a curve you follow the same logic you would to draw a line. You need to create a Flash movie, and initialize its dimensions, rate, and background color. Then you need to create a new SWFShape() object and set the style of the line you are going to use by using the setLine() function. Take a look at the following function to see how to draw a curve using the drawCurveTo() function:

<?php
$myMovie = new SWFMovie();
$myMovie->setDimension(400, 300);
$myMovie->setRate(30);
$myMovie->setBackground(200, 200, 200);

// Draw a curve
$curve = new SWFShape();
$curve ->setLine(5, 0, 0, 0, 255);
$curve ->movePenTo(40, 20);
$curve ->drawCurveTo(100, 100, 100, 20);

// Now add the shapes to the movie
$myMovie->add($curve);

// Now output the movie
header(“Content-type:application/x-shockwave-flash”);
$myMovie->output();
?>

Take a look at the results in Figure 12.4

Figure 12.4. Results of the DrawCurveTo() function.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset