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

              
                
              
            
!

CSS

              
                body {
  width: 100vw;
  height: 100vh;
  font-family: 'Montserrat', sans-serif;
  background-image: radial-gradient(circle, #edc9db, #f391c3);
}

.app {
  width: 100vw;
  height: 100vh;
  display: grid;
  align-items: center;
  justify-content: center;
}

.heart {
  perspective: 1000px;
  width: 100px;
  height: 100px;
  border: none;
  background: none;
  cursor: pointer;
  outline: none;
  
  // Shadow
  &::after {
    content: '';
    position: absolute;
    border-radius: 100%;
    left: 10%;
    bottom: -20%;
    width: 80%;
    height: 20%;
    background-color: hsl(334deg 40% 50% / 40%);
    filter: blur(5px);
    transform: scale(var(--scale));
  }
  
  svg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: visible;
  }
  
  .inner-wrapper {
    width: 100%;
    height: 100%;
    transform: scale(var(--scale));
    transition: 0.1s ease-out;
  }
  
  .splash {
    width: 100%;
    height: 100%;
    
    path {
      stroke: hsl(334deg 60% 80%);
      stroke-width: 0;
      fill: none;
      transform-origin: center;
      animation-timing-function: linear;
      animation-duration: 0.5s;
      animation-name: splash;
    }
  }
  
  .heart-stroke {
    fill: none;
    stroke: rgba(white, 1);
    stroke-width: 20px;
    stroke-dasharray: 300 1206;
    stroke-linecap: round;
  }
  
  .heart-layer {
    transition: fill 0.3s ease-out;
    fill: hsl(334deg var(--lightness) 50%);
    stroke: hsl(334deg var(--lightness) 35%);
    stroke-width: 10px;
  }
  
  .heart-shine {
     filter: blur(3px);
  }
}

@keyframes splash {
  0% {
    transform: scale(0.5);
    stroke-width: 10;
  }
  
  50% {
    transform: scale(1.5);
    stroke-width: 100;
  }
  
  100% {
    transform: scale(2.5);
    stroke-width: 1;
  }
}
              
            
!

JS

              
                import React, {useEffect, useState, useRef, memo} from 'https://cdn.skypack.dev/react';
import ReactDOM from 'https://cdn.skypack.dev/react-dom';
import { nanoid } from 'https://cdn.skypack.dev/nanoid@4.0.0';

const PATH_LENGTH = 1506;
const LAYERS = 20;
const LAYER_GAP = 2; // In pixels

const clamp = (min, max, n) => (
  Math.min(max, Math.max(min, n))
);

const useDocumentEvent = (event, handler) => {
  useEffect(() => {
    const events = event.split(' ');
    events.forEach(event => document.addEventListener(event, handler));
    return () => events.forEach(event => document.removeEventListener(event, handler));
  }, []);
};

// Read more about this hook here: https://yoavik.com/snippets/use-perishable
const usePerishable = () => {
    const [items, setItems] = useState([]);
    const timeouts = useRef({});

    useEffect(() => {
        // Cleanup pending timeouts when the component unmounts
        return () => Object.values(timeouts.current).forEach(clearTimeout);
    }, []);

    return {items, add: (delay, data) => {
        // Generate a unique ID
        const id = nanoid();
        // Create a new item with the given id & data
        setItems(arr => [...arr, { id, data }]);
        // Create a timeout to remove the item after the given delay
        timeouts.current[id] = setTimeout(() => {
            setItems(ids => ids.filter(item => item.id !== id));
            delete timeouts.current[id];
        }, delay);
    }};
};

