css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="container-outer">
  <div class="container-inner">
    <button data-sketchy="button" class="sketchy-button">
      <!--So Sketchy-->
    </button>
  </div>
</div>
            
          
!
            
              html, body {
  height: 100%;
  margin: 0;
}

html {
  text-align: center;
  background: white;
}

.container-outer {
  display: table;
  width: 100%;
  height: 100%;
}

.container-inner {
  display: table-cell;
  vertical-align: middle;
  width: 100%;
}

.sketchy-button {
  position: relative;
  font-size: 3rem;
  margin: 1em;
  padding: 1.5em 4em;
  border: 0;
  color: white;
  background: none;
  
  font-family: "Comic Sans MS", sans-serif;
  
  > svg {
    position: absolute;
    top: 0;
    left: 0;
    z-index: -1;
    
    // Scale and transform to render SVG filters at a higher resolution.
    width: 200%;
    transform-origin: 0% 0%;
    transform: translateZ(0) scale(0.5);
    
    // SVG's default rendering looks fine on Retina screens, so disable the scaling.
    // (This class is added to <html> on load.)
    .hi-dpi & {
      width: 100%;
      transform: none;
    }
  }
  
  &:focus {
    outline: none;
    
    > svg rect {
      stroke: #66afe9;
      stroke-width: 2;
      stroke-opacity: 1;
    }
  }
}
            
          
!
            
              // Initialize button(s) on load

window.onload = function() {
  var sketchyBtns = document.querySelectorAll('[data-sketchy="button"]')

  for (var i = 0; i < sketchyBtns.length; i++) {
    var btn = sketchyBtns[i]
    var btnBG = new SketchySVG(btn)
    btn.appendChild(btnBG.svg)
  }
  
  // Add class for retina
  if(isRetina()) {
    document.documentElement.classList.add('hi-dpi');
  }
}

// Detect Hi-DPI screens
function isRetina() {
  if(window.devicePixelRatio > 1) {
    return true;
  }

  if(window.matchMedia && window.matchMedia('(-webkit-min-device-pixel-ratio: 1.5), (min--moz-device-pixel-ratio: 1.5), (-o-min-device-pixel-ratio: 3/2), (min-resolution: 1.5dppx)').matches) {
    return true;
  }

  return false;
}

// Global variables

var svgNS = 'http://www.w3.org/2000/svg' // SVG namespace
var svgID = 0 // Iterates to to allow each instance to have a unique ID

// Constructor

function SketchySVG(element) {
  this.el = element

  var filterID = 'SketchyFilter' + svgID
  svgID++

  var svg = document.createElementNS(svgNS, 'svg')
  var defs = document.createElementNS(svgNS, 'defs')

  var filter = document.createElementNS(svgNS, 'filter')
  filter.setAttribute('id', filterID)

  // Turbulence filter
  // Generates a randomized pattern to use for displacement
  var turbulence = document.createElementNS(svgNS, 'feTurbulence')
  turbulence.setAttribute('baseFrequency', '0.009')
  turbulence.setAttribute('numOctaves', '10')
  turbulence.setAttribute('type', 'turbulence')
  var seed = Math.round(Math.random() * 1000)
  turbulence.setAttribute('seed', seed)
  turbulence.setAttribute('result', 'turbulenceResult')

  // Displacement filter
  // Offsets pixels according to turbulence output
  var displacement = document.createElementNS(svgNS, 'feDisplacementMap')
  displacement.setAttribute('in', 'SourceGraphic')
  displacement.setAttribute('in2', 'turbulenceResult')
  displacement.setAttribute('xChannelSelector', 'R')
  displacement.setAttribute('scale', '7')
  displacement.setAttribute('result', 'displacementResult')

  // Create a rectangle and apply the filter
  var rect = document.createElementNS(svgNS, 'rect')
  rect.setAttribute('fill', '#000000')
  rect.setAttribute('filter', 'url(#' + filterID + ')')

  // Add text within the SVG and apply the same filter
  var text = document.createElementNS(svgNS, 'text')
  text.setAttribute('text-anchor', 'middle')
  text.setAttribute('fill', '#ffffff')
  text.appendChild(document.createTextNode('So Sketchy'))
  text.setAttribute('filter', 'url(#' + filterID + ')')

  // Add everything to the SVG DOM object
  filter.appendChild(turbulence)
  filter.appendChild(displacement)
  defs.appendChild(filter)
  svg.appendChild(defs)
  svg.appendChild(rect)
  svg.appendChild(text)

  // Save references to SVG elements
  this.svg = svg
  this.rect = rect
  this.text = text
  this.turbFilter = turbulence

  this.resize()

  window.addEventListener('resize', this.resize.bind(this))

  // Animation properties
  this.isSketching = false
  this.sketchingTicks = 0
  this.sketchingTimeout = 0
  this.seed = seed
  this.fps = 12

  this.startSketching()
}

// Resize to fit container

SketchySVG.prototype.resize = function() {
  var w = this.el.offsetWidth
  var h = this.el.offsetHeight

  this.svg.setAttribute('viewBox', '0 0 ' + w + ' ' + h)
  this.rect.setAttribute('width', (w - 8))
  this.rect.setAttribute('height', (h - 8))
  this.rect.setAttribute('x', '4')
  this.rect.setAttribute('y', '4')

  this.text.setAttribute('x', w / 2)
  this.text.setAttribute('y', h * 7 / 11)
}

SketchySVG.prototype.startSketching = function() {
  clearTimeout(this.sketchingTimeout)

  this.isSketching = true
  this.sketchingTicks = 0

  setTimeout(this.tickSketching.bind(this), (1000 / this.fps))
}

SketchySVG.prototype.tickSketching = function() {
  clearTimeout(this.sketchingTimeout)

  this.sketchingTicks++

  // Reset seed every few frames to "loop" the animation
  if (this.sketchingTicks % 25 === 0) {
    this.seed -= 24
  } else {
    this.seed++
  }

  this.turbFilter.setAttribute('seed', this.seed)

  if (this.isSketching) {
    this.sketchingTimeout = setTimeout(this.tickSketching.bind(this), (1000 / this.fps))
  }
}

SketchySVG.prototype.stopSketching = function() {
  clearTimeout(this.sketchingTimeout)

  this.isSketching = false
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console