Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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="caffeine-machine">

    <!-- Top Section -->
    <div class="top-section">
        <div class="top-section__base">
            
            <div class="slots">
            </div>
            
            <div class="led"></div>
            
            <div class="spout"></div>
        </div>
        
        <div class="lever-container">
            <div class="lever-base"></div>
       
            <!-- Lever -->
            <svg viewBox="0 0 114 355" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" class="lever">
                <g id="lever">
                    <rect id="socket" fill="#3A6255" x="43" y="355" width="28" height="91.5"></rect>
                    <rect id="rod" fill="#D8D8D8" x="43" y="57" width="28" height="298"></rect>
                    <g id="top">
                        <circle fill="#F0A830" cx="57" cy="57" r="57"></circle>
                        <path d="M102.570209,40.1833305 C102.676794,40.0838119 102.777304,39.9779482 102.871517,39.8656698 C105.889032,36.2695351 101.304331,27.4546828 92.6313004,20.177146 C83.9582698,12.8996092 74.4812099,9.91524313 71.4636947,13.5113778 C71.3644458,13.629658 71.273421,13.753584 71.1904954,13.8829152 C76.2444491,16.5602964 82.1808506,20.6036755 88.1462014,25.6091992 C94.0729258,30.5823114 99.0593916,35.6922002 102.570209,40.1833305 Z" id="highlight" fill="#FFFFFF" transform="translate(87.494931, 26.085223) rotate(7.000000) translate(-87.494931, -26.085223) "></path>
                    </g>
                </g>
            </svg>
        </div>
        <!-- End Lever -->
    </div>

    <!-- Bottom Section -->
    <div class="bottom-section">
        <div class='liquid-base'>
            <div id="liquid"></div>
        </div>
        <div class='cup-base'>
            <div id="cup">
                <svg viewBox="0 0 272 170" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
                    <g>
                        <path d="M56.4777408,0 L56.4777408,23.4472425 C42.7970624,22.5906306 29.0726032,22.5906306 15.3981762,23.4472425 C11.7841784,24.2538225 -0.408387727,40.0041139 0.0105359081,51.5839228 C0.617031005,68.8035833 8.0701274,97.9656931 22.2822853,105.975274 C24.4831972,107.050714 42.1655405,109.689314 56.6465583,112.134081 C63.0242148,152.307023 96.3630583,169.995618 162.20916,169.995618 C236.139869,170.358269 272.19235,148.230321 272.19235,96.8902531 L272.19235,0 L56.4777408,0 Z M26.8029162,93.7889593 C20.4252598,86.6109742 14.1038866,67.1403914 13.5911719,57.2988008 C13.2222695,49.9457468 16.7862568,37.9282604 24.433176,35.3897026 C30.6607688,33.3388543 46.0984302,34.8269666 55.4773272,35.0708214 L55.4773272,99.4037949 C45.2668343,98.1595266 29.9792364,97.5029995 26.8029162,93.7889593 Z" id="Page-1" fill="#CFCFCF" fill-rule="nonzero" transform="translate(136.096175, 85.000000) scale(-1, 1) translate(-136.096175, -85.000000) "></path>
                        <g id="text" transform="translate(0.000000, 18.000000)">
                            <rect x="0" y="4" width="215" height="90" fill="transparent"></rect>
                            <text id="cup-name" x="107.5" y="45" dy="0.4em" text-anchor="middle" font-size="50" font-family="Pacifico" font-weight="normal" fill="#FFFFFF">Espresso</text>
                        </g>
                    </g>
                </svg>
            </div>
        </div>
       <svg id="bottom" viewBox="0 0 960 467" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
            <title>
                bottom
            </title>
            <defs>
                <text id="b" font-family="Pacifico" font-size="41" fill="#FFF">
                    <tspan x="0" y="53">Caffeine-o-matic</tspan>
                </text>
                <filter x="-50%" y="-50%" width="200%" height="200%" filterUnits="objectBoundingBox" id="a">
                    <feOffset dy="2" in="SourceAlpha" result="shadowOffsetOuter1"/>
                    <feColorMatrix values="0 0 0 0 0.290221632 0 0 0 0 0.290175289 0 0 0 0 0.290237457 0 0 0 1 0" in="shadowOffsetOuter1"/>
                </filter>
            </defs>
            <g fill="none" fill-rule="evenodd">
                <g transform="translate(0 287)">
                    <path fill="#578475" d="M39.676 2.683l26.82 26.82-38.383 38.384-26.82-26.82zM891.717 27.213l26.82-26.82 37.676 37.676-26.82 26.82z"/>
                    <rect fill="#78C0A8" y="31" width="960" height="149" rx="15"/>
                    <path d="M738 132h174.506M738 136h174.506" stroke="#578475" stroke-width="2" stroke-linecap="square"/>
                    <g fill="#FFF" transform="translate(653 70)">
                        <use filter="url(#a)" xlink:href="#b"/>
                        <use xlink:href="#b"/>
                    </g>
                </g>
                <path fill="#FFF9E5" d="M38 0h885v316.01H38z"/>
                <path fill="#588475" d="M36 292.234h889v26H36z"/>
            </g>
        </svg>
    </div>

