cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <div class="wrapper">
    <canvas id="canvas" width="500" height="500"></canvas>

    <div class="controls">
        <input type="range" class="controls__range js-maxSections" step="10" min="10" max="300" value="150" />
        <input type="range" class="controls__range js-maxWidth" step="10" min="50" max="800" value="300" />

        <button type="button" name="button" class="controls__button js-random">
        Random</button>

        <a href="" class="controls__button js-download" download="Striped Cones">Download</a>
    </div>
</div>
            
          
!
            
              HTML, BODY {
    height: 100%;
}

HTML {
    font-size: 14px;
}

BODY {
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background: #000;
    background-image: linear-gradient(to right, hsla(0, 0%, 100%, .045) 50%, transparent 50%);
    background-size: 2rem 100%;
    font-family: Trebuchet MS;
}

.wrapper {
    position: relative;
}

canvas {
    margin: 0;
    max-width: 100%;
    height: auto;
}

.controls {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: .25rem;
    box-sizing: border-box;
    background: rgba(0, 0, 0, .75);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.controls__range {
    flex-grow: 1;
    margin: 1rem;
}

.controls__button {
    flex-grow: 1;
    min-height: 2rem;
    margin: .5rem;
    padding: 0;
    max-width: 150px;
    background: transparent;
    border: 1px solid #FFF;
    border-radius: 5px;
    text-align: center;
    text-decoration: none;
    font-size: 1rem;
    line-height: 2rem;
    color: #FFF;
    transition: all .25s;
}

.controls__button:active {
    background: #FFF;
    color: orangered;
}
            
          
!
            
              var doc = document;
var canvas = doc.querySelector('#canvas');
var ctx = canvas.getContext('2d');

var width = window.innerWidth;
var height = window.innerHeight;
canvas.width = width;
canvas.height = height;

var maxSectionsInput = doc.querySelector('.js-maxSections');
var maxWidthInput = doc.querySelector('.js-maxWidth');
var controls = doc.querySelector('.controls');
var randomBtn = doc.querySelector('.js-random');
var downloadBtn = doc.querySelector('.js-download');

var maxSections = 150;
var maxLineWidth = 250;

var lineStep = 0;
var sectionLength = 0;
var rowLength = 0;
var xOffset = 0;

calsVars();

//---------------------------------------------

var colors = [
  'crimson',
  'tomato',
  'salmon',
  'orangered',
  'palegoldenrod',
  'khaki',
  'gold',
  'yellowgreen',
  'olive',
  'aquamarine',
  'mediumturquoise',
  'darkturquoise',
  'darkcyan',
  'skyblue',
  'cornflowerblue',
  'rebeccapurple',
  'purple',
  'darkviole'
];

function getColorPos(prevColorPos) {
  var colorPos = Math.floor(Math.random() * colors.length);

  while (colorPos === prevColorPos) {
    colorPos = Math.floor(Math.random() * colors.length);
  }

  prevColorPos = colorPos;
  return colorPos;
}

//---------------------------------------------

function drawSection(params) {
  var x = params.x;
  var y = params.y;
  var lineWidth = params.lineWidth;
  var color = params.color;

  ctx.strokeStyle = color;
  ctx.lineWidth = lineWidth;
  ctx.lineCap = 'round';

  ctx.beginPath();
  ctx.moveTo(x, y);
  ctx.lineTo(x, y + sectionLength);
  ctx.stroke();
}

//---------------------------------------------

function drawBlock(x) {
  var lastColorPos = 0;
  var colorPos1 = null;
  var colorPos2 = null;

  for (var i = 0; i < maxSections; i++) {
    var lineWidth = maxLineWidth - lineStep * i;

    var currentColorPos1 = getColorPos(colorPos1);
    colorPos1 = currentColorPos1;
    var currentColorPos2 = getColorPos(colorPos2);
    colorPos2 = currentColorPos2;

    drawSection({
      x: x + maxLineWidth/2,
      y: i * sectionLength,
      lineWidth: lineWidth,
      color: colors[currentColorPos2]
    });

    drawSection({
      x: x - maxLineWidth,
      y: height - i * sectionLength,
      lineWidth: lineWidth,
      color: colors[currentColorPos1]
    });

  }
}

//---------------------------------------------

function drawCones() {
  for (var i = 0; i <= rowLength + 2; i++) {
    drawBlock((i - 1) * maxLineWidth + xOffset);
  }
}

//---------------------------------------------

function calsVars() {
  lineStep = maxLineWidth / maxSections;
  sectionLength = height / maxSections;
  rowLength = Math.round(width / maxLineWidth);
  xOffset = (width - rowLength * maxLineWidth) / 2;
}

function maxSectionsChange() {
  maxSections = +this.value;
  calsVars();
  drawCones();
}

function maxWidthChange() {
  maxLineWidth = +this.value;
  calsVars();
  drawCones();
}

//---------------------------------------------

function rand(min, max) {
  return Math.round(Math.random() *(max - min) + min);
}

function randomizeVals() {
  maxSections = rand(maxSectionsInput.min, maxSectionsInput.max);
  maxSectionsInput.value = maxSections;

  maxLineWidth = rand(maxWidthInput.min, maxWidthInput.max);
  maxWidthInput.value = maxLineWidth;

  calsVars();
  drawCones();
}

//---------------------------------------------

function download() {
  var dataUrl = canvas.toDataURL();
  downloadBtn.href = dataUrl;
}

//---------------------------------------------

drawCones();

maxSectionsInput.addEventListener('input', maxSectionsChange);
maxWidthInput.addEventListener('input', maxWidthChange);

randomBtn.addEventListener('click', randomizeVals);
downloadBtn.addEventListener('click', download);

            
          
!
999px
Loading ..................

Console