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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <div id="input">
  <label for="triangle-length">Side Length<span class="minmax">Min: 50, Max: 150</span> </label>
  <input id="triangle-length" type="number" min="50" max="150" value="50"/>
  <h3>Colors</h3>
  <input type="radio" id="user-choice" name="colors" checked/>
  <input type="radio" id="random-choice" name="colors"/>
  <label for="user-choice">Customize</label>
  <label for="random-choice">Randomize</label>
  <div id="user-colors-palette">
    <div id="colors"></div>
    <div id="add-color-cont">
      <div id="add-color-input-cont">
        <input type="text" id="add-color-input" placeholder="Add Color Hex (e.g. #aEEaaa, aFFaAa)" />
        <button id="add-color">+</button>
      </div>
      <p id="invalid-color">This is not a valid color</p>
    </div>
  </div>
  <div id="random-colors-palette">
    <label for="number-of-colors">Number of Colors <span class="minmax">Min: 3, Max: 20</span></label>
    <input id="number-of-colors" type="number" min="3" max="20" value="7"/>
    <input id="should-mute" type="checkbox" checked/>
    <label for="should-mute">Monochrome</label>
    <div id="base-color">Base Color</div>
    <button id="generate-base-color">Generate Base Color <span class="cycle"></span></button>
    <hr/>
      <button id="generate-palette">Generate Palette <span class="cycle"></span></button>
  </div>
</div>

<canvas/>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}
html, body {
  margin: 0;
  font-family: system-ui;
  height: 100%;
  overflow: hidden;
}
#input {
  position: absolute;
  background-color: white;
  padding: 1rem;
  height: 100%;
  overflow: auto;
}
input {
  &[type="radio"] {
    display: none;
  }
  &[name="colors"] {
    & ~ label {
      padding: 0.75rem;
      display: inline-block;
      font-size: 0.8rem;
      font-weight: bold;
      letter-spacing: 0.5px;
      text-transform: uppercase;
      background-color: white;
      &:hover {
        cursor: pointer;
      }
    }
    @each $tab in random, user {
      &##{$tab}-choice:checked ~ label[for="#{$tab}-choice"] {
        background-color: black;
        color: white;
      }
    &##{$tab}-choice:checked ~ ##{$tab}-colors-palette {
      display: block;
  }
    }
  }
}

label[for="triangle-length"] {
  &:before {
    content: '';
    display: inline-block;
    position: relative;
    width: 0;
    height: 0;
    border-left: 6px solid transparent;
    border-right: 6px solid transparent;
    border-bottom: 11px solid black;
    margin-right: 5px;
  }
  & + input {
    padding: 0.3rem 0.4rem;
    font-size: 0.8rem;
  }
}

.delete-color {
  font-size: 1.2rem;
  border: 0;
  background: transparent;
  float: right;
  padding: 0;
  width: 2rem;
  height: 2rem;
  &:hover {
    cursor: pointer;
    color: black!important;
    #user-colors-palette & + .color {
      background-position: 15.5% 100%;
    }
  }
}

#user-colors-palette, #random-colors-palette {
  border: 1px solid black;
  display: none;
  padding: 0.5rem;
}

#user-colors-palette {
  .color {
    display: block;
    padding: 0.5rem;
    font-size: 1rem;
    border: 0;
    background-size: 200% 100%;
    background-position: 0 100%;
    transition: 0.2s;
  }
}

.minmax {
  color: gray;
  text-transform: uppercase;
  font-size: 10px;
  display: block;
}
label[for="number-of-colors"], label[for="triangle-length"] {
  float: left;
  margin-right: 5px;
  margin-top: 0.3rem;
  & + input {
    font-size: 1rem;
    margin-top: 0;
    padding: 0.15rem 0.3rem;
    &:focus {
      border-color: black;
      outline: 0;
    }
  }
}

