css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <html>
    <head></head>
    <body>
        <canvas></canvas>
    </body>
</html>
            
          
!
            
              html, body { width: 100%; height: 100%; margin: 0; }
canvas { position: absolute; width: 100%; height: 100%; }
            
          
!
            
              // You can change and play with the parameters below from line 46

(function(){
    var deviceRatio = window.devicePixelRatio || 1;

    // detect mouse move
    var mouseX = null;
    var mouseY = null;
    var mousedown = false;

    function saveMouseCoords(event) {
        mouseX = event.clientX * deviceRatio;
        mouseY = event.clientY * deviceRatio;
    }

    window.addEventListener('mousedown', function(event) {
        mousedown = true;
        saveMouseCoords(event);

        window.addEventListener('mousemove', saveMouseCoords);
    });
    window.addEventListener('mouseup', function(event) {
        mousedown = false;

        window.removeEventListener('mousemove', saveMouseCoords);
    });

    // get canvas
    var canvas = document.querySelector("canvas");
    ctx = canvas.getContext('2d');

    var width = Math.floor(window.innerWidth * deviceRatio);
    var height = Math.floor(window.innerHeight * deviceRatio);

    canvas.width = width;
    canvas.height = height;

    ctx.fillStyle = 'rgba(0, 0, 0, 1)';
    ctx.fillRect(0, 0, canvas.width, canvas.height);

    var center = {
        x: canvas.width / 2,
        y: canvas.height / 2
    };

    var gravity = 0.0002;
    var mouseRepel = 0.3;
    var mouseDistanceFeel = 500;
    var bouncabilityMin = 0.4;
    var bouncabilityMax = 0.75;
    var bouncabilitySecondAxis = 0.85;
    var pointsNumber = 800;

    var points = [];
    var i;
    for (i = 0; i < pointsNumber; i++) {
        points[i] = [
            Math.random() * width | 0,  // x
            Math.random() * height | 0, // y
            Math.random() * 0.7 - 0.35, // x velocity
            Math.random() * 0.7 - 0.35  // y velocity
        ];
    }

    var prevTime = Date.now();
    var nowTime = prevTime;
    var timeDiff,
        gravityVAdjustment;
    var xMouseDistance, yMouseDistance, xMouseRatio, yMouseRatio;
    var abs = Math.abs;
    var bouncabilityThisFrame;

    function setBouncability() {
        if(bouncabilityThisFrame === null) {
            bouncabilityThisFrame = bouncabilityMin +
                Math.random() * (bouncabilityMax - bouncabilityMin)
        }
    }

    function revertVelocity(axis) {
        // velocity vector change
        points[i][axis + 2] *= -1 * bouncabilityThisFrame;
        if(axis === 0) {
            points[i][3] *= bouncabilitySecondAxis;
        } else {
            points[i][2] *= bouncabilitySecondAxis;
        }
    }

    function frame() {
        prevTime = nowTime;
        nowTime = Date.now();

        timeDiff = nowTime - prevTime;

        // clear
        ctx.fillStyle = 'rgba(0, 0, 0, 0.16)';
        // ctx.fillStyle = 'rgb(0, 0, 0)';
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        ctx.fillStyle = 'rgb(30, 70, 255)';

        gravityVAdjustment = timeDiff * gravity;

        bouncabilityThisFrame = null;
        for (i = 0; i < pointsNumber; i++) {
            // Adjust y velocity with gravity
            points[i][3] = points[i][3] + gravityVAdjustment;

            // Adjust y velocity with mouse
            if(mousedown) {
                xMouseDistance = points[i][0] - mouseX;
                xMouseRatio = (1 - abs(xMouseDistance) / mouseDistanceFeel);

                if(xMouseRatio > 0 && xMouseRatio <= 1) {
                    yMouseDistance = points[i][1] - mouseY;
                    yMouseRatio = (1 - abs(yMouseDistance) / mouseDistanceFeel);

                    if(yMouseRatio > 0 && yMouseRatio <= 1) {

                        if(xMouseDistance > 0 && xMouseDistance < mouseDistanceFeel) {
                            points[i][2] += mouseRepel * xMouseRatio * yMouseRatio * yMouseRatio;
                        } else if(xMouseDistance < 0 && xMouseDistance > -mouseDistanceFeel ) {
                            points[i][2] -= mouseRepel * xMouseRatio * yMouseRatio * yMouseRatio;
                        }

                        if(yMouseDistance > 0 && yMouseDistance < mouseDistanceFeel) {
                            points[i][3] += mouseRepel * yMouseRatio * xMouseRatio * xMouseRatio;
                        } else if(yMouseDistance < 0 && yMouseDistance > -mouseDistanceFeel ) {
                            points[i][3] -= mouseRepel * yMouseRatio * xMouseRatio * xMouseRatio;
                        }
                    }
                }

            }

            points[i][0] = (points[i][0] + timeDiff * points[i][2]);
            points[i][1] = (points[i][1] + timeDiff * points[i][3]);

            // Check for collision and bounce
            if( points[i][0] < 0 ) {
                setBouncability();
                // coordinates
                points[i][0] *= -1 * bouncabilityThisFrame;
                revertVelocity(0);
            } else if( points[i][0] > width ) {
                setBouncability();
                // coordinates
                points[i][0] = width - ((points[i][0] - width) * bouncabilityThisFrame);
                revertVelocity(0);
            }

            if( points[i][1] < 0 ) {
                setBouncability();
                // coordinates
                points[i][1] *= -1 * bouncabilityThisFrame;
                revertVelocity(1);
            } else if( points[i][1] > height ) {
                setBouncability();
                // coordinates
                points[i][1] = height - ((points[i][1] - height) * bouncabilityThisFrame);
                revertVelocity(1);
            }

            ctx.fillRect(points[i][0]-1, points[i][1]-1, 2, 2);
        }

        window.requestAnimationFrame( frame );
    }
    frame();

})();
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console