// David Tinapple Task06 10-06-02


// init all the necessary variables once with meaningless values
var time = 0;
var timeadd = 0;
var x_loops = 0;
var y_loops = 0;
var x = 0;
var y = 0;
var new_x = 0;
var new_y = 0;


// time goes from 0 to 1
// when time hits 1 this function is called
// it clears the canvas, resets the penweight, resets time, and randomizes some variables
function initCanvas() {
Canvas.setPenWeight(.01);
Canvas.setBackgroundColor(1, 1, 1);
time = 0;
timeadd = (Math.random() * .01) + .001;
x_loops = (10 + random(100)); // random x curlieness
y_loops = (10 + random(100)); // random y curlieness
x = .5;
y = 1;
new_x = 0;
new_y = 0;
}


// the drawing function
// is called for each new frame
// uses sine and cosine to draw the line
// starts thick and light and as time goes from 0-1 gets darker and thiner
function graphline() {
new_x = ((Math.sin(Math.PI * x_loops * time)) + 1) * .5 ; // essentially, x = sin(time)
new_y = ((Math.cos(Math.PI * y_loops * time)) + 1) * .5 ; // essentially, y = cos(time)
Canvas.setPenWeight(.001 + ((1 - time) * .1)); // draw thinner and thinner
Canvas.setPenColor((1 - time), (1 - time), (1 - time)); //draw darker and darker
// multiply by .8 and add .1 in both x and y to avoide cliping the edges
Canvas.drawLine(((x * .8)+.1),((y * .8)+.1), ((new_x * .8) + .1) , ((new_y * .8) + .1) ); // draw a line from old position to new
x = new_x; // make the new position old
y = new_y; // make the new position old
time = time + timeadd; // move time forward
if (time > 1) initCanvas(); // at the end of the drawing, clear and start over
}


// the main program
Canvas.onEnterFrame = graphline;
this.onLoad = initCanvas;