API Reference

Canvas

createCanvas(width, height)

Creates the drawing canvas. Call this once at the start of your sketch.

Parameter Type Description
width int Canvas width in pixels
height int Canvas height in pixels
createCanvas(400, 400)
background(30)
fill(255, 100, 100)
circle(200, 200, 120)
resizeCanvas(width, height)

Resizes an existing canvas.

Parameter Type Description
width int New width in pixels
height int New height in pixels
createCanvas(200, 200)
background(100)
resizeCanvas(400, 300)
background(50)
circle(200, 150, 80)
background(*args)

Fills the entire canvas with a color. Accepts the same arguments as fill().

Parameter Type Description
*args color Grayscale, RGB, RGBA, or a color object
createCanvas(400, 400)
background(30, 30, 60)
fill(255)
circle(200, 200, 100)

Shapes

rect(x, y, w, h, r=0)

Draws a rectangle. The optional r parameter rounds all corners.

Parameter Type Description
x float x position (top-left by default)
y float y position
w float Width
h float Height
r float Corner radius (default 0)
createCanvas(400, 400)
background(220)
fill(100, 149, 237)
rect(100, 100, 200, 150, 12)
circle(x, y, diameter)

Draws a circle by specifying its centre and diameter.

Parameter Type Description
x float Centre x
y float Centre y
diameter float Diameter of the circle
createCanvas(400, 400)
background(220)
fill(255, 100, 100)
circle(200, 200, 140)
ellipse(x, y, w, h)

Draws an ellipse. By default x/y is the centre.

Parameter Type Description
x float Centre x
y float Centre y
w float Width
h float Height
createCanvas(400, 400)
background(220)
fill(180, 120, 220)
ellipse(200, 200, 300, 150)
line(x1, y1, x2, y2)

Draws a line between two points.

Parameter Type Description
x1 float Start x
y1 float Start y
x2 float End x
y2 float End y
createCanvas(400, 400)
background(30)
stroke(255, 200, 0)
strokeWeight(3)
line(50, 50, 350, 350)
point(x, y)

Draws a single point. Its size is set by strokeWeight().

Parameter Type Description
x float x position
y float y position
createCanvas(400, 400)
background(30)
stroke(255)
strokeWeight(6)
for i in range(20):
    point(random(0, 400), random(0, 400))
triangle(x1, y1, x2, y2, x3, y3)

Draws a triangle defined by three vertices.

Parameter Type Description
x1,y1 float First vertex
x2,y2 float Second vertex
x3,y3 float Third vertex
createCanvas(400, 400)
background(220)
fill(255, 165, 0)
triangle(200, 60, 60, 340, 340, 340)
quad(x1, y1, x2, y2, x3, y3, x4, y4)

Draws a quadrilateral defined by four vertices.

Parameter Type Description
x1,y1 … x4,y4 float Four corner vertices in order
createCanvas(400, 400)
background(220)
fill(80, 180, 120)
quad(100, 200, 200, 80, 320, 160, 280, 320)
arc(x, y, w, h, start, stop, mode=OPEN)

Draws an arc. Angles are in the current angleMode. mode can be OPEN, CLOSE, CHORD, or PIE.

Parameter Type Description
x, y float Centre position
w, h float Width and height of the bounding ellipse
start, stop float Start and stop angle
mode const OPEN, CLOSE, CHORD, or PIE
createCanvas(400, 400)
background(220)
fill(255, 80, 80)
arc(200, 200, 260, 260, 0, PI * 1.5, PIE)
beginShape() / vertex(x, y) / endShape(mode=OPEN)

Draws a custom polygon. Call beginShape(), add vertices with vertex(), then endShape(). Pass CLOSE to automatically close the shape.

Parameter Type Description
x, y float Vertex coordinates
mode const OPEN (default) or CLOSE
createCanvas(400, 400)
background(220)
fill(100, 160, 240)
beginShape()
vertex(200, 60)
vertex(320, 180)
vertex(270, 340)
vertex(130, 340)
vertex(80, 180)
endShape(CLOSE)
curveVertex(x, y)

