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 id="controls">
  <h1>CSS transforms</h1>
  <p>This tool allows you to preview the result of a <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/transform">CSS transformed</a> element.<br />When you change any of the transform or transform-origin values, the preview area will update accordingly and will show both the unstyled element and the transformed element.</p>
  <p>
    <label for="transform">transform</label>
    <input id="transform" value="matrix(0.9999999, 0.5, 0, 1, 0, 0)" size="40" />
    <label for="origin">transform origin</label>
    <input id="origin" value="center center" />
  </p>
  <p>
    Some examples
    <button>rotate(90deg) - center</button>
    <button>rotate(54deg) - top left</button>
    <button>matrix(1, 0.5, 1, 0, 0, 0) - center center</button>
    <button>scale(.5) translate(-100px, -100px) rotate(30deg) - top right</button>
    <button>rotate3d(.5,1,2,200deg) - center</button>
  </p>
</div>
<div id="code">
  <div id="sampleDiv"></div>
</div>
<div id="visualization"></div>
              
            
!

CSS

              
                body {
  font: 14px Arial;
  color: #333;
  background: white;
  padding: 10px;
}
a {
  color: #A7D8FF;
}
input, button {
  padding: 5px;
  font-size: 14px;
}

#controls {
  padding-bottom: 10px;
  border-bottom: 1px solid #ddd;
}
#controls h1 {
  margin: 0;
}
label {
  font-weight: bold;
}
#code {
  position: absolute;
  visibility: hidden;
}
#sampleDiv {
  width: 300px;
  height: 200px;
}
#visualization {
  text-align: center;
  padding-top: 10px;
  position: relative;
}
#visualization #canvas {}
#visualization .message {
  display: block;
  position: absolute;
  text-align: center;
  top: 20px;
  width: 100%;
  color: red;
}
              
            
!

JS

              
                var CANVAS_MAX_DIM = 600;
var CANVAS_PADDING = 20;

var target = document.getElementById("sampleDiv");
var transformInput = document.getElementById("transform");
var originInput = document.getElementById("origin");

function setTransform() {
  target.style.transform = transformInput.value;
  target.style.webkitTransform = transformInput.value;
}

function setOrigin() {
  target.style.transformOrigin = originInput.value;
  target.style.webkitTransformOrigin = originInput.value;
}

transformInput.addEventListener("keyup", function(event) {
  setTransform();
  visualize();
}, false);
originInput.addEventListener("keyup", function(event) {
  setOrigin();
  visualize();
}, false);

[].forEach.call(document.querySelectorAll("button"), function(button) {
  button.addEventListener("click", function(event) {
    var values = this.innerHTML.split(" - ");
    transformInput.value = values[0];
    originInput.value = values[1];
    setTransform();
    setOrigin();
    visualize();

  });
});

// Launch the first preview, with the values
setTransform();
setOrigin();
visualize();

