Responsive Turtle Graphic

 
 
 

Using a turtle graphic, this composition could respond to user's mouse location and action of left click. Whenever the mouse is pressed, a new turtle is drawn. 

To view the entire interactive Javascript, please click here.

function setup() {
    createCanvas(400, 400);
   
    frameRate(10);
}
function draw() {
    for (var i=0;i<30;i++) {
        strokeWeight(15);
        stroke(220-i);
        line(0,i*15,width,i*15);
    }//background color
  
    push();
    var turtle = makeTurtle(0, 45);
    turtle.penDown();
    turtle.setColor(255);
    turtle.setWeight(3);
    strokeJoin(MITER);
     strokeCap(PROJECT);
    var i = 0;
    while ( i < width) {
        i++;
        turtle.left(90);
        turtle.forward(40);
        turtle.right(90);
        turtle.forward(40);
        turtle.right(90);
        turtle.forward(30);
        turtle.right(90);
        turtle.forward(20);
        turtle.right(90);
        turtle.forward(10);
        turtle.right(90);
        turtle.forward(10);
        turtle.left(90);
        turtle.forward(10);
        turtle.left(90);
        turtle.forward(20);
        turtle.left(90);
        turtle.forward(30);
        turtle.left(90);
        turtle.forward(40);
    }
    noLoop();
    turtle.penUp();
    pop();//upper frame

    push();
    var turtle = makeTurtle(0, height-5);
    turtle.penDown();
    turtle.setColor(255);
    turtle.setWeight(3);
    strokeJoin(MITER);
     strokeCap(PROJECT);
    var i = 0;
    while ( i < width) {
        i++;
        turtle.left(90);
        turtle.forward(40);
        turtle.right(90);
        turtle.forward(40);
        turtle.right(90);
        turtle.forward(30);
        turtle.right(90);
        turtle.forward(20);
        turtle.right(90);
        turtle.forward(10);
        turtle.right(90);
        turtle.forward(10);
        turtle.left(90);
        turtle.forward(10);
        turtle.left(90);
        turtle.forward(20);
        turtle.left(90);
        turtle.forward(30);
        turtle.left(90);
        turtle.forward(40);
      
    }
    noLoop();
    turtle.penUp();
    pop();//bottom frame
       
}

function mousePressed()  {
    var turtle = makeTurtle(mouseX, mouseY);
    turtle.penDown();
    turtle.setColor(255);
    for (var i = 0; i < 20; i++) {
        turtle.forward(50);
        turtle.right(137.5);
        turtle.forward(50); 
    }
    turtlePenUp();
    //make random turtles

}

function turtleLeft(d){this.angle-=d;}function turtleRight(d){this.angle+=d;}
function turtleForward(p){var rad=radians(this.angle);var newx=this.x+cos(rad)*p;
var newy=this.y+sin(rad)*p;this.goto(newx,newy);}function turtleBack(p){
this.forward(-p);}function turtlePenDown(){this.penIsDown=true;}
function turtlePenUp(){this.penIsDown = false;}function turtleGoTo(x,y){
if(this.penIsDown){stroke(this.color);strokeWeight(this.weight);
line(this.x,this.y,x,y);}this.x = x;this.y = y;}function turtleDistTo(x,y){
return sqrt(sq(this.x-x)+sq(this.y-y));}function turtleAngleTo(x,y){
var absAngle=degrees(atan2(y-this.y,x-this.x));
var angle=((absAngle-this.angle)+360)%360.0;return angle;}
function turtleTurnToward(x,y,d){var angle = this.angleTo(x,y);if(angle< 180){
this.angle+=d;}else{this.angle-=d;}}function turtleSetColor(c){this.color=c;}
function turtleSetWeight(w){this.weight=w;}function turtleFace(angle){
this.angle = angle;}function makeTurtle(tx,ty){var turtle={x:tx,y:ty,
angle:0.0,penIsDown:true,color:color(128),weight:1,left:turtleLeft,
right:turtleRight,forward:turtleForward, back:turtleBack,penDown:turtlePenDown,
penUp:turtlePenUp,goto:turtleGoTo, angleto:turtleAngleTo,
turnToward:turtleTurnToward,distanceTo:turtleDistTo, angleTo:turtleAngleTo,
setColor:turtleSetColor, setWeight:turtleSetWeight,face:turtleFace};
return turtle;}