Adds a Catmull-Rom curve vertex inside a beginShape()/endShape() block. Needs at least 4 points; the first and last are control points.

Parameter Type Description
x, y float Vertex coordinates
createCanvas(400, 400)
background(220)
noFill()
stroke(80, 120, 220)
strokeWeight(3)
beginShape()
for x in range(0, 450, 50):
    curveVertex(x, 200 + sin(x * 0.05) * 100)
endShape()
bezier(x1, y1, cx1, cy1, cx2, cy2, x2, y2)

Draws a cubic Bézier curve.

Parameter Type Description
x1, y1 float Start point
cx1, cy1 float First control point
cx2, cy2 float Second control point
x2, y2 float End point
createCanvas(400, 400)
background(220)
noFill()
stroke(200, 80, 80)
strokeWeight(3)
bezier(50, 350, 50, 50, 350, 50, 350, 350)

Color & Fill

fill(*args)

Sets the fill color for shapes drawn after this call. Accepts grayscale (1 value), RGB (3 values), RGBA (4 values), or a color object.

Parameter Type Description
*args color 1–4 numbers (0–255 in RGB mode) or a color object
createCanvas(400, 400)
background(220)
fill(255, 100, 50)
circle(130, 200, 100)
fill(50, 120, 255)
circle(270, 200, 100)
noFill()

Disables fill so shapes are drawn as outlines only.

createCanvas(400, 400)
background(220)
noFill()
stroke(80, 80, 200)
strokeWeight(3)
circle(200, 200, 160)
stroke(*args)

Sets the stroke (outline) color. Same argument forms as fill().

Parameter Type Description
*args color 1–4 numbers or a color object
createCanvas(400, 400)
background(220)
stroke(220, 50, 50)
strokeWeight(4)
fill(255)
rect(100, 100, 200, 200)
noStroke()

Removes the stroke from all shapes drawn after this call.

createCanvas(400, 400)
background(220)
noStroke()
fill(100, 200, 100)
rect(80, 80, 240, 240, 20)
strokeWeight(weight)

Sets the thickness of lines and shape outlines in pixels.

Parameter Type Description
weight float Stroke thickness in pixels
createCanvas(400, 400)
background(220)
for i in range(1, 6):
    strokeWeight(i * 2)
    line(50, i * 60, 350, i * 60)
strokeCap(cap)

Sets the style for line endings.

Parameter Type Description
cap const ROUND, SQUARE, or PROJECT
createCanvas(400, 400)
background(220)
strokeWeight(16)
strokeCap(ROUND)
line(80, 150, 320, 150)
strokeCap(SQUARE)
line(80, 220, 320, 220)
strokeCap(PROJECT)
line(80, 290, 320, 290)
strokeJoin(join)

Sets the style for corners where lines meet.

Parameter Type Description
join const MITER, BEVEL, or ROUND
createCanvas(400, 400)
background(220)
strokeWeight(12)
noFill()
strokeJoin(ROUND)
beginShape()
vertex(100, 300)
vertex(200, 80)
vertex(300, 300)
endShape()

Color Functions

color(*args)

Creates a reusable color object. Accepts the same arguments as fill().

Parameter Type Description
*args color Grayscale, RGB, RGBA, or HSB/HSL values depending on colorMode
createCanvas(400, 400)
red  = color(220, 60, 60)
blue = color(60, 100, 220)
background(220)
fill(red)
circle(140, 200, 120)
fill(blue)
circle(260, 200, 120)
lerpColor(c1, c2, amt)

Interpolates between two colors. amt=0 returns c1, amt=1 returns c2.

Parameter Type Description
c1 color Start color
c2 color End color
amt float Amount to interpolate (0.0–1.0)
createCanvas(400, 80)
c1 = color(255, 50, 50)
c2 = color(50, 100, 255)
for i in range(20):
    fill(lerpColor(c1, c2, i / 19))
    noStroke()
    rect(i * 20, 0, 20, 80)
getPixelColour(x, y)

Returns the color of the pixel at (x, y) as a color object.

