Skip to content

Instantly share code, notes, and snippets.

@justvanrossum justvanrossum/Jellyfish.py
Last active Jun 28, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.