typemytype / drawbot

http://www.drawbot.com
Other
398 stars 61 forks source link

`linkRect` example #489

Open roberto-arista opened 1 year ago

roberto-arista commented 1 year ago

Hey all!

I need to make some internal links in a PDF, and I've tried the example for linkRect. I've found it a bit confusing. So I edited it a bit:

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

# start a loop over all wanted pages
for i in range(totalPages):
    # create a new page
    newPage(300, 300)
    # add a link destination with a given name
    # the name must refer to a linkRect name
    linkDestination(f"beginPage_{i}", (width(), height()))
    # set the font size
    fontSize(30)
    # draw the page number
    text(f"{i}", (width()/2, height()/2))

I added page numbers, reduced a bit the variability of the TOC rects, and adjusted the landing point of linkDestination to the top of the page, so that the expected page is inside the viewport (instead of the following one).

If you like it, I can open a pull request