Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                %h1 Interactive CSS 3D Transform

#tips
    Drag the square around. Also, use your mouse wheel to scale.

#transform

#css
    perspective(500px) rotateX(0deg) rotateY(0deg) scale(1)

              
            
!

CSS

              
                @import compass

@import url(https://fonts.googleapis.com/css?family=Rokkitt)
@import url(https://fonts.googleapis.com/css?family=Numans)

body
  font-family: Numans

#tips, h1
  color: #444
  user-select: none
  margin: 5px
    
#transform
  width: 300px
  height: 300px
  background: #DD312F
  border: 5px solid #444
  color: #444
  margin: 50px auto
  cursor: move
    
#css
  font-family: Rokkitt
  background: #002A38
  border: 1px solid #444
  color: #B98D00
  padding: 10px
  
  &:before
    content: 'transform: '
    color: #D33683
              
            
!

JS

              
                var el          = document.getElementById('transform'),
    css         = document.getElementById('css'),
    width       = el.clientWidth,
    height      = el.clientHeight,
    perspective = 500,
    rotateX     = 0,
    rotateY     = 0,
    scale       = 100,
    isDown      = false,
    reverse     = false,
    lastX,
    lastY;

function scaleVal() {
  var strScale = scale.toString();
  if (scale >= 100) {
    return parseFloat(strScale[0] + '.' +  strScale.slice(1));
  }
  return parseFloat('0.' + strScale);
}

function updateTransform() {
  var pProp = 'perspective(' + perspective +  'px)',
      xProp = 'rotateX('     + rotateX     + 'deg)',
      yProp = 'rotateY('     + rotateY     + 'deg)',
      sProp = 'scale('       + scaleVal()  +    ')',
      props = [pProp, xProp, yProp, sProp].join(' ');
  el.style[Modernizr.prefixed('transform')] = props;
  css.innerText = props;
}

function wheel(e) {
  e.preventDefault();
  var delta = e.wheelDelta || -e.detail;
  scale += delta > 0 ? 5 : -5;
  updateTransform();
}

el.addEventListener('mousedown', function(e) {
  isDown = true;
  lastX  = e.clientX;
  lastY  = e.clientY;
}, false);

el.addEventListener('mousewheel', wheel, false);
el.addEventListener('DOMMouseScroll', wheel, false);

document.addEventListener('mousemove', function(e) {
  if (isDown) {
    xMultiplier = 180 / height / scaleVal();
    yMultiplier = 180 / width  / scaleVal();
    rotateX += (lastY - e.clientY) * xMultiplier;
    rotateY += (lastX - e.clientX) * yMultiplier * (reverse ? 1 : -1);
    rotateX  = Math.floor(rotateX % 360);
    rotateY  = Math.floor(rotateY % 360);
    reverse  = Math.abs(Math.floor(rotateX / 90) % 4) > 1;
    lastX    = e.clientX;
    lastY    = e.clientY;
    updateTransform();
  }
}, false);

document.addEventListener('mouseup', function() {
  isDown  = false;
}, false);
              
            
!
999px

Console