1. NodeBox 1
    1. Homepage
    2. NodeBox 3Node-based app for generative design and data visualization
    3. NodeBox OpenGLHardware-accelerated cross-platform graphics library
    4. NodeBox 1Generate 2D visuals using Python code (Mac OS X only)
  2. Gallery
  3. Documentation
  4. Forum
  5. Blog

Boids in Core Image

Posted by Tom De Smedt on May 19, 2008



Ever wondered how to create a visualizer like the one in iTunes? Me too. Here is a simple test of a dynamic Core Image with some boids flying around in it.

Each frame, a screenshot of the current composition is added to the background and scaled, rotated, faded and blur, leading to the smoke trail effect.

# FIREFLIES
#############################################################################
 
size(640, 480)
 
# Create a flock of boids.
boids = ximport("boids")
flock = boids.flock(6, 0, 0, WIDTH, HEIGHT)
flock.goal(WIDTH/2, HEIGHT/2, 0)
flock.noperch()
flock.scatter(chance=0.005, frames=10)
for i in range(10): 
    flock.update()
 
# Create a cached circle layer representing a boid.
# This is faster than creating the path from scratch each frame.
coreimage = ximport("coreimage")
canvas = coreimage.canvas(WIDTH, HEIGHT)
coreimage.PATH_PADDING = 0
firefly = canvas.append(oval(0, 0, 8, 8, draw=False), fill=color(1))
firefly.blur = 0
firefly = firefly.render()
screenshot = None
 
speed(30)
def draw():
    #flock.goal(MOUSEX, MOUSEY, 0)
    background(0.05, 0.05, 0)
    canvas = coreimage.canvas(WIDTH+10, HEIGHT+10)
    # Each frame, put a screenshot of the composition on the background,
    # scale, rotate, fade and blur it.
    global screenshot
    if screenshot:
        l = canvas.append(screenshot)
        l.scale(1.005)
        l.rotate(0.3)
        l.opacity=0.985
        l.blur = 1.0
    # Update the boids' position and draw them on the canvas.
    # Colorize each boid according to its x, y, and z-position.
    flock.update(limit=10, cohesion=40, alignment=100, goal=100)
    for boid in flock:
        l = canvas.append(firefly)
        l.filter("levels", 
            r=boid.y*0.020, 
            g=boid.z*0.010, 
            b=boid.z*0.001)
        l.x = boid.x
        l.y = boid.y
        l.opacity = 0.3 + boid.z*0.01
    # Grab a screenshot.
    screenshot = canvas.flatten()
    canvas.draw()