Skip to main content
100%

Sol LeWitt in Vanilla JavaScript

✓ Published2🌍 Public
Xxinyuwangxyw
Last edited Mar 16, 2023
Created on Mar 8, 2023

This example recreates a Sol LeWitt wall drawing using only Vanilla JavaScript and the SVG API, with no external libraries. The SVG dynamically adapts to the browser window’s dimensions, generating a composition of alternating horizontal and vertical bars. It uses SVG `<mask>` elements with circles to clip the bar patterns, revealing a circular region in the center where the orientation of the lines shifts, mimicking the geometric interplay found in LeWitt’s conceptual art. The code builds all shapes procedurally via `createElementNS`.

AI-generated description

A reproduction of a Sol LeWitt art piece.

  // creating a mask
  const mask_front = document.createElementNS(
    'http://www.w3.org/2000/svg',
    'mask'
  );
  mask_front.setAttribute('id', 'mask_front');
  svg.appendChild(mask_front);

  // creating a circle in this mask
  const circle = document.createElementNS(
    'http://www.w3.org/2000/svg',
    'circle'
  );
  circle.setAttribute('cx', width / 2);
  circle.setAttribute('cy', height / 2);
  circle.setAttribute('r', 250);
  circle.setAttribute('fill', "white");
  mask_front.appendChild(circle);
  
  // creating horizontal bars
  const m = 100;
  for (let i = 0; i < m; i++) {
    const rect = document.createElementNS(
      'http://www.w3.org/2000/svg',
      'rect'
    );
    rect.setAttribute('y', 20 * i);
    rect.setAttribute('width', 1000);
    rect.setAttribute('height', 10);
    rect.setAttribute("mask", "url(#mask_front)");
    svg.appendChild(rect);
  }
MIT Licensed

Similar vizzes