#random-colors-palette {
  button {
    border: 1px solid black;
    font-size: 0.8rem;
    margin-top: 0.75rem;
    text-transform: uppercase;
    font-weight: bold;
    padding: 0.5rem;
    width: 100%;
    span {
      display: inline-block;
      vertical-align: middle;
      height: 1rem;
      width: 1rem;
      margin-top: -0.2rem;
      margin-left: 0.2rem;
      position: relative;
      background-image: linear-gradient(transparent 35%, white 35%, white 65%, transparent 65%), radial-gradient(circle at center, white 45%, black 45%, black 70%, transparent 70%);
      &:before, &:after {
        content: '';
        display: block;
        position: absolute;
        top: 50%;
        border-left: 4px solid transparent;
        border-right: 4px solid transparent;
      }
      &:before {
        border-top: 5px solid black;
        margin-top: -3px;
        margin-left: -2px;
      }
      &:after {
        border-bottom: 5px solid black;
        right: 0;
        margin-right: -2px;
        margin-top: -2.5px;
      }
    }
    &:hover {
      color: white;
      background-color: black;
      cursor: pointer;
      span {
        background-image: linear-gradient(transparent 35%, black 35%, black 65%, transparent 65%), radial-gradient(circle at center, black 54%, white 54%, white 70%, transparent 70%);
        &:before {
          border-top: 5px solid white;
        }
        &:after {
          border-bottom: 5px solid white;
        }
      }
    }
  }
  hr {
    margin: 10px 0;
  }
  #should-mute {
    display: none;
    & + label {
      display: block;
      margin: 1rem 0;
      &:before {
        content: '';
        display: block;
        position: relative;
        float: left;
        height: 9px;
        width: 9px;
        border: 1px solid white;
        outline: 1px solid black;
        margin: 4px 5px 0 0;
      }
    }
    & ~ #base-color, & ~ #generate-base-color {
      display: none;
    }
    &:checked {
      & + label:before {
        background-color: black;
      }
      & ~ #base-color, & ~ #generate-base-color {
        display: block;
      }
    }
  }
}
#base-color {
  padding: 0.75rem 1rem;
}

#add-color-cont {
  border: 1px solid black;
  #add-color-input-cont {
    display: flex;
  }
  input {
    border: 0;
    padding: 0.5rem;
    flex-grow: 2;
    transition: 0.1s;
    font-size: 1rem;
    &:focus {
      outline: 0;
      background-color: mix(white, silver, 50%);
      & + button {
        background-position: -2% 100%;
        &:hover {
          background-position: -100% 100%;
        }
      }
    }
  }
  button {
    background-image: linear-gradient(to right, white 50%, black 50%);
    background-size: 200% 100%;
    border: 0;
    font-size: 1rem;
    padding: 0 0.6rem;
    transition: 0.1s;
    &:hover {
      cursor: pointer;
      color: white;
      background-position: 100% 100%;
    }
  }
  #invalid-color {
    display: none;
    padding: 0.5rem;
    margin: 0;
    color: white;
    background-color: red;
    text-transform: uppercase;
    font-size: 0.7rem;
    letter-spacing: 0.5px;
    font-weight: bold;
  }
}
              
            
!

JS

              
                //   Color Stuff
var colors = ["D7D1CF", "E29A4F", "7F44B3", "779CB4", "E2A3BC"];

// Necessary Functions
function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}

function getContrast50(hexcolor){
  return (parseInt(hexcolor, 16) > 0xffffff/2) ? 'black':'white';
}

function isHexValid(str){
  return /^#?[0-9a-fA-F]{6}$/i.test(str) || /^#?[0-9a-fA-F]{3}$/i.test(str) || /^#?[0-9a-fA-F]{2}$/i.test(str)
}

// Style Base Color Elm
function setBaseColor(base_color){
  var base_color_elm = document.getElementById('base-color');
  base_color_elm.style.backgroundColor = '#' + Please.NAME_to_HEX(base_color);
  base_color_elm.style.color = getContrast50(Please.NAME_to_HEX(base_color).toLowerCase());
}

// Add colors to customize tab

