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

              
                <button class="button">
  <span class="button__backdrop"></span>
  <span class="canvas__holder">
    <canvas class="button__canvas"></canvas>
  </span>
  <span class="button__foreground"></span>
  <span class="button__text">Get Updates</span>
</button>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Inter:wght@500&display=swap');
@import url("https://unpkg.com/open-props/open-props.min.css");

*,
*:after,
*:before {
	box-sizing: border-box;
	-webkit-tap-highlight-color: transparent;
}

html {
	font-size: 16px;
}

body {
	background: hsl(0 0% 4%);
	display: grid;
	place-items: center;
	min-height: 100vh;
	font-family: "Inter", sans-serif, system-ui;
}

.button {
	--smoke: hsl(0 0% 10% / 0.5);
	--backdrop: hsl(0 0% 50%);
	--spread: 0.25rem;
	--active: 1;
	position: relative;
	padding: 0;
	font-weight: 500;
	display: grid;
	grid-template: 1fr / 1fr;
	place-items: center;
	border: 0;
	background: transparent;
	cursor: pointer;
}

.button:is(:hover, :focus-visible) {
	--active: 1.02;
}

.button:active {
	--active: 0.98;
}

.button > * {
	grid-area: 1 / 1;
}

.button__foreground {
	box-shadow: 0 0 var(--spread) 0 var(--backdrop) inset;
	z-index: 3;
}

.button__foreground:after {
	content: "";
	position: absolute;
	inset: 0;
	background:
		radial-gradient(100% 120% at 50% 95%, var(--smoke) 30%, transparent);
	border-radius: calc(Infinity * 1px);
	box-shadow: 0 0 var(--spread) 0rem var(--backdrop) inset;
}

.button__text {
	font-size: 3.25rem;
	padding: 1.8rem 3.9rem;
	z-index: 3;
	white-space: nowrap;
	background: linear-gradient(90deg, hsl(0 0% 86%), hsl(0 0% 96%));
  opacity: 0.9;
	-webkit-background-clip: text;
	color: transparent;
}

.canvas__holder {
	position: relative;
	overflow: hidden;
	z-index: 1;
}

.button__canvas {
	position: absolute;
	width: 100%;
	height: 100%;
	inset: 0;
	z-index: 1;
}

.button__backdrop {
	background: radial-gradient(120% 150% at 50% 150%, hsl(0 0% 4% / 1) 30%, transparent), var(--backdrop);
}

.button > * {
	height: 100%;
	width: 100%;
	
}

.button > *:not(.button__text) {
	border-radius: calc(Infinity * 1px);
	scale: var(--active);
	transition: scale 0.2s var(--ease-elastic-3);
}


              
            
!

JS

              
                import gsap from 'https://cdn.skypack.dev/gsap@3.11.0'

const CONFIG = {
  hue: 10,
  blur: 14,
  active: false,
  winddown: 1.5,
  saturation: 95,
  lightness: 65,
  size: 50,
  sizeMultiplier: 0.5,
  scaleBump: 1.75,
}

const RATIO = window.devicePixelRatio || 1

const BUTTON = document.querySelector('.button')
const CANVAS = BUTTON.querySelector('.button__canvas')
CANVAS.height = CANVAS.offsetHeight * RATIO
CANVAS.width = CANVAS.offsetWidth * RATIO
const CONTEXT = CANVAS.getContext('2d')
CONTEXT.filter = `blur(${CONFIG.blur}px) brightness(1.5)`


let TRAILS = []
let activeTrail

let head = {
  active: 1,
}
const FPS = 24
gsap.ticker.fps(FPS)


/**
 * When drawing, you need to iterate over any active trails
 * and draw their nodes with the assigned hue
 * */
