Last active
September 16, 2021 06:08
-
-
Save justvanrossum/a74eb9c57206b553fc28731c7ff6b8d3 to your computer and use it in GitHub Desktop.
A grid of moving rectangles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Inspired by: | |
# https://www.instagram.com/p/_hZD9hn9Xn/ | |
def square(x, y, size, phase): | |
turns = phase // 2 | |
q = (phase % 2) / 2 | |
with savedState(): | |
translate(x, y) | |
scale(size) | |
rotate(-90 * turns, center=(0.5, 0.5)) | |
rect(0, 0, 1 - q, q) | |
canvasSize = 500 | |
nSquares = 8 | |
squareSize = canvasSize / nSquares | |
nFrames = 80 | |
for frame in range(nFrames): | |
framePhase = 8 * frame / nFrames | |
newPage(canvasSize, canvasSize) | |
frameDuration(1/20) | |
rect(0, 0, canvasSize, canvasSize) | |
fill(1) | |
for i in range(nSquares): | |
for j in range(nSquares): | |
phase = 2 * (i + j) / nSquares + framePhase | |
x = i * squareSize | |
y = j * squareSize | |
square(x, y, squareSize, phase) | |
saveImage("RectangleGrid.gif") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment