Nested Squares

to generate a new and unique variation

Source Code

Every variart piece is open source, you can see exactly how it is drawn from the code.

// Concentric squares on a grid, each ring twisted a little further than
// the one outside it. The margin is generous so nothing important lands
// on the wrapped edge of a canvas print.
var columns = intVariable('columns', 2, 6);
var rings = intVariable('rings', 2, 6);
var twist = intVariable('twist', 0, 30);
var tint = intVariable('tint', 0, 100);

var margin = 70;
var cell = (width - margin * 2) / columns;

// Only lay down rows that fit whole inside the margin, then centre the
// block vertically so the top and bottom gutters match.
var rows = Math.max(1, Math.floor((height - margin * 2) / cell));
var offsetY = (height - rows * cell) / 2;

for (var i = 0; i < columns; i++) {
  for (var j = 0; j < rows; j++) {
    var cx = margin + cell * i + cell / 2;
    var cy = offsetY + cell * j + cell / 2;

    for (var r = 0; r < rings; r++) {
      var size = cell * 0.85 * (1 - r / rings);
      var shade = (tint / 100 + (i + j) * 0.04 + r * 0.02) % 1;

      s.rect(cx - size / 2, cy - size / 2, size, size)
        .attr({
          fill: 'none',
          stroke: Snap.hsl(shade, 0.45, 0.55),
          strokeWidth: 2
        })
        .transform('r' + (twist * (r + 1)) + ',' + cx + ',' + cy);
    }
  }
}