</div>
              
            
!

CSS

              
                $TURQUOISE: #78C0A8;
$MEDIUM_TURQUOISE: #5E9E89;
$DARK_TURQUOISE: #588475;
$WARNING: #F8E81C;
$SUCCESS: #B8E986;
$ERROR: #DD2F09;

$machine-max-width: 960px;

$lever-height: 355px;
$lever-module-width: 90px;
$lever-socker-height: 91.5px;

@keyframes shift_lever_rod { 
    0% {  transform: translateY(0)scaleY(1); } 
    100% {  transform: translateY($lever-socker-height)scaleY(0.05); }
}
@keyframes shift_lever_top { 
    0% { transform: translateY(0%); }
    100% { transform: translateY($lever-height + 10px); }
}
@keyframes pour_liquid { 
    0% { transform: translate(-50%, -100%); }    
    10% { transform: translate(-50%, -100%); }  
    20% { transform: translate(-50%, -77.777%)scaleX(0.9); }  
    30% { transform: translate(-50%, -55.555%)scaleX(0.75); }  
    40% { transform: translate(-50%, -33.333%)scaleX(0.85); }  
    50% { transform: translate(-50%, -11.111%)scaleX(0.65); }  
    55% { transform: translate(-50%, 0%)scaleX(0.75); }  
    60% { transform: translate(-50%, 11.111%)scaleX(0.65); }  
    65% { transform: translate(-50%, 22.222%)scaleX(0.4); }  
    70% { transform: translate(-50%, 100%)scaleX(0); }  
    100% { transform: translate(-50%, 100%)scaleX(0); }
}
@keyframes move_cup { 
    0% { transform: translateX(100%); }
    10% {  transform: translateX(0%); }
    70% {  transform: translateX(0%); }
    80% {  transform: translateX(-100%); }
    100% { transform: translateX(-100%); }
}

* {
    box-sizing: content-box;
}

img {
    max-width: 100%;
    height: auto;
    display: inline-block;
    vertical-align: middle;
}

body {
    background-color: #4A4A4A;
    display: flex;
    flex-flow: row wrap;
    align-items: flex-end;
    justify-content: center;
    height: 100vh;
}

#caffeine-machine {
    width: calc(100vw - #{$lever-module-width});
    max-width: 700px;
    padding-right: $lever-module-width;

    &.inactive {}
    &.processing {
        .led {
            background-color: $WARNING;
            box-shadow: 0 0 20px $WARNING;
        }
    }
    &.success {
        .led {
            background-color: $SUCCESS;
            box-shadow: 0 0 20px $SUCCESS;
        }
        
        .slots .slot .column i {
            color: white;
            background-color: $SUCCESS;
        }
    }
    &.error {
        .led {
            background-color: $ERROR;
            box-shadow: 0 0 10px $ERROR;
        }
        
        .slots .slot .column i {
            color: $ERROR;
        }
    }
}

.top-section {
    max-width: $machine-max-width;
    width: 100%;
    margin: 0 auto;
    position: relative;
    z-index: 1;
    
    .top-section__base {
        display: flex;
        flex-flow: row wrap;
        align-items: center;
        justify-content: center;
        position: relative;
        z-index: 2;
        background-color: $TURQUOISE;
        height: 40vh;
        border-radius: 15px;
        margin: 0 auto;
        
        .led {
            position: absolute;
            right: 27.5px;
            bottom: 27.5px;
            height: 27.5px;
            width: 27.5px;
            border-radius: 50%;
            border: 1px solid rgba(0,0,0,0.15);
            background-color: rgba(0,0,0,0.25);
        }
        
        .spout {
            position: absolute;
            bottom: 0;
            left: 50%;
            width: 15%;
            height: 5%;
            border-radius: 0 0 5px 5px;
            transform: translate(-50%, 100%);
            background-color: $DARK_TURQUOISE;
        }
        
        .slots {
            display: flex;
            background-color: $DARK_TURQUOISE;
            border-radius: 10px;
            width: 66%;
            height: 40%;
            justify-content: center;
            
            .slot {
                flex: 1 1 0px;
                border-right: 10px solid $TURQUOISE;
                box-shadow: inset 0 5px 10px rgba(0,0,0,0.2);
                text-align: center;
                position: relative;
                overflow: hidden;
                
                &:first-child { border-radius: 10px 0 0 10px; }
                &:last-child {
                    border-radius: 0 10px 10px 0;
                    border-right: none;
                }              

                .column {
                    height: 300%;
                    display: flex;
                    flex-flow: column wrap;

                    i {
                        flex: 1;
                        position: relative;
                        height: 100%;
                        font-size: 5rem;
                        vertical-align: middle;
                        transition: all 0.1s ease-in-out;
                        color: $TURQUOISE;

                        &:before {
                            position: absolute;
                            top: 50%;
                            left: 50%;
                            transform: translate(-50%, -50%);
                        }
                    }    
                }


            }
        }
    }
    
    .lever-container {
        position: absolute;
        transform: translateY(-50%);
        top: 50%;
        right: 0;
        z-index: 3;
        
        .lever-base {
            width: $lever-module-width;
            height: $lever-module-width * 1.8;
            background-color: $MEDIUM_TURQUOISE;
            border-radius: 0 15px 15px 0;
            transform: translateX(100%);
        }
    
        .lever {
            z-index: 2;
            position: absolute;
            top: 0;
            right: 0;
            transform: translate(100%, -100%);
            overflow: visible;
            cursor: pointer;

            #rod { transform-origin: bottom center; }

            &.animate {
                #rod {
                    animation: 0.5s shift_lever_rod ease-in-out 2;
                }
                #top {
                    animation: 0.5s shift_lever_top ease-in-out 2;
                }

                &.reverse {
                    #top, #rod {
                        animation-direction: reverse;
                    }
                }
            }        
        }    
    }
    
}

.bottom-section {
    flex: 1;
    position: relative;
    z-index: 0;
    max-width: $machine-max-width;
    margin: 0 auto;
    
    #bottom {
        width: 100%;
    }
    
    .liquid-base {
        background-color: transparent;
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        width: 100%;
        height: 60%;
        overflow: hidden;
        
        #liquid {
            position: absolute;
            top: 0;
            left: 50%;
            height: 1000px;
            width: 2%;
            background: linear-gradient(to right, #9e5600 40%,#eac096 100%);
            transform: translate(-50%, -100%);
            
            &.pouring {
                animation: 6s pour_liquid linear;
            }
        }
    }
    
    .cup-base {
        background-color: transparent;
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        width: 100vw;
        height: 67.5%;
        overflow: hidden;
        
        #cup {
            width: 100vw;
            height: 100%;
            position: relative;
            transform: translateX(100%);
            
            &.pouring {
                animation: 6s move_cup ease-out;
            }
            
            svg {
                position: absolute; 
                bottom: 0;
                left: 50%;
                width: 25%;
                max-width: 272px;
                transform: translate(calc(-50% + 27.5px), 0);
            }
        }
        

    }

}

@media screen and (max-width: 39.9375em) {
    .top-section .top-section__base .slots .slot .column i {
        font-size: 3rem;
    }
}
              
            
!

JS

              
                // -----------
