Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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 id='app'></div>
              
            
!

CSS

              
                *,
*::after,
*::before {
  margin: 0;
  padding: 0;
  border: 0;
  box-sizing: inherit;
}

html {
  box-sizing: border-box;
  font-size: 62.5%;
}


              
            
!

JS

              
                function getRandomInt(min, max) {
  min = Math.ceil(min)
  max = Math.floor(max)
  return Math.floor(Math.random() * (max - min + 1)) + min
}

const drawShape = {
  circle: function (ctx, fill, dia, line, x, y) {
    ctx.beginPath()
    ctx.fillStyle = fill
    ctx.strokeStyle = fill
    ctx.lineWidth = 2
    ctx.arc(x, y, dia, 0, 2 * Math.PI)
    ctx.closePath()

    line !== true ? ctx.fill() : ctx.stroke()
  },

  triangle: function (ctx, fill, dia, line, x, y) {
    dia = dia * 2
    let height = dia * Math.cos(Math.PI / 6)

    ctx.beginPath()
    ctx.moveTo(x, y)
    ctx.lineTo(x + dia, y)
    ctx.lineTo(x + dia / 2, y + height)
    ctx.closePath()

    // the outline
    ctx.lineWidth = 1
    ctx.strokeStyle = fill
    ctx.stroke()

    // the fill color
    ctx.fillStyle = fill

    // either fill
    line !== true ? ctx.fill() : ctx.stroke()
  },

  semicircle: function (ctx, fill, dia, line, x, y) {
    ctx.beginPath()
    ctx.fillStyle = fill
    ctx.strokeStyle = fill
    ctx.lineWidth = 2
    ctx.arc(x, y, dia, 1.5 * Math.PI, 0.5 * Math.PI)
    ctx.closePath()

    line !== true ? ctx.fill() : ctx.stroke()
  },
  square: function (ctx, fill, dia, line, x, y) {
    ctx.beginPath()
    ctx.moveTo(x, y)
    ctx.lineTo(x + dia, y)
    ctx.lineTo(x + dia, y + dia)
    ctx.lineTo(x, y + dia)
    ctx.closePath()

    // the outline
    ctx.lineWidth = 1
    ctx.strokeStyle = fill
    ctx.stroke()

    // the fill color
    ctx.fillStyle = fill

    // either fill
    line !== true ? ctx.fill() : ctx.stroke()
  }
}

// Individual particle
class Point {
  constructor(x, y, canvas, dia) {
    this.canvas = canvas
    this.x = x || 0
    this.y = y || 0
    this.vx = 0
    this.vy = 0
    this.speed = Math.random() * 0.5 + 0.2
    this.angle = Math.random() * 360
    this.diaSet = dia || 2 + Math.random() * 10
    this.dia = this.diaSet
    this.age = 0
    let hue = Math.floor(Math.random() * 360)
    this.fill = 'hsl(' + hue + ', 95%, 70%)'
    this.line = Math.random() > 0.5 ? true : false
    this.shape = Object.keys(drawShape)[
      getRandomInt(0, Object.keys(drawShape).length - 1)
    ]
  }

  emit(life) {
    let s = this.speed * 2
    this.angle += Math.random() * 10 - 5
    this.x += s * Math.cos((this.angle * Math.PI) / 180)
    this.y += s * Math.sin((this.angle * Math.PI) / 180)
    this.age += 1 / life
    this.boundary()
  }

  boundary() {
    if (this.x < 0) {
      this.x = this.canvas.width
    }
    if (this.x > this.canvas.width) {
      this.x = 0
    }
    if (this.y < 0) {
      this.y = this.canvas.height
    }
    if (this.y > this.canvas.height) {
      this.y = 0
    }
  }

  field(life) {
    let s = this.speed
    this.angle += Math.random() * 10 - 5
    this.x += s * Math.cos((this.angle * Math.PI) / 180)
    this.y += s * Math.sin((this.angle * Math.PI) / 180)
    this.age += 1 / life
    this.boundary()
  }

  shrink(life) {
    this.dia = (1 - this.age) * this.diaSet

    //to cater for if we adjust rate of emission so cycles per particle becomes > life
    if (this.age > 1) {
      this.dia = 0
    }
  }

  draw() {
    let ctx = this.canvas.getContext('2d'),
      x = this.x,
      y = this.y,
      dia = this.dia

    drawShape[this.shape](ctx, this.fill, dia, this.line, x, y)
  }
}

class ParticleGroup {
  constructor(canvas) {
    this.canvas = canvas
  }

  setPosition(x, y) {
    this.x = x
    this.y = y
  }

  getPosition(x, y) {
    return {
      x: this.x,
      y: this.y
    }
  }

  spawn(x, y, amount, dia) {
    var arr = []
    dia = dia || false

    amount = amount || 1

    if (amount > 1) {
      for (let i = 0; i < amount; i++) {
        if (dia) {
          arr.push(new Point(x, y, this.canvas, dia))
        } else {
          arr.push(new Point(x, y, this.canvas))
        }
      }
    } else {
      arr = new Point(x, y, this.canvas, dia)
    }

    return arr
  }
}

// Particle Emitter
class Emitter extends ParticleGroup {
  constructor(x, y, life, mouse, dia, canvas) {
    super(canvas)

    if (mouse === undefined) {
      this.mouse = true
    } else {
      this.mouse = mouse
    }

    this.particles = []
    this.x = x || 0
    this.y = y || 0
    this.life = life || 20
    this.canvas = canvas
    this.dia = dia || false
  }

  animate() {
    let particles = this.particles
    if (this.mouse) {
      this.setPosition(window.originx, window.originy)
    }

    let mul = 1

    let throttler = 0.53 // towards 0.5 slows it further
    let rdm = Math.round(Math.random() * throttler)

    for (let i = 0; i < mul; i++) {
      !!rdm && particles.push(this.spawn(this.x, this.y, 1))
    }

    if (particles.length > this.life * mul) {
      for (let i = 0; i < mul; i++) {
        !!rdm && particles.shift()
      }
    }

    this.render(this.canvas)
  }

  render() {
    let particles = this.particles

    for (let i = 0; i < particles.length; i++) {
      const p = particles[i]
      p.draw()
      p.emit(this.life)
      p.shrink()
    }
  }
}

// Particle Field
class Field extends ParticleGroup {
  constructor(x, y, life, canvas) {
    super(canvas)
    this.particles = []
    this.canvas = canvas
    this.x = x || 0
    this.y = y || 0
    this.life = life

    for (let i = 0; i < this.life; i++) {
      let x = Math.random() * canvas.width,
        y = Math.random() * canvas.height

      this.particles.push(this.spawn(x, y, 1))
    }
  }

  animate() {
    this.render(this.canvas)
  }

  render(canvas) {
    let particles = this.particles

    for (let i = 0; i < particles.length; i++) {
      const p = particles[i]
      p.draw()
      p.field(this.life)
    }
  }
}

// get the mouse position relative to the canvas
function getMousePos(cvs, e) {
  const rect = cvs.getBoundingClientRect()
  return {
    x: e.clientX - rect.left,
    y: e.clientY - rect.top
  }
}

// Update canvas size to fill its container
function resizeCanvas(canvas) {
  //get canvas and parent container references
  //const canvas = document.querySelector('canvas')
  const parent = canvas.parentNode

  //get size of container
  const styles = getComputedStyle(parent)
  const parentWidth = parseInt(styles.getPropertyValue('width'), 10)
  const parentHeight = parseInt(styles.getPropertyValue('height'), 10)

  //rezize canvas in line with parent container
  canvas.width = parentWidth
  canvas.height = parentHeight

  //position the particle origin at centre of canvas
  window.originx = canvas.width / 2
  window.originy = canvas.height / 2
}

//init
function canvasInit(cvs) {
  resizeCanvas(cvs)

  window.addEventListener('resize', resizeCanvas, false)
  window.addEventListener('mousemove', function (e) {
    const mouse = getMousePos(cvs, e)
    window.mouse = mouse
    window.originx = mouse.x
    window.originy = mouse.y
  })

  var network = new Field(0, 0, 20, cvs)
  var emit = new Emitter(0, 0, 20, undefined, null, cvs)

  const callback = function () {
    network.animate()
    emit.animate()
  }

  return callback
}

const useCanvas = () => {
  const canvasRef = React.useRef(null)

  React.useEffect(() => {
    const canvas = canvasRef.current
    const ctx = canvas.getContext('2d')
    //let frameCount = 0
    let animationFrameId

    const callback = canvasInit(canvas)

    const render = () => {
      ctx.clearRect(0, 0, canvas.width, canvas.height)
      // frameCount++
      // draw(ctx, frameCount)
      callback()
      animationFrameId = window.requestAnimationFrame(
        render.bind(null, callback)
      )
    }
    render()

    return () => {
      window.cancelAnimationFrame(animationFrameId)
    }
  }, [])

  return canvasRef
}

const Canvas = (props) => {
  const { draw, ...rest } = props
  const canvasRef = useCanvas(draw)

  return <canvas ref={canvasRef} {...rest} />
}
 
 const theme = {
  colorBtn: "#f08a5d",
  //colorNav: "#b83b5e",
  btnSize: "200px"
};

const Nav = styled.div`
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-end;
  margin: 2vw 5vw 10vw 5vw;
  padding: 1rem;
  position: relative;
  background: #7F7FD5;  /* fallback for old browsers */
background: -webkit-linear-gradient(to right, #91EAE4, #86A8E7, #7F7FD5);  /* Chrome 10-25, Safari 5.1-6 */
background: linear-gradient(to right, #91EAE4, #86A8E7, #7F7FD5); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
`

const Navbox = styled.div`
  height: 100%;
  display: flex;
  flex-direction: row;
  justify-content: space-evenly;
  align-items: center;
  font-weight: 500;
  width: 70%;
  `

const LinkCanvas=({className, children})=> (
 <div className={className + " canvasContainer"}>
    <Canvas/>
    <div>
      {children}
    </div>
  </div>)

const Link = styled(LinkCanvas)`
  padding: 0.5rem 2rem;
  margin: 0 1rem;
  height:30vh;
  flex: 0 1 ${props => props.theme.btnSize};
  display:flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  white-space: nowrap;
  border-radius: 100px;
  z-index:3;
  transition: all 0.2s;
  position: relative;
  background: #c2e59c; /* fallback for old browsers */
  background: -webkit-linear-gradient(
    to right,
    #64b3f4,
    #c2e59c
  ); /* Chrome 10-25, Safari 5.1-6 */
  background: linear-gradient(
    to right,
    #64b3f4,
    #c2e59c
  ); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
  overflow: hidden;
  
  > div {
    font-size: 2.5rem;
    color: white;
    text-transform:uppercase;
  }
  
  > canvas {
    position: absolute;
    top: 0;
    left: 0;
    pointer-events: none;
  }

  &:hover {
    transform: translateY(-3px);
  }
`

    
 const App = () => (
<styled.ThemeProvider theme={theme}>  
  <Nav>
    <Navbox>
      <Link>Home</Link>
      <Link>Portfolio</Link>
      <Link>Contact</Link>
    </Navbox>
  </Nav>
</styled.ThemeProvider>
);
 
ReactDOM.render(<App />, document.getElementById('app'));
              
            
!
999px

Console