alchan-Project 05-wallpaper

sharp

// palette
var dark = {r: 179, g: 191, b: 191}
var medium = {r: 203, g: 214, b: 214}
var light = {r: 217, g: 225, b: 225}

function setup() {
    createCanvas(480, 480);
    background(250);
    rectMode(CENTER);
    angleMode(DEGREES);
    noLoop();
}

function draw() {
    var offsetX = 50;
    var offsetY = 52;
    var rowNum;
    var slant = -20;
    var which = 1;

    for(var y = 0; y < 5; y++) {
      //check if the row is odd or even and offset odd rows
      if(y % 2 == 0) {
        offsetX = 10;
        rowNum = 6;
      } else {
        offsetX = 60;
        rowNum = 5;
      }
      for(var x = 0; x < rowNum; x++) {
        scratch(x*130 - offsetX, y*150 - offsetY);

        //determine which type of knife to draw
        which = nf(random(0,3)+1, 1, 0);
        if(which == 1) {
          sharp(x*100 + offsetX, y*100 + offsetY, slant);
        } else if(which == 2) {
          sharp2(x*100 + offsetX, y*100 + offsetY, slant);
        } else if(which == 3) {
          sharp3(x*100 + offsetX, y*100 + offsetY, slant);
        }
      }
    }
}

// KNIFE 1
function sharp(xPos, yPos, angle) {
  var sharpX = 0;
  var sharpY = 0;

  var sharpW = 8;
  var sharpH = 30;
  var guardW = 10 + sharpW;
  var guardH = 3;
  var handleW = 6;
  var handleH = 20;

  noStroke();
  push();
  translate(xPos, yPos);
  rotate(angle);
  // blade 1
  fill(medium.r, medium.g, medium.b);
  rect(sharpX, sharpY, sharpW, sharpH);
  triangle(sharpX + sharpW/2, sharpY + sharpH/2,
    sharpX, sharpY + sharpH/2,
    sharpX, sharpY + sharpH/2 + 12);
  fill(light.r, light.g, light.b);
  rect(sharpX - sharpW/4, sharpY, sharpW/2, sharpH);
  triangle(sharpX - sharpW/2, sharpY + sharpH/2,
    sharpX, sharpY + sharpH/2,
    sharpX, sharpY + sharpH/2 + 12);

  // guard 1
  fill(dark.r, dark.g, dark.b);
  rect(sharpX, sharpY - sharpH/2 - guardH/2, guardW, guardH);
  ellipse(sharpX - guardW/2, sharpY - sharpH/2 - guardH/2, guardH * 1.5);
  ellipse(sharpX + guardW/2, sharpY - sharpH/2 - guardH/2, guardH * 1.5);

  // handle 1
  rect(sharpX, sharpY - sharpH/2 - handleH/2, handleW, handleH);

  // pommel 1
  ellipse(sharpX, sharpY - sharpH/2 - handleH, handleW + 2);
  pop();
}

// KNIFE 2
function sharp2(xPos, yPos, angle){
  var sharp2X = -5;
  var sharp2Y = -18;

  var sharp2W = 25;
  var sharp2H = 80;
  var handle2W = 6;
  var handle2H = 22;

  noStroke();
  push();
  translate(xPos, yPos, angle);
  rotate(angle);
  // blade 2
  fill(medium.r, medium.g, medium.b);
  arc(sharp2X, sharp2Y, sharp2W, sharp2H, 0, 90, PIE);
  fill(light.r, light.g, light.b);
  rect(sharp2X - 1, sharp2Y + sharp2H/4, 2, sharp2H/2);

  // handle 2
  fill(dark.r, dark.g, dark.b);
  rect(sharp2X + sharp2W/2 - handle2W/2, sharp2Y - handle2H/2,
    handle2W, handle2H);
  ellipse(sharp2X + sharp2W/2 - handle2W/2, sharp2Y - handle2H,
    handle2W);
  pop();
}

// KNIFE 3
function sharp3(xPos, yPos, angle) {
  var sharp3X = 0;
  var sharp3Y = 0;

  var sharp3W = 8;
  var sharp3H = 30;
  var handle3W = 6;
  var handle3H = 20;
  var guard3W = sharp3W + 4;
  var guard3H = 4;

  noStroke();
  push();
  translate(xPos, yPos);
  rotate(angle);

  // handle 3
  fill(dark.r, dark.g, dark.b)
  rect(sharp3X, sharp3Y - sharp3H/2 - handle3H/2,
    handle3W, handle3H);

  // guard 3
  rect(sharp3X, sharp3Y - sharp3H/2 - guard3H/2-1,
    guard3W, guard3H);
  // pommel 3
  ellipse(sharp3X, sharp3Y - sharp3H/2 - handle3H,
    handle3W+3);
  fill(light.r, light.g, light.b);
  ellipse(sharp3X, sharp3Y - sharp3H/2 - handle3H,
    handle3W-2);

  // blade 3
  fill(light.r, light.g, light.b);
  quad(sharp3X - sharp3W/2, sharp3Y - sharp3H/2,
    sharp3X + sharp3W/2, sharp3Y - sharp3H/2,
    sharp3X + sharp3W/2, sharp3Y + sharp3H/2 + 8,
    sharp3X - sharp3W/2, sharp3Y + sharp3H/2);
  // highlights
  fill(medium.r, medium.g, medium.b);
  quad(sharp3X - sharp3W/2, sharp3Y,
    sharp3X + sharp3W/2, sharp3Y + 8,
    sharp3X + sharp3W/2, sharp3Y + 4,
    sharp3X - sharp3W/2, sharp3Y - 4);
    quad(sharp3X - sharp3W/2, sharp3Y - 8,
      sharp3X + sharp3W/2, sharp3Y,
      sharp3X + sharp3W/2, sharp3Y - 16,
      sharp3X - sharp3W/2, sharp3Y - 16);
    pop();
}

// LINES
function scratch(xPos, yPos) {
  var spacing = 20;

  noFill();
  stroke(255);
  strokeWeight(4);
  line(xPos, yPos, xPos + 100, yPos + 100);
  line(xPos, yPos + spacing, xPos + 100, yPos + 100 + spacing);
  line(xPos, yPos + spacing*2, xPos + 100, yPos + 100 + spacing*2);

}

I wanted to play around with creating functions and more complex shapes, so the elements themselves are more complicated than the repetition. The type of knife is also randomly generated, so the pattern is slightly different every time.

Leave a Reply