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.

            
              <!DOCTYPE html>
<html lang="en">

<head>
    <title>three.js webgl - geometry - vertex colors</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" />
    <link href="https://fonts.googleapis.com/css?family=Press+Start+2P" rel="stylesheet">
    <link type="text/css" rel="stylesheet" href="main.css" />
</head>

<body>
    <div id="canvas"></div>

    <div id="landingScreen" class="landing-screen">
        <section class="landing-screen__message">
            <h1 class="landing-screen__message__header">
                Github Contribution Visualizer
            </h1>

            <h2 class="landing-screen__message__sub-header">
                Cubes are generated for each contribution the specified
                Github user made on a given day. The more commits they have
                made that day, the larger (and darker) the cube will be.
            </h2>

            <input id="usernameInput" type="text" class="landing-screen__message__input"
                placeholder="Enter the Github username that you would like to visualize" />

            <p id="error" class="landing-screen__message__error">Sorry, not enough github contributions.</p>

            <button class="landing-screen__submit" id="submit">
                Let's Go
                <div id="loader" class="loader">
                    <span></span>
                    <span></span>
                    <span></span>
                    <span></span>
                </div>
            </button>
        </section>
    </div>

    <figure id="contributionData" class="contribution-data">
        <p id="contributionText" class="contribution-data__text"></p>
    </figure>


</body>

</html>
            
          
!
            
              body,
html,
#canvas {
    background-color: #0f151d;
    background: radial-gradient(
        circle,
        rgba(40, 50, 74, 1) 0%,
        rgba(15, 21, 29, 1) 100%
    );
    cursor: move;
    font-family: arial, sans-serif;
    letter-spacing: 1px;
    margin: 0;
    height: 100%;
    width: 100%;
    overflow: hidden;
}

/* Commit message */
.contribution-data {
    color: #fff;
    margin: 0;
    opacity: 0;
    padding: 0px 20px;
    position: absolute;
    top: 0;
    right: 0;
    transition: opacity 200ms ease;
    z-index: 1;
}

.contribution-data__text {
    font-size: 24px;
}

/* Intro Screen Message */
.landing-screen {
    background-color: #0f151d;
    cursor: pointer;
    opacity: 1;
    position: absolute;
    left: 0;
    top: 0;
    transition: opacity 300ms ease;
    height: 100%;
    width: 100%;
    z-index: 2;
}

.landing-screen__message {
    border: 1px solid #fff;
    color: #fff;
    padding: 40px;
    position: absolute;
    min-width: 470px;
    max-width: 600px;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
}

.landing-screen__message__header {
    font-family: "Press Start 2P", cursive;
    font-size: 28px;
    line-height: 1.5;
    margin-bottom: 30px;
    margin-top: 0;
    text-align: center;
}

.landing-screen__message__sub-header {
    font-size: 22px;
    font-weight: normal;
    margin-bottom: 40px;
    line-height: 1.5;
}

.landing-screen__message__input {
    display: block;
    font-size: 16px;
    margin-bottom: 20px;
    padding: 20px;
    width: calc(100% - 40px);
}

.landing-screen__message__error {
    color: red;
    opacity: 0;
    margin-bottom: 20px;
    transition: opacity 200ms ease;
}

.landing-screen__submit,
.landing-screen__submit:focus {
    border: 2px solid #fff;
    background: #0f151d;
    color: #fff;
    cursor: pointer;
    display: block;
    font-family: "Press Start 2P", cursive;
    font-size: 18px;
    outline: transparent;
    margin: 0 auto;
    padding: 25px 30px;
    position: relative;
    text-transform: uppercase;
    transition: all 200ms ease;
}

.landing-screen__submit:hover {
    background: #fff;
    color: #0f151d;
}

/* Loading Spinner */
.loader {
    display: none;
    position: absolute;
    left: 50%;
    top: 50%;
    width: 50px;
    height: 50px;
    transform: translate(-50%, -50%);
}

.loader span {
    border: 6px solid #fff;
    border-radius: 50%;
    border-color: #fff transparent transparent transparent;
    box-sizing: border-box;
    display: block;
    position: absolute;
    width: 40px;
    height: 40px;
    margin: 6px;
    animation: spin 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
}

.loader span:nth-child(1) {
    animation-delay: -0.45s;
}

.loader span:nth-child(2) {
    animation-delay: -0.3s;
}