Parameter Type Description
x int Pixel x coordinate
y int Pixel y coordinate
createCanvas(400, 400)
background(255, 100, 50)
c = getPixelColour(200, 200)
print(c)
colorMode(mode, max1=255, max2=255, max3=255, maxA=255)

Switches between RGB, HSB, and HSL color modes and optionally sets the value range for each channel.

Parameter Type Description
mode const RGB, HSB, or HSL
max1–maxA float Maximum value for each channel (default 255)
createCanvas(400, 80)
colorMode(HSB, 360, 100, 100)
for i in range(36):
    fill(i * 10, 90, 90)
    noStroke()
    rect(i * 11, 0, 11, 80)

Text

text(s, x, y)

Draws text on the canvas at position (x, y).

Parameter Type Description
s str The string to draw
x, y float Position (baseline left by default)
createCanvas(400, 200)
background(30)
fill(255)
textSize(48)
textAlign(CENTER, CENTER)
text('PyAngelo!', 200, 100)
textSize(size)

Sets the font size for subsequent text() calls.

Parameter Type Description
size float Font size in pixels
createCanvas(400, 300)
background(220)
fill(30)
for i, s in enumerate([16, 24, 36, 48]):
    textSize(s)
    text(str(s) + 'px', 20, 40 + i * 60)
textAlign(hAlign, vAlign=BASELINE)

Sets horizontal and vertical text alignment.

Parameter Type Description
hAlign const LEFT, CENTER, or RIGHT
vAlign const TOP, CENTER, BOTTOM, or BASELINE
createCanvas(400, 200)
background(220)
fill(30)
textSize(28)
line(200, 0, 200, 200)
textAlign(LEFT)
text('left', 200, 80)
textAlign(RIGHT)
text('right', 200, 130)
textFont(family)

Sets the font family for subsequent text() calls.

Parameter Type Description
family str A CSS font family name (e.g. 'monospace', 'serif')
createCanvas(400, 200)
background(220)
fill(30)
textSize(32)
textFont('serif')
text('Serif font', 20, 80)
textFont('monospace')
text('Monospace', 20, 140)
textStyle(style)

Sets the text style.

Parameter Type Description
style const NORMAL, BOLD, ITALIC, or BOLDITALIC
createCanvas(400, 200)
background(220)
fill(30)
textSize(30)
textStyle(BOLD)
text('Bold', 20, 70)
textStyle(ITALIC)
text('Italic', 20, 130)
textWidth(s)

Returns the pixel width of a string in the current font settings.

Parameter Type Description
s str The string to measure
createCanvas(400, 200)
background(220)
textSize(32)
fill(30)
msg = 'Hello!'
w = textWidth(msg)
text(msg, 20, 100)
stroke(200, 50, 50)
noFill()
rect(20, 70, w, 40)

Transforms

translate(x, y)

Moves the coordinate origin by (x, y). Subsequent drawing calls use the new origin.

Parameter Type Description
x float Horizontal translation
y float Vertical translation
createCanvas(400, 400)
background(220)
translate(200, 200)
fill(255, 100, 100)
rect(-60, -60, 120, 120)
rotate(angle)

Rotates subsequent drawings around the current origin. Angle is in radians unless angleMode(DEGREES) is set.

Parameter Type Description
angle float Rotation angle
createCanvas(400, 400)
background(220)
translate(200, 200)
for i in range(8):
    rotate(PI / 4)
    fill(i * 30, 100, 200)
    rect(20, -10, 120, 20, 4)
scale(x, y=None)

Scales subsequent drawings. If only x is given, scales uniformly.

Parameter Type Description
x float Horizontal scale factor
y float Vertical scale factor (defaults to x)
createCanvas(400, 400)
background(220)
translate(200, 200)
for s in [1.0, 0.7, 0.4]:
    scale(s)
    noFill()
    stroke(80)
    rect(-60, -60, 120, 120)
push() / pop()

push() saves the current transform and style state. pop() restores it. Use them together to isolate transform changes.

createCanvas(400, 400)
background(220)
translate(200, 200)
push()
rotate(PI / 4)
fill(255, 100, 100)
rect(-60, -60, 120, 120)
pop()
fill(100, 100, 255)
circle(0, 0, 40)

Modes

