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.page-wrap
  %div.color-chooser-container 
    - 16.times do
      %div.color

    %div.sclear
      &nbsp;

  %div.panel-container
    - 8.times do |count|
      %div.panel
        %div.info
            
          
!
            
              @function random-color($base-hue) {
  @return adjust-color(
    $base-hue, 
    $hue: (random(30) - 15),
    $saturation: (random(40) - 20), 
    $lightness: (random(30) - 20)
  );
}
@function random-transform() {
  @return (
    translateX((random(100) - 50) + px)
    translateY((random(60) - 30) + px)
    rotate((random(5) - 2.5) + deg)
  );
}
  
@mixin vertical-center {
  // Center Vertically
  // Per http://zerosixthree.se/vertical-align-anything-with-just-3-lines-of-css/
  position: relative;
  top: 50%;
  
  $translateYAmount: -50%;
  transform: translateY($translateYAmount);
  -ms-transform: translateY($translateYAmount);
  -webkit-transform: translateY($translateYAmount);
}

body {
  margin: 0;
  padding: 0;
}

.page-wrap {
  margin: 0;
  padding: 5px 0;
  height: 600px;
}

.color-chooser-container {
  display: none; // hide for now
  
  $container-width: 500px;
  
  width: $container-width;
  margin: 50px auto;
  
  .color {
    // 2cols
    $dimns: $container-width / 8 - 1px;
    width: $dimns;
    height: $dimns;
    padding: 0;
    margin: 0 1px 1px 0;
    
    float: left;
    cursor: pointer;
    
    @for $i from 1 through 16 {
      &:nth-child(#{$i}) {
        background-color: adjust-hue(#FF0000, $i * 20)
      }
    }
  }

  .sclear {
    clear: both;
  }
}

.panel-container {
  margin: 0 auto;
  // width is up here to avoid rollover nightamre
  width: 400px;
  padding: 10px 0;
  
  @include vertical-center;
}

.panel {
  margin: 10px 0;
  background-color: white;
  
  // From original plate, W:H should be 4:1
  width: 100%;
  height: 100px;
  font-size: 86px;
  
  // TRANSITIONS
  $transitionDuration: 0.5s;
  $transitionEasing: ease-in-out;
  
  -webkit-transition: $transitionDuration $transitionEasing;
  -moz-transition: $transitionDuration $transitionEasing;
  -o-transition: $transitionDuration $transitionEasing;
  transition: $transitionDuration $transitionEasing;
  
  // COLOR INFO
  .info {
    opacity: 0;
    
    padding: 5px;
    color: white;
    font-family: "HelveticaNeue-Bold", Helvetica, Arial, sans-serif;
    text-transform: uppercase;
    text-align: center;
    letter-spacing: 0px;
    
    $transitionDuration: 0.08s;
    -webkit-transition: $transitionDuration $transitionEasing;
    -moz-transition: $transitionDuration $transitionEasing;
    -o-transition: $transitionDuration $transitionEasing;
    transition: $transitionDuration $transitionEasing;
  }
  
  &:hover {
    cursor: pointer;
    
    .info {
      opacity: 1;
    }
  }
}
            
          
!
            
              _.templateSettings = {
  interpolate: /\#\{(.+?)\}/g
};

function randomInt(maximum) {
  return Math.floor(Math.random() * maximum);
}

// Pagewrap needs to absolutely set 
// for the vertical center trick to work.
function updateDimensionsFromViewport() {
  var $window = $(window);
  var viewportHeight = $window.height();
  var viewportWidth = $window.width();
  
  var viewportHeightAfterPadding = viewportHeight * 0.8;
  
  $('.page-wrap').css({
    'height': String(viewportHeightAfterPadding) + 'px',
    'padding': String(viewportHeight * 0.1) + 'px'
  });
  
  var panelHeight = (viewportHeightAfterPadding) / 8;
  var panelWidth = panelHeight * 4;
  
  var panelHeightString = String(panelHeight) + "px";
  var panelWidthString = String(panelWidth) + "px";
  
  $('.panel-container').css('width', panelWidthString);
  $('.panel').css({
    height: panelHeightString,
    margin: String(panelHeight * 0.1) + 'px',
    'font-size': String(Math.floor(panelHeight * 0.86)) + 'px'
  });
}

// Update $panel background with a variation of the baseColor
function updatePanelColor($panel, baseColor) {
  var spinAmount = randomInt(30) - 15;
  var saturateAmount = randomInt(40) - 20;
  var lightenAmount = randomInt(30) - 20;

  // https://github.com/bgrins/TinyColor
  var tweakedColor = tinycolor(baseColor)
    .spin(spinAmount)
    .saturate(saturateAmount)
    .lighten(lightenAmount)
  ;

  $panel.css('background-color', tweakedColor);
  $panel.find('.info').html(tweakedColor.toHexString());
};

// Update $panel's transform
function updatePanelTransform($panel) {
  var transformValues = {
    translateXAmount: randomInt(100) - 50,
    translateYAmount: randomInt(60) - 30,
    rotateAmount: randomInt(5) - 2.5
  };
  
  var transformTemplate = [
    "translateX(#{translateXAmount}px)",
    "translateY(#{translateYAmount}px)",
    "rotate(#{rotateAmount}deg)"
  ].join(' ');
  
  var transformString = _.template(transformTemplate)(transformValues);
  
  $panel.css('transform', transformString);
  $panel.css('-webkit-transform', transformString);
}

// Update all panels with a new color.
function updateScene(color) {
  $(".panel-container .panel").each(function(index) {
    var $panel = $(this);
    updatePanelColor($panel, color);
    updatePanelTransform($panel);
  });
}

$('body').ready(function() {
  // Initial update
  updateDimensionsFromViewport();
  updateScene(tinycolor.random().toHexString());
  
  // Bind window resizes to update dimensions
  $(window).on("resize", updateDimensionsFromViewport);
  
  // Bind color picker clicks to color choose
  $("div.color-chooser-container .color").click(function(evt) {
    var $target = $(evt.target);
    var targetColor = $target.css('background-color');
    
    updateScene(targetColor);
  });
  
  // Bind global clicks to random color
  $('.page-wrap').click(function() {
    updateScene(tinycolor.random().toHexString());
  });
  
  // Bind shake events to random color select
  // https://github.com/alexgibson/shake.js/
  new Shake().start(); 
  window.addEventListener(
    'shake', 
    function() {
      updateScene(tinycolor.random().toHexString());
    }, 
    false
  );
});

// SHAKE JS COPYPASTA... No CDNJS yet
/*
 * Author: Alex Gibson
 * https://github.com/alexgibson/shake.js
 * License: MIT license
 */

(function(global, factory) {
    if (typeof define === 'function' && define.amd) {
        define(function() {
            return factory(global, global.document);
        });
    } else if (typeof module !== 'undefined' && module.exports) {
        module.exports = factory(global, global.document);
    } else {
        global.Shake = factory(global, global.document);
    }
} (typeof window !== 'undefined' ? window : this, function (window, document) {

    'use strict';

    function Shake(options) {
        //feature detect
        this.hasDeviceMotion = 'ondevicemotion' in window;

        this.options = {
            threshold: 15, //default velocity threshold for shake to register
            timeout: 1000 //default interval between events
        };

        if (typeof options === 'object') {
            for (var i in options) {
                if (options.hasOwnProperty(i)) {
                    this.options[i] = options[i];
                }
            }
        }

        //use date to prevent multiple shakes firing
        this.lastTime = new Date();

        //accelerometer values
        this.lastX = null;
        this.lastY = null;
        this.lastZ = null;

        //create custom event
        if (typeof document.CustomEvent === 'function') {
            this.event = new document.CustomEvent('shake', {
                bubbles: true,
                cancelable: true
            });
        } else if (typeof document.createEvent === 'function') {
            this.event = document.createEvent('Event');
            this.event.initEvent('shake', true, true);
        } else {
            return false;
        }
    }

    //reset timer values
    Shake.prototype.reset = function () {
        this.lastTime = new Date();
        this.lastX = null;
        this.lastY = null;
        this.lastZ = null;
    };

    //start listening for devicemotion
    Shake.prototype.start = function () {
        this.reset();
        if (this.hasDeviceMotion) {
            window.addEventListener('devicemotion', this, false);
        }
    };

    //stop listening for devicemotion
    Shake.prototype.stop = function () {
        if (this.hasDeviceMotion) {
            window.removeEventListener('devicemotion', this, false);
        }
        this.reset();
    };

    //calculates if shake did occur
    Shake.prototype.devicemotion = function (e) {
        var current = e.accelerationIncludingGravity;
        var currentTime;
        var timeDifference;
        var deltaX = 0;
        var deltaY = 0;
        var deltaZ = 0;

        if ((this.lastX === null) && (this.lastY === null) && (this.lastZ === null)) {
            this.lastX = current.x;
            this.lastY = current.y;
            this.lastZ = current.z;
            return;
        }

        deltaX = Math.abs(this.lastX - current.x);
        deltaY = Math.abs(this.lastY - current.y);
        deltaZ = Math.abs(this.lastZ - current.z);

        if (((deltaX > this.options.threshold) && (deltaY > this.options.threshold)) || ((deltaX > this.options.threshold) && (deltaZ > this.options.threshold)) || ((deltaY > this.options.threshold) && (deltaZ > this.options.threshold))) {
            //calculate time in milliseconds since last shake registered
            currentTime = new Date();
            timeDifference = currentTime.getTime() - this.lastTime.getTime();

            if (timeDifference > this.options.timeout) {
                window.dispatchEvent(this.event);
                this.lastTime = new Date();
            }
        }

        this.lastX = current.x;
        this.lastY = current.y;
        this.lastZ = current.z;

    };

    //event handler
    Shake.prototype.handleEvent = function (e) {
        if (typeof (this[e.type]) === 'function') {
            return this[e.type](e);
        }
    };

    return Shake;
}));
            
          
!
999px
Loading ..................

Console