function visualize() {
  var targetTransform = getComputedStyle(target).transform ||
        getComputedStyle(target).webkitTransform;

  var targetTransformOrigin = getComputedStyle(target).transformOrigin ||
      getComputedStyle(target).webkitTransformOrigin;

  var container = document.getElementById("visualization"),
      canvas = document.querySelector("#canvas"),
      ctx,
      message;

  if (!canvas) {
    // Create the visualization canvas to draw everything out
    canvas = document.createElement("canvas");
    canvas.setAttribute("id", "canvas");
    canvas.setAttribute("width", CANVAS_MAX_DIM);
    canvas.setAttribute("height", CANVAS_MAX_DIM);
    container.appendChild(canvas);
    ctx = canvas.getContext("2d");

    message = document.createElement("h2");
    message.className = "message";
    container.appendChild(message);
  } else {
    ctx = canvas.getContext("2d");
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    message = container.querySelector(".message");
  }

  function showMessage(msg) {
    message.innerHTML = msg;
    message.style.display = "block";
  }
  
  function hideMessage() {
    message.style.display = "none";
  }
  
  // Get the coords of the target without transformation
  function getOriginalTargetRect() {
    target.style.transform = "none";
    target.style.webkitTransform = "none";

    var rect = {
      x1: CANVAS_PADDING,
      y1: CANVAS_PADDING,
      x2: CANVAS_PADDING + target.clientWidth,
      y2: CANVAS_PADDING,
      x3: CANVAS_PADDING + target.clientWidth,
      y3: CANVAS_PADDING + target.clientHeight,
      x4: CANVAS_PADDING,
      y4: CANVAS_PADDING + target.clientHeight
    };
    target.style.transform = targetTransform;
    target.style.webkitTransform = targetTransform;
    // If for some reasons re-assigning the style didn't work
    if (target.style.transform === "none" ||
        target.style.webkitTransform === "none") {
      return null;
    } else {
      return rect;
    }
  }

  var originalTargetRect = getOriginalTargetRect();

  function getOrigin() {
    var offset = targetTransformOrigin.split(" ");
    offset.forEach(function(item, index) {
      offset[index] = parseInt(item, 10) + CANVAS_PADDING;
    });
    return offset;
  }

  // Get the coords of the transformed target
  function getTransformedTargetRect() {
    // Get the transformation matrix from the computed style
    var tr = targetTransform, transformMatrix = [];
    tr = tr.substring(0, tr.length - 1).substring(tr.indexOf("(") + 1).split(",");
    
    tr.forEach(function(value, index) {
      tr[index] = parseFloat(value, 10);
    });

    if (tr.length === 6) {
      // 2d transform
      transformMatrix = [
        [tr[0], tr[2], tr[4], 0],
        [tr[1], tr[3], tr[5], 0],
        [0,     0,     1,     0],
        [0,     0,     0,     1]
      ];
    } else {
      // 3d transform
      transformMatrix = [
        [tr[0], tr[4], tr[8],  tr[12]],
        [tr[1], tr[5], tr[9],  tr[13]],
        [tr[2], tr[6], tr[10], tr[14]],
        [tr[3], tr[7], tr[11], tr[15]]
      ];
    }
    
    function multiplyMatricesAndGetXY(m1, m2) {
      var m = [];
      for (var m1Line = 0; m1Line < m1.length; m1Line ++) {
        m[m1Line] = 0;
        for (var m2Col = 0; m2Col < m2.length; m2Col ++) {
          m[m1Line] += m1[m1Line][m2Col] * m2[m2Col];
        }
      }
      return {x: m[0], y: m[1]};
    }
    
    // transform-origin might be set and we need to take it into account
    var offset = getOrigin();
    
    // Each of the shape's corners in a matrix
    var topLeftMatrix = [originalTargetRect.x1 - offset[0], originalTargetRect.y1 - offset[1], 1, 1];
    var topRightMatrix = [originalTargetRect.x2 - offset[0], originalTargetRect.y2 - offset[1], 1, 1];
    var bottomRightMatrix = [originalTargetRect.x3 - offset[0], originalTargetRect.y3 - offset[1], 1, 1];
    var bottomLeftMatrix = [originalTargetRect.x4 - offset[0], originalTargetRect.y4 - offset[1], 1, 1];

    var topLeft = multiplyMatricesAndGetXY(transformMatrix, topLeftMatrix);
    var topRight = multiplyMatricesAndGetXY(transformMatrix, topRightMatrix);
    var bottomRight = multiplyMatricesAndGetXY(transformMatrix, bottomRightMatrix);
    var bottomLeft = multiplyMatricesAndGetXY(transformMatrix, bottomLeftMatrix);

    return {
      x1: topLeft.x + offset[0],
      y1: topLeft.y + offset[1],
      x2: topRight.x + offset[0],
      y2: topRight.y + offset[1],
      x3: bottomRight.x + offset[0],
      y3: bottomRight.y + offset[1],
      x4: bottomLeft.x + offset[0],
      y4: bottomLeft.y + offset[1]
    };
  }

  if (originalTargetRect) {
    var transformedTargetRect = getTransformedTargetRect();
    
    if(transformedTargetRect) {
      
      hideMessage();
    // Resize rects to get everything in our canvas's viewport
      var allXs = [], allYs = [];
      for(var i = 1; i < 5; i ++) {
        allXs.push(originalTargetRect["x" + i]);
        allXs.push(transformedTargetRect["x" + i]);
        allYs.push(originalTargetRect["y" + i]);
        allYs.push(transformedTargetRect["y" + i]);
      }
      var minX = Math.min.apply(Math, allXs);
      var maxX = Math.max.apply(Math, allXs);
      var minY = Math.min.apply(Math, allYs);
      var maxY = Math.max.apply(Math, allYs);

      var spanX = maxX - minX;
      var spanY = maxY - minY;
      var isWide = spanX > spanY;

      var CANVAS_WIDTH = isWide ? CANVAS_MAX_DIM : CANVAS_MAX_DIM * Math.min(spanX, spanY) / Math.max(spanX, spanY);
      var CANVAS_HEIGHT = !isWide ? CANVAS_MAX_DIM : CANVAS_MAX_DIM * Math.min(spanX, spanY) / Math.max(spanX, spanY);

      canvas.setAttribute("width", CANVAS_WIDTH);
      canvas.setAttribute("height", CANVAS_HEIGHT);

      function mapX(x) {
        return CANVAS_PADDING + ((CANVAS_WIDTH - 2 * CANVAS_PADDING) / (maxX - minX)) * (x - minX);
      }
      function mapY(y) {
        return CANVAS_PADDING + ((CANVAS_HEIGHT - 2 * CANVAS_PADDING) / (maxY - minY)) * (y - minY);
      }

      for(var i = 1; i < 5; i ++) {
        originalTargetRect["x" + i] = mapX(originalTargetRect["x" + i]);
        originalTargetRect["y" + i] = mapY(originalTargetRect["y" + i]);
        transformedTargetRect["x" + i] = mapX(transformedTargetRect["x" + i]);
        transformedTargetRect["y" + i] = mapY(transformedTargetRect["y" + i]);
      }

      // Draw both boxes
      function drawRect(rect, fillStyle, strokeStyle) {
        ctx.beginPath();
        ctx.moveTo(Math.round(rect.x1) + .5, Math.round(rect.y1) + .5);
        ctx.lineTo(Math.round(rect.x2) + .5, Math.round(rect.y2) + .5);
        ctx.lineTo(Math.round(rect.x3) + .5, Math.round(rect.y3) + .5);
        ctx.lineTo(Math.round(rect.x4) + .5, Math.round(rect.y4) + .5);
        ctx.lineTo(Math.round(rect.x1) + .5, Math.round(rect.y1) + .5);
        ctx.lineWidth = 1;
        ctx.fillStyle = fillStyle;
        ctx.fill();
        ctx.lineWidth = 2;
        ctx.strokeStyle = strokeStyle;
        ctx.stroke();
      }

      drawRect(originalTargetRect, "#1F303F", "#B2D8FF");
      drawRect(transformedTargetRect, "rgba(200, 200, 200, .5)", "#B2D8FF");

      // Draw connecting lines
      function drawLine(x1, y1, x2, y2) {
        if (Math.abs(x2-x1) < 20 && Math.abs(y2-y1) < 20) {
          return;
        }
        ctx.beginPath();
        ctx.moveTo(Math.round(x1) + .5, Math.round(y1) + .5);
        ctx.lineTo(Math.round(x2) + .5, Math.round(y2) + .5);
        ctx.lineWidth = 2;
        ctx.strokeStyle = '#329AFF';
        ctx.stroke();

        // Drawing the arrow tips
        ctx.save();
        ctx.beginPath();
        ctx.lineWidth = 2;
        ctx.fillStyle = '#329AFF';
        ctx.translate(x2, y2);
        var radians = Math.atan((y1 - y2) / (x1 - x2));
        radians += ((x1 >= x2) ? -90 : 90) * Math.PI / 180;
        ctx.rotate(radians);
        ctx.moveTo(0, 0);
        ctx.lineTo(8, 20);
        ctx.lineTo(-8, 20);
        ctx.closePath();
        ctx.fill();
        ctx.restore();
      }

      drawLine(originalTargetRect.x1, originalTargetRect.y1, transformedTargetRect.x1, transformedTargetRect.y1);
      drawLine(originalTargetRect.x2, originalTargetRect.y2, transformedTargetRect.x2, transformedTargetRect.y2);
      drawLine(originalTargetRect.x3, originalTargetRect.y3, transformedTargetRect.x3, transformedTargetRect.y3);
      drawLine(originalTargetRect.x4, originalTargetRect.y4, transformedTargetRect.x4, transformedTargetRect.y4);

      // Draw the transform origin
      var origin = getOrigin();
      ctx.beginPath();
      ctx.arc(mapX(origin[0]), mapY(origin[1]), 5, 0, 2 * Math.PI, false);
      ctx.fillStyle = '#ddd';
      ctx.fill();
      ctx.strokeStyle = '#329AFF';
      ctx.stroke();

      ctx.beginPath();
      ctx.arc(mapX(origin[0]), mapY(origin[1]), 10, 0, 2 * Math.PI, false);
      ctx.strokeStyle = '#329AFF';
      ctx.stroke();
    }
  } else {
    showMessage("Oops, something went wrong with " + targetTransform + " / " + targetTransformOrigin);
  }
}
              
            
!
999px

Console