Pages

The origin of the drawing board is at the bottom left.

newPage(width=None, height=None)

Create a new canvas to draw in. This will act like a page in a pdf or a frame in a mov.

Optionally a width and height argument can be provided to set the size. If not provided the default size will be used.

Alternatively size(‘A4’) with a supported papersizes or size(‘screen’) setting the current screen size as size, can be used.

# loop over a range of 100
for i in range(100):
    # for each loop create a new path
    newPage(500, 500)
    # set a random fill color
    fill(random(), random(), random())
    # draw a rect with the size of the page
    rect(0, 0, width(), height())

All supported papersizes: 10x14, 10x14Landscape, A0, A0Landscape, A1, A1Landscape, A2, A2Landscape, A3, A3Landscape, A4, A4Landscape, A4Small, A4SmallLandscape, A5, A5Landscape, B4, B4Landscape, B5, B5Landscape, Executive, ExecutiveLandscape, Folio, FolioLandscape, Ledger, LedgerLandscape, Legal, LegalLandscape, Letter, LetterLandscape, LetterSmall, LetterSmallLandscape, Quarto, QuartoLandscape, Statement, StatementLandscape, Tabloid, TabloidLandscape.

newDrawing()

Reset the drawing stack to the clean and empty stack.

# draw a rectangle
rect(10, 10, width()-20, height()-20)
# save it as a pdf
saveImage("~/Desktop/aRect.pdf")

# reset the drawing stack to a clear and empty stack
newDrawing()

# draw an oval
oval(10, 10, width()-20, height()-20)
# save it as a pdf
saveImage("~/Desktop/anOval.pdf")
endDrawing()

Explicitly tell drawBot the drawing is done. This is advised when using drawBot as a standalone module.

Size

size(width, height=None)

Set the width and height of the canvas. Without calling size() the default drawing board is 1000 by 1000 points.

Alternatively size(‘A4’) with a supported papersizes or size(‘screen’) setting the current screen size as size, can be used.

Afterwards the functions width() and height() can be used for calculations.

You have to use size() before any drawing-related code, and you can’t use size() in a multi-page document. Use newPage(w, h) to set the correct dimensions for each page.

# set a canvas size
size(200, 200)
# print out the size of the page
print((width(), height()))

# set a color
fill(1, 0, 0)
# use those variables to set a background color
rect(0, 0, width(), height())

All supported papersizes: 10x14, 10x14Landscape, A0, A0Landscape, A1, A1Landscape, A2, A2Landscape, A3, A3Landscape, A4, A4Landscape, A4Small, A4SmallLandscape, A5, A5Landscape, B4, B4Landscape, B5, B5Landscape, Executive, ExecutiveLandscape, Folio, FolioLandscape, Ledger, LedgerLandscape, Legal, LegalLandscape, Letter, LetterLandscape, LetterSmall, LetterSmallLandscape, Quarto, QuartoLandscape, Statement, StatementLandscape, Tabloid, TabloidLandscape.

sizes(paperSize=None)

Returns the width and height of a specified canvas size. If no canvas size is given it will return the dictionary containing all possible page sizes.

Page Attributes

width()

Returns the width of the current page.

height()

Returns the height of the current page.

pageCount()

Returns the current page count.

pages()

Return all pages.

# set a size
size(200, 200)
# draw a rectangle
rect(10, 10, 100, 100)
# create a new page
newPage(200, 300)
# set a color
fill(1, 0, 1)
# draw a rectangle
rect(10, 10, 100, 100)
# create a new page
newPage(200, 200)
# set a color
fill(0, 1, 0)
# draw a rectangle
rect(10, 10, 100, 100)

# get all pages
allPages = pages()
# count how many pages are available
print(len(allPages))

# use the `with` statement
# to set a page as current context
with allPages[1]:
    # draw into the selected page
    fontSize(30)
    text("Hello World", (10, 150))

# loop over allpages
for page in allPages:
    # set the page as current context
    with page:
        # draw an oval in each of them
        oval(110, 10, 30, 30)
frameDuration(seconds)

When exporting to mov or gif each frame can have duration set in seconds.

# setting some variables
# size of the pages / frames
w, h = 200, 200
# frame per seconds
fps = 30
# duration of the movie
seconds = 3
# calculate the lenght of a single frame
duration = 1 / fps
# calculate the amount of frames needed
totalFrames = seconds * fps

# title page
newPage(w, h)
# set frame duration to 1 second
frameDuration(1)
# pick a font and font size
font("Helvetica", 40)
# draw the title text in a box
textBox("Rotated square", (0, 0, w, h * .8), align="center")

# loop over the amount of frames needed
for i in range(totalFrames):
    # create a new page
    newPage(w, h)
    # set the frame duration
    frameDuration(duration)
    # set a fill color
    fill(1, 0, 0)
    # translate to the center of the page
    translate(w / 2, h / 2)
    # rotate around the center
    rotate(i*10)
    # draw the rect
    rect(-50, -50, 50, 50)

# save the image as a mov on the desktop
saveImage('~/Desktop/frameDuration.gif')
linkURL(url, (x, y, w, h))

Add a clickable rectangle for an external url link.

The link rectangle will be set independent of the current context transformations.

linkRect(name, (x, y, w, h))

Add a clickable rectangle for a link within a PDF. Use linkDestination(name, (x, y)) with the same name to set the destination of the clickable rectangle.

The link rectangle will be set independent of the current context transformations.

# a variable with the amount of pages we want
totalPages = 10
# create the first page with a index
newPage()
# set a font size
fontSize(30)
# start a loop over all wanted pages
for i in range(totalPages):
    # set a random fill color
    fill(random(), random(), random())
    # draw a rectangle
    rect(10, 50 * i, 50, 50)
    # add a clickable link rectangle with a unique name
    linkRect(f"beginPage_{i}", (10, 10 + 50 * i, 50, 50))

# start a loop over all wanted pages
for i in range(totalPages):
    # create a new page
    newPage()
    # add a link destination with a given name
    # the name must refer to a linkRect name
    linkDestination(f"beginPage_{i}", (0, 0))
linkDestination(name, (x, y))

Add a destination point for a link within a PDF. Setup a clickable retangle with linkRect(name, (x, y, w, h)) with the same name.

The destination position will be set independent of the current context transformations.