Pieces

to generate a new and unique variation

Source Code

Every variart piece is a small program. This is the one that drew what you're looking at.

var shadow = s.filter(Snap.filter.shadow(0, 1, 2, '#000', 0.25));
var background = s.gradient("r(0.5, 0.5, 0.5)#C9BBAA-#B3A392");

var colors = ['#ABADAA', '#ECDEC6', '#D6CBBE', '#51342D', '#66473A', '#131217'];
var margin = 50;

var count = intVariable('Pieces', 8, 30);
var tilt = intVariable('Tilt', 0, 40);

s.rect(0, 0, width, height).attr({fill: background});

// Keeps a slat's rotated corners on the face of the canvas.
function fits(x, y, w, h, angle){
  var a = angle * Math.PI / 180;
  var dx = Math.abs(w / 2 * Math.cos(a)) + Math.abs(h / 2 * Math.sin(a));
  var dy = Math.abs(w / 2 * Math.sin(a)) + Math.abs(h / 2 * Math.cos(a));
  return x - dx >= margin && x + dx <= width - margin &&
         y - dy >= margin && y + dy <= height - margin;
}

// Bands down the canvas, so the slats spread out rather than clumping.
for (var i = 0; i < count; i++){
  for (var attempt = 0; attempt < 50; attempt++){
    var w = getRandomInt(30, 300);
    var h = getRandomInt(6, 50);
    var angle = getRandomInt(-tilt, tilt);
    var x = getRandomInt(margin, width - margin);
    var y = margin + (height - margin * 2) * (i + getRandomArbitary(0, 1)) / count;
    if (fits(x, y, w, h, angle)) {
      s.rect(x - w / 2, y - h / 2, w, h)
        .attr({fill: colors[getRandomInt(0, colors.length - 1)], filter: shadow})
        .transform('r' + angle + ',' + x + ',' + y);
      break;
    }
  }
}