// ----------- StateManager
// -----------

function StateManager(args){
    this._state = null;
    this.states = args.states || [];
    this.node   = args.node || null;
}

StateManager.prototype = {
    setNodeState: function(){
        this.node.classList.remove.apply(this.node.classList, this.states);
        this.node.classList.add(this.state);
    },
    get state(){
        return this._state || this.states[0];
    },
    set state(_state){
        if(this.states.indexOf(_state) === -1){
            throw new Error('Not a valid state', _state);
        } else if(this._state !== _state){
            this._state = _state;
            this.setNodeState();
        }
    }
};


// -----------
// ----------- Machine
// -----------

function Machine(args){
    this.node   = args.node;
    this.states = args.states;
}

Machine.prototype = Object.create(StateManager.prototype);

const MACHINE = new Machine({
    node: document.getElementById('caffeine-machine'),
    states: [
        'inactive',
        'processing',
        'error',
        'success'
    ]
});

// -----------
// ----------- Lever
// -----------

function Lever(args){
    this.node   = args.node;
    this.states = args.states;
}

Lever.prototype = Object.create(StateManager.prototype);

// --- Events
Lever.prototype.onLeverPull = function(){
    this.node.classList.add('animate');
    this.state = 'disabled';
};

Lever.prototype.onAnimationIteration = function(){
    if(!this.node.classList.contains('reverse')){
        this.node.classList.add('reverse');
        SLOTS.spin();
    } 
}

Lever.prototype.onAnimationEnd = function(){
    if(this.node.classList.contains('reverse')){
        this.node.classList.remove('animate', 'reverse');
        this.state = 'enabled'; 
    }
}

// --- Initializer
Lever.prototype.init = function(){
    const onLeverPull          = this.onLeverPull.bind(this);
    const onAnimationIteration = this.onAnimationIteration.bind(this);
    const onAnimationEnd       = this.onAnimationEnd.bind(this);

    this.node.addEventListener('click', onLeverPull);
    this.node.addEventListener('animationiteration', onAnimationIteration);
    this.node.addEventListener('animationend', onAnimationEnd);

    this.setNodeState();
};

const LEVER = new Lever({
    node: document.querySelector('.lever'),
    states: [
        'enabled',
        'disabled'
    ]
});

LEVER.init();

// -----------
// ----------- Slot
// -----------

function Slot(args){
    this.icons = args.icons;
    this.parentNode = SLOTS_NODE;
    this.states = [
        'inactive',
        'spinning'
    ];
    this.timePerRotation = args.timePerRotation || 100;
    this.spinDuration = args.spinDuration || 2000;
    this.animationTimestamp = null;
    this.winner = null;
    this.parent = null;
}

Slot.prototype = Object.create(StateManager.prototype);

Slot.prototype.pickWinner = function(){
    this.winner = Math.floor(Math.random() * this.icons.length);   
}

Slot.prototype.onAnimationEnd = function(){
    const y = this.winner * (100 / this.icons.length);
    this.node.firstElementChild.style.transform = 'translateY(-' + y + '%)';
    
    this.parent.generator.next(this.winner);
    this.state = 'inactive';
}

Slot.prototype._animate = function(timestamp){
    timestamp = timestamp || performance.now();
    if(!this.animationTimestamp){ this.animationTimestamp = timestamp; }
    
    const progress = timestamp - this.animationTimestamp;
    const y = Math.min(100, (progress % this.timePerRotation));
        
    this.node.firstElementChild.style.transform = 'translateY(-' + y + '%)';

    if(progress < this.spinDuration){
        window.requestAnimationFrame(this.animate);   
    } else {
        this.onAnimationEnd();
    }
}
    
Slot.prototype.spin = function(){
    this.reset();
    this.animate();
    this.state = 'spinning';   
}

Slot.prototype.reset = function(){
    this.animationTimestamp = null;
    this.pickWinner();
}

Slot.prototype.init = function(parent){
    this.animate = this._animate.bind(this);
    this.parent = parent;
    this.render();
}

