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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

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.

            
              <h3 style="margin-left: 64px;">Shadow interpolation</h3>
<p style="width: 500px; margin: 0 0 48px 64px">
  This pen displays shadows at all elevations used in the Material Spec.
  The shadow definitions are generated by interpolating between the 
  hand-crafted reference elevations.
</p>
<div class="container"></div>

            
          
!
            
              * {
  box-sizing: border-box;
}

html {
  font-family: 'Roboto';
}

.container {
  position: relative;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}

.box {
  display: flex;
  align-items: center;
  justify-content: center;
  
  width: 100px; 
  height: 100px;
  margin: 0 60px 80px;

  border-radius: 3px;
  font-size: .8em;
  color: #9E9E9E; 
  background: white;
}
            
          
!
            
              /**
 * The shadow class encapsulates the properties of a single CSS shadow effect.
 */
class Shadow {
  constructor(x, y, blur, spread, color) {
    // Round all parameters, as shadow definitions do not support subpixels
    this.x = Math.round(x);
    this.y = Math.round(y);
    this.blur = Math.round(blur);
    this.spread = Math.round(spread);
    this.color = color;
  }

  toCSS() {
    return `${this.x}px ${this.y}px ${this.blur}px ${this.spread}px ${this.color}`;
  }
}


const SHADOW_COLORS = {
  KEY_UMBRA: 'rgba(0, 0, 0, 0.2)',
  KEY_PENUMBRA: 'rgba(0, 0, 0, 0.14)',
  AMBIENT: 'rgba(0, 0, 0, 0.12)'
};


/**
 * The hand-designed reference shadow sets.
 */
const REFERENCE_SHADOWS = [
  {
    elevation: 0,
    shadows: [
      new Shadow(0, 0, 0, 0, SHADOW_COLORS.KEY_UMBRA),
      new Shadow(0, 0, 0, 0, SHADOW_COLORS.KEY_PENUMBRA),
      new Shadow(0, 0, 0, 0, SHADOW_COLORS.AMBIENT),
    ]
  },
  {
    elevation: 2,
    shadows: [
      new Shadow(0, 3, 1, -2, SHADOW_COLORS.KEY_UMBRA),
      new Shadow(0, 2, 2, 0, SHADOW_COLORS.KEY_PENUMBRA),
      new Shadow(0, 1, 5, 0, SHADOW_COLORS.AMBIENT),
    ]
  },
  {
    elevation: 3,
    shadows: [
      new Shadow(0, 3, 3, -2, SHADOW_COLORS.KEY_UMBRA),
      new Shadow(0, 3, 4, 0, SHADOW_COLORS.KEY_PENUMBRA),
      new Shadow(0, 1, 8, 0, SHADOW_COLORS.AMBIENT),
    ]
  },
  {
    elevation: 4,
    shadows: [
      new Shadow(0, 2, 4, -1, SHADOW_COLORS.KEY_UMBRA),
      new Shadow(0, 4, 5, 0, SHADOW_COLORS.KEY_PENUMBRA),
      new Shadow(0, 1, 10, 0, SHADOW_COLORS.AMBIENT),
    ]
  },
  {
    elevation: 6,
    shadows: [
      new Shadow(0, 3, 5, -1, SHADOW_COLORS.KEY_UMBRA),
      new Shadow(0, 6, 10, 0, SHADOW_COLORS.KEY_PENUMBRA),
      new Shadow(0, 1, 18, 0, SHADOW_COLORS.AMBIENT),
    ]
  },
  {
    elevation: 8,
    shadows: [
      new Shadow(0, 5, 5, -3, SHADOW_COLORS.KEY_UMBRA),
      new Shadow(0, 8, 10, 1, SHADOW_COLORS.KEY_PENUMBRA),
      new Shadow(0, 3, 14, 2, SHADOW_COLORS.AMBIENT),
    ]
  },
  {
    elevation: 16,
    shadows: [
      new Shadow(0, 8, 10, -5, SHADOW_COLORS.KEY_UMBRA),
      new Shadow(0, 16, 24, 2, SHADOW_COLORS.KEY_PENUMBRA),
      new Shadow(0, 6, 30, 5, SHADOW_COLORS.AMBIENT),
    ]
  }
];


/**
 * Calculates the set of shadows at a given elevation.
 */
function calculateShadowSetAtDepth(elevation) {
  let bounds = findBoundingShadowSets(elevation),
      min = bounds[0],
      max = bounds[1],
      x = (elevation - min.elevation) / (max.elevation - min.elevation),
      elevationShadows = [];

  for (let i = 0; i < min.shadows.length; i++) {
    elevationShadows.push(lerpShadow(x, min.shadows[i], max.shadows[i]));
  }

  return elevationShadows;
}


/**
 * Returns the two reference shadows whose elevations bound the specified
 * elevation. In the case where the supplied elevation exceeds the maximum
 * reference elevation, the last two reference shadows are returned.
 */
function findBoundingShadowSets(elevation) {
  if (elevation < 0) {
    throw 'Shadows with negative elevation are impossible'
  }

  for (let i = 0; i < REFERENCE_SHADOWS.length - 1; i++) {
    let lower = REFERENCE_SHADOWS[i],
        upper = REFERENCE_SHADOWS[i + 1];

    if (lower.elevation <= elevation && upper.elevation > elevation) {
      return [lower, upper];
    }
  }

  return REFERENCE_SHADOWS.slice(-2);
}


/**
 * Performs linear interpolation between shadows by interpolating each property
 * individually. Returns the value between shadow1 and shadow2 proportional to x
 * (when x is between 0 and 1. When x is outside this range, the return value is
 * a linear extrapolation).
 */
function lerpShadow(x, shadow1, shadow2) {
  return new Shadow(
    lerp(x, shadow1.x, shadow2.x),
    lerp(x, shadow1.y, shadow2.y),
    lerp(x, shadow1.blur, shadow2.blur),
    lerp(x, shadow1.spread, shadow2.spread),
    shadow1.color // No need to lerp the shadow color, as in this example
  );              // shadow1.color == shadow2.color
}


/**
 * Performs linear interpolation between values a and b. Returns the value
 * between a and b proportional to x (when x is between 0 and 1. When x is
 * outside this range, the return value is a linear extrapolation).
 */
function lerp(x, a, b) {
  return a + x * (b - a);
}


/**
 * Adds shadowed box elements to the DOM at each elevation.
 */
function main() {
  let container = document.querySelector('.container');
  for (let elevation = 1; elevation <= 24; elevation++) {
    let box = document.createElement('div');
    box.classList.add('box');
    box.innerHTML = `${elevation}dp`;
    box.style.boxShadow = 
      calculateShadowSetAtDepth(elevation)
        .map(shadow => shadow.toCSS())
        .join(',\n');
    container.appendChild(box);
  }
}

main();

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console