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

              
                 <section class="phone-content">
  <phone-container>      
    <svg id="phone-svg" viewBox="0 0 1335 2132">
      
      <filter id="drop-shadow" x="-100%" y="-100%" width="300%" height="300%">
        <feGaussianBlur id="shadow-blur" in="SourceAlpha" stdDeviation="50"/>
        <feOffset dx="0" dy="0" result="offsetblur"/>
        <feFlood id="shadow-color" flood-color="#fff"/>
        <feComposite in2="offsetblur" operator="in"/>
        <feMerge>
          <feMergeNode/>
          <feMergeNode in="SourceGraphic"/>
        </feMerge>
      </filter>
      
      <mask id="phone-mask" maskUnits="userSpaceOnUse">
        <path id="phone-mask-path" d="M390.001733,1932.5 C390.001733,1932.5 565.999988,1931.5 586.05712,1931.5 C606.114252,1931.5 617.023773,1915.5 620.828278,1915.5 C660.021292,1915.5 735.169897,1915.5 737.998019,1915.5 C740.826141,1915.5 746.051941,1931.5 778.812117,1931.5 C811.572292,1931.5 971.998267,1932.5 971.998267,1932.5 C1043.24228,1932.5 1106,1869.76062 1106,1797.49805 L1106,338.501954 C1106,255.587048 1040.06342,200.5 978.998267,200.5 L903.563583,200.5 C877.936549,200.5 860.246162,215.5 860.246162,215.5 L797.920589,215.5 C797.920589,215.5 776.386439,200.5 760.492377,200.5 C744.598316,200.5 379.001733,200.5 379.001733,200.5 C307.757721,200.5 250,260.459622 251,340.704544 L251,604.642372 L258,630.93573 L258,745.743147 L251,769.688698 L251,865.914447 L258,887.652675 L258,1128.96869 L251,1148.60567 L252,1791.49805 C252,1878.48584 318.756326,1932.5 390.001733,1932.5 Z" />
      </mask>
                          
      <path id="phone-outline" filter="url(#drop-shadow)" d="M390.001733,1932.5 C390.001733,1932.5 565.999988,1931.5 586.05712,1931.5 C606.114252,1931.5 617.023773,1915.5 620.828278,1915.5 C660.021292,1915.5 735.169897,1915.5 737.998019,1915.5 C740.826141,1915.5 746.051941,1931.5 778.812117,1931.5 C811.572292,1931.5 971.998267,1932.5 971.998267,1932.5 C1043.24228,1932.5 1106,1869.76062 1106,1797.49805 L1106,338.501954 C1106,255.587048 1040.06342,200.5 978.998267,200.5 L903.563583,200.5 C877.936549,200.5 860.246162,215.5 860.246162,215.5 L797.920589,215.5 C797.920589,215.5 776.386439,200.5 760.492377,200.5 C744.598316,200.5 379.001733,200.5 379.001733,200.5 C307.757721,200.5 250,260.459622 251,340.704544 L251,604.642372 L258,630.93573 L258,745.743147 L251,769.688698 L251,865.914447 L258,887.652675 L258,1128.96869 L251,1148.60567 L252,1791.49805 C252,1878.48584 318.756326,1932.5 390.001733,1932.5 Z" />
      
      <g>
        <rect id="background" fill="#b8c2de" width="100%" height="100%" mask="url(#phone-mask)" /> 
        <g id="ripple-layer" mask="url(#phone-mask)"></g>
      </g>      
      <image x="1" y="1" width="1333px" height="2130px" xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/phone.png?v=1" /> 
    </svg>   
  </phone-container>
</section>    
  
<section class="color-content">
  <color-sidebar>      
    <color-picker></color-picker>    
  </color-sidebar>   
</section>

              
            
!

CSS

              
                $colorSize: 40px;
$phoneSize: 90vmin;
$phoneWidth: 1335px;
$phoneHeight: 2132px;
$sidebarWidth: 62px;
$aspectRatio: $phoneWidth / $phoneHeight;

* {
  box-sizing: border-box;
}

body {
  overflow-x: hidden;
}

.color-content {
  
}

color-sidebar {
  display: block;
  position: absolute;
  top: 0;
  left: 0;  
  padding: 10px;
  z-index: 2;
  touch-action: none;
}

color-picker {
  display: block;
  position: relative;
}

color-item,
color-draggable {
  display: block;
  border-radius: 50%; 
  visibility: hidden;
  opacity: 0;  
  box-shadow: 0px 2px 5px 0px rgba(0,0,0,0.26);  
  cursor: move;
	cursor: -webkit-grab;
	cursor: grab;
}

color-item {
  position: relative; 
  margin: 10px 10px 0 10px;  
  width: $colorSize;
  height: $colorSize;
  min-width: $colorSize;
  min-height: $colorSize; 
}