const useCursorTilt = ({ref, tilt, bounds}) => {
  const [rotate, setRotate] = useState([-25,25]);
  useDocumentEvent('mousemove', e => {
    requestAnimationFrame(() => {
      const {left, top, width, height} = ref.current.getBoundingClientRect();
      const [x, y] = [e.clientX, e.clientY];
      const rect = bounds 
      ? {top: top - bounds, left: left - bounds, width: width + bounds * 2, height: height + bounds * 2}
      : {top: 0, left: 0, width: window.innerWidth, height: window.innerHeight};

      setRotate([
        (-(clamp(0, rect.height, y - rect.top) - (rect.height/2)) / rect.height) * tilt,
        ((clamp(0, rect.width, x - rect.left) - (rect.width/2)) / rect.width) * tilt,
      ]);
    });
  })
  return rotate;
};

const Splash = memo(({circles}) => {
  return (
    <svg viewBox='0 0 500 430' className='splash'>
      {circles.map(({id}) => (
        <HeartPath key={id}/>
      ))}
    </svg>
  );
});

const HeartPath = () => (
  <path d="M500 143.64C500 286.45 321.49 322.15 250.08 429.26C178.68 322.15 0.17 286.45 0.17 143.64C0.17 72.24 53.72 0.83 142.98 0.83C214.38 0.83 250.08 72.24 250.08 72.24C250.08 72.24 285.79 0.83 357.19 0.83C446.45 0.83 500 72.24 500 143.64Z"/>
);

const ShineSVG = ({x, opacity, rotate, translateZ}) => (
  <svg viewBox="0 0 500 430" style={{transform: `rotateX(${rotate.x}deg) rotateY(${rotate.y}deg) translateZ(${translateZ}px) scale(0.9)`}} className='heart-shine'>
    <defs>
      <clipPath id="heart">
        <HeartPath/>
      </clipPath>
    </defs>
    <rect x={500 - x * (700)} y='0' width='200' height='430' fill={`rgba(255,255,255,${opacity}`} clipPath='url(#heart)'/>
  </svg>
);

const HeartSVG = ({rotate: {x, y}, translateZ, strokeDashoffset, scale = 1, className}) => (
  <svg className={className} viewBox="0 0 500 430" style={{transform: `rotateX(${x}deg) rotateY(${y}deg) translateZ(${translateZ}px) scale(${scale})`, strokeDashoffset}}>
    <HeartPath/>
  </svg>
);

const Heart = () => {
  const ref = useRef();
  const [love, setLove] = useState(1);
  const [pressed, setPressed] = useState(false);
  const { items, add } = usePerishable();
  const [x,y] = useCursorTilt({ref, tilt: 50, bounds: 50});
  const offset = (Math.atan2(y, x) / Math.PI) * (PATH_LENGTH / 2) + PATH_LENGTH / 2;
  useDocumentEvent('mouseup', () => {
    setPressed(pressed => {
      if (pressed) {
        setLove(a => {
          if (a >= 1) {
            return 0;
          }
          add(1000);
          return a + 0.3;
        });
      }
      return false;
    });
  });
  
  return (
    <button className='heart' onMouseDown={() => setPressed(true)} ref={ref} style={{'--lightness': `${love * 80 + 20}%`, '--scale': 0.8 + love * 0.2 - pressed * 0.1}}>
      <div className='inner-wrapper'>
        <Splash circles={items}/>
        {[... new Array(LAYERS)].map((_, i) => (
          <HeartSVG className='heart-layer' rotate={{x, y}} translateZ={i * LAYER_GAP} scale={Math.sin((i / LAYERS)*Math.PI)/10 + 1}/>
        ))}
        <HeartSVG className='heart-stroke' rotate={{x, y}} translateZ={(LAYERS + 1) * LAYER_GAP} strokeDashoffset={offset} scale={0.9}/>
        <ShineSVG x={y / 50 + 0.5} opacity={x / 200 + 0.5} rotate={{x, y}} translateZ={(LAYERS + 1) * LAYER_GAP}/>
        
      </div>
    </button>
  );
};

const App = () => (
  <div className='app'>
    <Heart/>
  </div>
);

ReactDOM.render(
  <App/>,
  document.body
);
              
            
!
999px

Console