// variables we need for this demo var dx, dy; // offsets of the pen strokes, in pixels var pen, prev_pen; // keep track of whether pen is touching paper var start_x, start_y; var end_x, end_y; var x, y; var rnn_state; // store the hidden states of rnn's neurons var pdf; // store all the parameters of a mixture-density distribution var temperature = 0.65; // controls the amount of uncertainty of the model var screen_width=960, screen_height=500; // stores the browser's dimensions var line_color, line_color2; var reset_counter = 0; var line_buffer = []; var restart = function() { // reinitialize variables before calling p5.js setup. // initialize the scale factor for the model. Bigger -> large outputs Model.set_scale_factor(10.0); // initialize pen's states to zero. [dx, dy, prev_pen] = Model.zero_input(); // the pen's states // randomize the rnn's initial states rnn_state = Model.random_state(); // define color of line // line_color = color(random(64, 224), random(64, 224), random(64, 224)) line_color = color(10) line_color2 = color(150, 150, 150, 100) line_color3 = color(10, 10, 10, 100) fill_r = focusedRandom(190, 210, 2.0) fill_g = focusedRandom(210, 230, 2.0) fill_b = focusedRandom(235, 255, 2.0, 255) fill(fill_r, fill_g, fill_b, 255); strokeWeight(10) }; function setup () { restart(); // initialize variables for this demo createCanvas(960, 500); frameRate(60); background(255, 255, 255, 255); fill(200, 220, 255, 255); } var min_lb_len = 20; var max_lb_len = 400; function draw () { clear(); // using the previous pen states, and hidden state, get next hidden state rnn_state = Model.update([dx, dy, prev_pen], rnn_state); // get the parameters of the probability distribution (pdf) from hidden state pdf = Model.get_pdf(rnn_state); // sample the next pen's states from our probability distribution sample = Model.sample(pdf, temperature); [dx, dy, prev_pen] = sample; line_buffer.push(sample); // keep the last elements while(line_buffer.length > max_lb_len) { var s = line_buffer.shift(); y += s[1]; } if(line_buffer.length < min_lb_len) { return; } // scan line and get total dx, dy for scaling var total_dx = 0; var total_dy = 0; for(var i=1; i 3600) { restart(); reset_counter = 0; } } function keyTyped() { if (key == '!') { saveBlocksImages(); } else if (key == '@') { saveBlocksImages(true); } else if (key == '#') { console.log("RESET") restart() } }