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

              
                
<div id="container">
    <div id="box"></div>
</div>

<div id="range-value">0</div>
<input id="range" type="range" min="0" max="0.6" step="0.01" value="0.2"/>

<div id="debug"></div>

              
            
!

CSS

              
                * {
    box-sizing: border-box;
}

#container {
    position: relative;
    width: 400px;
    height: 400px;
    background: #CCCCCC;
}

#box {
    position: absolute;
    left: 0;
    top: 0;
    width: 20px;
    height: 20px;
    background: #CC0000;
}


#range-value {
    width: 400px;
    background: #EEEEEE;
    border: 1px solid #CCCCCC;
    text-align: center;
}

#range {
    width: 400px;
}

#debug {
    background: #EEEEEE;
    border: 1px solid #CCCCCC;
    min-height: 25px;
}
              
            
!

JS

              
                let log = function (arg) { console.log(arg); }

let container;
let box;
let debug;
let innertia;

function CursorFollow() {
    log("CursorFollow()");

    let container = document.getElementById("container");
    let box = document.getElementById("box");
    let slider = document.getElementById("range");
    let sliderValue = document.getElementById("range-value");
    let debug = document.getElementById("debug");

    let containerX = 0;
    let containerY = 0;
    let limX = 0;
    let limY = 0;

    let inertia = 0.3;

    let cx = 0; // current
    let cy = 0;
    let tx = 0; // target
    let ty = 0;
    let x = 0; // working value
    let y = 0;

    let firstRun = true;

    function init() {

        inertia = parseFloat(slider.value);

        containerX = window.scrollX + container.getBoundingClientRect().left;
        containerY = window.scrollY + container.getBoundingClientRect().top;
        limX = container.getBoundingClientRect().width - box.getBoundingClientRect().width;
        limY = container.getBoundingClientRect().height - box.getBoundingClientRect().height;

        cx = parseFloat(getComputedStyle(box).left);
        cy = parseFloat(getComputedStyle(box).left);

        slider.addEventListener("change", sliderChangeHandler);
        container.addEventListener("mousemove", mouseMoveHandler);

        update();
    }
    // - - - - - -


    function sliderChangeHandler(e){
        inertia = parseFloat(slider.value);
    }
    // - - - - - -


    function mouseMoveHandler(e) {
        // log(e);
        // problem is offsetX&Y goes to zero when mouse doesnt move!
        tx = e.clientX - containerX;
        ty = e.clientY - containerY;

        if (tx > limX){
            tx = limX;
        }
        if (ty > limY){
            ty = limY;
        }

    }
    // - - - - - -


    function update() {

        // So we dont have to getComputerStlye in update loop
        if (firstRun){
            firstRun = false;
        } else {
            cx = x;
            cy = y;
        }


        // debug.innerHTML = cx;
        x = inertiaTo(cx,tx,inertia);
        y = inertiaTo(cy,ty,inertia);

        box.style.left = x + "px";
        box.style.top = y + "px";

        debug.innerHTML = x + ":" + y;

        sliderValue.innerHTML = "inertia: " + inertia;

        requestAnimationFrame(update);
    }
    // - - - - - -


    function inertiaTo(current,target,amount){
        if (amount==1){
            return target;
        }
        let distToGo = target-current;
        let delta = current + (distToGo * amount);

        if (Math.abs(distToGo) < 0.01){
            distToGo = 0;
            delta = target;
        }

        // debug.innerHTML = distToGo;
        return delta;
    }
    // - - - - - -


    // Constructor simulation
    init();
    // - - - - - -

}





new CursorFollow();
              
            
!
999px

Console