Skip to content

Instantly share code, notes, and snippets.

@justvanrossum
Last active March 10, 2020 17:11
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justvanrossum/b65f4305ffcf2690bc65 to your computer and use it in GitHub Desktop.
Save justvanrossum/b65f4305ffcf2690bc65 to your computer and use it in GitHub Desktop.
DrawBot: Generate a jellyfish-like pulsating blobby animation.
# Generate a jellyfish-like pulsating blobby animation.
from random import seed
def varyPoint(pt, radius, phase):
x, y = pt
dx = radius * cos(phase)
dy = radius * sin(phase)
return x + dx, y + dy
def drawBlob(blobPhase, blobRadius):
points = [] # list of off curve points forming the blob.
for i in range(numBlobPoints):
a = 2 * pi * i / numBlobPoints
x = blobRadius * cos(a)
y = blobRadius * sin(a)
rPhase, rSign = randomPhases[i]
points.append(varyPoint((x, y), 0.2 * blobRadius, rPhase + rSign * 2 * pi * blobPhase))
# Add a final 'fake' point, to tell the pen there is *no* on curve point at all
points.append(None)
bezPath = BezierPath()
bezPath.qCurveTo(*points)
bezPath.closePath()
drawPath(bezPath)
seed(0) # Ok ok, make this animation predictable.
numBlobPoints = 5
randomPhases = [(2 * pi * random(), choice([-1, 1])) for i in range(numBlobPoints)]
canvasSize = 500
nBlobs = 24
nFrames = 95
for frame in range(nFrames):
framePhase = frame / nFrames
newPage(canvasSize, canvasSize)
frameDuration(1/20)
fill(0)
rect(0, 0, canvasSize, canvasSize)
translate(canvasSize/2, canvasSize/2)
strokeWidth(2)
stroke(1)
fill(None)
for i in range(nBlobs):
blobPhase = i / nBlobs
radius = 5 + i * 10
drawBlob(framePhase + blobPhase * 0.75, radius)
saveImage("Jellyfish.gif")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment