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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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 class="lightsaber__drag">
  <div class="lightsaber"><svg class="svg-saber" viewBox="0 0 229 3706" fill="none" xmlns="http://www.w3.org/2000/svg">
  <g class="saber__handle">
    <rect x="70" y="3404" width="92" height="302" rx="14" fill="#939393"/>
    <rect x="70" y="3404" width="92" height="302" rx="14" fill="url(#a)"/>
    <rect x="89" y="2916" width="50" height="273" fill="#000"/>
    <rect x="89" y="2916" width="50" height="273" fill="url(#b)"/>
    <rect x="70" y="3126" width="93" height="200" fill="#000"/>
    <rect x="70" y="3126" width="93" height="200" fill="url(#c)"/>
    <path d="M70 3315.5h93l10 36.5H60l10-36.5z" fill="#000"/>
    <path d="M70 3315.5h93l10 36.5H60l10-36.5z" fill="url(#d)"/>
    <rect x="81" y="2931" width="66" height="63" fill="#C4C4C4"/>
    <rect x="81" y="2931" width="66" height="63" fill="url(#e)"/>
    <rect x="70" y="2924" width="88" height="59" fill="#BDBDBD"/>
    <rect x="70" y="2924" width="88" height="59" fill="url(#f)"/>
    <rect x="60" y="3126" width="112" height="11" rx="5.5" fill="#BDBDBD"/>
    <rect x="60" y="3126" width="112" height="11" rx="5.5" fill="url(#g)"/>
    <rect x="60" y="3147" width="112" height="11" rx="5.5" fill="#BDBDBD"/>
    <rect x="60" y="3147" width="112" height="11" rx="5.5" fill="url(#h)"/>
    <rect x="60" y="3168" width="112" height="11" rx="5.5" fill="#BDBDBD"/>
    <rect x="60" y="3168" width="112" height="11" rx="5.5" fill="url(#i)"/>
    <rect x="60" y="3189" width="112" height="11" rx="5.5" fill="#BDBDBD"/>
    <rect x="60" y="3189" width="112" height="11" rx="5.5" fill="url(#j)"/>
    <rect x="60" y="3210" width="112" height="11" rx="5.5" fill="#BDBDBD"/>
    <rect x="60" y="3210" width="112" height="11" rx="5.5" fill="url(#k)"/>
    <rect x="60" y="3231" width="112" height="11" rx="5.5" fill="#BDBDBD"/>
    <rect x="60" y="3231" width="112" height="11" rx="5.5" fill="url(#l)"/>
    <rect x="60" y="3252" width="112" height="11" rx="5.5" fill="#BDBDBD"/>
    <rect x="60" y="3252" width="112" height="11" rx="5.5" fill="url(#m)"/>
    <rect x="60" y="3273" width="112" height="11" rx="5.5" fill="#BDBDBD"/>
    <rect x="60" y="3273" width="112" height="11" rx="5.5" fill="url(#n)"/>
    <rect x="60" y="3294" width="112" height="11" rx="5.5" fill="#BDBDBD"/>
    <rect x="60" y="3294" width="112" height="11" rx="5.5" fill="url(#o)"/>
    <rect x="60" y="3315" width="112" height="11" rx="5.5" fill="#BDBDBD"/>
    <rect x="60" y="3315" width="112" height="11" rx="5.5" fill="url(#p)"/>
    <rect x="71" y="3041" width="86" height="45" fill="#BDBDBD"/>
    <rect x="71" y="3041" width="86" height="45" fill="url(#q)"/>
    <rect x="60" y="3352" width="113" height="248" fill="#BDBDBD"/>
    <rect x="60" y="3352" width="113" height="248" fill="url(#r)"/>
    <rect x="60" y="3628" width="113" height="63" fill="#BDBDBD"/>
    <rect x="60" y="3628" width="113" height="63" fill="url(#s)"/>
    <rect x="57" y="2916" width="115" height="26" fill="#D0D0D0"/>
    <rect x="57" y="2916" width="115" height="26" fill="url(#t)"/>
    <rect x="44" y="2916" width="141" height="8" rx="4" fill="#CBCBCB"/>
    <rect x="44" y="2916" width="141" height="8" rx="4" fill="url(#u)"/>
    <circle cx="89" cy="3435" r="18" fill="#5A5A5A"/>
    <circle cx="89" cy="3435" r="14" fill="#FD2626"/>
  </g>
  <g class="saber__beam beam">
    <g filter="url(#v)">
      <g class="saber__beam beam__bleed">
        <rect x="80" y="80" width="69" height="2836" rx="34.5"/>
      </g>
    </g>
    <g clip-path="url(#w)" class="beam__core">
      <rect x="80" y="109" width="69" height="2836" rx="34.5"/>
    </g>
  </g>
  <defs>
    <linearGradient id="a" x1="162" y1="3552.44" x2="70" y2="3552.44" gradientUnits="userSpaceOnUse">
      <stop stop-opacity=".25"/>
      <stop offset=".302" stop-color="#fff" stop-opacity="0"/>
      <stop offset=".578" stop-color="#fff"/>
      <stop offset=".781" stop-color="#404040"/>
      <stop offset="1" stop-color="#fff" stop-opacity="0"/>
    </linearGradient>
    <linearGradient id="b" x1="139" y1="3052.5" x2="89" y2="3052.5" gradientUnits="userSpaceOnUse">
      <stop stop-color="#fff" stop-opacity="0"/>
      <stop offset=".74"/>
      <stop offset="1" stop-color="#fff" stop-opacity="0"/>
    </linearGradient>
    <linearGradient id="c" x1="163" y1="3226" x2="70" y2="3226" gradientUnits="userSpaceOnUse">
      <stop stop-color="#fff" stop-opacity="0"/>
      <stop offset=".74"/>
      <stop offset="1" stop-color="#fff" stop-opacity="0"/>
    </linearGradient>
    <linearGradient id="d" x1="173" y1="3344.5" x2="60" y2="3334" gradientUnits="userSpaceOnUse">
      <stop stop-color="#fff" stop-opacity="0"/>
      <stop offset=".74"/>
      <stop offset="1" stop-color="#fff" stop-opacity="0"/>
    </linearGradient>
    <linearGradient id="e" x1="147" y1="2962" x2="81" y2="2962" gradientUnits="userSpaceOnUse">
      <stop stop-opacity=".25"/>
      <stop offset=".141" stop-color="#fff" stop-opacity="0"/>
      <stop offset=".496" stop-color="#fff"/>
      <stop offset=".693" stop-color="#404040"/>
      <stop offset="1" stop-color="#fff" stop-opacity="0"/>
    </linearGradient>
    <linearGradient id="f" x1="158" y1="2953" x2="70" y2="2953" gradientUnits="userSpaceOnUse">
      <stop stop-opacity=".25"/>
      <stop offset=".151" stop-color="#fff" stop-opacity="0"/>
      <stop offset=".46" stop-color="#fff"/>
      <stop offset=".682" stop-color="#404040"/>
      <stop offset="1" stop-color="#fff" stop-opacity="0"/>
    </linearGradient>
    <linearGradient id="g" x1="172" y1="3131.41" x2="60" y2="3131.41" gradientUnits="userSpaceOnUse">
      <stop stop-opacity=".25"/>
      <stop offset=".151" stop-color="#fff" stop-opacity="0"/>
      <stop offset=".46" stop-color="#fff"/>
      <stop offset=".682" stop-color="#404040"/>
      <stop offset="1" stop-color="#fff" stop-opacity="0"/>
    </linearGradient>
    <linearGradient id="h" x1="172" y1="3152.41" x2="60" y2="3152.41" gradientUnits="userSpaceOnUse">
      <stop stop-opacity=".25"/>
      <stop offset=".151" stop-color="#fff" stop-opacity="0"/>
      <stop offset=".46" stop-color="#fff"/>
      <stop offset=".682" stop-color="#404040"/>
      <stop offset="1" stop-color="#fff" stop-opacity="0"/>
    </linearGradient>
    <linearGradient id="i" x1="172" y1="3173.41" x2="60" y2="3173.41" gradientUnits="userSpaceOnUse">
      <stop stop-opacity=".25"/>
      <stop offset=".151" stop-color="#fff" stop-opacity="0"/>
      <stop offset=".46" stop-color="#fff"/>
      <stop offset=".682" stop-color="#404040"/>
      <stop offset="1" stop-color="#fff" stop-opacity="0"/>
    </linearGradient>
    <linearGradient id="j" x1="172" y1="3194.41" x2="60" y2="3194.41" gradientUnits="userSpaceOnUse">
      <stop stop-opacity=".25"/>
      <stop offset=".151" stop-color="#fff" stop-opacity="0"/>
      <stop offset=".46" stop-color="#fff"/>
      <stop offset=".682" stop-color="#404040"/>
      <stop offset="1" stop-color="#fff" stop-opacity="0"/>
    </linearGradient>
    <linearGradient id="k" x1="172" y1="3215.41" x2="60" y2="3215.41" gradientUnits="userSpaceOnUse">
      <stop stop-opacity=".25"/>
      <stop offset=".151" stop-color="#fff" stop-opacity="0"/>
      <stop offset=".46" stop-color="#fff"/>
      <stop offset=".682" stop-color="#404040"/>
      <stop offset="1" stop-color="#fff" stop-opacity="0"/>
    </linearGradient>
    <linearGradient id="l" x1="172" y1="3236.41" x2="60" y2="3236.41" gradientUnits="userSpaceOnUse">
      <stop stop-opacity=".25"/>
      <stop offset=".151" stop-color="#fff" stop-opacity="0"/>
      <stop offset=".46" stop-color="#fff"/>
      <stop offset=".682" stop-color="#404040"/>
      <stop offset="1" stop-color="#fff" stop-opacity="0"/>
    </linearGradient>
    <linearGradient id="m" x1="172" y1="3257.41" x2="60" y2="3257.41" gradientUnits="userSpaceOnUse">
      <stop stop-opacity=".25"/>
      <stop offset=".151" stop-color="#fff" stop-opacity="0"/>
      <stop offset=".46" stop-color="#fff"/>
      <stop offset=".682" stop-color="#404040"/>
      <stop offset="1" stop-color="#fff" stop-opacity="0"/>
    </linearGradient>
    <linearGradient id="n" x1="172" y1="3278.41" x2="60" y2="3278.41" gradientUnits="userSpaceOnUse">
      <stop stop-opacity=".25"/>
      <stop offset=".151" stop-color="#fff" stop-opacity="0"/>
      <stop offset=".46" stop-color="#fff"/>
      <stop offset=".682" stop-color="#404040"/>
      <stop offset="1" stop-color="#fff" stop-opacity="0"/>
    </linearGradient>
    <linearGradient id="o" x1="172" y1="3299.41" x2="60" y2="3299.41" gradientUnits="userSpaceOnUse">
      <stop stop-opacity=".25"/>
      <stop offset=".151" stop-color="#fff" stop-opacity="0"/>
      <stop offset=".46" stop-color="#fff"/>
      <stop offset=".682" stop-color="#404040"/>
      <stop offset="1" stop-color="#fff" stop-opacity="0"/>
    </linearGradient>
    <linearGradient id="p" x1="172" y1="3320.41" x2="60" y2="3320.41" gradientUnits="userSpaceOnUse">
      <stop stop-opacity=".25"/>
      <stop offset=".151" stop-color="#fff" stop-opacity="0"/>
      <stop offset=".46" stop-color="#fff"/>
      <stop offset=".682" stop-color="#404040"/>
      <stop offset="1" stop-color="#fff" stop-opacity="0"/>
    </linearGradient>
    <linearGradient id="q" x1="157" y1="3063.12" x2="71" y2="3063.12" gradientUnits="userSpaceOnUse">
      <stop stop-opacity=".25"/>
      <stop offset=".151" stop-color="#fff" stop-opacity="0"/>
      <stop offset=".46" stop-color="#fff"/>
      <stop offset=".682" stop-color="#404040"/>
      <stop offset="1" stop-color="#fff" stop-opacity="0"/>
    </linearGradient>
    <linearGradient id="r" x1="173" y1="3473.9" x2="60" y2="3473.9" gradientUnits="userSpaceOnUse">
      <stop stop-opacity=".25"/>
      <stop offset=".302" stop-color="#fff" stop-opacity="0"/>
      <stop offset=".578" stop-color="#fff"/>
      <stop offset=".781" stop-color="#404040"/>
      <stop offset="1" stop-color="#fff" stop-opacity="0"/>
    </linearGradient>
    <linearGradient id="s" x1="173" y1="3658.97" x2="60" y2="3658.97" gradientUnits="userSpaceOnUse">
      <stop stop-opacity=".25"/>
      <stop offset=".302" stop-color="#fff" stop-opacity="0"/>
      <stop offset=".578" stop-color="#fff"/>
      <stop offset=".781" stop-color="#404040"/>
      <stop offset="1" stop-color="#fff" stop-opacity="0"/>
    </linearGradient>
    <linearGradient id="t" x1="172" y1="2929" x2="57" y2="2929" gradientUnits="userSpaceOnUse">
      <stop stop-opacity=".25"/>
      <stop offset=".177" stop-color="#fff" stop-opacity="0"/>
      <stop offset=".43" stop-color="#fff"/>
      <stop offset=".677" stop-color="#404040"/>
      <stop offset="1" stop-color="#fff" stop-opacity="0"/>
    </linearGradient>
    <linearGradient id="u" x1="44" y1="2920" x2="185" y2="2920" gradientUnits="userSpaceOnUse">
      <stop stop-color="#fff" stop-opacity="0"/>
      <stop offset=".307" stop-color="#5E5E5E"/>
      <stop offset=".583" stop-color="#fff"/>
      <stop offset=".818" stop-color="#fff" stop-opacity="0"/>
      <stop offset="1" stop-opacity=".25"/>
    </linearGradient>
    <clipPath id="w">
      <rect width="141" height="2807" fill="#fff" transform="translate(44 109)"/>
    </clipPath>
    <filter id="v" x="0" y="0" width="329" height="2996" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">
      <feFlood flood-opacity="0" result="BackgroundImageFix"/>
      <feBlend in="SourceGraphic" in2="BackgroundImageFix" result="shape"/>
      <feGaussianBlur stdDeviation="40" result="effect1_foregroundBlur"/>
    </filter>
  </defs>
</svg>

  </div>
</div>
              
            
!

CSS

              
                *
  box-sizing border-box

:root
  --beam-hue 90

body
  min-height 100vh
  display grid
  place-items center
  overflow hidden
  background hsl(0, 0%, 4%)

.lightsaber
  width clamp(10px, 4vmin, 44px)
  position absolute
  left 50%
  transform translate(-50%, 0)

  &__drag
    height 44px
    width 44px

.beam
  &__bleed rect
    fill 'hsl(%s, 90%, 50%)' % var(--beam-hue)
  &__core rect
    fill 'hsl(%s, 90%, 95%)' % var(--beam-hue)

.svg-saber
  position absolute
  bottom 0
  transform translate(0, 21.25%)
  left 0
  overflow visible !important
  display none
              
            
!

JS

              
                const {
  gsap: {
    set,
    to,
    timeline,
    registerPlugin,
    utils: { mapRange, clamp, random },
  },
  Draggable,
  InertiaPlugin,
} = window

registerPlugin(InertiaPlugin)

let shakeReset
let comparator
let INERT_COUNT = 0
let toggleTimeout
let humInterval
let SHAKING = false
const SABER = document.querySelector('.lightsaber')
const SABER_DRAG = document.querySelector('.lightsaber__drag')
const BOUNDS = 800
const SHAKE_BOUND = 200
const ROTATE = 35
const SHAKE_THRESHOLD = 6
const SHAKE_TIMEOUT = 500
const AUDIO = {
  HUM: new Audio('https://assets.codepen.io/605876/lightsaber-hum.mp3'),
  ON: new Audio('https://assets.codepen.io/605876/lightsaber-on.mp3'),
  OFF: new Audio('https://assets.codepen.io/605876/lightsaber-off.mp3'),
  ON_TWO: new Audio('https://assets.codepen.io/605876/lightsaber-on--two.mp3'),
  SPIN: new Audio('https://assets.codepen.io/605876/lightsaber-spin.mp3'),
  SWING_ONE: new Audio(
    'https://assets.codepen.io/605876/lightsaber-swing--one.mp3'
  ),
  SWING_TWO: new Audio(
    'https://assets.codepen.io/605876/lightsaber-swing--two.mp3'
  ),
}
const STATE = {
  ON: false,
}

set('.saber__beam', {
  scaleY: 0,
  transformOrigin: '50% 99%',
})

set(SABER_DRAG, {
  transformOrigin: '50% 500%',
})

const toggleBeam = () => {
  STATE.ON = !STATE.ON
  if (STATE.ON) {
    to('.saber__beam', {
      scaleY: 1,
      duration: 0.1,
      onStart: () => {
        AUDIO[`ON${Math.random() > 0.5 ? '_TWO' : ''}`].play()
        AUDIO.OFF.pause()
        AUDIO.OFF.currentTime = 0
      },
      onComplete: () => {
        PULSE.play()
        humInterval = setInterval(() => {
          AUDIO.HUM.play()
        }, random(3000, 10000))
      },
    })
  } else {
    clearInterval(humInterval)
    to('.saber__beam', {
      scaleY: 0,
      duration: 0.1,
      onStart: () => {
        AUDIO.OFF.play()
        AUDIO.ON.pause()
        AUDIO.ON.currentTime = 0
      },
      onComplete: () => {
        PULSE.pause()
      },
    })
  }
}

const TWIRL = () => {
  timeline({
    onStart: () => {
      AUDIO.SPIN.play()
    },
  })
    .to(SABER, {
      rotate: `${Math.random() > 0.5 ? '+' : '-'}=360`,
    })
    .to(
      SABER_DRAG,
      {
        yPercent: 150,
        yoyo: true,
        repeat: 1,
        duration: 0.25,
      },
      '<'
    )
}

