# Graphics Docs

Our sandbox module contains a number of methods that allows the drawing of graphics to a canvas. A list of supported methods, with examples, is provided here.

# Before You Begin

Before you can begin drawing graphics to the canvas, you must be sure to import our sandbox module and create a canvas.

```import sandbox
canvas = sandbox.createCanvas()
```

# Drawing Shapes

#### Line

A line can be drawn by passing along two tuples representing the starting and ending points of the line.

`line((x1,y1),(x2,y2))`
```# Creates a line from (50,50) to (100,100)
canvas.line((50,50),(100,100))
```

#### Rectangle

A rectangle can be drawn by passing along two tuples representing the opposite vertexes of the rectangle.

`rectangle((x1,y1),(x2,y2))` `rect((x1,y1),(x2,y2))`
```# Draws rectangle from (100,200) to (200,300)
canvas.rectangle((100,200),(200,300))
# Draws rectangle from (0,0) to (20,20)
canvas.rect((0,0),(20,20))
```

#### Square

A square can be drawn by passing along the position of the top-left vertex (tuple) and the length.

Note: If a negative size is provided, the position provided will actually represent the bottom-right vertex.

`square((x,y),length)`
```# Draws square of size 50 start from (75,75)
canvas.square((75,75),50)
```

#### Circle

A circle can be drawn by passing along a tuple (representing the center) and a radius.

`circle((x,y),radius)`
```# Draws circle of radius 50 with center point (100,200)
canvas.circle((100,200),50)
```

#### Arc

A portion of a circle can be created by passing a center point (tuple), radius, starting degree, and ending degree of the arc (clockwise).

`arc((x,y),radius,start,end)`
```# Draws arc with center (50,50), radius 20, from 0 to 180 degrees,
# representing the bottom half of a circle, from right to left.
canvas.arc((50,50),20,0,180)
```

#### Ellipse

An ellipse can be drawn by passing a center point (tuple), and two radii.

Note: The ellipse can only be drawn in browsers supporting the ellipse method of the Canvas 2D API.

`ellipse((x,y),radius1,radius2)`
```# Draws ellipse with center (100,100), radius 50 (horizontal),
canvas.ellipse((100,100),50,20)
```

#### PolyLine

A polyline (series of connected segments) can be drawn by passing along a sequence of tuples each representing a point on the polyline.

`polyLine([(x1,y1),(x2,y2),...,(xn,yn)])` `polyline([(x1,y1),(x2,y2),...,(xn,yn)])`
```# Uses a polyline to draw a square
canvas.polyline([(10,10),(10,20),(20,20),(20,10),(10,10)])
```

# Filling Shapes

#### FillRectangle

Similar to drawing a rectangle, but instead, the rectangle will be filled using the current fill color. No lines will be drawn.

`fillRectangle((x1,y1),(x2,y2))` `fillRect((x1,y1),(x2,y2))`
```# Draws a filled rectangle from (10,40) to (60,100)
canvas.fillRectangle((10,40),(60,100))
# Draws a filled rectangle from (100,10) to (120,40)
canvas.fillRect((100,10),(120,40))
```

#### FillSquare

Similar to drawing a square, but instead, the square will be filled using the current fill color. No lines will be drawn.

`fillSquare((x,y),length)`
```# Fills square of size 50 start from (75,75)
canvas.fillSquare((75,75),50)
```

#### FillCircle

Similar to drawing a circle, but instead, the circle will be filled using the current fill color. No lines will be drawn.

`fillCircle((x,y),radius)`
```# Fills a circle of radius 100 with center point (200,200)
canvas.fillCircle((100,200),100)
```

#### FillArc

Similar to drawing an arc, but instead, the arc will be filled using the current fill color. No lines will be drawn.

Note, the filling of the arc differs from what one might exepct. When filling an arc, the missing piece is not a "slice" of the circle (as in a pie chart). Instead, an imaginary secant/chord is drawn between the starting and ending points of the arc. The shape formed by the arc and secant/chord will be filled.

`fillArc((x,y),radius,start,end)`
```# Fills an arc with center (200,200), radius 50, from 45 to
# 315 degrees. Resembles a circle with its right side removed.
canvas.fillArc((200,200),50,45,315)
```

