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.

            
              <div class="container">
    <div class="cloud"></div>
    <div class="cloud"></div>
    <div class="cloud"></div>
    <div class="mega-baby" data-status="idle">
        <div class="body">
            <div class="upper-body">
                <div class="head">
                    <i class="face"></i>
                    <div class="eye left-eye"></div>
                    <div class="eye right-eye"></div>
                </div>
                <div class="arm left-arm">
                    <i class="hand left-hand"></i>
                    <i class="bicep left-bicep"></i>
                </div>
                <i class="torso"></i>
                <div class="arm right-arm">
                    <i class="bicep right-bicep"></i>
                    <i class="hand right-hand"></i>
                </div>
            </div>
            <div class="lower-body">
                <i class="abs"></i>
                <i class="leg left-leg"></i>
                <i class="leg right-leg"></i>
            </div>
        </div>
        <div class="feet">
            <i class="foot left-foot"></i>
            <i class="foot right-foot"></i>
        </div>
    </div>
</div>
<canvas class="fx"></canvas>
            
          
!
            
              html,
body {
    height: 100%;
}

body {
    cursor: crosshair;
    height: 100%;
    width: 100%;
}

.container {
    align-items: center;
    background: var(--sb-white);
    bottom: 0;
    display: flex;
    justify-content: center;
    left: 0;
    overflow: hidden;
    position: absolute;
    right: 0;
    top: 0;
}
    
.fx {
    height: 100%;
    left: 0;
    position: absolute;
    top: 0;
    width: 100%;
    z-index: 2;
}

.cloud {
    background: no-repeat url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/59639/cloud.png') center center;
    height: 120px;
    position: absolute;
    top: 2%;
    width: 100%;
    animation: cloud 15s linear 0s infinite normal backwards;
    
    &:nth-child(2n) {
        background-position: 0% 50%;
        top: 10%;
        animation-duration: 11s;
    }
    
    &:nth-child(3n) {
        background-position: 100% 50%;
        top: 18%;
        animation-duration: 11s;
    }
}

.mega-baby {
    position: relative;
    text-align: center;
    z-index: 1;
    
    i {
        background: no-repeat url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/59639/mega-baby.png');
        background-size: 120px 600px;
        display: block;
        height: 60px;
        margin: 0 auto;
        width: 60px;
        
        &.face { background-position: 0 0; }
        &.left-hand { background-position: 0 -60px; }
        &.right-hand { background-position: 0 -120px; }
        &.left-bicep { background-position: 0 -180px; }
        &.torso { background-position: 0 -240px; }
        &.right-bicep { background-position: 0 -300px; }
        &.abs { background-position: 0 -360px; }
        &.left-leg { background-position: 0 -420px; }
        &.right-leg { background-position: 0 -480px; }
        &.foot { background-position: 0 -540px; }
    }
    
    .head {
        height: 60px;
        margin: 0 auto 4px;
        position: relative;
        width: 60px;
        z-index: 1;
        
        &:before,
        &:after {
            background: no-repeat url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/59639/mega-baby.png') -60px -10px;
            background-size: 120px 600px;
            content: "";
            height: 15px;
            left: 12px;
            position: absolute;
            top: 28px;
            width: 15px;
            z-index: 1;
        }
        
        &:after {
            background-position: -60px -27px;
            left: auto;
            right: 12px;
        }
        
        .face {
            height: 100%;
            position: absolute;
            width: 100%;
            z-index: 3;
        }
        
        .eye {
            height: 9px;
            position: absolute;
            top: 31px;
            width: 9px;
            z-index: 2;
            
            &:before {
                background: no-repeat url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/59639/mega-baby.png') -60px 0;
                background-size: 120px 600px;
                content: "";
                display: block;
                height: 100%;
                margin-left: 3px;
                width: 100%;
            }
            
            &.left-eye {
                left: 15px;
            }
            
            &.right-eye {
                right: 14px;
            }
        }
    }
    
    .upper-body {
        
        .hand,
        .bicep,
        .arm,
        .torso {
            display: inline-block;
        }
    }
    
    .leg,
    .foot {
        display: inline-block;
    }
    
    .left-foot {
        margin: 0 70px 0 10px;
    }
    
    &[data-status="idle"] {
        animation: breathe 2s ease-in-out 0s infinite alternate;

        .body,
        .hand,
        .abs,
        .head,
        .arm,
        .upper-body {
            animation: breathe 2s ease-in-out 0s infinite alternate;
        }
    }
}

@keyframes breathe {
    0% { transform: translate3d(0, 0, 0); }
    100% { transform: translate3d(0, 3px, 0); }
}

@keyframes cloud {
    0% { transform: translate3d(100%, 0, 0); }
    100% { transform: translate3d(-100%, 0, 0); }
}
            
          
!
            
              const container = document.querySelector('.mega-baby')
const leftEye = document.querySelector('.left-eye')
const rightEye = document.querySelector('.right-eye')
const fx = document.querySelector('.fx')
const fxContext = fx.getContext('2d')
const flame = document.createElement('img')
let mrBurns = []
let currentBurn = {}
let leftEyePosition = { x: 0, y: 0 }
let rightEyePosition = { x: 0, y: 0 }
let cursorPosition = { x: 0, y: 0 }
let isLasers = false
let animationFrame = null

