daphnel-Project 11-Composition

Composition

var turtle;
var turtles=[];
var nTurtles=10;
var s;
var r;
var g;
var b;

function setup(){
    createCanvas(480,480);
    background(0);
    frameRate(10);
    //background before the black and white turtles appear
    var turtle=makeTurtle(width/2,height/2);
    turtle.setColor(color(255,255,0,100));
    turtle.setWeight(1);
    turtle.penDown();
    var goldenRatio=1+sqrt(5)/2;
    for(var i=0; i<150; i++){
    var s=5;
    turtle.right(60*goldenRatio);
    turtle.forward(s*i);
    }
}

function draw(){
    for(var i=0; i<turtles.length; i++){
        var step=random(-3,3);
        turtles[i].setWeight(i*3);
        var r=random(100,255);
        var g=random(150,255)
        var b=random(150,255)
        turtles[i].setColor(r,g,b);
        turtles[i].penDown();
        turtles[i].forward(step*i);
        turtles[i].left(25);
        turtles[i].penUp();
    }
}
function mousePressed(){
    //click to make turtles appear
    turtles.push(makeTurtle(mouseX,mouseY));

}
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;
}

While I was experimenting with the golden ratio assignment previously, I was able to make some interesting designs as I played around with the code. I decided to use one of them as a basis for my background. This code starts off with a canvas with a patterned background. When you click on the canvas, turtles are made. I tend to like it when the entire canvas is covered by something and is constantly flickering like lights.

Leave a Reply