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

              
                
<svg id="oscar" width="150" height="150" viewBox="0 0 110 160" > 
  <g id="os" transform="translate(-475 -60)">
      <path id="oscar01" d="M578.405 216.419c-2.326.076-11.33.793-12.522-2.472-2.734-7.49-19.739-62.238 1.265-104.35 16.99 3.51 13.197-25.146-2.156-15.755 1.095-19.633-46.322-43.678-77.462-12.795-12.064 11.964-9.658 37.569 9.408 40.55 29.211 4.567 31.585-1.607 17.555 10.639-8.394 29.809 4.232 50.283 2.414 73.418l-26.564.736c-22.18 2.171-13.038 9.209 27.433 10.727" fill="green" stroke="#505050" stroke-width="3"/>
    <path id="oscar02" class="hidden" d="M578.405 216.419c-2.326.076-11.33.793-12.522-2.472-2.734-7.49-19.739-77.238 1.265-119.35 16.99 3.51 13.197-25.146-2.156-15.755 1.095-19.633-46.322-43.678-77.462-12.795-12.064 11.964-9.658 37.569 9.408 40.55 56.428-9.959 58.873 27.504 21.439 45.87 6.017 23.946.348 30.052-1.47 53.187l-26.564.736c-22.18 2.171-13.038 9.209 27.433 10.727" fill="green" stroke="#505050" stroke-width="3"/>
    
    <text class="hey" x="500" y="140">HEY!</text>
  </g>
  
  <text class="touch" x="20" y="75">from TOP</text>
  <text class="touch" x="20" y="75">from RIGHT</text>
  <text class="touch" x="20" y="75">from BOTTOM</text>
  <text class="touch" x="20" y="75">from LEFT</text>
  
  
  
</svg>



              
            
!

CSS

              
                
body{
  margin:0;
  background-color:grey;
}
#oscar{
  position:absolute;
  left:50%;
  top:50%;
  width:150px;
  height:160px;
  visibility:hidden;
  overflow:visible;
  cursor:pointer;
}
svg{
  overflow:visible;
  background-color: #9a9a9a;
}
.hidden{
  visibility:hidden;
}
text{
  font-size:24px;
  font-family:sans-serif;
  font-weight:600;
  text-anchor:end;
  fill:#5f5f5f;
  visibility:hidden;
}

.test{
  position:absolute;
  width:100px;
  height:100px;
  margin:50px;
  font-size:30px;
  font-family:sans-serif;
  color:#8c8c8c;
  text-align:center;
  background-color:green;
  cursor:pointer;
}
              
            
!

JS

              
                
// https://css-tricks.com/direction-aware-hover-effects/

const oscar = document.querySelector('#oscar');
gsap.set(oscar, {xPercent:-50, yPercent:-50, autoAlpha:1});

gsap.defaults({overwrite:true});

const touch = function(here){
  
  var actionX = [-475, -500, -475, -450]
  var actionY = [ -35,  -60,  -85,  -60]
  
  const tl = gsap.timeline({})
  .to('#oscar01', {morphSVG:'#oscar02', duration:0.4})
  .to('.hey', {autoAlpha:1, duration:0.1}, 0.1)
  .to('#os', {x:actionX[here], y:actionY[here], duration:0.5, ease: "none"},0)
  
  return tl;
}

oscar.addEventListener('mouseenter', (e) => {
  
  const where = getDirectionKey(e, oscar);
  touch(where);

});

oscar.addEventListener('mouseleave', () => {
  
  gsap.timeline({})
  .to('#oscar01', {morphSVG:'#oscar01', duration:0.1})
  .to('.hey', {autoAlpha:0, duration:0.1}, 0.1)
  .to('#os', {x:-475, y:-60, duration:0.1, ease: "none"},0)

});


const getDirectionKey = (ev, node) => {
  const { width, height, top, left } = node.getBoundingClientRect();
  const l = ev.pageX - (left + window.pageXOffset);
  const t = ev.pageY - (top + window.pageYOffset);
  const x = (l - (width/2) * (width > height ? (height/width) : 1));
  const y = (t - (height/2) * (height > width ? (width/height) : 1));
  return Math.round(Math.atan2(y, x) / 1.57079633 + 5) % 4;
  
}

              
            
!
999px

Console