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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <img id="img" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/106114/fiat-500.jpg" />


<div id="info">Move your mouse around...</div>
              
            
!

CSS

              
                body {
  overflow: hidden;
}

#info {
  position: fixed;
  padding: 10px;
  left: 0;
  top: 0;
  color: #333;
  user-select: none;
  font-weight: bold;
  text-shadow:
    -1px -1px 0 rgba(255,255,255,0.5),
    1px -1px 0 rgba(255,255,255,0.5),
    -1px 1px 0 rgba(255,255,255,0.5),
    1px 1px 0 rgba(255,255,255,0.5);
}
              
            
!

JS

              
                // Acceleration value between 0 and 1
// Smaller values result in a slower/smoother looking animation
let acceleration = 0.05;

let img = {
  element: document.querySelector("#img"),
  maxX: 0,
  maxY: 0,
  x: 0,
  y: 0
};

let mouse = { x: 0, y: 0 };

let vw = 0;
let vh = 0;

let setX = gsap.quickSetter(img.element, "x", "px");
let setY = gsap.quickSetter(img.element, "y", "px");
let mapX, mapY;

function init() {
  
  // Sets viewport dimensions and img x/y limits
  resize();  
  window.addEventListener("mousemove", onMove);
  window.addEventListener("resize", resize);  
  gsap.ticker.add(update);
}

function update() {
    
  // adjust speed for higher refresh monitors
  const dt = 1.0 - Math.pow(1.0 - acceleration, gsap.ticker.deltaRatio()); 
  
  let x = mapX(mouse.x);
  let y = mapY(mouse.y);
  
  img.x += (x - img.x) * dt;
  img.y += (y - img.y) * dt;
  setX(img.x);
  setY(img.y);
}

function resize() {
  
  vw = window.innerWidth;
  vh = window.innerHeight;
  
  img.maxX = vw - img.element.naturalWidth;
  img.maxY = vh - img.element.naturalHeight;
  
  mapX = gsap.utils.mapRange(0, vw, 0, img.maxX);
  mapY = gsap.utils.mapRange(0, vh, 0, img.maxY);
}

function onMove(event) {
  mouse.x = event.clientX;
  mouse.y = event.clientY;
}

// Initialize app once image has loaded
window.addEventListener("load", init);









              
            
!
999px

Console