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 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 class="canvas">
  <div class="intro">
    <h1 class="heading">Click and drag to make</h1>  
    <div class="title">D<div class="donut static"><div class="icing static-icing"></div></div>NUTS</div>
  </div>
  <div class="sizer hidden"></div>
</div>
              
            
!

CSS

              
                $color-bg: #C44D58;
$color-white: #fff;
$color-donut: #dbbe83;
$color-donut-dark: darken(#dbbe83,5%);
$color-icing: #4ECDC4;
$color-icing-primary: #7145c4;
$color-icing-secondary: #a045c4;
$color-icing-tertiary: #c441b2;
$color-shadow: rgba(0,0,0,.05);
$color-sizer: #C7F464;

body{
  letter-spacing:.05em;
  color:darken($color-bg,30%);
  font-family: 'Source Sans Pro', sans-serif;
  background:$color-bg;
}

.canvas{
  position:fixed;
  top:0;
  right:0;
  bottom:0;
  left:0;
  z-index:100;
  overflow:hidden;
}

.intro{
  user-select: none; 
  text-align:center;
  margin:auto;
  position:absolute;
  top:50%;
  left:50%;
  transform:translate(-50%,-50%);
  .heading{
    font-weight:700;
    font-size:22px;
    letter-spacing:.1em;
    color:$color-white;
  }
  .title{
    display:block;
    font-size:74px;
    font-weight:700;
    color:$color-white;
    text-shadow: 0 2px 4px darken($color-bg, 10%);
    white-space:nowrap;
  }
}

.sizer{
  border-radius:100%;
  z-index:1;
  position:absolute;
  width:100px;
  height:100px;
  background:rgba($color-sizer,.2);
  border: 3px dashed $color-sizer;
  opacity:.9;
  transition:opacity .2s ease;
  animation:sizer 40s linear infinite;
  &.hidden{
    opacity:0;
  }
}

.donut{
  z-index:1;
  position:absolute;
  background:$color-donut-dark;
  width:100px;
  height:100px;
  border-radius:100%;
  animation: drop 1s ease-in;
  box-shadow: .1rem .3rem .25rem $color-shadow;
  overflow:hidden;
  &:before{
    z-index:3;
    content:'';
    display:block;
    position:absolute;
    top:35%;
    right:35%;
    bottom:35%;
    left:35%;
    background:$color-bg;
    border-radius:100%;
    border:4px solid $color-donut-dark;
    box-shadow:inset .1rem .3rem .25rem $color-shadow;

  }  
  &:after{
    content:'';
    display:block;
    position:absolute;
    top:-2%;
    left:-2%;;
    width:95%;
    height:95%;
    background:$color-donut;
    border-radius:100%;
  }
  &.static{
    animation:none;
    position:relative;
    display:inline-block;
    width:50px;
    height:50px;

  }
  .icing{
    position:absolute;
    top:6%;
    right:6%;
    bottom:6%;
    left:6%;
    background:$color-icing;
    border-radius:100%;
    border:2px solid $color-donut-dark;
    z-index:2;
    &:after{
      content:'';
      display:block;
      position:absolute;
      top:24%;
      left:24%;
      width:50%;
      height:50%;
      background:$color-shadow;
      border-radius:100%;
    }
    &.color-1{
      background:$color-icing-primary;
    }
    &.color-2{
      background:$color-icing-secondary;
    }
    &.color-3{
      background:$color-icing-tertiary;
    }
  }
}

.sprinkle-wrapper{
  position:absolute;
  width:50%;
  left:0;
  top:45%;
  height:10%;
  z-index:10;
  transform-origin:right center;

  .sprinkle{
    position:absolute;
    top:0;
    left:15%;
    width:20%;
    box-shadow:inset -1px -1px 0 2px $color-shadow;
    background:$color-white;
    border-radius:30%/100%;
    &:after{
      content:'';
      display:block;
      padding-top:40%;
    }    
  }

}

@keyframes sizer{
  100%{
    transform:rotate(360deg);
  }
}

@keyframes drop{
  40%{ 
    margin-left:.5rem
  }
  70%{ 
    margin-right:.5rem
  }
  100%{
    transform: translateY(100vh) rotate(180deg);
  }
}
              
            
!

JS

              
                var canvas = document.querySelector('.canvas');
var sizer = document.querySelector('.sizer');
var startX = '';
var startY = ''; 
var size = '100';
var index = 0;

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

function createSprinkle(){
  var sprinkle = document.createElement('div');
  sprinkle.classList.add('sprinkle');
  return sprinkle;
}

function addSprinkles(target){
  var count = Math.floor(getRandomArbitrary(10,16));
  console.log(count);

  for (var i = 0; i < count; i++){
    var sprinkleWrapper = document.createElement('div');
    sprinkleWrapper.classList.add('sprinkle-wrapper');
    sprinkleWrapper.style.transform = 'rotateZ('+ i * (360/count) +'deg)';
    var sprinkle = document.createElement('div');
    sprinkle.classList.add('sprinkle');
    sprinkle.style.transform = 'rotateZ('+ i * (360/count) +'deg)';
    sprinkleWrapper.appendChild(sprinkle);
    target.appendChild(sprinkleWrapper);
  }
}

function createDonut(x,y){
  //increment the color index
  index = index < 3 ? index + 1 : 0;
  var donut = document.createElement('div');
  donut.classList.add('donut');
  donut.style.top = y + 'px';
  donut.style.left = x + 'px';
  donut.style.width = size + 'px';
  donut.style.height = size + 'px';
  var icing = document.createElement('div');
  icing.classList.add('icing');
  icing.classList.add('color-' + index);
  addSprinkles(icing);
  donut.appendChild(icing);
  canvas.appendChild(donut);
  //remove the element once the animation finishes
  setTimeout(function(){
    canvas.removeChild(donut);
  }, 900)
}

function handleMousedown(e){
  startX = e.clientX;
  startY = e.clientY;
  sizer.style.width = 0;
  sizer.style.height = 0;
  sizer.style.left = e.clientX + 'px';
  sizer.style.top = e.clientY + 'px';
  window.addEventListener('mousemove', handleMousemove);
  window.addEventListener('mouseup', handleMouseup);
}

function handleMousemove(e){
  sizer.classList.remove('hidden');
  var width = Math.abs(startX - e.clientX);
  var height = Math.abs(startY - e.clientY);
  size = Math.max(height, width) * 2;
  sizer.style.width = size + 'px';
  sizer.style.height = size + 'px';
  sizer.style.marginLeft = -size/2 + 'px';
  sizer.style.marginTop = -size/2 + 'px';
}

function handleMouseup(e){
  e.preventDefault();
  sizer.classList.add('hidden');
  createDonut(startX - (size/2), startY - (size/2));
  window.removeEventListener('mousemove', handleMousemove);
}

function handleClick(e){
  createDonut(canvas.offsetWidth/2 - size/2, size/2);  
}

window.addEventListener('mousedown', handleMousedown);

canvas.addEventListener('touchend', handleClick);

addSprinkles(document.querySelector('.static-icing'));
              
            
!
999px

Console