const DRAW = () => {
  CONTEXT.clearRect(0, 0, CANVAS.width, CANVAS.height)

  // Need to loop over all your trails and render as necessary
  for (let t = 0; t < TRAILS.length; t++) {
    const TRAIL = TRAILS[t]
    // if (TRAIL.nodes.length === 0) TRAILS.splice(t, 1)
    CONTEXT.fillStyle = `hsl(${TRAIL.hue} ${CONFIG.saturation}% ${CONFIG.lightness}%)`
    for (const NODE of TRAIL.nodes) {
      const { x, y, active } = NODE
      const RENDER_SIZE = active * CONFIG.size
      CONTEXT.beginPath()
      CONTEXT.arc(
        x,
        y,
        RENDER_SIZE * 0.5,
        0,
        Math.PI * 2
      )
      CONTEXT.fill()
    }
  }
  if (CONFIG.active) {
    const RENDER_SIZE = CONFIG.size * head.active
    CONTEXT.fillStyle = `hsl(${head.hue} ${CONFIG.saturation}% ${CONFIG.lightness}%)`
    CONTEXT.beginPath()
    CONTEXT.arc(
      head.x,
      head.y,
      RENDER_SIZE * 0.5,
      0,
      Math.PI * 2
    )
    CONTEXT.fill()
  } 
}

const ADD_NODE = ({ x, y }) => {
  const BOUNDS = BUTTON.getBoundingClientRect()

  const normalizedX = x - BOUNDS.x
  const normalizedY = y - BOUNDS.y

  const NEW_NODE = {
    id: crypto.randomUUID(),
    x: normalizedX,
    y: normalizedY,
    active: 1,
  }

  head.hue = activeTrail.hue
  head.x = NEW_NODE.x
  head.y = NEW_NODE.y

  activeTrail.nodes.push(NEW_NODE)
}

/**
 * Each trail is an Object with an id,
 * Array of nodes and a dedicated hue
 * */
let count = 0
const INITIATE_TRAIL = ({x, y }) => {
  CONFIG.size = CANVAS.height * CONFIG.sizeMultiplier
  const NEW_TRAIL = {
    id: crypto.randomUUID(),
    hue: count === 0 ? 10 : gsap.utils.random(0, 359),
    nodes: [],
  }
  TRAILS.push(NEW_TRAIL)
  activeTrail = TRAILS[TRAILS.length - 1]
  ADD_NODE({ x, y })
  count++
  CONFIG.active = true
}

// These are still running when you leave and the new activeTrail interferes with it
const REMOVE_NODE = (nodeId, trail) => {
  const node = activeTrail.nodes.filter(node => node.id === nodeId)[0]
  if (node) {
    gsap.to(node, {
      active: 0,
      duration: CONFIG.winddown,
      onComplete: () => {
        trail.nodes.splice(
          trail.nodes.findIndex((node) => node.id === nodeId),
          1
        )
      }
    })
  }
}

const UPDATE_TRAIL = ({ x, y }) => {
  if (activeTrail.nodes.length > 0) {
    REMOVE_NODE(activeTrail.nodes[activeTrail.nodes.length - 1].id, activeTrail)
  }
  ADD_NODE({ x, y })
}

const WIND_TRAIL = () => {
  CONFIG.active = false
  if (activeTrail && activeTrail.nodes.length > 0) {
    for (let n = 0; n < activeTrail.nodes.length; n++) {

      REMOVE_NODE(activeTrail.nodes[n].id, activeTrail)
    }
  }
}

const SCALE_UP = () => {
  gsap.to(head, { active: CONFIG.scaleBump, duration: 0.15 })
}
const SCALE_DOWN = () => {
  gsap.to(head, { active: 1, duration: 0.15 })
}

BUTTON.addEventListener('pointerenter', INITIATE_TRAIL)
BUTTON.addEventListener('pointermove', UPDATE_TRAIL)
BUTTON.addEventListener('pointerleave', WIND_TRAIL)
BUTTON.addEventListener('pointerdown', SCALE_UP)
BUTTON.addEventListener('pointerup', SCALE_DOWN)

gsap.ticker.add(DRAW)
              
            
!
999px

Console