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

Dendrite

Yesterday Frederik wrote this inspiring interactive script that draws living lines. You can press the mouse and draw lines, and then they come alive like organic, pulsating dendrites.

You can learn an interesting trick or two about interaction, sine waves and Bezier paths by looking at the source code.

 


Play movie

size(400, 400)
speed(100)
from random import seed
from math import sin
 
lines = []
down = False
 
def append_line():
    global down
    if mousedown:
        if len(lines) > 0 and lines[-1] == (MOUSEX, MOUSEY):
            return
        if down:
            lines.append((LINETO, MOUSEX, MOUSEY, FRAME))
        else:
            down = True
            lines.append((MOVETO, MOUSEX, MOUSEY, FRAME))
    else:
        down = False
        
 
def draw_line():
    if len(lines) == 0: return
    nofill()
    stroke(1,1,1,0.5)
    strokewidth(0.25)
    p = BezierPath()
    for cmd, x, y,t in lines:
        if cmd == MOVETO:
            p.moveto(x, y)
        else:
            p.lineto(x, y)
    p.inheritFromContext()
    p.draw()    
 
def draw_ch_line():
    for i in range(5):
        seed(i)
        if len(lines) == 0: return
        nofill()
        stroke(1,1,1,1)
        strokewidth(0.1)
        p = BezierPath()
        for cmd, x, y, t in lines:
            d = sin((FRAME - t) / 10.0)*15.0
            x += random(-d, d) 
            y += random(-d, d)
            if cmd == MOVETO:
                p.moveto(x, y)
            else:
                p.lineto(x, y)
        p.inheritFromContext()
        p.draw()    
 
def setup():
    pass
 
def draw():
    background(0.18,0.0,0.15)
    append_line()
    draw_ch_line()

 

Created by Frederik De Bleser