123

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.

            
              *
  box-sizing border-box
  cursor pointer
  -webkit-tap-highlight-color transparent

body
  margin 0
  min-height 100vh
  padding 0
  background #111
  overflow hidden
  transform-style preserve-3d

  &:after
    content ''
    position absolute
    background linear-gradient(90deg, #666 10%, silver)
    width 10px
    height 10px
    top 50%
    left 50%
    transform translate(-50%, 120px)
    border-radius 0 0 100% 100%

canvas
  max-width 100vw
  transform translate3d(0, 0, 100px)
  z-index 3
  position absolute
  height 100vh
  width 100vw

.tesla-coil
  &__head
    border-radius 100%
    height 50px
    width 50px
    background radial-gradient(circle at 60% 40%, silver 50%, #000), silver
    position absolute
    top 50%
    left 50%
    transform translate(-50%, -50%) translate3d(0, 0, 100px)
    z-index 3
    perspective 350px
    transform-style preserve-3d

    &--active
      box-shadow 0 0 15px 4px #a537fd

  &__body
    height 180px
    position absolute
    top 50%
    left 50%
    width 10px
    background linear-gradient(90deg, #666 10%, silver)
    transform translate(-50%, 0) rotateX(-30deg)
    position absolute
    perspective 350px
    transform-style preserve-3d

  &__ring
    content ''
    height calc(var(--size) * 1px)
    width calc(var(--size) * 1px)
    border-radius 100%
    border 15px solid #555
    position absolute
    top 50%
    left 50%
    transform translate(-50%, 0) rotateX(90deg)

    &:nth-of-type(1)
      --size 60
      top 10%
    &:nth-of-type(2)
      --size 70
      top 20%
    &:nth-of-type(3)
      --size 80
      top 30%

  &__base
    $size = 60px
    height $size
    transform-style preserve-3d
    width $size
    position absolute
    top 90%
    left 50%
    transform translate(-50%, -50%) rotateY(35deg)

    div
      background-color silver
      height 100%
      position absolute
      width  100%

      &:nth-of-type(1)
        background #333
        transform rotateY(270deg) translateX(-50%)
        transform-origin center left

      &:nth-of-type(2)
        transform rotateX(90deg) translateY(-50%)
        transform-origin top center

      &:nth-of-type(3)
        background #666
        transform translateZ(($size / 2))
            
          
!
            
              const { React, ReactDOM } = window
const { useEffect, useState, useRef, Fragment } = React
const { render } = ReactDOM
const rootNode = document.getElementById('app')

const randomInRange = (min, max) => {
  return Math.floor(Math.random() * (max - min)) + min
}

const FIELD = 65
const BLUR = 25
const WIDTH = 5
const COLOR = '#a537fd'

const generateSegments = (startX, startY) => {
  const result = []
  let refX = startX
  let refY = startY
  const getDistance = (x, y) => {
    const xLength = innerWidth / 2 - x
    const yLength = innerHeight / 2 - y
    const distance = Math.sqrt(xLength * xLength + yLength * yLength)
    return distance
  }
  const addSegment = (refX, refY) => {
    let angleToCenter =
      Math.atan2(innerHeight / 2 - refY, innerWidth / 2 - refX) *
      (180 / Math.PI)
    const lowerAngle = angleToCenter - FIELD
    const upperAngle = angleToCenter + FIELD
    const angle = randomInRange(lowerAngle, upperAngle) * (Math.PI / 180)
    const distance = getDistance(refX, refY)
    const radius = randomInRange(10, Math.min(60, distance))
    const x = refX + radius * Math.cos(angle)
    const y = refY + radius * Math.sin(angle)
    refX = x
    refY = y
    result.push([x, y])
    if (getDistance(x, y) > 100) {
      addSegment(x, y)
    }
  }
  addSegment(refX, refY)
  return result
}

const App = () => {
  const [active, setActive] = useState(false)
  const x = useRef(0)
  const y = useRef(0)
  const canvas = useRef(null)
  const context = useRef(null)
  const draw = useRef(null)

  const drawLine = (x, y) => {
    context.current.beginPath()
    context.current.moveTo(x, y)
    const segments = generateSegments(x, y, 10)
    for (const segment of segments)
      context.current.lineTo(segment[0], segment[1])
    context.current.lineTo(innerWidth / 2, innerHeight / 2)
    context.current.stroke()
  }

  const startDraw = () => {
    context.current.clearRect(0, 0, innerWidth, innerHeight)
    drawLine(x.current, y.current)
    draw.current = requestAnimationFrame(startDraw)
  }

  const setCanvasProperties = () => {
    canvas.current.width = canvas.current.style.width = innerWidth
    canvas.current.height = canvas.current.style.height = innerHeight
    context.current = canvas.current.getContext('2d')
    context.current.lineWidth = WIDTH
    context.current.lineCap = 'round'
    context.current.lineJoin = 'round'
    context.current.strokeStyle = COLOR
    context.current.shadowColor = COLOR
    context.current.shadowBlur = BLUR
  }
  const updateXY = e => {
    x.current = e.touches && e.touches.length ? e.touches[0].pageX : e.pageX
    y.current = e.touches && e.touches.length ? e.touches[0].pageY : e.pageY
  }
  const updateAndStart = e => {
    activate()
    updateXY(e)
  }
  const activate = () => setActive(true)
  const deactivate = () => setActive(false)

  useEffect(
    () => {
      if (active) {
        draw.current = requestAnimationFrame(startDraw)
      } else {
        if (context.current)
          context.current.clearRect(0, 0, innerWidth, innerHeight)
        cancelAnimationFrame(draw.current)
        draw.current = null
      }
    },
    [active]
  )

  useEffect(() => {
    setCanvasProperties()
    document.body.addEventListener('mousedown', updateAndStart)
    document.body.addEventListener('mousemove', updateXY)
    document.body.addEventListener('mouseup', deactivate)
    document.body.addEventListener('touchstart', updateAndStart)
    document.body.addEventListener('touchmove', updateXY)
    document.body.addEventListener('touchend', deactivate)
    window.addEventListener('resize', setCanvasProperties)

    return () => {
      document.body.removeEventListener('mousedown', updateAndStart)
      document.body.removeEventListener('mousemove', updateXY)
      document.body.removeEventListener('mouseup', deactivate)
      document.body.removeEventListener('touchstart', updateAndStart)
      document.body.removeEventListener('touchmove', updateXY)
      document.body.removeEventListener('touchend', deactivate)
      window.removeEventListener('resize', setCanvasProperties)
    }
  }, [])

  return (
    <Fragment>
      <canvas ref={canvas} />
      <div
        className={`tesla-coil__head ${active && 'tesla-coil__head--active'}`}
      />
      <div className="tesla-coil__body">
        <div className="tesla-coil__ring" />
        <div className="tesla-coil__ring" />
        <div className="tesla-coil__ring" />
        <div className="tesla-coil__base">
          <div />
          <div />
          <div />
        </div>
      </div>
    </Fragment>
  )
}

render(<App />, rootNode)

            
          
!
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.

Console