Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <p>After Richard Paul Lohse's <a href="https://assets.catawiki.nl/assets/2018/12/10/6/3/3/633ccdb7-700f-4b59-9383-3fb90908f401.jpg" target="_blank">Geometric Komposition </a></p>
<p>Click to move columns</p>
              
            
!

CSS

              
                body {
	display: flex;
	overflow: hidden;
  flex-direction:column;
	align-items: center;
	justify-content: center;

	width: 100vw;
	height: 100vh;
	margin: 0;
	padding: 0;

	color: var(--white);
	background: #444444;

	--white: #FFFEE0;
}
canvas {
	border: 10px solid var(--white);
	box-shadow: 0 0 5px rgba(0,0,0,.5);
}
a, a:link, a:active {
  color:#fff;
}
p{
  margin: 0;
}
canvas{
  margin-top: 15px;
}
              
            
!

JS

              
                /*
 Title: Geometric Komposition
 Date: 1950s
 Artist: Richard Paul Lohse
 Code: Alberto García Ariza
 Original: https://assets.catawiki.nl/assets/2018/12/10/6/3/3/633ccdb7-700f-4b59-9383-3fb90908f401.jpg
 */


//setup numbers
const stage = 500;
const nCols = 15;
const nRows = nCols;
const modSize = stage / nRows;

//color setups
const spectrum = [
  '#FED016',
  '#FF9E01',
  '#FF6E1F',
  '#DC3F30',
  '#B32642',
  '#933F7A',
  '#5B4686',
  '#3966BB',
  '#4184B9',
  '#5AAA87',
  '#78C75D',
  '#9FD947',
  '#C9F339',
  '#FFFF43',
  '#FFFF43'
];
let colorOffsets = [0, 8, 2, 10, 4, 12, 6, 14, 7, 1, 9, 3, 11, 5, 13];
//objects
let cols = []; //will store the objects
for (let i = 0; i < nCols; i++) {
  cols[i] = {
    colors: [],
    offset: undefined,
    isMoving: false,
    movement: undefined,
    initH: -modSize,
    target: -modSize
  };
}

//moving stuff
let colMoving = []
let direction;
let pyIni = [];

function setup() {
  //setup data Board
  for (let i = 0; i < nCols; i++) {
    let col = cols[i];
    col.offset = colorOffsets[i];
    let colorIndex = col.offset;
    for (let j = 1; j <= nRows + 1; j++) {
      col.colors[j] = colorIndex;
      colorIndex = getNextColor(colorIndex);
    }
    //setup the first
    col.colors[0] = getPrevColor(col.colors[1]);
  }

  //setup visual
  createCanvas(stage, stage);
  background(0);
  noSmooth();
  noStroke();
  // frameRate(10);
}

function draw() {
  //MODELO
  updatePositions()
  //REPRESENTACION
  background(0);
  for (let i = 0; i < nCols; i++) {
    drawColumn(i);
  }
}

function mouseClicked() {
  shuffleCol(colMoving, direction);
}

getNextColor = a => { return (a == (spectrum.length - 1)) ? 0 : (a + 1); }

getPrevColor = a => { return (a == 0) ? (spectrum.length - 1) : (a - 1); }

function shuffleCol() {
  colMoving = cols[Math.floor(Math.random() * nCols)];
  direction = (Math.round(Math.random() * 10) % 2) ? 1 : -1;
  colMoving.isMoving = true;
  colMoving.movement = direction;
}
function updatePositions() {
  for (let i = 0; i < nCols; i++) {
    if (cols[i].isMoving) {
      moveCol(cols[i]);
    }
  }
}

function moveCol(column) {
  let dir = column.movement;
  column.target = (dir > 0) ? 0 : -modSize * 2;
  let dist = (column.target - column.initH);
  column.initH += dist * .2;
  if (Math.abs(dist) < 1) {
    column.initH = column.target;
    updateColors(column);
  }
}

function updateColors(column) {
  for (let i = 0; i <= column.colors.length - 1; i++) {
    let myColor = column.colors[i];
    if (column.movement > 0) {
      column.colors[i] = getPrevColor(column.colors[i]);
    } else {
      column.colors[i] = getNextColor(column.colors[i]);
    }
  }
  //reset values
  column.isMoving = false;
  column.initH = -modSize;
  column.target = - modSize;
}
function drawColumn(index) {
  let myCol = cols[index];
  let px = modSize * index;
  let py = myCol.initH;
  for (let i = 0; i <= nRows + 1; i++) {
    let colorIndex = myCol.colors[i];
    let myColor = color(spectrum[colorIndex]);
    fill(myColor);
    rect(px, py, modSize, modSize);
    py += modSize;
  }

}
              
            
!
999px

Console