Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <canvas id="canvas"></canvas>
              
            
!

CSS

              
                body, html {
  margin: 0;
  overflow: hidden;
}

canvas {
  display: block;
  cursor: pointer;
}
              
            
!

JS

              
                /*
  Johan Karlsson, 2021
  https://twitter.com/DonKarlssonSan
  MIT License, see Details View
  
  steps is how many pixels wide each square is  
  If steps === 4
  We need 8 bits total:
  0123 4567

  8 bit unsigned integer: 0-255
  first four bits are columns
  next four bits are rows

    0 1 2 3
  4
  5
  6
  7

  10001000 (binary) = 136 (decimal)
  with AND logic becomes:
    0 1 2 3
  4 1 0 0 0 
  5 0 0 0 0 
  6 0 0 0 0
  7 0 0 0 0

  A pixel in the upper left
*/

let canvas;
let ctx;
let w, h;
const operators = "^|&";

function setup() {
  canvas = document.querySelector("#canvas");
  ctx = canvas.getContext("2d");
  resize();
  window.addEventListener("resize", () => {
    resize();
    draw();
  });
  canvas.addEventListener("click", draw);
}

function resize() {
  w = canvas.width = window.innerWidth;
  h = canvas.height = window.innerHeight;
}

function draw() {
  ctx.fillStyle = "white";
  ctx.fillRect(0, 0, w, h);
  ctx.fillStyle = "black";
  let n = Math.min(w, h);
  const size = 40;
  const cols = Math.ceil(w / size);
  const rows = Math.ceil(h / size);
  const steps = 10; // per side
  
  // If each square is 10 x 10 -> we need 20 bits
  const maxVal = Math.pow(2, steps*2);
  const patternDec = Math.round(Math.random() * maxVal);
  const operatorIndex = Math.floor(Math.random() * operators.length);
  
  for(let col = 0; col < cols; col++) {
    for(let row = 0; row < rows; row++) {
      const x = col * size;
      const y = row * size;
      drawSquare(x, y, size, patternDec, operatorIndex, steps);
    }
  }
}

function drawSquare(x0, y0, size, pattern, operatorIndex, steps, angle) {
  ctx.save();
  ctx.translate(x0 + size / 2, y0 + size / 2);
  const stepSize = size / steps;
  const patternBinaryString = pattern.toString(2);
  const angle2 = Math.floor(Math.random() * 4) * Math.PI / 2;
  ctx.rotate(angle2);
  for(let col = 0; col < steps; col++) {
    const patternColPart = patternBinaryString[col];
    for(let row = 0; row < steps; row++) {
      const patternRowPart = patternBinaryString[row + steps];
      
      const combined = evalOperation(patternColPart, patternRowPart, operatorIndex);
      if(combined) {
        const x = -size / 2 + col * stepSize;
        const y = -size / 2 + row * stepSize;
        ctx.fillRect(x, y, stepSize, stepSize);
      }
      
    }
  }
  ctx.restore();
}

function evalOperation(a, b, operatorIndex) {
  const operator = operators[operatorIndex];
  const operations = {
    "^" : (a, b) => a ^ b,
    "&" : (a, b) => a & b,
    "|" : (a, b) => a | b 
  };
  return operations[operator](a, b);
}

setup();
draw();
              
            
!
999px

Console