function addColorElms(canvas, ctx, side_length){
  for(var i = 0; i < colors.length; i++){
    var color_elm_cont = document.createElement('div');
    var color_elm = document.createElement('div');
    var delete_button = document.createElement('button');
    delete_button.className = 'delete-color';
    delete_button.innerText = '×';
    delete_button.style.color = getContrast50(colors[i]);
    delete_button.addEventListener('click', function(ev){
      ev.target.parentNode.parentNode.removeChild(ev.target.parentNode);
      var color_val = ev.target.nextSibling.innerText;
      color_val = color_val.indexOf('#') > -1 ? color_val.substring(1, color_val) : color_val;
     var color_index = colors.indexOf(color_val);
      colors.splice(color_index, 1);
      drawShapes(canvas, ctx, side_length);
    });
    color_elm.id = 'color-' + i;
    color_elm.value = colors[i];
    color_elm.style.backgroundImage = 'linear-gradient(to right, #' + colors[i] + ' 50%, white 50%)';
    color_elm.innerText = colors[i].toUpperCase();
    color_elm.classList.add('color');
    color_elm.style.color = getContrast50(colors[i]);
    color_elm_cont.appendChild(delete_button);
    color_elm_cont.appendChild(color_elm);
    document.getElementById('colors').appendChild(color_elm_cont);
    }
}

// Drawing function

function drawShapes(canvas, ctx, side_length){
  var center_coords = [];
  //   Draw triangles
  var column_width = Math.ceil((side_length/2) * Math.sqrt(3));
  var column_count = Math.ceil(canvas.width / column_width);
  var triangles_per_column = (canvas.height/side_length) * 2;
  var column_x_coords = Array(column_count).fill().map(function(val, index){ return index * column_width });
  for(var i = 0; i < column_count; i++){
    var start_x = column_width * i;
    var start_y = -side_length / 2;
    if(i === 0 || (i % 2 === 0)){  start_y -= side_length / 2; }
    for(var j = 0; j < triangles_per_column + 2; j++){
      if(j === 0 || j % 2 === 0){
          ctx.beginPath();
          ctx.moveTo(start_x, start_y);
          ctx.lineTo(start_x + column_width, (start_y + (side_length/2)));
          ctx.lineTo(start_x, start_y + side_length + 1);
          ctx.lineTo(start_x, start_y);
          center_coords.push({
            x: start_x + column_width, 
            y: start_y + (side_length/2)
          });
          center_coords.push({
            x: start_x + (column_width / 3),
            y: start_y + (side_length / 2)
          });
          start_x = column_width * (i + 1);
        } else {
          ctx.beginPath();
          ctx.moveTo(start_x, start_y);
          ctx.lineTo(start_x - column_width, start_y + (side_length/2));
          ctx.lineTo(start_x, start_y + side_length);
          ctx.lineTo(start_x, start_y);
          center_coords.push({
            x: start_x - column_width, 
            y: (start_y + (side_length/2))
          });
          center_coords.push({
            x: start_x - (column_width / 3), 
            y: (start_y + (side_length/2))
          });
          start_x = column_width * i;
        }
      var random_color = '#' + colors[getRandomInt(0, colors.length)];
      ctx.fillStyle = random_color;
      ctx.strokeStyle = random_color;
      ctx.fill();
      ctx.stroke();
      start_y += side_length/2;
    }
  }
//   Time to draw circles
  center_coords = center_coords.filter(function(coord, index, self){
    return index === self.findIndex(function(coord_obj){
      return coord_obj.x === coord.x && coord_obj.y === coord.y
    });
  });
  for(var i = 0; i < center_coords.length; i++){
    var center = center_coords[i];
    var number_of_circles_at_pt = getRandomInt(1, 3);
    var radii = [];
    while(radii.length < number_of_circles_at_pt){
      radii.push(getRandomInt((side_length * 5) / 100, (side_length * 30) / 100));
    }
    radii.sort(function(a, b){return b-a});
    for(var j = 0; j < number_of_circles_at_pt; j++){
      var radius = radii[j];
      var grads = [];
      //       random if gradient or not
      if(getRandomInt(1, 13) % 3 === 0){
        var stop_count = getRandomInt(2, 5);
        var percent_count = 100 / stop_count;
        for(var k = 0; k < stop_count; k++){
          var start = k === 0 ? 0 : (k * 2 * Math.PI) / stop_count;
          var end = k === stop_count ? 2 * Math.PI : ((k + 1) * 2 * Math.PI) / stop_count;
          var piece_color = colors[getRandomInt(0, colors.length)];
          ctx.beginPath();
          ctx.fillStyle = '#' + piece_color;
          ctx.moveTo(center.x, center.y);
          ctx.arc(center.x, center.y, radius, start, end);
          ctx.closePath();
          ctx.fill();
        }
      } else {
        var piece_color = colors[getRandomInt(0, colors.length)];
        ctx.beginPath();
        ctx.fillStyle = '#' + piece_color;
        ctx.moveTo(center.x, center.y);
        ctx.arc(center.x, center.y, radius, 0, 2 * Math.PI);
        ctx.closePath();
        ctx.fill();
      }
    }
  }
}

