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.

            
              <link href='https://fonts.googleapis.com/css?family=Dosis:200,800' rel='stylesheet' type='text/css'>

<div id="container" class="centered"></div>
            
          
!
            
              body {
    font-family: 'Dosis', sans-serif;
    margin: 0;
    background-color: #281f20;
}

#container {
    width: 512px;
    height: 320px;
    top: -32px;
}

canvas {
    position: absolute;
}

.centered {
    position: absolute;
    margin: auto;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}
            
          
!
            
              const TWO_PI = Math.PI * 2;
const PI = Math.PI; // consistency!!
const HALF_PI = Math.PI * 0.5;

var viewWidth = 512,
    viewHeight = 320,
    container = document.getElementById("container");

var masterTimeline;

var badgeCanvas,
    ribbonCanvas,
    ribbonTextWrapper;

var ribbonColor = '#916858',
    ribbonBackColor = '#63453a',
    textColor = '#c79f73',
    badgeColor = '#63453a',
    badgeInsideColor = '#916858',
    badgeCirclesColor = '#c79f73';


window.onload = function() {
    masterTimeline = new TimelineMax();

    masterTimeline.append(drawRibbon());
    masterTimeline.append(drawBadge());
    masterTimeline.append(showBadgeText());
    masterTimeline.append(showRibbonText(), '-=0.25');
    masterTimeline.append(TweenMax.to(badgeCanvas, 20, {rotation:360, ease:Linear.easeNone, repeat:-1}));
};

function createGrid() {
    var canvas = createCanvas(),
        c = canvas.getContext('2d');

    var lines = [],
        start,
        end,
        lineStyle = {
            lineWidth:0.1,
            strokeStyle:'#000'
        };

    for (var i = 0; i <= 24; i++) {
        start = new Point(32 * i, 0);
        end = new Point(32 * i, viewHeight);
        lines.push(new Line(start, end, lineStyle));
    }

    for (var j = 16; j >= 0; j--) {
        start = new Point(0, 32 * j);
        end = new Point(viewWidth, 32 * j);
        lines.push(new Line(start, end, lineStyle));
    }

    lines.forEach(function(l) {
        l.draw(c);
    });
}

function drawBadge() {
    var w = 256,
        h = 256;

    badgeCanvas = createCanvas(128, 32, w, h);

    var c = badgeCanvas.getContext('2d');

    badgeCanvas.style.zIndex = '-1';

    var center = new Point(128, 128);
    var bgCircle = new Circle(center, 0, {fillStyle:badgeColor});
    var fgCircle = new Circle(center, 0, {fillStyle:badgeInsideColor});

    var circles = [];
    var circleCount = 16;
    var circlesTl0 = new TimelineMax();

    for (var i = 0; i < circleCount; i++) {
        var x = Math.cos(TWO_PI * i / circleCount - HALF_PI) * 116 + center.x;
        var y = Math.sin(TWO_PI * i / circleCount - HALF_PI) * 116 + center.y;

        circles[i] = new Circle(new Point(x, y), 0, {fillStyle:badgeCirclesColor});
        circles[i].pivot.copy(center);

        circlesTl0.to(circles[i], 0.3, {radius:8, ease:Back.easeOut}, 0.05 * i);
    }
    var tl = new TimelineMax({onUpdate:function() {
        c.clearRect(0, 0, w, h);
        bgCircle.draw(c);
        fgCircle.draw(c);

        circles.forEach(function(crl){
            crl.draw(c);
        });
    }});

    tl.to(bgCircle, 0.3, {radius:128, ease:Cubic.easeIn});
    tl.to(fgCircle, 0.3, {radius:104, ease:Cubic.easeOut});
    tl.append(circlesTl0);

    return tl;
}