const throttle = (fn, threshhold, scope) => {
    threshhold || (threshhold = 250);
    let last, deferTimer;
    return function () {
        var context = scope || this;
        var now = +new Date,
            args = arguments;
        if (last && now < last + threshhold) {
            // hold on to it
            clearTimeout(deferTimer);
            deferTimer = setTimeout(function () {
                last = now;
                fn.apply(context, args);
            }, threshhold);
        } else {
            last = now;
            fn.apply(context, args);
        }
    }
}

const getEyePosition = () => {
    const left = leftEye.getBoundingClientRect()
    const right = rightEye.getBoundingClientRect()
    leftEyePosition = {
        x: left.left + (left.width / 2),
        y: left.top + (left.height / 2)
    }
    rightEyePosition = {
        x: right.left + (right.width / 2),
        y: right.top + (right.height / 2)
    }
}

const getRotation = (el, { x, y }) => {
    const diffX = cursorPosition.x - x
    const diffY = cursorPosition.y - y
    const rad = Math.atan2(diffY, diffX)
    return `rotate(${rad * (180 / Math.PI)}deg)`
}

const drawBurn = ({ opacity, points }) => {
    const opacityBase = opacity * 0.8
    const opacityDiff = opacity * 0.2
    let lastX = 0
    let lastY = 0
    for (let i = 0; i < points.length; i++) {
        const { x, y } = points[i]
        if (Math.abs(x - lastX) >= 30 ||
            Math.abs(y - lastY) >= 30) {
            lastX = x
            lastY = y
            fxContext.save()
            fxContext.globalAlpha = opacityBase + (opacityDiff * Math.random())
            fxContext.translate(x, y)
            fxContext.scale(opacity, opacity)
            fxContext.drawImage(flame, -30, -60, 60, 60)
            fxContext.restore()
        }
    }
}

const addNewBurn = throttle(() => {
    if (currentBurn.points) {
        currentBurn.points.push({
            x: cursorPosition.x,
            y: cursorPosition.y
        })
    }
}, 10)

const sickLasersMyDude = () => {
    getEyePosition()
    
    fx.width = window.innerWidth
    fx.height = window.innerHeight
    
    // draw lasers
    if (isLasers) {
        fxContext.save()
        fxContext.beginPath()
        fxContext.lineWidth = 3
        fxContext.lineCap = 'round'
        fxContext.moveTo(leftEyePosition.x, leftEyePosition.y)
        fxContext.lineTo(cursorPosition.x, cursorPosition.y)
        fxContext.moveTo(rightEyePosition.x, rightEyePosition.y)
        fxContext.lineTo(cursorPosition.x, cursorPosition.y)

        fxContext.shadowBlur = 10
        fxContext.shadowColor = '#FD0100'

        fxContext.strokeStyle = '#FD0100'
        fxContext.globalAlpha = 0.7 + (Math.random() * 0.3)
        fxContext.closePath()
        fxContext.stroke()

        fxContext.lineWidth = 2
        fxContext.strokeStyle = '#FFF'
        fxContext.stroke()
        fxContext.restore()
        
        addNewBurn()
    }
    
    if (currentBurn.points) {
        drawBurn(currentBurn)
    }
    
    for (let i =  mrBurns.length - 1; i >= 0; i--) {
        const burn = mrBurns[i]
        drawBurn(burn)
        burn.opacity -= 0.002
        if (burn.opacity <= 0) {
            mrBurns.splice(i, 1)
        }
    }

    if (isLasers) {
        fxContext.save()
        fxContext.beginPath()
        fxContext.fillStyle = '#FFF'
        fxContext.arc(cursorPosition.x, cursorPosition.y, 4, 0, Math.PI * 2)
        fxContext.closePath()
        fxContext.fill()
        fxContext.shadowBlur = 10
        fxContext.shadowColor = '#FD0100'
        fxContext.globalAlpha = 0.3 + (Math.random() * 0.7)
        fxContext.fill()
        fxContext.fill()
        fxContext.restore()
    }
    
    animationFrame = window.requestAnimationFrame(sickLasersMyDude)
}

const cancelLasers = () => {
    container.dataset.status = 'idle'
    isLasers = false
    if (currentBurn.points) {
        mrBurns.push(currentBurn)
    }
    currentBurn = {}
}

window.addEventListener('mousemove', e => {
    cursorPosition = {
        x: e.pageX,
        y: e.pageY
    }
    leftEye.style.transform = getRotation(leftEye, leftEyePosition)
    rightEye.style.transform = getRotation(rightEye, rightEyePosition)
})

window.addEventListener('resize', () => {
    getEyePosition()
    fx.width = window.innerWidth
    fx.height = window.innerHeight
    mrBurns = []
})

window.addEventListener('mouseout', cancelLasers)

document.addEventListener('mousedown', e => {
    e.preventDefault()
    isLasers = true
    currentBurn = {
        opacity: 1,
        points: []
    }
})

document.addEventListener('mouseup', e => {
    e.preventDefault()
    cancelLasers()
})

// start it up
getEyePosition()
sickLasersMyDude()

flame.src = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/59639/fire.png'
            
          
!
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