# DrawBot: Generate a jellyfish-like pulsating blobby animation.

> Source: <https://gist.github.com/justvanrossum/b65f4305ffcf2690bc65>
> Published: 2016-01-06 20:16:10+00:00

Jellyfish.py

      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

 
    Show hidden characters

# 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")
