Shapes (Circle, Rectangle, Polygon, etc.)¶
A number of basic shapes can be drawn using helper functions.
Rectangles¶
The rect()
function draws a rectangle with the top-left corner at (x, y)
and the specified width
and height
.
Rounded Rectangles¶
The roundedRect()
function draws a rectangle with rounded corners. The radius
parameter specifies the radius of the corners.
Circles¶
The circle()
function draws a circle with the center at (x, y)
and the specified radius
.
Ellipses¶
The ellipse()
function draws an ellipse with the center at (x, y)
and the specified radiusX
and radiusY
.
Arcs¶
I've honestly only used arcs to draw pie charts, so thats what im going to focus on in this example.
// arc(cx, cy, radius, startAngle, endAngle, direction)
// CCW = Counter Clockwise
$vg->arc(700, 200, 50, 0, 3.14, VGContext::CCW);
Again this can be utilized to draw pies! Here is an example of how to draw a circular progress indicator.
$cx = 400;
$cy = 200;
$radius = 50;
$progress = 0.5; // 50%
$startAngle = -M_PI * 0.5;
$endAngle = $startAngle + (M_PI * 2 * $progress);
$vg->beginPath();
$vg->moveTo($cx, $cy);
$startX = $cx + $radius * cos($startAngle);
$startY = $cy + $radius * sin($startAngle);
$vg->lineTo($startX, $startY);
$vg->arc($cx, $cy, $radius, $startAngle, $endAngle, VGContext::CW);
$vg->closePath();
$vg->fillColor(VGColor::green());
$vg->fill();
Polygons¶
If you want to draw a custom shape you can simply issue a series of moveTo()
and lineTo()
commands to draw a polygon.
This will allow you to draw basically any shape you want. Here is an example of how to draw a star.
$vg->beginPath();
$vg->moveTo(400, 100);
$vg->lineTo(450, 200);
$vg->lineTo(550, 200);
$vg->lineTo(475, 250);
$vg->lineTo(500, 350);
$vg->lineTo(400, 300);
$vg->lineTo(300, 350);
$vg->lineTo(325, 250);
$vg->lineTo(250, 200);
$vg->lineTo(350, 200);
$vg->closePath();
$vg->fillColor(VGColor::yellow());
$vg->fill();
// draw some eyes
$vg->beginPath();
$vg->circle(400 - 30, 230, 10);
$vg->circle(400 + 30, 230, 10);
$vg->fillColor(VGColor::black());
$vg->fill();