.loader span:nth-child(3) {
    animation-delay: -0.15s;
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

            
          
!
            
              (function () {
    var container,
        camera,
        canvas,
        controls,
        scene,
        mouse = new THREE.Vector2(),
        renderer,
        raycaster,
        INTERSECTED;

    function initRenderer() {
        container = document.getElementById("canvas");
        renderer = new THREE.WebGLRenderer({
            alpha: true
        });
        renderer.setClearColor(0xffffff, 0);
        renderer.setPixelRatio(window.devicePixelRatio);
        renderer.setSize(window.innerWidth, window.innerHeight);
        container.appendChild(renderer.domElement);
    }

    function initCamera() {
        camera = new THREE.PerspectiveCamera(
            20,
            window.innerWidth / window.innerHeight,
            1,
            20000
        );
        camera.position.z = 8000;
    }

    function initScene() {
        controls = new THREE.OrbitControls(camera, renderer.domElement);
        raycaster = new THREE.Raycaster();
        scene = new THREE.Scene();

        var ambientLight = new THREE.AmbientLight(0x1a1a1a);
        scene.add(ambientLight);

        var hemisphereLight = new THREE.HemisphereLight(0xc6c6c6, 0xa1a1a1, 1);
        scene.add(hemisphereLight);
    }

    function initCanvas() {
        canvas = document.createElement("canvas");
        canvas.width = 128;
        canvas.height = 128;
        var context = canvas.getContext("2d");
        context.fillRect(0, 0, canvas.width, canvas.height);
    }

    function addContributionCubes(contributions) {
        contributions
            .filter(contribution => contribution.count > 0)
            .forEach(contribution => {
                var sphereGeometry = new THREE.BoxGeometry(
                    (contribution.count + 3) * 10,
                    (contribution.count + 3) * 10,
                    (contribution.count + 3) * 10
                );
                var material = new THREE.MeshPhongMaterial({
                    color: new THREE.Color(contribution.color)
                });

                material.contributionData = {
                    date: contribution.date,
                    count: contribution.count
                };

                var mesh = new THREE.Mesh(sphereGeometry, material);

                mesh.position.set(
                    Math.floor(Math.random() * (1000 + 1000 + 1)) - 1000,
                    Math.floor(Math.random() * (1000 + 1000 + 1)) - 1000,
                    Math.floor(Math.random() * (1000 + 1000 + 1)) - 1000
                );
                scene.add(mesh);
            });
    }

    function initEventListeners() {
        window.addEventListener("resize", onWindowResize, false);
        window.addEventListener("mousemove", onMouseMove, false);
    }

    function onMouseMove() {
        event.preventDefault();
        mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
        mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
    }

    function onWindowResize() {
        windowHalfX = window.innerWidth / 2;
        windowHalfY = window.innerHeight / 2;
        camera.aspect = window.innerWidth / window.innerHeight;
        camera.updateProjectionMatrix();
        renderer.setSize(window.innerWidth, window.innerHeight);
    }

    function animate() {
        requestAnimationFrame(animate);
        handleIntersections();

        controls.update();
        camera.lookAt(scene.position);
        renderer.render(scene, camera);
    }

    function injectStyles(elementId, stylesString) {
        var el = document.getElementById(elementId);
        el.setAttribute("style", stylesString)
    }

    function fetchUserData(userName) {
        showLoader();

        fetch(`https://github-contributions-api.now.sh/v1/${userName}`)
            .then(response => response.json())
            .then(data => {
                console.log(data);
                if (data.contributions.length > 1) {
                    injectStyles("landingScreen", "opacity: 0; pointer-events: none;");
                    init(data.contributions);
                } else {
                    injectStyles("error", "opacity: 1;");
                    hideLoader();
                }
            })
            .catch(error => {
                console.log("ERROR FETCHING: ", error);
            });
    }

    function handleContributionHover(contributionData) {
        var contributionText = document.getElementById("contributionText");

        contributionText.innerText = `${contributionData.count} ${
            contributionData.count > 1 ? "commits" : "commit"
        } on ${contributionData.date}`;

        injectStyles("contributionData", "opacity: 1;")
        injectStyles("canvas", "cursor: pointer;")
    }

    function handleContributionUnHover() {
        injectStyles("contributionData", "opacity: 0;")
        injectStyles("canvas", "cursor: move;")
    }

    function showLoader() {
        injectStyles("submit", "background: transparent; color: transparent;")
        injectStyles("loader", "display: block;")
    }

    function hideLoader() {
        injectStyles("submit", "")
        injectStyles("loader", "")
    }

    function handleIntersections() {
        raycaster.setFromCamera(mouse, camera);

        var intersects = raycaster.intersectObjects(scene.children);

        if (intersects.length > 0) {
            if (INTERSECTED != intersects[0].object) {
                if (INTERSECTED)
                    INTERSECTED.material.emissive.setHex(
                        INTERSECTED.currentHex
                    );

                INTERSECTED = intersects[0].object;
                INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
                INTERSECTED.material.emissive.setHex(0xff0000);

                handleContributionHover(INTERSECTED.material.contributionData);
            }
        } else {
            if (INTERSECTED)
                INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex);

            handleContributionUnHover();

            INTERSECTED = null;
        }
    }

    function handleUserInput() {
        document
            .getElementById("submit")
            .addEventListener("click", function (event) {
                event.preventDefault();

                var inputField = document.getElementById("usernameInput");
                var inputValue = inputField.value;

                if (inputValue.length > 1) {
                    fetchUserData(inputValue.trim());
                } else {
                    inputField.focus();
                }
            });

        document.getElementById("usernameInput").addEventListener("keydown", function (event) {
            if (event.keyCode === 13) {
                event.preventDefault();

                var inputField = document.getElementById("usernameInput");
                var inputValue = inputField.value;

                if (inputValue.length > 1) {
                    fetchUserData(inputValue.trim());
                } else {
                    inputField.focus();
                }
            }
        })
    }

    function init(contributions) {
        initRenderer();
        initCamera();
        initScene();
        initCanvas();
        addContributionCubes(contributions);
        initEventListeners();
        animate();
    }

    handleUserInput();
})();
            
          
!
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.

Console