cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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>
<head>
    <meta charset="UTF-8">
    <title>Arcade Cabinet</title>

    <link rel="stylesheet" href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/styles.css"/>
    <meta name="viewport" content="initial-scale=1,user-scalable=no,width=device-width,minimal-ui"/>
    <style>
        body { touch-action: none; }
    </style>
</head>

<body class="template">

<div class="cabinet-container" id="container">
    <div id="cabinet" class="cabinet">
        <div class="cabinet-shadow"></div>
        <div id="leftside" class="cabinet-side cabinet-side-left"></div>
        <div id="rightside" class="cabinet-side cabinet-side-right"></div>
        <div class="cabinet-banner"></div>
        <div class="cabinet-banner-bottom"></div>
        <div class="cabinet-screen"></div>
        <div class="cabinet-front-mid-top"></div>
        <div class="cabinet-front-mid"></div>
        <div class="cabinet-front-mid-bottom"></div>
        <div class="cabinet-front-bottom"></div>
        <div class="cabinet-top"></div>
        <div class="cabinet-back"></div>
        <div class="cabinet-back-top"></div>
        <div class="cabinet-bottom"></div>
        <div id="joystick1" class="joystick js1">
            <div class="handle"></div>
        </div>
        <div id="joystick2" class="joystick js2">
            <div class="handle"></div>
        </div>
    </div>
</div>

<div class="controls" data-label="Cabinets">
</div>


<a href="https://createjs.com/" target="_blank"><img id="logo" src="https://createjs.com/mediakit/createjs-badge-reverse.png"></a>

<img id="badge" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/Button_Drag_White.svg">
            
          
!
            
              html, body {
  margin:0px;
  padding:0px;
  background-color:rgba(0, 0, 0, 1.00);

  overflow:hidden;
}

#logo { 
	position: absolute;
  right:0px;
  bottom:0px;
}

#badge {
  width:60px;
  height:70px;
  position: absolute;
  left:10px;
  bottom:10px;
}

            
          
