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

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

              
                #app
              
            
!

CSS

              
                *
  box-sizing border-box

body
  overflow hidden
  background #222

#app
  min-height 100vh
  display flex
  align-items center
  justify-content center
  flex-direction column


:root
  --size 25

.christmas-tree
  list-style-type none
  display flex
  flex-direction column
  padding 0
  margin 0
  place-content center
  place-items center

  &__star
    height calc(var(--size) * 1px)
    width calc(var(--size) * 1px)
    background 'hsl(58, 100%, %s)' % ( calc(var(--shade, 50%) * 1%))
    z-index 4
    transform rotate(calc(var(--rotation, 0) * 1deg))

  &__log
    height calc(var(--size) * 2px)
    width calc(var(--size) * 1px)
    background 'hsl(28, 100%, %s)' % ( calc(var(--shade, 25) * 1%))
    transform rotate(calc(var(--rotation, 0) * 1deg)) scaleX(calc(1 + (var(--scale) / 10)))
    position absolute

    &--base
      transform translate(0, -10%) scaleX(2.2)
      position static

  &__regenerate
    height 44px
    width 44px
    margin-top 1.5rem
    background 0
    border 0
    outline 0 transparent
    padding 0
    display flex
    align-items center
    justify-content center
    opacity .45
    transition opacity .1s, transform .1s


    &:active
      transform scale(0.85)

    &:disabled

      &:hover
        opacity .45
      svg
        opacity 0.1

    &:hover
      opacity 1

    svg
      fill #ffffff
      height 40px
      width 40px
      transition opacity .1s

  &__bauble
    height calc(var(--size) * 1px)
    width calc(var(--size) * 1px)
    transform translate(calc(var(--x) * 1%), 0) rotate(calc(var(--rotation, 0) * 1deg)) scale(0)
    background 'hsl(%s, 100%, 50%)' % ( calc(var(--hue, 0)) )
    position absolute
    z-index 3

  &__branch
    list-style-type none
    padding 0
    margin 0
    display flex
    flex-direction row
    place-content center

  &__leaf
    height calc(var(--size) * 1px)
    width calc(var(--size) * 1px)
    transform-origin center
    transform translate(-100vw, 0) scale(1.5) rotate(calc(var(--rotation, 0) * 1deg))
    background 'hsl(100, %s, %s)' % ( calc(var(--saturation, 100) * 1%) calc(var(--shade, 50) * 1%))
    z-index 2
              
            
!

JS

              
                const {
  React,
  ReactDOM: { render },
  gsap: { to, fromTo, timeline },
} = window

const { useEffect, useState, useRef, Fragment } = React

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

const AUDIO = {
  POP: new Audio('https://assets.codepen.io/605876/pop.mp3'),
  CLICK: new Audio('https://assets.codepen.io/605876/click.mp3'),
  SPARKLE: new Audio('https://assets.codepen.io/605876/sparkle.mp3'),
}

const TIERS = 11
const STAGGER = 0.015
const DURATION = 0.25
const ROOT_NODE = document.getElementById('app')
const ROTATIONS = new Array(TIERS - 1).fill().map(() => randomInRange(-30, 30))
const SHADES = new Array(TIERS).fill().map(() => randomInRange(20, 30))

const genBranches = () =>
  new Array(TIERS).fill().reduce((a, c, i) => {
    return [...a, a[i - 1] ? a[i - 1] + Math.floor(Math.random() * 2) : 1]
  }, [])

