More examples
(
// first boot the localhost server
s.boot;
)
(
// then, make a synthdef
SynthDef("grain", { arg freq = 440, mul = 1, dur = 1;
Out.ar(
0,
SinOsc.ar(freq, 0, mul)
*
EnvGen.kr(Env.perc(0.01, 0.99), doneAction: 2, timeScale: dur)
)
}).load(s);
)
(
// plot grains, x = time, y = freq, grainAmplitude = alpha color of ovals
// Rendering may take a few seconds ...
var routine;
var num = 51;
var x = 0, y = 0;
var txt;
txt = "Grains";
n = NB.new("granular", 600, 470);
n.publishCode(
"fontsize".cmd(24),
"text".cmd(txt, 10, 25, n.width, 50),
"stroke(random(), random(), random(), random())",
"strokewidth(1)",
"line(0, HEIGHT / 2, WIDTH, HEIGHT / 2)"
);
routine = Routine({
var freq, mul, dur, scaleOval;
scaleOval = 100;
num.do({ arg item, i;
x = (n.width / num) + x;
y = freq = 100.rrand(n.height);
mul = 0.01.rrand(0.75);
dur = 0.1.rrand(1);
Synth("grain", [\freq, freq, \mul, mul, \dur,dur]);
n.fill(1.0.rand, 1.0.rand, 1.0.rand, mul);
n.oval(10 * i + 10, n.height - y, scaleOval * dur, scaleOval * mul);
0.1.wait;
});
n.renderAndDisplayImage
});
routine.play(AppClock);
)
/* ----- ----- ----- ----- */
/* ----- ----- ----- ----- */
(
n = NB.new("testing", 500, 400);
n.publish(
"
fill(random(), random(), random(), random(0, 0.3))
rect(0, 0, WIDTH, HEIGHT)
fill
fill(random(), random(), random(), random())
rect(10, 10, 30, 30)
oval(40, 40, 20, 10)
for i in range(10):
fill(random(), random(), random(), random())
rect(random(40, 200), random(100, 200), random(10, 20), random(10, 20))
for i in range(111):
fill(random(), random(), random(), random())
skew(random(-20, 20), random(-20, 20))
text(choice((\"this\", \"may\", \"be\")), random(0, WIDTH), random(0, HEIGHT))
"
);
n.renderAndDisplayImage
)
/* ----- ----- ----- ----- */
/* ----- ----- ----- ----- */
(
// copied very literally from the nodebox demo of the same name
n = NB.new("HowCurvesWork", 200, 200);
n.publishAndDisplayCode(
"# This boring example demonstrate how curves work, and goes a bit
# into the different parameters for drawing curves on screen.
import math
# Setup colors: no fill is needed, and stroke the curves with black.
nofill()
stroke(0)
# Set the initial position
x,y = 50, 50
width = 50
# The dx and dy parameters are the relative control points.
# When using math.pi/2, you actually define the lower half
# of a circle.
dy = width/(math.pi / 2)
# Begin drawing the path. The starting position is on the
# given x and y coordinates.
beginpath(x, y)
# Calculate the control points.
cp1 = (x, y + dy)
cp2 = (x + width, y + dy)
# Draw the curve. The first four parameters are the coordinates
# of the two control curves; the last two parameters are
# the coordinates of the destination point.
curveto(cp1[0], cp1[1], cp2[0], cp2[1], x + width, y)
# End the path; ending the path automatically draws it.
endpath()
# To demonstrate where the control points actually are,
# we draw them using lines.
# The first control point starts at the x,y position.
line(x, y, cp1[0], cp1[1])
# The second control point is the ending point.
line(x + width, y, cp2[0], cp2[1])
# To liven things up just a little bit, little ovals are
# drawn in red on the position of the control points.
nostroke()
fill(1,0,0)
oval(cp1[0] - 2, cp1[1] - 2, 4, 4)
oval(cp2[0] - 2, cp2[1] - 2, 4, 4)"
)
)
/* ----- ----- ----- ----- */
/* ----- ----- ----- ----- */
(
var num = 20;
var box;
var off = 5;
var w = 150;
var h = 40;
n = NB("square", 600, 400);
n.background(0.9.rand + 0.25, 0.8.rand + 0.4, 0.8.rand + 0.2);
n.text("Shuffle cards", 425, 50);
num.do({ arg item, i;
n.translate("random(-15, 15)", "random(-15, 15)");
n.stroke(0, 1, 0, i/num);
n.box(10 + (i * off), 10 + (i * off), w, h)
});
num.do({ arg item, i;
n.stroke(1, 0, 0, i/num);
n.box(100 + (i * off), 100 + (i * off), w, h)
});
(num * 2).do({ arg item, i;
n.stroke(0, 0, 1, i/num);
n.box(95.rrand(105) + (i * off), 10 + (i * off), w, h)
});
num.do({ arg item, i;
n.stroke("random()", "random()", "random()", i/num);
n.box(190 + (i * off), 120 + (i * off), w, h)
});
n.renderAndDisplayImage;
)
/* ----- ----- ----- ----- */
/* ----- ----- ----- ----- */
(
// Not an efficient way to draw lines
n = NB.new("rect_points");
n.basicPointPrim_("rect");
y = n.height / 2;
n.width.do({ arg xCoord, i;
n.point(xCoord, y)
});
n.renderAndDisplayImage
)
(
// Scatter
n = NB.new("oval_points");
n.basicPointPrim_("oval");
n.width.do({ arg xCoord, i;
n.point(n.width.rand, n.height.rand)
});
n.renderAndDisplayImage
)
matrix(neo, smith, func, copies)
neo - number of points on the x axis
smith - number of points on the y axis
func - a function, evaluated for each x,y pair
copies - a number or a function; how many copies of the matrix? The 2nd film had 6? 7? 8?
layered - a boolean (true or false) - are matrices layered on top of each other or are they distributed equally across the canvas? (default is true).
(
var box;
box = { arg x, y;
n.stroke(1);
n.beginpath(x, y);
n.lineto(x + 100, 100 + y);
n.endpath;
n.beginpath(x, 100 + y);
n.lineto(x + 100, 0 + y);
n.endpath;
n.fill(1, 0, 0);
n.oval(45 + x, 45 + y, 10, 10);
50.rrand(100).do({
n.fill("random()", "random()", "random()");
if(0.5.coin, {
n.point(x + 25.rand2, y + 25.rand2)
},{
n.python(["rect", "oval"].choose.cmd(
x + 30.rand2,
y + 30.rand2,
10.rand,
10.rand
))
});
});
n.skew("random(-20, 20)", "random(-20, 45)");
n.fontsize("random(13, 24)");
n.python(
"text".cmd(
["where", "is", "that", "much", "needed", "help", "file", "dog"].choose,
x + 30,
y + 40
)
);
n.reset;
};
#r, g, b, a = Array.fill(4, { "random(0, 0.5)" });
n = NB("matrix1", 400, 400, true);
n.background(r, g, b, a);
n.matrix(400, 400, { arg x, y;
case
{ x==0 and: { y==0 } } { box.value(x, y) }
{ x==100 and: { y==0 }} { box.value(x, y) }
{ x==200 and: { y==0 }} { box.value(x, y) }
{ x==300 and: { y==0 }} { box.value(x, y) }
{ x==0 and: { y==100 }} { box.value(x, y) }
{ x==100 and: { y==100 }} { box.value(x, y) }
{ x==200 and: { y==100 }} { box.value(x, y) }
{ x==300 and: { y==100 }} { box.value(x, y) }
{ x==0 and: { y==200 }} { box.value(x, y) }
{ x==100 and: { y==200 }} { box.value(x, y) }
{ x==200 and: { y==200 }} { box.value(x, y) }
{ x==300 and: { y==200 }} { box.value(x, y) }
{ x==0 and: { y==300 }} { box.value(x, y) }
{ x==100 and: { y==300 }} { box.value(x, y) }
{ x==200 and: { y==300 }} { box.value(x, y) }
{ x==300 and: { y==300 }} { box.value(x, y) };
});
n.renderAndDisplayImage(5);
)
/* ----- ----- ----- ----- */
/* ----- ----- ----- ----- */
(
#r, g, b, a = Array.fill(3, { "random(0.6, 1.0)" });
w = h = 400;
c = w/10;
x = y = c/2;
n = NB("matrix2", 400, 400, true);
n.background(r, g, b);
n.matrix(w, h, {
arg neo, smith;
if(neo == smith or: { neo == (h-smith + 60.rand2) } or: { neo == (h - smith) }, {
#r, g, b = Array.fill(3, { 1.0.rand });
n.fill(r, g, b);
n.point(neo, smith);
if(r<0.01, {
n.skew("random(-20, 20)", "random(-20, 45)");
n.fontsize("random(10, 16)");
n.python(
"text".cmd(
["where", "is", "the", "help", "readme", "file"].choose,
neo + 30,
smith + 40
)
);
n.reset;
});
if((r+g+b)>2, {
n.python(["rect", "oval"].choose.cmd(neo.rand, smith.rand, 4, 3.rrand(6)));
if((r+g+b )>2.24, {
n.python(["rect", "oval"].choose.cmd(neo, smith, 4, 3.rrand(6)))
});
});
if((r+g+b)<0.5, {
#r, g, b, a = [r, g, b, r+g+b * 1.5].scramble;
n.fill(r, g, b, a);
n.python(["rect", "oval"].choose.cmd(neo, smith, 7.rrand(11), 9.rrand(16)))
});
});
}
);
#r, g, b, a = Array.fill(4, { "random()" });
n.fill(r, g, b, a);
n.oval(w/2 - x, h/2 - y, c, c);
n.renderAndDisplayImage(10)
)