angleMode(mode)

Sets whether angle arguments to rotate(), arc(), and trig functions use RADIANS (default) or DEGREES.

Parameter Type Description
mode const RADIANS or DEGREES
createCanvas(400, 400)
background(220)
angleMode(DEGREES)
translate(200, 200)
for d in range(0, 360, 30):
    rotate(30)
    fill(d, 150, 200)
    rect(30, -8, 100, 16, 4)
rectMode(mode)

Changes how rect() interprets its x/y arguments.

Parameter Type Description
mode const CORNER (default), CORNERS, CENTER, or RADIUS
createCanvas(400, 400)
background(220)
rectMode(CENTER)
fill(100, 149, 237)
rect(200, 200, 200, 120)
ellipseMode(mode)

Changes how ellipse() and circle() interpret their position arguments.

Parameter Type Description
mode const CENTER (default), CORNER, CORNERS, or RADIUS
createCanvas(400, 400)
background(220)
ellipseMode(CORNER)
fill(180, 120, 220)
ellipse(100, 100, 200, 150)
yAxisMode(mode)

Switches the y-axis direction. JAVASCRIPT (default) has y increasing downward. CARTESIAN flips it so y increases upward.

Parameter Type Description
mode const JAVASCRIPT or CARTESIAN
createCanvas(400, 400)
yAxisMode(CARTESIAN)
background(220)
translate(200, 200)
for i in range(5):
    fill(i * 50, 100, 200)
    circle(0, i * 40, 30)

Images

loadImage(path)

Loads an image from a URL or uploaded asset path. Must be awaited.

Parameter Type Description
path str URL or relative path to the image file
createCanvas(400, 400)
img = await loadImage('https://picsum.photos/400')
image(img, 0, 0)
image(img, x, y, w=None, h=None)

Draws an image on the canvas. Omit w/h to use the image's natural size.

Parameter Type Description
img image Image loaded with loadImage()
x, y float Position (top-left by default, affected by imageMode)
w, h float Width and height to draw (optional)
createCanvas(400, 400)
img = await loadImage('https://picsum.photos/200')
image(img, 100, 100, 200, 200)

Animation & Time

animate(fps=60)

Waits until the next frame should be drawn. Must be awaited inside an animation loop.

Parameter Type Description
fps int Target frames per second (default 60)
createCanvas(400, 400)
x = 0
while True:
    background(30)
    fill(255, 200, 50)
    circle(x, 200, 40)
    x = (x + 3) % 400
    await animate()
sleep(seconds)

Pauses execution for the given number of seconds. Must be awaited.

Parameter Type Description
seconds float Time to wait in seconds
createCanvas(400, 200)
for c in [(255,80,80), (80,255,80), (80,80,255)]:
    background(*c)
    await sleep(0.8)
millis()

Returns the number of milliseconds elapsed since the sketch started.