color-draggable {
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;  
  position: absolute; 
}

color-draggable.dragging {
	cursor: -webkit-grabbing;
	cursor: grabbing;
}

.phone-content {
  position: fixed;  
  top: 0;
  left: 0;  
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;  
  padding-left: $sidebarWidth / 2;  
  background: linear-gradient(white, #f2f2f2);
}

phone-container {
  display: block;
  position: relative;
  width: $phoneSize * $aspectRatio;
  padding-top: 1 / $aspectRatio * 100%; 
  visibility: hidden;
  opacity: 0;
}

#phone-svg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;  
  overflow: visible !important;
}

#phone-mask-path {
  fill: #fff; 
  stroke: #fff; 
  stroke-width: 4;
}

#phone-outline {
  fill: white;
  stroke: white; 
  stroke-width: 3; 
  vector-effect: non-scaling-stroke;
  opacity: 0;  
  stroke: rgba(0,0,0,0.2);
}
              
            
!

JS

              
                console.clear();
const log = console.log.bind(console);

const svgns = "http://www.w3.org/2000/svg"; 

SVGElement.prototype.getTransformToElement = SVGElement.prototype.getTransformToElement || function(toElement) {
  return toElement.getScreenCTM().inverse().multiply(this.getScreenCTM());  
};  

//
// RIPPLE
// =========================================================================== 
class Ripple {

  element = document.createElementNS(svgns, "circle");

  constructor(x, y, public color) {

    const dx = x - offsetX;
    const dy = y - offsetY;

    const element = this.element;            
    const radius  = this.getRadius(dx, dy, phoneRect.width, phoneRect.height);

    rippleLayer.appendChild(element);

    element.setAttribute("r", 5);
    
    TweenLite.set(element, {
      transformOrigin: "center",        
      fill: color,
      x: x,
      y: y
    });
      
    TweenLite.to(element, rippleDuration, {
      scale: radius / 2,
      ease: Power1.easeIn,
      callbackScope: this,
      onComplete: this.removeRipple
    });
  }

  getRadius(x, y, w, h) {

    const cx = w / 2;
    const cy = h / 2;

    const dx = x < cx ? w - x : x;
    const dy = y < cy ? h - y : y;

    return Math.sqrt(dx * dx + dy * dy);
  }

  removeRipple() {

    TweenLite.set(background, { fill: this.color });
    rippleLayer.removeChild(this.element);
  }
}

//
// COLOR ITEM
// =========================================================================== 
class ColorItem {

  itemElement = document.createElement("color-item");
  dragElement = document.createElement("color-draggable");

  draggable = new Draggable(this.dragElement, {
    trigger: this.itemElement,
    onPress: this.onPress,
    onDragStart: this.onDragStart,
    onDrag: this.onDrag,     
    onRelease: this.onRelease,      
    callbackScope: this,
    
    cursor: grab,
  });  

  constructor(public color) {

    const itemElement = this.itemElement;
    const dragElement = this.dragElement;

    colorPicker.appendChild(itemElement);
    itemElement.appendChild(dragElement);    
    
    TweenLite.set(dragElement, {
      autoAlpha: 0,
      scale: 0.25,
    });
    
    TweenLite.set([itemElement, dragElement], { 
      backgroundColor: color 
    });
  }

  hitTest(x, y) {

    let inside = false;

    if (phoneRect.contains(x, y)) {
      if (phonePoly.contains(x, y)) {
        inside = true;
      }
    }

    return inside;
  }

  onPress() {

    this.itemElement.style.setProperty("z-index", Draggable.zIndex);
    this.dragElement.style.setProperty("cursor", grabbing);
    
    TweenLite.set(this.dragElement, {
      autoAlpha: 1,
      scale: 1,
      x: 0,
      y: 0
    });

    TweenLite.to(shadowColor, 0.2, { attr: { "flood-color": this.color }});
    TweenLite.to(phoneOutline, 0.2, { stroke: this.color });
    
    outlineAnimation.reverse();
    
    this.draggable.update();
  }

  onDragStart() {
    
    TweenLite.to(this.dragElement, 0.25, {
      autoAlpha: 1,
      scale: 1.5,
      ease: Power1.easeInOut
    }); 
  }
  
  onRelease() {

    const x = this.draggable.pointerX - window.pageXOffset;
    const y = this.draggable.pointerY - window.pageYOffset;

    const point = toLocal(x, y);      
    const inside = this.hitTest(point.x, point.y);

    let duration = 0.2;

    if (inside) {
      const ripple = new Ripple(point.x, point.y, this.color);
      duration = 0;       
    }      

    outlineAnimation.reverse();

    TweenLite.to(this.dragElement, duration, {
      autoAlpha: 0,
      scale: 0.25,
      ease: Power1.easeIn
    });
  }