const App = () => {
  const [branches, setBranches] = useState(genBranches())
  const ranRef = useRef(0)
  const generatorRef = useRef(null)
  const regen = () => {
    if (generatorRef.current.disabled === false) {
    new timeline({
      onStart: () => {
        AUDIO.CLICK.play()
        generatorRef.current.disabled = true
      },
      onComplete: () => {
        ranRef.current++
        setBranches(genBranches())
      },
    })
      .add(
        to('.christmas-tree__star', DURATION, {
          rotation: 'random(720, 1440)',
          scale: 0,
        })
      )
      .add(
        to(
          [...document.querySelectorAll('.christmas-tree__bauble')].sort(
            () => 0.5 - Math.random()
          ),
          DURATION,
          {
            scale: 0,
            stagger: STAGGER,
          }
        )
      )
      .add(
        to(
          [...document.querySelectorAll('.christmas-tree__leaf')].sort(
            () => 0.5 - Math.random()
          ),
          DURATION,
          {
            x: '100vw',
            y: 'random(-1000, 1000)',
            stagger: STAGGER,
            rotation: 'random(720, 1440)',
          }
        )
      )
    }
  }
  useEffect(() => {
    new timeline({
      onStart: () => generatorRef.current.disabled = true,
      onComplete: () => (generatorRef.current.disabled = false),
    })
      .set('.christmas-tree__star', {
        rotation: 'random(-720, -1440)',
        scale: 0
      })
      .set([...document.querySelectorAll('.christmas-tree__bauble')].sort(
            () => 0.5 - Math.random()
          ),{
            scale: 0,
            rotation: 'random(0, 360)',
          })
      .add(
        fromTo(
          [...document.querySelectorAll('.christmas-tree__log')].reverse(),
          {
            y: ranRef.current > 0 ? 0 : '-100vh',
          },
          {
            stagger: STAGGER,
            y: 0,
            duration: DURATION,
          }
        )
      )
      .add(
        fromTo(
          [...document.querySelectorAll('.christmas-tree__leaf')].sort(
            () => 0.5 - Math.random()
          ),
          {
            x: '-100vw',
            y: 'random(-1000, 1000)',
            rotation: 'random(-720, -1080)',
          },
          {
            duration: DURATION,
            x: 0,
            y: 0,
            stagger: STAGGER,
          }
        )
      )
      .to
        (
          [...document.querySelectorAll('.christmas-tree__bauble')].sort(
            () => 0.5 - Math.random()
          ),
          {
            ease: 'elastic.out(1, 0.3)',
            stagger: {
              each: 0.05,
              onStart: () => {
                AUDIO.POP.play()
                console.info('playing')
              },
            },
            scale: 'random(0.75, 1.25)',
            duration: DURATION * 2,
          }
        )
      
      .to(
          '.christmas-tree__star',
          {
            duration: DURATION,
            onStart: () => AUDIO.SPARKLE.play(),
            rotation: 'random(0, 360)',
            scale: 'random(1.2, 2.5, 0.1)',
          }
      )
  }, [branches])
  const hueBase = randomInRange(0, 360)
  const hueRange = randomInRange(0, 39)
  const saturated = Math.random() > 0.5
  const BAUBLE_PROB = Math.min(0.65, Math.random())
  const PARTY_HUE = Math.random() > 0.5
  return (
    <Fragment>
      <ul className="christmas-tree">
        <li
          className="christmas-tree__star"
          style={{ '--shade': randomInRange(50, 90) }}
        />
        {branches.map((leaves, index) => (
          <ul
            key={`branch--${index}--${ranRef.current}`}
            className="christmas-tree__branch">
            <li
              className="christmas-tree__log"
              style={{
                '--shade': SHADES[index],
                '--scale': index + 1,
                '--rotation': ROTATIONS[index],
              }}
            />
            {new Array(leaves).fill().map((leaf, leafIndex) => (
              <Fragment key={`leaf--${index}--${leafIndex}`}>
                <li
                  className="christmas-tree__leaf"
                  style={{
                    '--shade': saturated
                      ? randomInRange(20, 65)
                      : randomInRange(60, 100),
                    '--saturation': saturated ? 80 : 0,
                    '--rotation': randomInRange(0, 360),
                  }}
                />
                {Math.random() > BAUBLE_PROB && (
                  <li
                    className="christmas-tree__bauble"
                    style={{
                      '--x': (leafIndex - leaves / 2) * 100,
                      '--hue': PARTY_HUE
                        ? randomInRange(0, 360)
                        : randomInRange(hueBase - hueRange, hueBase + hueRange),
                    }}
                  />
                )}
              </Fragment>
            ))}
          </ul>
        ))}
        <li
          className="christmas-tree__log christmas-tree__log--base"
          style={{ '--shade': SHADES[10] }}
        />
      </ul>
      <button
        className="christmas-tree__regenerate"
        ref={generatorRef}
        title="Regenerate tree"
        onClick={regen}>
        <svg viewBox="0 0 24 24">
          <path d="M17.65,6.35C16.2,4.9 14.21,4 12,4A8,8 0 0,0 4,12A8,8 0 0,0 12,20C15.73,20 18.84,17.45 19.73,14H17.65C16.83,16.33 14.61,18 12,18A6,6 0 0,1 6,12A6,6 0 0,1 12,6C13.66,6 15.14,6.69 16.22,7.78L13,11H20V4L17.65,6.35Z" />
        </svg>
      </button>
    </Fragment>
  )
}
render(<App />, ROOT_NODE)

              
            
!
999px

Console