createCanvas(400, 200)
while True:
    background(30)
    fill(255)
    textSize(36)
    textAlign(CENTER, CENTER)
    text(str(millis() // 1000) + 's', 200, 100)
    await animate()
frameCount (global variable)

The number of frames that have been drawn since the sketch started. Incremented by each await animate() call.

createCanvas(400, 200)
while True:
    background(30)
    fill(255)
    textSize(32)
    textAlign(CENTER, CENTER)
    text('Frame ' + str(frameCount), 200, 100)
    await animate()
deltaTime (global variable)

Milliseconds elapsed since the previous frame. Useful for frame-rate-independent movement.

createCanvas(400, 400)
x = 0
while True:
    background(30)
    x += deltaTime * 0.1
    fill(100, 200, 255)
    circle(x % 400, 200, 40)
    await animate()

Mouse Input

mouseX, mouseY (global variables)

Current mouse position on the canvas. Updated every frame.

createCanvas(400, 400)
while True:
    background(30)
    fill(255, 150, 50)
    circle(mouseX, mouseY, 40)
    await animate()
pmouseX, pmouseY (global variables)

Mouse position on the previous frame. Useful for drawing lines that follow mouse movement.

createCanvas(400, 400)
background(30)
while True:
    if mouseIsPressed:
        stroke(255, 100, 100)
        strokeWeight(3)
        line(pmouseX, pmouseY, mouseX, mouseY)
    await animate()
mouseIsPressed (global variable)

True while any mouse button is held down.

createCanvas(400, 400)
while True:
    if mouseIsPressed:
        background(80, 30, 30)
        fill(255, 100, 100)
    else:
        background(30)
        fill(200)
    circle(200, 200, 120)
    await animate()
mouseButton (global variable)

The mouse button currently pressed. Values: LEFT, RIGHT, CENTER.

createCanvas(400, 200)
while True:
    background(30)
    fill(255)
    textAlign(CENTER, CENTER)
    textSize(24)
    btn = str(mouseButton) if mouseIsPressed else 'none'
    text('Button: ' + btn, 200, 100)
    await animate()

Keyboard Input

keyIsPressed (global variable)

True while any key is held down.

createCanvas(400, 200)
while True:
    background(30)
    fill(255)
    textAlign(CENTER, CENTER)
    textSize(28)
    text('Hold a key!' if not keyIsPressed else key, 200, 100)
    await animate()
key (global variable)

The string value of the last key pressed (e.g. 'a', 'Enter', ' ').

createCanvas(400, 200)
while True:
    background(30)
    fill(255)
    textAlign(CENTER, CENTER)
    textSize(48)
    text(key, 200, 100)
    await animate()
isKeyDown(code)

Returns True if the given key is currently held. Use KEY_* constants.

Parameter Type Description
code const A KEY_* constant such as KEY_LEFT, KEY_SPACE, KEY_A
createCanvas(400, 400)
x, y = 200, 200
while True:
    background(30)
    if isKeyDown(KEY_LEFT):  x -= 3
    if isKeyDown(KEY_RIGHT): x += 3
    if isKeyDown(KEY_UP):    y -= 3
    if isKeyDown(KEY_DOWN):  y += 3
    fill(100, 200, 255)
    circle(x, y, 40)
    await animate()
wasKeyPressed(code)

Returns True once per keypress — the event is consumed so repeated calls in the same frame return False. Useful for discrete actions like jumping.

Parameter Type Description
code const A KEY_* constant
createCanvas(400, 400)
count = 0
while True:
    if wasKeyPressed(KEY_SPACE):
        count += 1
    background(30)
    fill(255)
    textAlign(CENTER, CENTER)
    textSize(36)
    text('Jumps: ' + str(count), 200, 200)
    await animate()

Console I/O

print(*args, sep=' ', end='\n')

Prints values to the console. Works like Python's built-in print().

Parameter Type Description
*args any Values to print
sep str Separator between values (default space)
end str String appended after the last value (default newline)
print('Hello, PyAngelo!')
for i in range(1, 6):
    print('Line', i)
input(prompt='')

Displays a prompt and waits for the user to type a line. Must be awaited.

Parameter Type Description
prompt str Text shown to the user before the input field
name = await input('What is your name? ')
print('Hello,', name + '!')
setTextColour(*args)

Changes the color of text printed to the console after this call.

Parameter Type Description
*args color Same arguments as fill()
setTextColour(255, 100, 100)
print('Red text')
setTextColour(100, 255, 100)
print('Green text')
setTextColour(100, 150, 255)
print('Blue text')
setHighlightColour(*args)

Changes the background (highlight) color of subsequent console output.

Parameter Type Description
*args color Same arguments as fill()
setHighlightColour(50, 50, 120)
print('Highlighted!')
setHighlightColour(0)
print('Normal')
setConsoleSize(height)

Resizes the console panel to the given pixel height.

Parameter Type Description
height int Console height in pixels
setConsoleSize(300)
for i in range(20):
    print('Line', i)

Math & Utility

dist(x1, y1, x2, y2)

Returns the Euclidean distance between two points.

Parameter Type Description
x1,y1 float First point
x2,y2 float Second point
createCanvas(400, 400)
while True:
    background(30)
    d = dist(mouseX, mouseY, 200, 200)
    fill(255)
    textAlign(CENTER, CENTER)
    textSize(24)
    text('dist: ' + str(round(d)), 200, 200)
    await animate()
lerp(start, stop, amt)

Linear interpolation between start and stop. amt=0 returns start, amt=1 returns stop.

Parameter Type Description
start float Start value
stop float End value
amt float Interpolation amount (0.0–1.0)
createCanvas(400, 400)
x = 0
while True:
    x = lerp(x, mouseX, 0.1)
    background(30)
    fill(255, 200, 50)
    circle(x, 200, 40)
    await animate()
constrain(n, low, high)

Clamps n to the range [low, high].

Parameter Type Description
n float Value to constrain
low float Minimum allowed value
high float Maximum allowed value
createCanvas(400, 400)
while True:
    background(30)
    x = constrain(mouseX, 100, 300)
    fill(100, 200, 255)
    circle(x, 200, 50)
    await animate()
map(value, start1, stop1, start2, stop2)

Re-maps a number from one range to another.

Parameter Type Description
value float The value to re-map
start1, stop1 float Input range
start2, stop2 float Output range
createCanvas(400, 400)
while True:
    background(30)
    r = map(mouseX, 0, 400, 0, 255)
    b = map(mouseY, 0, 400, 0, 255)
    fill(r, 100, b)
    circle(200, 200, 160)
    await animate()
random(low_or_list=1, high=None)

Returns a random float in [0, low) when called with one number, in [low, high) with two numbers, or a random element when passed a list.

Parameter Type Description
low_or_list float|list Upper bound, or a list to choose from
high float Upper bound (optional)
createCanvas(400, 400)
background(220)
for _ in range(60):
    fill(random(255), random(255), random(255), 180)
    noStroke()
    circle(random(400), random(400), random(20, 60))
noise(x, y=0, z=0)

Returns a Perlin noise value in [0, 1] for the given coordinates. Nearby inputs return similar values, producing smooth variation.

Parameter Type Description
x float First noise coordinate
y float Second coordinate (optional)
z float Third coordinate (optional)
createCanvas(400, 200)
t = 0
while True:
    background(30)
    noFill()
    stroke(100, 200, 255)
    strokeWeight(2)
    beginShape()
    for x in range(0, 401, 5):
        y = noise(x * 0.01, t) * 180 + 10
        vertex(x, y)
    endShape()
    t += 0.02
    await animate()
noiseSeed(seed)

Seeds the Perlin noise generator for reproducible results.

Parameter Type Description
seed int Integer seed value
createCanvas(400, 200)
noiseSeed(42)
background(30)
noFill()
stroke(100, 200, 255)
strokeWeight(2)
beginShape()
for x in range(401):
    y = noise(x * 0.01) * 180 + 10
    vertex(x, y)
endShape()
noiseDetail(lod, falloff)

Adjusts the character of Perlin noise. lod is the number of octaves; falloff controls how much each octave contributes.

Parameter Type Description
lod int Number of noise octaves (higher = more detail)
falloff float Amplitude falloff per octave (0.0–1.0)
createCanvas(400, 200)
noiseDetail(8, 0.5)
background(30)
noFill()
stroke(255, 180, 50)
strokeWeight(2)
beginShape()
for x in range(401):
    y = noise(x * 0.005) * 180 + 10
    vertex(x, y)
endShape()

Constants

Math

PI HALF_PI QUARTER_PI TWO_PI TAU

Angles

RADIANS DEGREES

Shape & Image Modes

CORNER CORNERS CENTER RADIUS OPEN CLOSE CHORD PIE

Stroke

ROUND SQUARE PROJECT MITER BEVEL

Text Alignment

LEFT RIGHT CENTER TOP BOTTOM BASELINE

Text Style

BOLD ITALIC NORMAL BOLDITALIC

Color Mode

RGB HSB HSL

Y-Axis

CARTESIAN JAVASCRIPT

Key Codes

KEY_SPACE KEY_ENTER KEY_ESC KEY_BACKSPACE KEY_LEFT KEY_RIGHT KEY_UP KEY_DOWN KEY_SHIFT KEY_CTRL KEY_ALT KEY_A … KEY_Z KEY_0 … KEY_9