The newPage() function that we used in the previous example is used to create a new blank page of a PDF, but the great thing about DrawBot is that each new page of a PDF is really a new frame of an animation if you save your image is a gif or mov file!

DrawBot will show each frame of an animation as a separte page in its drawing area, to see the animation move you will need to tell DrawBot to save the image.

We'll jump ahead into some parts of Python that a developer with a little bit more experience would already be comfortable with (and if you're not yet, go through those resources linked to at the end of the Python Basics page!)

Building off of the previous text example, let's start by choosing a font, and filling a text box with a string.

# Document size
size(300, 300)

# Set a background color by drawing a rect
fill(1, 0.95, 0.85)
rect(0, 0, 300, 300)
# Create a string, and hold it aside under a variable name
greeting = "Hello!"
# Set the font, and the font size
font("American Typewriter", 50)
# Before drawing the text, set the text color
fill(1, 0, 0.5)
# Make a text box with our string
textBox(greeting, (10, 10, 280, 280))

Nothing all that exciting yet, but it's a start.

Let's take another lesson from a previous example, using a for loop to repeat some code.

We'll have the loop count through range(20), meaning that the same code will be repeated 20 times, and with each time through we'll end our instructions with newPage() to tell DrawBot to prepare a new page for the next loop to draw into.

# Document size
size(300, 300)

for i in range(20):
    # Set a background color by drawing a rect
    fill(1, 0.95, 0.85)
    rect(0, 0, 300, 300)
    # Create a string, and hold it aside under a variable name
    greeting = "Hello!"
    # Set the font, and the font size
    font("American Typewriter", 50)
    # Before drawing the text, set the text color
    fill(1, 0, 0.5)
    # Make a text box with our string
    textBox(greeting, (10, 10, 280, 280))
    # Make a new page after drawing
    newPage()

saveImage("/users/USERNAME/Desktop/animation.gif")

Just remember to change the USERNAME to your own user name, otherwise DrawBot will give you an error.

Great! If all went well, you'll see a sequence of the frames of the animation off to the left of the window, and an animated gif should have saved to your desktop. You can open the gif in a web browser, or select it and tap the space bar to open it in Quick Look to see it in motion.

But there's no motion yet! We've drawn 20 of the same frame. You will however notice that there's one completely blank frame at the end of the animation — our code set up a new blank page at the end of each loop, which means that the last time through it made a blank page but drew nothing into it.

I'll make a small change in two places, first I'll write a line of code to only make a newPage() when the condition if i < 19 is True. Remember, each time the for loop fetches a number out of the range(20) it gets assigned to the variable name i so we can have the code keep track of which frame it is on by watching this variable.

To make things move, how about we also subtract i from the font size. This means that in the first frame the font will be 50 - 0 points in size (since counting starts from zero), the next frame will be 50 - 1 in size, and so on.

# Document size
size(300, 300)

for i in range(20):
    # Set a background color by drawing a rect
    fill(1, 0.95, 0.85)
    rect(0, 0, 300, 300)
    # Create a string, and hold it aside under a variable name
    greeting = "Hello!"
    # Set the font, and the font size
    font("American Typewriter", 50 - i)
    # Before drawing the text, set the text color
    fill(1, 0, 0.5)
    # Make a text box with our string
    textBox(greeting, (10, 10, 280, 280))
    # Make a new page after drawing
    # unless if this is the last frame
    if i < 19:
        newPage()

saveImage("/users/USERNAME/Desktop/animation.gif")

Now it's moving!

Keep changing other variables, remember that it's fun and easy to add, subtract, multiply and divide numbers by i so that they move according to the frame number.

Let's multiply the string by i so that it repeats the greeting:

# Document size
size(300, 300)

for i in range(20):
    # Set a background color by drawing a rect
    fill(1, 0.95, 0.85)
    rect(0, 0, 300, 300)
    # Create a string, and hold it aside under a variable name
    greeting = "Hello!" * i
    # Set the font, and the font size
    font("American Typewriter", 50 - i)
    # Before drawing the text, set the text color
    fill(1, 0, 0.5)
    # Make a text box with our string
    textBox(greeting, (10, 10, 280, 280))
    # Make a new page after drawing
    # unless if this is the last frame
    if i < 19:
        newPage()

saveImage("/users/USERNAME/Desktop/animation.gif")

With what you learned here, go back to an earlier example and see if you can get it to move. Keep messing with the variables and see what happens!

size(300, 300)

for frame in range(20):
    # Background color
    fill(0.9, 1, 1)
    rect(0, 0, 300, 300)
    # Draw a bunch of ovals
    for i in range(frame):
      fill(i/20, frame/40, 1)
      size = i/ frame * 100
      oval(i*10, i*10, size, size)
    newPage()

saveImage("/users/USERNAME/Desktop/animation.gif")

This guide was first published on Dec 14, 2018. It was last updated on Dec 14, 2018.

This page (Animation) was last updated on Dec 12, 2018.

Text editor powered by tinymce.