function drawRibbon() {
    var w = 384,
        h = 96;

    ribbonCanvas = createCanvas(64, 224, w, h);

    var c = ribbonCanvas.getContext('2d');

    var leftTopCorner = new Polygon([
        new Point(0, 64),
        new Point(32, 64),
        new Point(32, 64),
        new Point(0, 64)
    ], {
        fillStyle:ribbonColor
    });
    var leftBottomCorner = new Polygon([
        new Point(0, 96),
        new Point(32, 96),
        new Point(32, 96),
        new Point(0, 96)
    ], {
        fillStyle:ribbonColor
    });
    var rightTopCorner = new Polygon([
        new Point(352, 64),
        new Point(384, 64),
        new Point(384, 64),
        new Point(352, 64)
    ], {
        fillStyle:ribbonColor
    });
    var rightBottomCorner = new Polygon([
        new Point(352, 96),
        new Point(384, 96),
        new Point(384, 96),
        new Point(352, 96)
    ], {
        fillStyle:ribbonColor
    });
    var leftRect = new Rect(32, 32, 0, 64, {fillStyle:ribbonColor});
    var rightRect = new Rect(352, 32, 0, 64, {fillStyle:ribbonColor});
    var leftSlope = new Polygon([
        new Point(96, 32),
        new Point(96, 32),
        new Point(96, 96),
        new Point(96, 96)
    ], {
        fillStyle:ribbonBackColor
    });
    var rightSlope = new Polygon([
        new Point(288, 32),
        new Point(288, 32),
        new Point(288, 96),
        new Point(288, 96)
    ], {
        fillStyle:ribbonBackColor
    });
    var leftCenterRect = new Rect(64, 0, 0, 64, {fillStyle:ribbonColor});
    var rightCenterRect = new Rect(320, 0, 0, 64, {fillStyle:ribbonColor});
    var centerRect = new Rect(64, 0, 256, 64, {strokeStyle:textColor, lineWidth:0.01, globalAlpha:0});

    var tl = new TimelineMax({onUpdate:function() {
        c.clearRect(0, 0, w, h);
//        c.strokeRect(0, 0, w, h);

        leftTopCorner.draw(c);
        leftBottomCorner.draw(c);
        rightTopCorner.draw(c);
        rightBottomCorner.draw(c);
        leftRect.draw(c);
        rightRect.draw(c);
        leftSlope.draw(c);
        rightSlope.draw(c);
        leftCenterRect.draw(c);
        rightCenterRect.draw(c);
        centerRect.draw(c);
    }});

    var leftTl = new TimelineMax(),
        rightTl = new TimelineMax();

    leftTl.to([leftBottomCorner.verts[0], leftBottomCorner.verts[1]], 0.15, {y:'-=32', ease:Cubic.easeIn}, '+=1');
    leftTl.to([leftTopCorner.verts[0], leftTopCorner.verts[1]], 0.15, {y:'-=32', ease:Cubic.easeOut});
    leftTl.to([leftTopCorner.verts[3], leftBottomCorner.verts[0]], 0.3, {x:32, ease:Cubic.easeIn});
    leftTl.to(leftRect, 0.3, {w:64, ease:Cubic.easeIn});
    leftTl.to([leftSlope.verts[0], leftSlope.verts[3]], 0.3, {x:'-=32', y:'-=32', ease:Cubic.easeOut});
    leftTl.to(leftCenterRect, 0.6, {w:128, ease:Cubic.easeIn});

    rightTl.to([rightBottomCorner.verts[0], rightBottomCorner.verts[1]], 0.15, {y:'-=32', ease:Cubic.easeIn}, '+=1');
    rightTl.to([rightTopCorner.verts[0], rightTopCorner.verts[1]], 0.15, {y:'-=32', ease:Cubic.easeOut});
    rightTl.to([rightTopCorner.verts[2], rightBottomCorner.verts[1]], 0.3, {x:352, ease:Cubic.easeIn});
    rightTl.to(rightRect, 0.3, {w:64, x:288, ease:Cubic.easeIn});
    rightTl.to([rightSlope.verts[0], rightSlope.verts[3]], 0.3, {x:'+=32', y:'-=32', ease:Cubic.easeOut});
    rightTl.to(rightCenterRect, 0.6, {w:128, x:192, ease:Cubic.easeIn});

    tl.insert(leftTl);
    tl.insert(rightTl);

    tl.set(centerRect.style, {globalAlpha:1});
    tl.to(centerRect.style, 0.3, {lineWidth:8, ease:Quint.easeOut});
    tl.to(centerRect, 0.3, {x:68, y:4, w:248, h:56, ease:Quint.easeOut}, '-=0.3');

    return tl;
}

function showRibbonText() {
    ribbonTextWrapper = document.createElement('div');

    var text = document.createElement('div');

    ribbonTextWrapper.style.position = 'absolute';
    ribbonTextWrapper.style.left = 128 + 'px';
    ribbonTextWrapper.style.top = 224 + 'px';
    ribbonTextWrapper.style.width = 256 + 'px';
    ribbonTextWrapper.style.height  = 64 + 'px';
    ribbonTextWrapper.style.overflow = 'hidden';

    text.style.position = 'absolute';
    text.style.top = -54 + 'px';
    text.style.width = 256 + 'px';
    text.style.height  = 64 + 'px';
    text.style.textAlign = 'center';
    text.style.fontWeight = 800;
    text.style.fontSize = '48px';
    text.style.color = textColor;
    text.innerHTML = 'PENS!';

    ribbonTextWrapper.appendChild(text);
    container.appendChild(ribbonTextWrapper);

    return TweenMax.to(text, 0.5, {top:0, ease:Back.easeOut});
}

