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

              
                <svg class="defs">
  <defs>
    <svg id="slash" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 4 20"><rect x="0" y="0" width="4" height="20"/></svg>
  </defs>
</svg>

<div class="container">
  <h1>Slash Rotation <svg class="slash" aria-hidden="true" role="image"><use xlink:href="#slash"/></svg></h1>
  <div class="slashfield" data-rows="3"></div>
  <h2>Five Rows</h2>
  <div class="slashfield" data-rows="5"></div>
  <h2>Two Rows</h2>
  <div class="slashfield" data-rows="2"></div>
</div>
              
            
!

CSS

              
                // Adding CSS Transitions has a cool effect,
// but really slows down performance
// $transition: transform .2s ease-out;
$transition: none;

.slashfield {
  overflow: hidden;
  margin-left: -8px;
  width: calc(100% + 16px);
  
  .slash {
    width: 4px;
    height: 20px;
    fill: #9c9393;
    padding: 8px 16px;
    vertical-align: top;
    will-change: transform;
    transform: rotate(45deg) translateZ(0);
    transform-origin: center;
    transition: $transition;
    &:hover {
      fill: #e95555;
    }
  }
}

// Demo Styles
body {
  color: #444;
  padding: 100px;
  background: #efefef;
  font-family: 'Space Mono';
}
.defs {
  width: 0;
  height: 0;
}
.container {
  margin: 0 auto;
  max-width: 1240px;
}
h1 {
  position: relative;
  .slash {
    width: 4px;
    height: 20px;
    fill: #e95555;
    transform: rotate(45deg) translateZ(0);
    transition: $transition;
  }
}
              
            
!

JS

              
                // Establish slashes, their size, and max width of field
var $slash = $('#slash');
var slashW = 36;
var slashH = 36;
var maxW = 1240;

// Draw the slashes
$('.slashfield').each(function(e) {
  var rows = $(this).data('rows');
  var slashAmount = rows*(maxW / slashW);
  var fieldW = $(this).width();
  
  $(this).height(slashH*rows);
  
  for(var i = 0;i < slashAmount; i++) {
    $(this).append('<svg class="slash" aria-hidden="true" role="image"><use xlink:href="#slash"/></svg>')
  }
});
// Get the drawn slashes and their size
var slashes = $('.slash');
var slashesW = slashes.outerWidth();
var slashesH = slashes.outerHeight();

// Track Mouse Movement
// Event throttling
var lastMove = 0;
var eventThrottle = 10;
$(window).on('mousemove', function(e) {
  e.preventDefault();
  var now = Date.now();
  if (now > lastMove + eventThrottle) {
    lastMove = now;
    var mousePos = {
      x: e.pageX,
      y: e.pageY
    }

    slashes.each(function(e) {
      var slash = $(this);

      var slashPos = {
        x: slash.offset().left + slashesW / 2,
        y: slash.offset().top + slashesH / 2
      }

      var angle = (Math.atan2(mousePos.y - slashPos.y, mousePos.x - slashPos.x) * 180 / Math.PI) - 90;
      if(angle >= 360) {
        angle -= 360;
      }
      // Change transformation
      slash.css('transform', 'rotate('+angle+'deg)');
    });
  }
});
              
            
!
999px

Console