cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              .image-container(style=`background-image: url(https://source.unsplash.com/random/400x400?bear&v=${Math.floor(20 * Math.random() + i)})`)
            
          
!
            
              *
  box-sizing border-box

body
  align-items center
  background radial-gradient(#e74c3c, black)
  display flex
  justify-content center
  height 100vh
  width 100vw
  overflow hidden
  margin 0
  padding 0

.image-container
  background-color #fafafa
  height 200px
  width 200px
  background-size cover
  background-position center

.rsizable
  position relative
  height calc(var(--height, 200) * 1px)
  width calc(var(--width, 200) * 1px)

  &__ghost
    border 5px dashed white
    height calc(var(--height) * 1px)
    width calc(var(--width) * 1px)
    position absolute
    left calc(var(--x) * 1px)
    top calc(var(--y) * 1px)

  $handleSize = 50px
  &__handle
    border-radius 100%
    height $handleSize
    width $handleSize
    display inline-block
    cursor var(--cursor, e)
    position absolute
    transform translate(var(--x, 0), var(--y, 0))

    &:after
      border-radius 100%
      content ''
      background-color white
      height $handleSize * 0.4
      width  $handleSize * 0.4
      position absolute
      top 50%
      left 50%
      transform translate(-50%, -50%)

    &--n
    &--s
      left 50%
      margin-left -($handleSize / 2)

    &--n
      top 0

    &--s
      bottom 0

    &--e
    &--w
      top 50%
      margin-top -($handleSize / 2)

    &--e
      right 0

    &--w
      left 0

    &--ne
      top 0
      right 0

    &--se
      bottom 0
      right 0

    &--nw
      top 0
      left 0

    &--sw
      bottom 0
      left 0

    &--nw
    &--n
    &--ne
      --y -50%

    &--nw
    &--w
    &--sw
      --x -50%

    &--ne
    &--e
    &--se
      --x 50%

    &--sw
    &--s
    &--se
      --y 50%


            
          
!
            
              class Resizable {
  defaultOptions = {
    ghosting: true,
    handles: ['n', 's', 'e', 'w', 'se', 'sw', 'ne', 'nw']
  }
  constructor(element, options) {
    this.element = element
    this.options = Object.assign({}, this.defaultOptions, options)
    this.handles = this.options.handles
    this.ghosting = this.options.ghosting
    this.create()
  }

  create = () => {
    const {
      element,
      options,
    } = this
    element.classList.add('rsizable')
    for (let handle of options.handles) this.createHandle(handle)
  }

  start = (e) => {
    let {
      pageX: startX,
      pageY: startY,
      touches,
    } = e

    if (touches && touches.length === 1) {
      startX = touches[0].pageX
      startY = touches[0].pageY
    }

    const {
      height,
      width,
      x,
      y,
    } = this.element.getBoundingClientRect()


    const resize = (evt) => {
      evt.preventDefault()
      const direction = e.target.getAttribute('data-rsize-direction')
      let { pageX: X, pageY: Y, touches } = evt
      if (touches && touches.length === 1) {
        X = touches[0].pageX
        Y = touches[0].pageY
      }
      for (const d of direction.split('')) {
        switch(d) {
          case 'n':
            // If position absolute, set top to Y
            this.element.style.setProperty('--height', height + (startY - Y))
            break
          case 's':
            this.element.style.setProperty('--height', height + (Y - startY))
            break
          case 'e':
            this.element.style.setProperty('--width', width + (X - startX))
            break
          case 'w':
            // If position absolute, set left to X
            this.element.style.setProperty('--width', width + (startX - X))
            break
        }
      }
    }

    const end = () => {
      if (this.ghost) this.ghost.remove()
      document.body.removeEventListener('mousemove', resize)
      document.body.removeEventListener('touchmove', resize)
      document.body.removeEventListener('mouseup', end)
      document.body.removeEventListener('touchend', end)
    }

    document.body.addEventListener('mousemove', resize)
    document.body.addEventListener('mouseup', end)

    // Add touch support 😉
    document.body.addEventListener('touchmove', resize)
    document.body.addEventListener('touchend', end)

    if (this.options.ghosting) {
      const ghost = document.createElement('span')
      ghost.classList.add('rsizable__ghost')
      ghost.style.setProperty('--x', x + 1)
      ghost.style.setProperty('--y', y + 1)
      ghost.style.setProperty('--height', height - 2)
      ghost.style.setProperty('--width', width - 2)
      document.body.appendChild(ghost)
      this.ghost = ghost
    }
  }

  createHandle = (direction) => {
    const newHandle = document.createElement('span')
    newHandle.classList.add('rsizable__handle', `rsizable__handle--${direction}`)
    newHandle.setAttribute('data-rsize-direction', direction)
    newHandle.style.setProperty('--cursor', `${direction}-resize`)
    this.element.appendChild(newHandle)
    newHandle.addEventListener('mousedown', this.start)
    newHandle.addEventListener('touchstart', this.start)
  }
}

const imageToResize = document.querySelector('.image-container')
const resizableImage = new Resizable(imageToResize, {})

            
          
!
999px
Loading ..................

Console