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

Random SVG

Posted by Sim Whittaker on Jun 28, 2010

I want to duplicate a series of SVG's at random sizes & variations in size etc. The code i've got so far from following the tutorials on here is below, but it doesn't really do anthing yet.

I'm pretty new to this, can anyone give me any pointers to achieve this effect? Also, is it correct that you can use an Ilustratot AI file as well? Thanks guys

size(800,1000)

svg = ximport("svg")

data = open("adam.svg").read()
paths = svg.parse(data)

transform(mode=CORNER)


def bounds(paths=[]):
""" Returns (x, y), (width, height) bounds for a group of paths.
"""
if len(paths) == 0:
return (0,0), (0,0)
l = t = float("inf")
r = b = float("-inf")
for path in paths:
(x, y), (w, h) = path.bounds
l = min(l, x)
t = min(t, y)
r = max(r, x+w)
b = max(b, y+h)
return (l, t), (r-l, b-t)

def drawpaths(paths=[], x=0, y=0, rotate=(random(),50,100), scale=(random(),50,100), origin=(random(),0,0)):
""" Draws a group of paths that rotate and scale from the given origin.
"""
_ctx.transform(CORNER)
_ctx.push()
_ctx.translate(x, y)
_ctx.rotate(rotate)
_ctx.scale(scale)
(x, y), (w, h) = bounds(paths)
_ctx.translate((-x-w)*origin[0], (-y-h)*origin[1])
for path in paths:
#_ctx.fill(path.fill)
#_ctx.stroke(path.stroke)
#_ctx.strokewidth(path.strokewidth)
# Use copies of the paths that adhere to the transformations.
_ctx.drawpath(path.copy())
_ctx.pop()

for i in range(20):
for path in paths:
try:
fill(path.fill)
except:
fill(random(), 0, 0)
drawpath(path.copy())
rotate=random(-10, 50)
scale=random(-10,30)