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

body
  margin 0
  min-height 100vh
  padding 0
  background #111
  overflow hidden

canvas
  position absolute
  top 0
  left 0
  right 0
  bottom 0

.tesla-coil
  height 30px
  width 80px
  background linear-gradient(0deg, #333 20%, silver, #333 80%)
  border-radius 15px
  position absolute
  top 50%
  left 50%
  z-index 2

  &:after
  &:before
    content ''
    position absolute
    height 50vh
    top 100%
    left 50%
    transform translate(-50%, 0)
    z-index -1

  &:before
    background linear-gradient(90deg, silver 50%, #333)
    width 10px

  &:after
    $gap = 30px
    $spread = 2px
    background repeating-linear-gradient(1deg, transparent ($gap - $spread), #444 $gap, transparent ($gap + $spread)), linear-gradient(90deg, #222 25%, silver 65%, #222)
    width 40px
    top 150%

  &:nth-of-type(1)
    transform translate(calc((var(--gap) / 2) * -1px), -50%) translate(-75%, 0)
  &:nth-of-type(2)
    transform translate(calc((var(--gap) / 2) * 1px), -50%) translate(-25%, 0)
            
          
!
            
              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 = '#19b5fe'
// const COLOR = '#f62459'
const getGap = () => Math.max(innerWidth * .25, 150)

const generateSegments = (startX, startY, endX, endY) => {
  const result = []
  let refX = startX
  let refY = startY
  const getDistance = (x, y) => {
    const xLength = endX - x
    const yLength = endY - y
    const distance = Math.sqrt(xLength * xLength + yLength * yLength)
    return distance
  }
  const addSegment = (refX, refY) => {
    let angleToCenter =
      Math.atan2(endY - refY, endX - 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) > 30) {
      addSegment(x, y)
    }
  }
  addSegment(refX, refY)
  return result
}

const App = () => {
  const x1 = useRef(null)
  const x2 = useRef(null)
  const y1 = useRef(null)
  const y2 = useRef(null)
  const gap = useRef(null)
  const canvas = useRef(null)
  const context = useRef(null)
  const draw = useRef(null)

  const drawLine = () => {
    context.current.beginPath()
    context.current.moveTo(x1.current, y1.current)
    const segments = generateSegments(x1.current, y1.current, x2.current, y2.current)
    for (const segment of segments)
      context.current.lineTo(segment[0], segment[1])
    context.current.lineTo(x2.current, y2.current)
    context.current.stroke()
  }

  const startDraw = () => {
    context.current.clearRect(0, 0, innerWidth, innerHeight)
    drawLine()
    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
    gap.current = getGap()
    document.documentElement.style.setProperty('--gap', gap.current)
    x1.current = (innerWidth / 2) - (gap.current / 2)
    x2.current = (innerWidth / 2) + (gap.current / 2)
    y1.current = y2.current = innerHeight / 2
  }

  useEffect(() => {
    setCanvasProperties()
    draw.current = requestAnimationFrame(startDraw)
    window.addEventListener('resize', setCanvasProperties)

    return () => {
      window.removeEventListener('resize', setCanvasProperties)
      cancelAnimationFrame(draw.current)
    }
  }, [])

  return (
    <Fragment>
      <canvas ref={canvas} />
      <div className="tesla-coil"/>
      <div className="tesla-coil"/>
    </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