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

              
                .tabs
    input(type="radio" name="tabs" id="sol1" checked)
    label(for="sol1") Range
    .tab
        #frame1.frame
        input(type="range" id="slider" min="0" max="34" step="1" value="0")

    input(type="radio" name="tabs" id="sol2")
    label(for="sol2") Drag / Swipe
    .tab
        #frame2.frame
        
    input(type="radio" name="tabs" id="sol3")
    label(for="sol3") Canvas
    .tab
        canvas(id="frame3" class="frame")
#overlay Loading...
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Open+Sans');
$frame-w: 480px;
$frame-h: 327px;
$frames: 34;

body {
    margin: 0;
    padding: 0;
    font-size: .8em;
    font-family: 'Open Sans', sans-serif;
    overflow: hidden;
    
    .tabs {
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        width: $frame-w;
        height: $frame-h + 24px;
        
        label {
            cursor: pointer;
            border-radius: 4px;
        }

        input[name="tabs"] {
            display: none;
            
            &:checked + label {
                background: #44abda;
                color: #fff;
                
                & + .tab {
                    display: block;
                }
            }
        }
        
        label {
            display: inline-block;
            width: 33.3333%;
            float: left;
            height: 24px;
            line-height: 24px;
            text-align: center;
        }
        
        .tab {
            display: none;
            position: absolute;
            width: 100%;
            height: $frame-h;
            top: 24px;
            text-align: center;
            
            .frame {
                width: 100%;
                height: 100%;
            }
            
            #slider {
                width: calc(100% - 80px);
            }
        }
    }
    
    #overlay {
        position: absolute;
        width: 100%;
        height: calc(100% - 20px);
        background: rgba(0, 0, 0, .3);
        text-align: center;
        color: #fff;
        font-size: 1.1em;
        padding-top: 20px;
    }
}
              
            
!

JS

              
                let frame1 = document.getElementById('frame1'),
    frame2 = document.getElementById('frame2'),
    frame3 = document.getElementById('frame3'),
    labels = document.querySelectorAll('label'),
    ctx = frame3.getContext('2d'),
    slider = document.getElementById('slider'),
    frameWidth = 480,
    frameHeight = 327,
    activeFrame = 0,
    frames = 34,
    xStart = null,
    s2Settings = {
        sensitivity: 40
    },
    s3Settings = {
        fps: 20,
        reverse: false
    },
    now, delta, then = Date.now(),
    interval = 1000 / s3Settings.fps,    
    runCanvas = false;

let gui = new dat.GUI(),
    s2 = gui.addFolder('Drag / Swipe'),
    s3 = gui.addFolder('Canvas');

s2.add(s2Settings, "sensitivity", 10, 80, 1);
s3.add(s3Settings, "fps", 1, 60, 1).onChange(() => {interval = 1000 / s3Settings.fps;});
s3.add(s3Settings, "reverse");

s2.open();
s3.open();

let sprite = new Image();
sprite.onload = function() {
    frame1.style.background = frame2.style.background = `url(${sprite.src})`;
    document.getElementById('overlay').style.display = 'none';
    
    solution1(); // input range
    solution2(); // drag / swipe
    solution3(); // canvas
    
    labels.forEach(function(element) {
      element.addEventListener('click', function() {
          runCanvas = false;
          if(this.getAttribute('for') == "sol3")
              runCanvas = true;
      });
    });
};
sprite.src = 'https://serving.photos.photobox.com/\
55967562d176c08ff2d7e23195f94e704faa7feede75617ac5\
905d8dca9295f1a547077a.jpg';

function solution1() {    
    slider.addEventListener("input", function() {
        activeFrame = parseInt(this.value);
        frame1.style.backgroundPositionX = `-${activeFrame * frameWidth}px`;
    });
}

function solution2() {
    frame2.addEventListener('touchstart', (e) => {
        xStart = e.touches ? e.touches[0].clientX : e.clientX;
    });
    frame2.addEventListener('mousedown', (e) => {
        xStart = e.touches ? e.touches[0].clientX : e.clientX;
    });
    frame2.addEventListener('touchend', () => { xStart = null; });
    frame2.addEventListener('mouseup', () => { xStart = null; });
    frame2.addEventListener("mousemove", move);
    frame2.addEventListener("touchmove", move);
}

function move(e) {
    if(!xStart)
        return;
    
    let xEnd = e.touches ? e.touches[0].clientX : e.clientX;
    if (xStart - xEnd > .5 * frameWidth / (10 + s2Settings.sensitivity)) {
        activeFrame++;
        if(activeFrame > frames)
            activeFrame = 0;
        frame2.style.backgroundPositionX = `${activeFrame * frameWidth}px`;
        xStart = xEnd;
    } else if(xEnd - xStart > .5 * frameWidth / (10 + s2Settings.sensitivity)) {
        activeFrame--;
        if(activeFrame < 0)
            activeFrame = frames;
        frame2.style.backgroundPositionX = `${activeFrame * frameWidth}px`;
        xStart = xEnd;
    }
}

function solution3() {
    frame3.width = frameWidth;
    frame3.height = frameHeight;
    animate();
}

function animate() { 
    now = Date.now();
    delta = now - then;
    
    if(runCanvas && delta > interval) {
        if(activeFrame > frames)
            activeFrame = 0;
        else if(activeFrame < 0)
            activeFrame = frames;

        ctx.drawImage(
            sprite, activeFrame * frameWidth, 0, frameWidth, frameHeight, 
            0, 0, frameWidth, frameHeight
        );
        
        activeFrame += (s3Settings.reverse) ? -1 : 1;
        then = now - (delta % interval);
    }
    window.requestAnimationFrame(animate);
}
              
            
!
999px

Console