function showBadgeText() {
    var wrapper = document.createElement('div'),
        text = document.createElement('span');

    wrapper.style.position = 'absolute';
    wrapper.style.left = 128 + 'px';
    wrapper.style.top = 96 + 'px';
    wrapper.style.width = 256 + 'px';
    wrapper.style.height  = 128 + 'px';
    wrapper.style.overflow = 'hidden';

    text.style.position = 'absolute';
    text.style.top = 128 + 'px';
    text.style.width = 256 + 'px';
    text.style.height  = 128 + 'px';
    text.style.textAlign = 'center';
    text.style.fontWeight = 800;
    text.style.fontSize = '96px';
    text.style.color = textColor;
    text.innerHTML = '30';

    wrapper.appendChild(text);
    container.appendChild(wrapper);

    return TweenMax.to(text, 0.5, {top:0, ease:Back.easeOut});
}

function createCanvas(x, y, width, height) {
    var canvas = document.createElement('canvas');

    x = x || 0;
    y = y || 0;
    width = width || viewWidth;
    height = height || viewHeight;

    canvas.width = width;
    canvas.height = height;
    canvas.style.left = x + 'px';
    canvas.style.top = y + 'px';
    canvas.style.width = width + 'px';
    canvas.style.height = height + 'px';

    container.appendChild(canvas);

    return canvas;
}

function Circle(center, radius, style) {
    this.center = center;
    this.radius = radius;
    this.style = style;

    this.phiStart = 0;
    this.phiEnd = TWO_PI;
    this.ccw = false; // counter clock wise

    this.pivot = new Point();
    this.rotation = 0;
}
Circle.prototype = {
    draw:function(ctx) {
        ctx.save();

        applyStyle(ctx, this.style);

        ctx.translate(this.pivot.x, this.pivot.y);
        ctx.rotate(this.rotation);
        ctx.translate(this.center.x - this.pivot.x, this.center.y - this.pivot.y);

        ctx.beginPath();
        ctx.arc(0, 0, this.radius, this.phiStart, this.phiEnd, this.ccw);

        if (this.style.fillStyle) ctx.fill();
        if (this.style.strokeStyle) ctx.stroke();

        ctx.restore();
    }
};


function Polygon(verts, style) {
    this.verts = verts;
    this.style = style;
}
Polygon.prototype = {
    draw:function(ctx) {
        ctx.save();

        applyStyle(ctx, this.style);

        ctx.beginPath();

        this.verts.forEach(function(v) {
            ctx.lineTo(v.x, v.y);
        });

        ctx.closePath();

        if (this.style.fillStyle) ctx.fill();
        if (this.style.strokeStyle) ctx.stroke();

        ctx.restore();
    }
};

function Rect(x, y, w, h, style) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.style = style;
}
Rect.prototype = {
    draw:function(ctx) {
        ctx.save();

        applyStyle(ctx, this.style);

        if (this.style.fillStyle) ctx.fillRect(this.x, this.y, this.w, this.h);
        if (this.style.strokeStyle) ctx.strokeRect(this.x, this.y, this.w, this.h);

        ctx.restore();
    }
};

function Line(start, end, style) {
    this.start = start;
    this.end = end;
    this.style = style;
}
Line.prototype = {
    draw:function(ctx) {
        ctx.save();

        applyStyle(ctx, this.style);

        ctx.beginPath();
        ctx.moveTo(this.start.x, this.start.y);
        ctx.lineTo(this.end.x, this.end.y);
        ctx.stroke();

        ctx.restore();
    }
};

function Point(x, y) {
    this.x = x || 0;
    this.y = y || 0;
}
Point.prototype = {
    copy:function(p) {
        this.x = p.x;
        this.y = p.y;
        return this;
    },
    clone:function() {
        return new Point().copy(this);
    }
};

function applyStyle(c, s) {
    for (var p in s) {
        if (p === 'lineDash') {c.setLineDash(s[p])}
        else c[p] = s[p];
    }
}
            
          
!
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