  onDrag() {

    const x = this.draggable.pointerX - window.pageXOffset;
    const y = this.draggable.pointerY - window.pageYOffset;

    const point = toLocal(x, y);      
    const inside = this.hitTest(point.x, point.y);

    outlineAnimation[inside ? "play" : "reverse"]();
  }
}

//
// RECTANGLE
// =========================================================================== 
class Rectangle {

  constructor(public x = 0, public y = 0, public width = 0, public height = 0) {

  }

  contains(x, y) {

    if (this.width <= 0 || this.height <= 0) {
      return false;
    }

    if (x >= this.x && x < this.x + this.width) {
      if (y >= this.y && y < this.y + this.height) {
        return true;
      }
    }

    return false;
  }
}

//
// POLYGON
// =========================================================================== 
class Polygon {

  constructor(public points) {

  }

  contains(x, y) {

    let inside = false;

    const points = this.points;
    const size = points.length / 2;

    // use raycasting to test hits
    // https://github.com/substack/point-in-polygon/blob/master/index.js
    for (let i = 0, j = size - 1; i < size; j = i++) {

      const xi = points[i * 2];
      const yi = points[(i * 2) + 1];
      const xj = points[j * 2];
      const yj = points[(j * 2) + 1];
      const intersect = ((yi > y) !== (yj > y)) && (x < ((xj - xi) * ((y - yi) / (yj - yi))) + xi);

      if (intersect) {
        inside = !inside;
      }
    }      

    return inside;
  }
}

//
// GLOBAL TO LOCAL
// ===========================================================================
function globalToLocal(element, svg) {

  const pt = svg.createSVGPoint();

  return (x, y) => {

    pt.x = x;
    pt.y = y;

    const globalPoint = pt.matrixTransform(svg.getScreenCTM().inverse());
    const localMatrix = element.getTransformToElement(svg).inverse();

    return globalPoint.matrixTransform(localMatrix);
  }
}

//
// INIT
// ===========================================================================
const colors = [
  "#b8c2de",
  "#537ebf",
  "#244191",
  "#ac286e",
  "#f17bb0",
  "#e8405d",
  "#f9a977",
  "#f9f06e",
  "#a7d492",
  "#88d3e1",
];

// const rippleDuration = 0.35;
const rippleDuration = 0.5;

const { grab, grabbing } = getCursors();

const svg = document.querySelector("#phone-svg");
const background = document.querySelector("#background");
const colorPicker = document.querySelector("color-picker");
const rippleLayer = document.querySelector("#ripple-layer");
const phoneOutline = document.querySelector("#phone-outline");
const shadowColor = document.querySelector("#shadow-color");
const shadowBlur = document.querySelector("#shadow-blur");

// Convert path data to polygon points
const pathData = MorphSVGPlugin.pathDataToBezier(phoneOutline);
const contour  = simplify(getContour(pathData, 50), 5, false);  

const points = contour.reduce((res, pt) => {
  res.push(roundTo(pt.x, 3));
  res.push(roundTo(pt.y, 3));
  return res;
}, []);

const box1 = phoneOutline.getBBox();
const box2 = background.getBBox();    

// Shapes for hit testing
const phoneRect = new Rectangle(box1.x, box1.y, box1.width, box1.height);
const colorRect = new Rectangle(box2.x, box2.y, box2.width, box2.height);
const phonePoly = new Polygon(points);

const toLocal = globalToLocal(background, svg);
const offsetX = phoneRect.x - colorRect.x;
const offsetY = phoneRect.y - colorRect.y;

const outlineAnimation = new TimelineLite()
  .to(phoneOutline, rippleDuration * 0.75, { opacity: 1, ease: Sine.easeInOut })
  .reverse();

const colorItems = colors.map(color => new ColorItem(color));

TweenLite.set("color-item", { autoAlpha: 1 });
TweenLite.set("phone-container", { autoAlpha: 0, scale: 0.5 });

window.addEventListener("load", onLoad);
window.focus();

function onLoad() {
  TweenLite.to("phone-container", 0.55, { autoAlpha: 1, scale: 1 });
}

function roundTo(value, place = 0, base = 10) {
  const p = Math.pow(base, place);
  return Math.round(value * p) / p;
}

function getCursors() {
  const element = document.createElement("color-draggable");
  document.body.appendChild(element);
  const style = window.getComputedStyle(element, null);
  const grab = style.getPropertyValue("cursor");
  element.classList.add("dragging");
  const grabbing = style.getPropertyValue("cursor");
  document.body.removeChild(element);
  return { grab, grabbing };
}



              
            
!
999px

Console