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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <button id="draggem" type="button" data-active="false">Drag me!</button>
              
            
!

CSS

              
                html {
  height: 100%;
}
body {
  display: flex;
  width: 100%;
  height: 100%;
  background-color: hsl(var(--hue, 0), 70%, 50%);
}
button {
  all: initial;
  display: block;
  margin: auto;
  padding: 0.3em 0.6em;
  min-width: 4.3em;
  text-align: center;
  font-size: 2.5em;
  font-family: 'Modak', sans-serif;
  background-color: black;
  border-radius: 0.3em;
  cursor: move;
  box-shadow: 0 0 0 transparent;
  transition: box-shadow 0.15s;
  
  color: hsl(var(--hue, 0), 80%, 60%);
  transform: translate(calc(var(--moveX, 0) * 1px), calc(var(--moveY, 0) * 1px));
}
button:hover,
button:focus,
button:active,
button[data-active="true"] {
  --sw: 0.1em;
  box-shadow:
    calc(var(--sw) * -1) var(--sw) 0 hsl(calc(var(--hue, 0) + 180), 80%, 60%),
    calc(var(--sw) * -1) calc(var(--sw) * -1) 0 hsl(calc(var(--hue, 0) + 180), 80%, 60%),
    var(--sw) var(--sw) 0 hsl(calc(var(--hue, 0) + 180), 80%, 60%),
    var(--sw) calc(var(--sw) * -1) 0 hsl(calc(var(--hue, 0) + 180), 80%, 60%);
}
              
            
!

JS

              
                (function() {
  // David Walsh's debouncer
  function debounce(func, wait, immediate) {
    let timeout
    return function() {
      let context = this,
          args = arguments
      let later = function() {
        timeout = null
        if (!immediate) func.apply(context, args)
      }
      let callNow = immediate && !timeout
      clearTimeout(timeout)
      timeout = setTimeout(later, wait)
      if (callNow) func.apply(context, args)
    }
  }
  function transpose(partialValue, totalValue, mapTo) {
    let ratio = partialValue / totalValue
    return Math.floor(ratio * mapTo)
  }
  function getCoordinates(e) {
    let xpos
    let ypos
    // This property check seems to be a lot more reliable
    // than checking for window.PointerEvents
    if (e.touches) {
      xpos = e.touches[0].clientX
      ypos = e.touches[0].clientY
    } else {
      xpos = e.clientX
      ypos = e.clientY
    }
    return {
      xpos: xpos,
      ypos: ypos
    }
  }
  function translateEl(xpos, ypos, el) {
    el.style.setProperty('--moveX', xpos)
    el.style.setProperty('--moveY', ypos)
  }
  
  const container = document.body
  const draggem = document.getElementById('draggem')
  
  if ('ontouchstart' in window) {
    // Prevent iOS browsers from scrolling around while trying to drag
    document.addEventListener('touchstart', function(e) {
      e.preventDefault()
    }, {passive: false})
    
    container.addEventListener('touchstart', startDrag, false)
    container.addEventListener('touchmove', drag, false)
    container.addEventListener('touchend', endDrag, false)
  } else {
    container.addEventListener('pointerdown', startDrag, false)
    container.addEventListener('pointermove', drag, false)
    container.addEventListener('pointerup', endDrag, false)
    container.addEventListener('pointerleave', endDrag, false)
  }
  
  let isActive = false
  let currentX
  let currentY
  let initialX
  let initialY
  let xOffset = 0
  let yOffset = 0
  
  // Define the edges of the button's containing element...
  function getBounds() {
    return {
      x1: (container.offsetWidth / 2) * -1,
      x2: container.offsetWidth / 2,
      y1: (container.offsetHeight / 2) * -1,
      y2: container.offsetHeight / 2
    }
  }
  let bounds = getBounds()
  // ...and keep those edges updated
  const updateBounds = debounce(function() {
    bounds = getBounds()
    // Reset the button and its effects so that it doesn't
    // disappear if the viewport is narrowed:
    currentX = 0
    currentY = 0
    xOffset = 0
    yOffset = 0
    translateEl(currentX, currentY, draggem)
    container.style.setProperty('--hue', 0)
  }, 100)
  window.addEventListener('resize', updateBounds)
  
  function startDrag(e) {
    let cursorCoords = getCoordinates(e)
    initialX = cursorCoords.xpos - xOffset
    initialY = cursorCoords.ypos - yOffset
    
    if (e.target === draggem) {
      isActive = true
      draggem.setAttribute('data-active', isActive)
      draggem.innerHTML = 'Whoaaaa'
    }
  }
  function drag(e) {
    let cursorCoords = getCoordinates(e)
    if (isActive) {
      e.preventDefault()
      e.stopPropagation()
      let minX = bounds.x1 + (draggem.offsetWidth / 2)
      let maxX = bounds.x2 - (draggem.offsetWidth / 2)
      let minY = bounds.y1 + (draggem.offsetHeight / 2)
      let maxY = bounds.y2 - (draggem.offsetHeight / 2)
      // console.log(minX, maxX, minY, maxY)
      // console.log(bounds.x1, bounds.x2, bounds.y1, bounds.y2)
      
      let dx = cursorCoords.xpos - initialX
      let dy = cursorCoords.ypos - initialY
      
      if (dx < minX) {
        dx = minX
      } else if (dx > maxX) {
        dx = maxX
      }
      if (dy < minY) {
        dy = minY
      } else if (dy > maxY) {
        dy = maxY
      }
      
      currentX = dx
      currentY = dy
      xOffset = currentX
      yOffset = currentY
      
      translateEl(currentX, currentY, draggem)
      
      let hueValue = transpose(
        Math.hypot(currentX, currentY),
        Math.hypot((window.innerWidth / 2), (window.innerHeight / 2)),
        360
      )
      container.style.setProperty('--hue', hueValue)
    }
  }
  function endDrag(e) {
    initialX = currentX
    initialY = currentY
    isActive = false
    draggem.setAttribute('data-active', isActive)
    draggem.innerHTML = 'Drag me!'
  }
  
})();
              
            
!
999px

Console