let prevent = false

SABER_DRAG.addEventListener('click', () => {
  toggleTimeout = setTimeout(() => {
    if (!prevent) {
      toggleBeam()
    }
    prevent = false
  }, 200)
})
SABER_DRAG.addEventListener('dblclick', () => {
  clearTimeout(toggleTimeout)
  prevent = true
  if (STATE.ON) TWIRL()
})

const PULSE = to('.beam__bleed', {
  paused: true,
  scaleX: 1.075,
  scaleY: 1.005,
  repeat: -1,
  duration: 0.1,
  transformOrigin: '50% 50%',
  yoyo: true,
})

Draggable.create(SABER_DRAG, {
  type: 'x,y',
  trigger: SABER_DRAG,
  inertia: true,
  bounds: document.body,
  dragResistance: 0.25,
  onDrag: function() {
    clearInterval(humInterval)
    set(SABER_DRAG, {
      x: this.x,
      y: this.y,
    })
    const INE_X = InertiaPlugin.getVelocity(SABER_DRAG, 'x')
    const MOM_X = clamp(-BOUNDS, BOUNDS, INE_X)
    const rotation = mapRange(-BOUNDS, BOUNDS, ROTATE, -ROTATE, MOM_X)


    if (INE_X > BOUNDS && STATE.ON) {
      AUDIO.SWING_ONE.play()
    } else if (INE_X < -BOUNDS && STATE.ON) {
      AUDIO.SWING_TWO.play()
    }

    to(SABER_DRAG, {
      rotation,
      duration: 0.25,
    })

    if (
      ((INE_X < -SHAKE_BOUND || INE_X > SHAKE_BOUND) && !SHAKING) ||
      (SHAKING && comparator(INE_X))
    ) {
      SHAKING = true
      INERT_COUNT += 1
      // Need to create a way of mapping the opposite
      if (INE_X < -SHAKE_BOUND) {
        // The next value must be INE_X > SHAKE_BOUND
        // Create a function
        comparator = val => val > SHAKE_BOUND
      } else {
        comparator = val => val < -SHAKE_BOUND
      }

      if (shakeReset) clearTimeout(shakeReset)
      shakeReset = setTimeout(() => {
        INERT_COUNT = 0
        SHAKING = false
        shakeReset = null
      }, SHAKE_TIMEOUT)
    }
  },
  onDragEnd: () => {
    if (INERT_COUNT > SHAKE_THRESHOLD && STATE.ON) {
      timeline({
        onComplete: () => {
          document.documentElement.style.setProperty(
            '--beam-hue',
            random(0, 359)
          )
          to(SABER, {
            rotation: 0,
            duration: 0.25,
          })
        },
      }).fromTo(
        SABER,
        {
          rotation: -5,
        },
        {
          rotation: 5,
          duration: 0.05,
          repeat: 2,
          yoyo: true,
        }
      )
    }

    if (STATE.ON) {
      humInterval = setInterval(() => {
        AUDIO.HUM.play()
      }, random(3000, 10000))
    }

    to(SABER_DRAG, {
      duration: 0.25,
      rotation: 0,
    })
  },
})


set('.svg-saber', {
  display: 'block'
})
              
            
!
999px

Console