コード例 #1
0
# which can be used to store and replay a sequence of image frames.
# This is useful for caching pre-rendered effects, like a spell or an explosion.

# This is just the explosion system from the previous example in a more condensed form.
s = System(gravity=(0, 1.0))
for i in range(80):
    s.append(Particle(x=200, y=250, mass=random(5.0,10.0), radius=5, life=30))
s.force(strength=4.5, threshold=70)

# Instead of drawing the system directly to the canvas,
# we render each step offscreen as an image,
# and then gather the images in an animation loop.
explosion = Animation(duration=0.75, loop=False)
for i in range(30):
    s.update(limit=20)
    img = render(s.draw, 400, 400)
    explosion.append(img)

# This can take several seconds:
# - calculating forces in the system is costly (if possible, load Psyco to speed them up),
# - rendering each image takes time (specifically, initialising a new empty image).
#   If possible, reduce the image frames in size (smaller images = faster rendering).

# When the user clicks on the canvas, the explosion animation is played.
# We keep a list of explosions currently playing:
explosions = []

def on_mouse_press(canvas, mouse):
    # When the mouse is clicked, start a new explosion at the mouse position.
    explosions.append((mouse.x, mouse.y, explosion.copy()))
コード例 #2
0
    ellipse(120, 120, 200, 200)
    fill(0, 1, 0, 0.5) # Transparent green.
    ellipse(170, 120, 200, 200)
    fill(0, 0, 1, 0.5) # Transparent blue. 
    ellipse(145, 160, 200, 200)
    fill(0)
    font("Droid Serif")
    text("hello", x=0, y=90, fontsize=70, width=300, align=CENTER)

# We call this a "procedural" image, because it is entirely created in code.
# Procedural images can be useful in many ways:
# - applying effects to text, 
# - caching a complex composition that is not frequently updated (for speed),
# - creating on-the-fly textures or shapes that are different every time,
# - using NodeBox from the command line without opening an application window.
img = render(function=hello, width=300, height=300)

# Note that we make the width and height of the offscreen canvas
# a little bit larger than the actual composition.
# This creates a transparent border, so effects don't get cut off
# at the edge of the rendered image.

# Images can be saved to file, even without starting canvas.run().
# To try it out, uncomment the following line:
#img.save("hello.png")

def draw(canvas):
    
    canvas.clear()

    # Apply a blur filter to the procedural image and draw it.
コード例 #3
0
# which can be used to store and replay a sequence of image frames.
# This is useful for caching pre-rendered effects, like a spell or an explosion.

# This is just the explosion system from the previous example in a more condensed form.
s = System(gravity=(0, 1.0))
for i in range(80):
    s.append(Particle(x=200, y=250, mass=random(5.0, 10.0), radius=5, life=30))
s.force(strength=4.5, threshold=70)

# Instead of drawing the system directly to the canvas,
# we render each step offscreen as an image,
# and then gather the images in an animation loop.
explosion = Animation(duration=0.75, loop=False)
for i in range(30):
    s.update(limit=20)
    img = render(s.draw, 400, 400)
    explosion.append(img)

# This can take several seconds:
# - calculating forces in the system is costly (if possible, load Psyco to speed them up),
# - rendering each image takes time (specifically, initialising a new empty image).
#   If possible, reduce the image frames in size (smaller images = faster rendering).

# When the user clicks on the canvas, the explosion animation is played.
# We keep a list of explosions currently playing:
explosions = []


def on_mouse_press(canvas, mouse):
    # When the mouse is clicked, start a new explosion at the mouse position.
    explosions.append((mouse.x, mouse.y, explosion.copy()))
コード例 #4
0
    fill(0, 1, 0, 0.5)  # Transparent green.
    ellipse(170, 120, 200, 200)
    fill(0, 0, 1, 0.5)  # Transparent blue.
    ellipse(145, 160, 200, 200)
    fill(0)
    font("Droid Serif")
    text("hello", x=0, y=90, fontsize=70, width=300, align=CENTER)


# We call this a "procedural" image, because it is entirely created in code.
# Procedural images can be useful in many ways:
# - applying effects to text,
# - caching a complex composition that is not frequently updated (for speed),
# - creating on-the-fly textures or shapes that are different every time,
# - using NodeBox from the command line without opening an application window.
img = render(function=hello, width=300, height=300)

# Note that we make the width and height of the offscreen canvas
# a little bit larger than the actual composition.
# This creates a transparent border, so effects don't get cut off
# at the edge of the rendered image.

# Images can be saved to file, even without starting canvas.run().
# To try it out, uncomment the following line:
#img.save("hello.png")


def draw(canvas):

    canvas.clear()
コード例 #5
0
    for i in range(n):
        pt1 = choice(points)
        pt2 = choice(points)
        while distance(pt1.x, pt1.y, pt2.x, pt2.y) > radius:
            pt2  = choice(points)      
        line(pt1.x + random(-m, m), 
             pt1.y + random(-m, m),
             pt2.x + random(-m, m), 
             pt2.y + random(-m, m))

# Render the function's output to an image.
# Rendering the image beforehand is much faster than calling spider() every frame.
stroke(0.1, 0.1, 0, 0.5)
strokewidth(1)

img = render(spider, 500, 150, 
    string = "SPIDER",     
      font = "Droid Sans", 
  fontsize = 100, 
      bold = True,
         x = 25,
         y = 25,
    radius = 30)

def draw(canvas):
    canvas.clear()
    translate(0, 200)
    image(img)

canvas.size = 500, 500
canvas.run(draw)
コード例 #6
0
        while distance(pt1.x, pt1.y, pt2.x, pt2.y) > radius:
            pt2 = choice(points)
        line(pt1.x + random(-m, m), pt1.y + random(-m, m),
             pt2.x + random(-m, m), pt2.y + random(-m, m))


# Render the function's output to an image.
# Rendering the image beforehand is much faster than calling spider() every frame.
stroke(0.1, 0.1, 0, 0.5)
strokewidth(1)

img = render(spider,
             500,
             150,
             string="SPIDER",
             font="Droid Sans",
             fontsize=100,
             bold=True,
             x=25,
             y=25,
             radius=30)


def draw(canvas):
    canvas.clear()
    translate(0, 200)
    image(img)


canvas.size = 500, 500
canvas.run(draw)