window.onload = function(){
//   Setup Stuff
    var canvas = document.getElementsByTagName('canvas')[0];
  canvas.width = window.screen.availWidth;
  canvas.height = window.screen.availHeight;
  var ctx = canvas.getContext('2d');
//   Side Length Stuff
  var side_length_elm = document.getElementById('triangle-length'),
      side_length = parseInt(side_length_elm.value);
  side_length_elm.addEventListener('change', function(ev){
    if(ev.target.value > 150){
      side_length = 150;
      ev.target.value = side_length;
    } else if (side_length < 50 || isNaN(side_length)){
      side_length = 50;
      ev.target.value = side_length;
    } else {
      side_length = ev.target.value;
    }
    drawShapes(canvas, ctx, side_length);
  });
  addColorElms(canvas, ctx, side_length);
  drawShapes(canvas, ctx, side_length);
  var base_color = Please.RandomColorName();
  setBaseColor(base_color);
  document.getElementById('generate-base-color').addEventListener('click', function(){
    base_color = Please.RandomColorName();
    setBaseColor(base_color);
  });
  document.getElementById('number-of-colors').addEventListener('blur', function(ev){
    if(parseInt(ev.target.value) > 20){
      ev.target.value = 20;
    } else if(parseInt(ev.target.value) < 3 || isNaN(ev.target.value)){
      ev.target.value = 3;
    }
  })
  document.getElementById('generate-palette').addEventListener('click', function(){
    var options = {
      format: 'hex',
      colors_returned: parseInt(document.getElementById('number-of-colors').value)
    }
    if(document.getElementById('should-mute').checked){
      delete options.full_random;
      options['base_color'] = base_color;
    } else {
      delete options.base_color;
      options['full_random'] = true;
    }
    colors = Please.make_color(options).map(function(color){
      return color.replace('#','');
    });
    document.getElementById('colors').innerHTML = '';
    addColorElms(canvas, ctx, side_length);
    drawShapes(canvas, ctx, side_length);  
  });
//   Add Color Button Event
  document.getElementById('add-color').addEventListener('click', function(ev){
    var add_color_input = document.getElementById('add-color-input');
    var color_to_add = add_color_input.value.replace('#', '');
    switch(color_to_add.length){
      case 3:
        color_to_add += color_to_add;
        break;
      case 2:
        color_to_add = color_to_add.repeat(3);
        break;
    }
      colors.push(color_to_add);
      document.getElementById('colors').innerHTML = '';
    addColorElms(canvas, ctx, side_length);
    drawShapes(canvas, ctx, side_length);
    add_color_input.value = '';
  });
  
  // Add Color Input
  document.getElementById('add-color-input').addEventListener('input', function(ev){
    document.getElementById('invalid-color').style.removeProperty('display');
    document.getElementById('add-color').disabled = false;
    if(!isHexValid(ev.target.value)){
      document.getElementById('invalid-color').style.display = 'block';
      document.getElementById('add-color').disabled = true;
    }
  });
}
              
            
!
999px

Console