Files
FreeJ/scripts/javascript/examples/test_rand.js
Jaromil a190f3f4e2 refactoring of garbage collection
testing that multiple scripts can be included from a main one
removed js garbage collection in controllers (was called after script load)
included JSON as a standard library (it works!)
modified example scripts to avoid namespace clash
2009-05-16 17:00:37 +02:00

102 lines
1.8 KiB
JavaScript

// freej script to test randomness
// this also shows keyboard interactivity
// and smart use of function pointers
// (C)2005 Denis Jaromil Rojo - GNU GPL
width = 400;
height = 300;
function draw_pixels(rand_geo) {
var x, y;
x = rand()%width;
y = rand()%height;
if(x<0) x = -x;
if(y<0) y = -y;
rand_geo.pixel(x,y);
}
function draw_triangles(rand_geo) {
var x1, x2, x3;
var y1, y2, y3;
x1 = rand()%width;
x2 = rand()%width;
x3 = rand()%width;
y1 = rand()%height;
y2 = rand()%height;
y3 = rand()%height;
if(x1<0) x1 = -x1;
if(x2<0) x2 = -x2;
if(x3<0) x3 = -x3;
if(y1<0) y1 = -y1;
if(y2<0) y2 = -y2;
if(y3<0) y3 = -y3;
rand_geo.trigon_fill(x1,y1,x2,y2,x3,y3);
}
function draw_ellipses(rand_geo) {
var x, y;
var rx, ry;
x = rand()%width;
y = rand()%height;
rx = rand()%(width/2);
ry = rand()%(height/2);
if(x<0) x = -x;
if(y<0) y = -y;
if(rx<0) rx = -rx;
if(ry<0) ry = -ry;
rand_geo.ellipse_fill(x, y, rx, ry);
}
function randomize_color(rand_geo) {
var r, g, b;
r = rand() % 255;
g = rand() % 255;
b = rand() % 255;
if(r<0) r = -r;
if(g<0) g = -g;
if(b<0) b = -b;
rand_geo.color(r,g,b,150);
}
rand_geo = new GeometryLayer(400,300);
//rand_geo.set_blit("alpha");
//rand_geo.set_blit_value(0.5);
rand_geo.activate(true);
//geo.start();
//geo.set_fps(24);
add_layer(rand_geo);
running = true;
rand_kbd = new KeyboardController();
rand_kbd.released_q = function() { quit(); }
rand_kbd.released_p = function() { drawer = draw_pixels; }
rand_kbd.released_t = function() { drawer = draw_triangles; }
rand_kbd.released_e = function() { drawer = draw_ellipses; }
register_controller( rand_kbd );
drawer = draw_triangles;
rand_bang = new TriggerController();
rand_bang.frame = function() {
randomize_color(rand_geo);
drawer(rand_geo);
}
register_controller(rand_bang);