#### FillEllipse

Similar to drawing an ellipse, but instead, the ellipse will be filled using the current fill color. No lines will be drawn.

Note: The ellipse can only be filled in browsers supporting the ellipse method of the Canvas 2D API.

`fillEllipse((x,y),radius1,radius2)`
```# Fills ellipse with center (100,100), radius 50 (horizontal),
canvas.fillEllipse((100,100),50,20)
```

#### FillPolyLine

Similar to drawing a polyline, but instead, the shape created by the polyline is filled using the current fill color. No lines will be drawn.

A polyline whose starting and ending points do not meet are "closed" for the purpose of filling.

`fillPolyLine([(x1,y1),(x2,y2),...,(xn,yn)])` `fillPolyline([(x1,y1),(x2,y2),...,(xn,yn)])` `fillPoly([(x1,y1),(x2,y2),...,(xn,yn)])`
```# Creates a filled hourglass-like figure
canvas.fillPolyline([(10,10),(50,50),(10,50),(50,10)])
```

#### Line Color

The line color is used to draw shapes and lines (it is not used in fills). The line color can be set to any HTML/CSS recognized color string. More information on HTML colors can be found here.

`lineColor("{color}")`
```# Draws a blue line
canvas.lineColor("blue")
canvas.line((50,50),(100,100))
# Draws a green line
canvas.lineColor("#00FF00")
canvas.line((50,100),(100,50))
# Draws a red circle
canvas.lineColor("rgb(255,0,0)")
canvas.circle((75,75),50)
```

#### Line Size

The line size is used to control the thickness of your lines. The line size can be set to any integer greater than 0.

`lineSize(size)`
```# Draws a line of size 5
canvas.lineSize(5)
canvas.line((50,50),(100,100))
```

#### Fill Color

The fill color is used to fill shapes. Like the line color, the fill color can be set to any HTML/CSS recognized color string. The fill color can also be set to a gradient object.

`fillColor("{color}")` `fillColor(gradient)`
```# Creates a "SeaGreen" circle.
canvas.fillColor("SeaGreen")
canvas.fillCircle((200,200),100)
```

#### FillPolyLine

Similar to drawing a polyline, but instead, the shape created by the polyline is filled using the current fill color. No lines will be drawn.

A polyline whose starting and ending points do not meet are "closed" for the purpose of filling.

`fillPolyLine([(x1,y1),(x2,y2),...,(xn,yn)])` `fillPolyline([(x1,y1),(x2,y2),...,(xn,yn)])` `fillPoly([(x1,y1),(x2,y2),...,(xn,yn)])`
```# Creates a filled hourglass-like figure
canvas.polyline([(10,10),(50,50),(10,50),(50,10)])
```

Linear gradient objects can be created to be used in the filling of shapes. A linear gradients are defined by starting and ending points (both tuples), and a sequence of color stops. Each color stop is a tuple consisting of a value (between 0 and 1) and string (representing the color).

Note: The gradient object is related to the canvas, not a specific shape. Therefore, when using a gradient to fill a shape, you must imagine that gradient being applied to the entire canvas, but only being revealed through your shape.

`linearGradient((x1,y1),(x2,y2),[(stop1,"{color1}"),(stop2,"{color2}"),...,(stopn,"{colorn}")])`
```# Creates a horizontal black-to-white linear gradient
# Sets fill color to gradient
# fills two rectangles with gradient
canvas.fillRect((50,150),(450,200))
canvas.fillRect((200,200),(300,400))

# Creates a diagonal gradient with a middle color stop
# for a specific square.
canvas.fillSquare((50,50),75)
```

# Clearing & Reseting

#### Clear

A rectangular area of the canvas can be cleared by passing along two tuples representing the starting and ending point of the rectangle.

`clear((x1,y1),(x2,y2))`
```# Clears the rectangle from (50,50) to (200,200)
canvas.clear((50,50),(200,200))
```

#### Reset

Resets the entire canvas by clearing all drawings and reseting colors (line/fill) and sizes (line).

`reset()`
```# Clears entire canvas and resets colors/sizes
canvas.reset()
```