Begin your 2023 with Genuary! #742
EthanThatOneKid
started this conversation in
Newsletter
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
ACM at CSUF welcomes Genurary 2023!
Welcome to your introductory Genuary 2023 codelab where you will learn how to use https://editor.p5js.org/ to generate artwork for Genuary 2023.
What is Genuary? ❄️
Genuary 2023's official website https://genuary.art/ defines Genuary as:
There is a unique prompt for each day in the month of January. All of these prompts encourage the practice of creative expression in programming.
In our club Discord server, members are encouraged to share their creations throughout the monthlong event: Genuary 2023.
What is this codelab about?
In this codelab, you will learn how to create your first generative artwork with https://editor.p5js.org/ for Genuary 2023.
Once you have completed this codelab, you will have created a generative artwork using the 10print algorithm.
Quickly, what is 10print?
10print is a generative algorithm that creates a pattern of lines.
source: Wikipedia
Our algorithm will draw out the pattern of lines on a canvas from the top left corner to the bottom right corner.
The algorithm is based on the following rules:
Supplemental reading:
Set up https://editor.p5js.org/ for Genuary
Sign in
Sign in to https://editor.p5js.org/ with your GitHub account.
Create a new sketch
To create a new sketch from scratch, go to https://editor.p5js.org/ and click on
File>New.Save the sketch with whatever name you like, or use a pattern such as
genuary-2023-01-01.In your sketch, you will see a
setup()function and adraw()function.Start by removing
background()from thedraw()function.Also, add a
stroke()function to thesetup()function to set the color of the lines to white.Make globals for the sketch
Above
setup(), we will define global variables for the sketch.Draw a diagonal line
In the
draw()function, draw a diagonal line withline().Press to reveal the code
Run▶️ the sketch to see that the line is drawn from the top left corner to the bottom right corner of the first square, which is 10 pixels by 10 pixels.
Draw a second diagonal line
To draw a second diagonal line, we will need to change the value of
xbyspixels in each frame to move the next diagonal line to the right.Press to reveal the code
Run▶️ the sketch to see that an entire row of diagonal lines is drawn.
Draw the second row of diagonal lines
To draw the second row of diagonal lines, we will need to change the value of
ybyspixels each timexreaches the end of the canvas to move the next diagonal line down. Do not forget to sendxback to0whenxreaches the end of the canvas.Press to reveal the code
Run▶️ the sketch to see that the entire canvas is filled with diagonal lines, but they are all drawn from the top left corner to the bottom right corner.
How many frames does it take to draw the entire canvas?
Since we know the
widthandheightof the canvas, we can calculate the number of frames it takes to fill the canvas with one diagonal line per frame.To do this, we will need to know the number of squares that fit horizontally and vertically on the canvas.
We can then multiply the number of squares that fit horizontally by the number of squares that fit vertically to get the total number of squares.
Press to reveal the answer
Assuming you are drawing one diagonal line per frame, we can then calculate the number of frames it takes to fill the canvas given that the canvas is
400pixels by400pixels and each square is10pixels by10pixels.Keep in mind how to get the number of frames it takes to fill the canvas in order to create a GIF later.
Randomize the direction of the diagonal line
To randomize the direction of the diagonal line, we can use
random()to generate a random number between0and1.We can tell our program to draw a line from the top left corner to the bottom right corner if the random number is greater than
0.5, and to draw a line from the bottom left corner to the top right corner of the random number is less than0.5.Press to reveal the code
Run▶️ the sketch to see that the direction of the diagonal line is randomized.
Draw infinitely
To start the animation over when it reaches the end of the canvas, we can set the
yvalue back to0whenyreaches the end of the canvas andclear()the canvas.Press to reveal the code
Save the animation as a GIF
Use
saveGif()to save the animation as a GIF.Hint: Review
saveGif()short on YouTube.Press to reveal the code
Allocate a boolean variable
filmingto keep track of whether the animation is being recorded to GIF.Set
filmingtofalsewhen the animation completes.Make your call to
saveGif()in themouseClicked()function. When the mouse is pressed, the animation will be restarted and recorded to GIF.Feel free to change the name of the GIF, but make sure the number of frames is the same as the number of frames it takes to fill the canvas.
Run▶️ the sketch and then click the canvas with your mouse to see that the animation is saved as a GIF.
Congratulations!
You have created your first animation for Genuary 2023 which concludes this
codelab!
Press to reveal the completed codelab
Next steps
Feel free to add more features to your animation, such as:
If you are interested in learning more about p5.js, check out the following references:
More on
saveGif()It is no coincidence that this codelab features the
saveGif()API. The first prompt of Genuary 2023 is "Perfect loop / Infinite loop / endless GIFs". ThesaveGif()API provided by p5.js is a powerful tool that happens to help with Genuary 2023 day 1 in particular, and even with other prompts later this month at the artist's discretion.Social media
You are encouraged to share your GIF on your favorite social media and tag it with #genuary and #genuary2023 and also #genuary1, #genuary2, etc, depending on which prompt you are working on.
Do not forget to send your GIF to the associated thread on our club Discord server, https://acmcsuf.com/discord!
Credits
Beta Was this translation helpful? Give feedback.
All reactions