!
            
              var dragProps,
            rotationY = -30,
            rotationX = 0,
            scaleFactor = 1,
            rotationDirection = 0,
            sideImage, activeTween,
            cabinetId, oldCabinetId,
            cabinetQueue, animationQueue, changeScreenTimeout,
            cabinet = document.getElementById("cabinet"),
            joystick1 = document.querySelector("#joystick1 .handle"),
            joystick2 = document.querySelector("#joystick2 .handle");

    function init() {
        createjs.Ticker.timingMode = createjs.Ticker.RAF;
        createjs.Ticker.on("tick", idle);

        // Feature-detect preserve-3d
        var detect = document.createElement("div");
        detect.style.transformStyle = "preserve-3d";
        if (detect.style.transformStyle.length == 0) {
            console.log("Error");
            return;
        }

        var sharedQueue = new createjs.LoadQueue(false);
        sharedQueue.on("complete", handleSharedComplete);
        sharedQueue.loadManifest([
            "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/background.png",
            "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/joystick.png",
            "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/frontBottom.jpg"
        ]);

        var el = document.getElementById("container");
        var scale = window.innerHeight/700;
        el.style.transform = el.style.webkitTransform = "scale("+scale+","+scale+")";
    }

    function handleSharedComplete(event) {
        setBackground("handle", "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/joystick.png", true);
        setBackground("cabinet-front-bottom", "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/frontBottom.jpg", true);
        loadCabinet("pacman");

        document.addEventListener("mousedown", handleMouseDown, false);
        if ("ontouchstart" in window) {
            document.addEventListener("touchstart", handleMouseDown, false);
        } else if (window.navigator["pointerEnabled"]) {
            document.addEventListener("pointerdown", handleMouseDown, false);
        }
    }

    function loadCabinet(id) {
        console.log(id)
        if (id == null) {
            id = "pacman";
        }
        if (cabinetId == id) {
            return;
        }

        oldCabinetId = cabinetId;
        cabinetId = id;

        if (cabinetQueue != null) {
            cabinetQueue.cancel();
            animationQueue.cancel();
        }

        cabinetQueue = new createjs.LoadQueue(false);
        cabinetQueue.on("complete", handleLoad);
        cabinetQueue.on("progress", handleProgress);
        cabinetQueue.loadManifest([
            "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/screen.png",
            {id:"side-image", src:"https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/side1.jpg"},
            "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/banner.jpg",
            "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/buttons.jpg",
            "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/speakerPanel.jpg",
            "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/frontpanel.jpg"
        ], true);

        animationQueue = new createjs.LoadQueue(false);
        animationQueue.loadFile("https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/screen.gif", false);
        animationQueue.on("complete", handleScreenLoad);

        cabinetQueue.next = animationQueue;
    }

    function handleProgress(event) {
       // LabTemplate.loadProgress(event.progress);
    }

    function handleLoad(event) {
        setBackground("cabinet-screen", "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/screen.png");
        setBackground("cabinet-banner", "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/banner.jpg");
        setBackground("cabinet-front-mid-top", "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/buttons.jpg");
        setBackground("cabinet-banner-bottom", "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/speakerPanel.jpg");
        setBackground("cabinet-front-mid", "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/frontpanel.jpg");

        var side = document.getElementById("rightside");
        side.firstChild && side.removeChild(side.firstChild);
        side.appendChild(getSide());

        side = document.getElementById("leftside");
        side.firstChild && side.removeChild(side.firstChild);
        side.appendChild(getSide());

        changeScreenTimeout = setTimeout(changeScreen, 5000);

        setTimeout(function() {
            cabinet.classList.remove(oldCabinetId);
            cabinet.classList.add(cabinetId);
        }, 10);

    }

    function handleScreenLoad(event) {
        if (changeScreenTimeout == null) {
            changeScreen();
        }
    }

    function changeScreen() {
        if (!animationQueue.loaded) {
            changeScreenTimeout = null;
            return;
        }
        setBackground("cabinet-screen", "https://s3-us-west-2.amazonaws.com/s.cdpn.io/1524180/screen.gif");

        if (rotationDirection == 0 && dragProps == null) {
            createjs.Tween.get(window).to({rotationDirection:1}, 1000, createjs.Ease.quadIn);
        }
    }

    function setBackground(className, src, globalCabinet) {
        if (globalCabinet != true) { src = src; }
        var divs = document.getElementsByClassName(className);
        if (divs != null) {
            for (var i= 0, l=divs.length; i<l; i++) {
                //divs[i].style.display = "none";

               /* (function(div) {
                    setTimeout(function () {
                        div.style.display = "block";
                    }, 10);
                }(divs[i]));*/

                divs[i].style.backgroundImage = "url('" + src + "')";
            }
        }
    }

    function idle() {
        if (dragProps) { return; }
        rotationY += rotationDirection;
        updateTransform();
    }

    function getSide() {
        var sideCanvas = document.createElement("canvas");
        var ctx = sideCanvas.getContext("2d");
        sideCanvas.width = 200;
        sideCanvas.height = 500;

        ctx.scale(2,2);
        ctx.fillStyle = "#0FF";
        ctx.beginPath();
        ctx.moveTo(0, 250);
        ctx.lineTo(0, 50);
        ctx.lineTo(75, 0);
        ctx.lineTo(100, 5);
        ctx.lineTo(100, 35);
        ctx.lineTo(65, 60);
        ctx.lineTo(75, 125);
        ctx.lineTo(100, 130);
        ctx.lineTo(100, 145);
        ctx.lineTo(85, 150);
        ctx.lineTo(85, 250);
        ctx.closePath();
        ctx.fill();

        ctx.globalCompositeOperation = "source-in";
        var sideImage = cabinetQueue.getResult("side-image");
        ctx.drawImage(sideImage, 0, 0, 100, 250);

        return sideCanvas;
    }

    function handleMouseDown(evt) {
        dragProps = getDragProps(evt);

        window.addEventListener("mousemove", handleMouseMove, false);
        window.addEventListener("mouseup", handleMouseUp, false);
        if ("ontouchstart" in window) {
            window.addEventListener("touchmove", handleMouseMove, false);
            window.addEventListener("touchend", handleMouseUp, false);
        } else if (window.navigator["pointerEnabled"]) {
            window.addEventListener("pointermove", handleMouseMove, false);
            window.addEventListener("pointerup", handleMouseUp, false);
        }

        // Reset Tweens
        rotationDirection = rotationDirection > 0 ? 0.5 : -0.5;
        createjs.Tween.removeAllTweens();

        evt.preventDefault();
    }

    function handleMouseMove(evt) {

        var pos = getDragProps(evt);

        if (dragProps == null) { return; }
        var dx = pos.x - dragProps.x;
        var dy = pos.y - dragProps.y;

        dragProps = pos;

        rotationY += dx * 0.2;
        rotationDirection = dx;
        rotationX += dy * 1;
        updateTransform();
        evt.preventDefault();
        //return false;
    }

    function getDragProps(evt) {
        if (evt.targetTouches) {
            var touch = evt.targetTouches[0];
            return {x:touch.clientX, y:touch.clientY};
        } else {
            return {x: evt.clientX||evt.pageX, y: evt.clientY||evt.pageY};
        }
    }

    function updateTransform() {
        var cabinetTransform = "translateZ(" + (rotationX).toFixed(2) + "px) rotateY(" + rotationY.toFixed(2) + "deg)";
        cabinet.style.transform = cabinet.style.webkitTransform = cabinetTransform;

        var joystickTransform = "rotateY(" + (360 - rotationY.toFixed(2)) + "deg)";
        joystick1.style.transform = joystick2.style.transform = joystick1.style.webkitTransform = joystick2.style.webkitTransform = joystickTransform;
    }

    function handleMouseUp(evt) {
        window.removeEventListener("mousemove", handleMouseMove);
        window.removeEventListener("mouseup", handleMouseUp);
        window.removeEventListener("touchmove", handleMouseMove);
        window.removeEventListener("touchend", handleMouseUp);
        window.removeEventListener("pointermove", handleMouseMove);
        window.removeEventListener("pointerup", handleMouseUp);

        var endDirection = rotationDirection > 0 ? 1 : -1;
        rotationDirection = Math.min(40, Math.max(-40, rotationDirection));
        activeTween = createjs.Tween.get(window, {override:true})
                .to({rotationDirection:endDirection*0.5}, Math.abs(rotationDirection*100), createjs.Ease.quadOut)
                .call(function() {
                   rotationY = rotationY%360;
                });
        dragProps = null;
    }
init();
            
          
!
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