Slot.prototype.render = function(){
    const slots = this.parentNode;

    const slot     = document.createElement('div');
    slot.className = 'slot';

    const column     = document.createElement('div');
    column.className = 'column';

    this.icons.forEach((icon)=>{
        let tag = document.createElement('i'); 
        tag.className = icon;
        column.appendChild(tag);
    });

    slot.appendChild(column);
    slots.appendChild(slot);

    this.node = slot;
}



// -----------
// ----------- Slots
// -----------

const SLOTS_NODE = document.querySelector('.slots');

function Slots(args){
    this.node   = args.node;
    this.states = args.states;
    this.slots  = args.slots;
    this.generator = null;
}

Slots.prototype = Object.create(StateManager.prototype);

Slots.prototype.beforeSpin = function(){    
    function* slotsFinished(){
        const a = yield;
        const b = yield;
        const c = yield;
        this.afterSpin([a, b, c]);
    };

    this.generator = slotsFinished.call(this);
    this.generator.next();
}

Slots.prototype.spin = function(){
    if(SLOTS.state !== 'spinning' && CUP.state !== 'pouring' && LIQUID.state !== 'pouring'){
        MACHINE.state = 'processing';
        this.state = 'spinning';
        this.beforeSpin();
        this.slots.forEach(slot => slot.spin() );
    }
}

Slots.prototype.afterSpin = function(values){
    const first = values[0];
    const slotsMatch = values.every((v)=> v === first); 
    
    if(slotsMatch){
        CUP.setTitle(this.getWinner(first));

        MACHINE.state = 'success';
        LIQUID.state = 'pouring';
        CUP.state = 'pouring';
    } else {
        MACHINE.state = 'error';
    }
    
    this.state = 'inactive';
}

Slots.prototype.getWinner = function(index){
    switch(index){
        case 0:
            return 'Coffee';
        case 1:
            return 'Espresso';
        case 2:
            return 'Tea';
        default: 
            throw Error('Not a valid winner');
            break;
    }        
}

Slots.prototype.render = function(){
    this.slots.forEach( slot => slot.init(this) );
}

Slots.prototype.init = function(){
    this.render();
}

const SLOTS = new Slots({
    node: SLOTS_NODE,
    states: [
        'inactive',
        'spinning'
    ],
    slots: [
        new Slot({
            icons: [
                'icon-coffee-pot',
                'icon-espresso-machine',
                'icon-tea-pot'
            ],
            spinDuration: 1500
        }), 
        new Slot({
            icons: [
                'icon-coffee-filter',
                'icon-espresso-tamper',
                'icon-tea-strainer'
            ],
            spinDuration: 2000
        }), 
        new Slot({
            icons: [
                'icon-coffee-beans',
                'icon-espresso-grounds',
                'icon-tea-leaf'
            ],
            spinDuration: 2500
        }),
    ]
});

SLOTS.init();

// -----------
// ----------- Liquid
// -----------

function Liquid(args){
    this.node   = args.node;
    this.states = args.states;
}

Liquid.prototype = Object.create(StateManager.prototype);

Liquid.prototype.onAnimationEnd = function(){
    this.state = 'inactive'; 
}

Liquid.prototype.init = function(){
    const onAnimationEnd = this.onAnimationEnd.bind(this);
    this.node.addEventListener('animationend', onAnimationEnd);
    this.setNodeState();
}

const LIQUID = new Liquid({
    node: document.getElementById('liquid'),
    states: [
        'inactive',
        'pouring',
    ]
});

LIQUID.init();

// -----------
// ----------- Cup
// -----------

function Cup(args){
    this.node   = args.node;
    this.states = args.states;
    this.cupName = document.getElementById('cup-name');
}

Cup.prototype = Object.create(StateManager.prototype);

Cup.prototype.onAnimationEnd = function(){
    this.state = 'inactive'; 
}

Cup.prototype.init = function(){
    const onAnimationEnd = this.onAnimationEnd.bind(this);
    this.node.addEventListener('animationend', onAnimationEnd);
    this.setNodeState();
}

Cup.prototype.setTitle = function(title){
    this.cupName.textContent = title;
}

const CUP = new Cup({
    node: document.getElementById('cup'),
    states: [
        'inactive',
        'pouring',
    ]
});

CUP.init();
              
            
!
999px

Console