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

              
                <!-- Bubbles made of SVG Gradients and SVG Masks just as experiment : ) 

How bubble was made: https://codepen.io/yoksel/full/BzkyBJ
-->

<div class="demo">
  <div class="demo__content">
    
    <svg class="svg svg--defs">  
        
        <!-- Bubble transparency -->
        <radialGradient id="grad--bw"
                        fx="25%" fy="25%">
            <stop offset="0%" 
                  stop-color="black"/>  
            <stop offset="30%" 
                  stop-color="black" 
                  stop-opacity=".2"/>
            <stop offset="97%" 
                  stop-color="white" 
                  stop-opacity=".4"/>
            <stop offset="100%" 
                  stop-color="black"/>
        </radialGradient>
        
        <mask id="mask" maskContentUnits="objectBoundingBox">
            <rect fill="url(#grad--bw)"
              width="1" height="1"></rect>
        </mask>
        
        <!-- Light spot -->
        <radialGradient id="grad--spot"
                        fx="50%" fy="20%">
            <stop offset="10%" 
                  stop-color="white"
                  stop-opacity=".7"/>  
            <stop offset="70%" 
                  stop-color="white"
                  stop-opacity="0"/>
        </radialGradient>
        
        <!-- Top & bottom light -->
        <radialGradient id="grad--bw-light"
                        _fx="25%" fy="10%">
            <stop offset="60%" 
                  stop-color="black" 
                  stop-opacity="0"/>
            <stop offset="90%" 
                  stop-color="white" 
                  stop-opacity=".25"/>
            <stop offset="100%" 
                  stop-color="black"/>
        </radialGradient>
        
        <mask id="mask--light-top" maskContentUnits="objectBoundingBox">
            <rect fill="url(#grad--bw-light)"
              width="1" height="1" transform="rotate(180, .5, .5)"></rect>
        </mask>
        
        <mask id="mask--light-bottom" maskContentUnits="objectBoundingBox">
            <rect fill="url(#grad--bw-light)"
              width="1" height="1"></rect>
        </mask>
        
        <!-- Colors of bubble -->
        <linearGradient id="grad"
              x1="0" y1="100%" x2="100%" y2="0">
            <stop offset="0%" stop-color="dodgerblue" 
                  class="stop-1"/>   
            <stop offset="50%" stop-color="fuchsia"
                  class="stop-2"/>
            <stop offset="100%" stop-color="yellow" 
                  class="stop-3"/>
        </linearGradient> 
        
        <mask id="mask--collapse" maskContentUnits="objectBoundingBox">
            <circle r=".5" cx=".5" cy=".5"
                    class="collapse-circle"
                    ></circle>
        </mask>
        
        <symbol id="splash">
            <g class="splash__group" 
               fill="none"
               stroke="white" stroke-opacity=".8">
                <circle r="49%" 
                    cx="50%" cy="50%"
                    stroke-width="3%"  
                    stroke-dasharray="1% 10%"  
                    class="splash__circle _hidden"
                    ></circle>
                <circle r="44%" 
                    cx="50%" cy="50%"
                    stroke-width="2%"
                    stroke-dasharray="1% 5%"                     class="splash__circle _hidden"
                    ></circle>
                <circle r="39%" 
                    cx="50%" cy="50%"
                    stroke-width="1%"  
                    stroke-dasharray="1% 8%"  
                    class="splash__circle _hidden"
                    ></circle>
                <circle r="33%" 
                    cx="50%" cy="50%"
                    stroke-width="3%"  
                    stroke-dasharray="1% 6%"  
                    class="splash__circle _hidden"
                    ></circle>
                <circle r="26%" 
                    cx="50%" cy="50%"
                    stroke-width="1%"  
                    stroke-dasharray="1% 7%"  
                    class="splash__circle _hidden"
                    ></circle>
                <circle r="18%" 
                    cx="50%" cy="50%"
                    stroke-width="1%"  
                    stroke-dasharray="1% 8%"  
                    class="splash__circle _hidden"
                    ></circle>
            </g>
        </symbol>
    </svg>
    
    <div class="demo__defs hidden">
        <svg class="svg bubble" viewBox="0 0 200 200">
            <g class="bubble__group">
              <ellipse rx="20%" ry="10%"
                   cx="150" cy="150"
                   fill="url(#grad--spot)"
                   transform="rotate(-225, 150, 150)"
                   class="shape _hidden"    
                   ></ellipse>     
              <circle r="50%" 
                    cx="50%" cy="50%"
                    fill="aqua"
                    mask="url(#mask--light-bottom)"
                    class="shape _hidden"
                    ></circle>
              <circle r="50%" 
                    cx="50%" cy="50%"
                    fill="yellow"
                    mask="url(#mask--light-top)"
                    class="shape _hidden"
                    ></circle>             
              <ellipse rx="55" ry="25"
                   cx="55" cy="55"
                   fill="url(#grad--spot)"
                   transform="rotate(-45, 55, 55)"
                   class="shape _hidden"    
                   ></ellipse> 
              <circle r="50%" 
                    cx="50%" cy="50%"
                    fill="url(#grad)"
                    mask="url(#mask)"
                    class="shape _hidden"
                    ></circle> 
            </g>
            
            <use xlink:href="#splash" class="bubble__splash"/>
                
            </g>
        </svg>
    </div>  
    
    <header class="demo__title demo-title">
        <svg class="svg demo-title__splash" viewBox="0 0 200 200">
            <use xlink:href="#splash"/>
        </svg>
        
        <h1 class="demo-title__content">
        Bubbles
            <span class="demo__tip">Click bubble to burst it!</span>
        </h1>
    </header> 
  </div>

</div>

              
            
!

CSS

              
                @import 'https://fonts.googleapis.com/css?family=Mouse+Memoirs';

HTML, BODY {
    width: 100vw;
    height: 100vh;
}

BODY {
    background-color: lightyellow;
    background: radial-gradient(lightyellow, gold, teal, indigo);
}

.hidden {
    display: none;
}

.marked {
    outline: 2px solid red;
    stroke: red;
    stroke-width: 2;
}

.svg {
    position: absolute;
    width: 250px;
    height: 250px; 
    overflow: visible;
    mix-blend-mode: multiply;
}

.svg--defs {
    width: 0;
    height: 0;
}

.demo {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    overflow: hidden;
    isolation: isolate;
}

.demo__title {
    position: absolute;
    // z-index: 1;
    right: 1rem;
    bottom: 1rem;
    text-shadow: 0 0 15px rgba(0,0,0,.5);
    font-family: 'Mouse Memoirs', sans-serif;
    font-size: 4.9rem;
    color: #FFF;    
    cursor: pointer;
}

.demo-title__splash {
    position: absolute;
    top: -75px;
    left: -40px;
    opacity: 0;
    pointer-events: none;
}

.demo__tip {
    display: block;
    font-size: 1rem;
    font-family: Trebuchet MS;
}

.bubble {
    cursor: pointer;
}

.bubble__splash {
    opacity: 0;
    pointer-events: none;
}


              
            
!

JS

              
                'use strict';

console.clear();
var maxBubbles = 25; //25
var container = document.querySelector('.demo');
var containerWidth = container.clientWidth;
var containerHeight = container.clientHeight;
var content = document.querySelector('.demo__content');
var title = document.querySelector('.demo__title');
title.content = document.querySelector('.demo-title__content');
title.splash = document.querySelector('.demo-title__splash');

var shape = document.querySelector('.bubble');
var shapeWidth = shape.clientWidth;
var shapeHeight = shape.clientHeight;

var bubbles = [];

var minX = 0;
var minY = 0;

var baseShapeSize = 200;
var minShapeSize = 50;

var time = 7;
var minTime = 4;

var posibleSides = ['top', 'right', 'bottom', 'left'];

//------------------------------

function Bubble( pos ) {
    this.bubble = shape.cloneNode( true ); 
    this.setSize();
    this.setPos();
    this.addAnimation();
    content.appendChild( this.bubble );
    this.content = this.bubble.querySelector('.bubble__group');
    this.splash = this.bubble.querySelector('.bubble__splash');
    this.isCollapsed = false;
    var that = this;
    
    this.bubble.onclick = function () {
        if ( !that.isCollapsed ) {
            that.isCollapsed = true;
            that.collapse();
        }
    }
}

//------------------------------

Bubble.prototype.collapse = function () {
    var that = this;
    
    function resetBubble() {
        var tl = new TimelineLite();
        that.setSize();
        that.setPos();

        tl.to( that.content, .3, {
            'scale': 1,
            'opacity': 1,
            'delay': 2,
            'onComplete': function() { 
                that.isCollapsed = false; 
                }
        } );
    }
    
    var tl = new TimelineLite();
    tl.set( this.content, {
            'scale': 0,
            'transform-origin': '100px 100px',
            'opacity': 0
        } );
    tl.set( this.splash, {
        'scale': .5,
        'transform-origin': '100px 100px',
        'opacity': 1,
    } );
    tl.to( this.splash, .15, {
        'scale': 1.5,
        'opacity': 0,
        'ease': Power1.easeOut,
        'onComplete': resetBubble
    } );
}

//------------------------------

Bubble.prototype.setPos = function () {
    var target = this.getSide();
    this.bubble.style.transform = 'translate3d(' + target.coords.x +'px, ' + target.coords.y + 'px, 0)';
}

//------------------------------

Bubble.prototype.setSize = function () {
    this.shapeSize = Math.round( Math.random() * (baseShapeSize - minShapeSize) ) + minShapeSize;
    this.bubble.style.width = this.shapeSize + 'px';   
    this.bubble.style.height = this.shapeSize + 'px';   
    
    this.maxX = containerWidth - this.shapeSize;
    this.maxY = containerHeight - this.shapeSize;
}

//------------------------------

Bubble.prototype.addAnimation = function () {
    
    var minX = 0;
    var newTime = Math.random() * time + minTime;
    var elem = this.bubble;
    var delay = Math.random() * time;
    var tl = new TimelineLite();
    var that = this;
    
    animate();
    
    function animate () {
        var target = that.getSide( that.side );
        that.side = target.side;
        var propSet = { x: target.coords.x,
                        y: target.coords.y,
                        ease: SlowMo.easeInOut,
                        delay: delay,
                        onComplete: animate
                    };        
        tl.to( elem, newTime, propSet);
        
        if ( delay ) {
            delay = 0;
        }
    }   
}

//------------------------------

Bubble.prototype.getSide = function () {
    var targetParams = {
        side: '',
        coords: {}
    };
    var maxRandX = Math.round( Math.random() * this.maxX );
    var maxRandY = Math.round( Math.random() * this.maxY );
    
    var sides = {'top': 
                    { x: maxRandX, 
                      y: minY },
                 'right': 
                    { x: this.maxX, 
                      y: maxRandY },
                 'bottom': 
                    { x: maxRandX, 
                      y: this.maxY },
                 'left': { 
                     x: minX, 
                     y: maxRandY }
                };
        
    delete sides[ this.side ];
    var keys = Object.keys( sides );    
    var randPos = Math.floor( Math.random() * keys.length );
    var newSide = keys[ randPos ];    
    
    targetParams.side = newSide;
    targetParams.coords = sides[ newSide ];
    
    return targetParams;
    
}

//------------------------------

function addBubble () {
    var bubble = new Bubble( i );
    bubbles.push( bubble );
}

//------------------------------

for ( var i = 0; i < maxBubbles; i ++ ) {
    addBubble();
}

//------------------------------

window.onresize = function () {
    containerWidth = container.clientWidth;
    containerHeight = container.clientHeight;
    
    bubbles.forEach( function ( item ) {
        item.maxX = containerWidth - item.shapeSize;
        item.maxY = containerHeight - item.shapeSize;
        // item.addAnimation();
    });
}

//------------------------------

title.onclick = function () {
    var that = this;
    
    function resetElem() {
        var tl = new TimelineLite();
        
        tl.to( that.content, .3, {
            'scale': 1,
            'opacity': 1,
            'onComplete': function() { 
                that.isCollapsed = false; 
                }
        } );
    }
    
    var tl = new TimelineLite();
    tl.set( this.content, {
            'scale': 0,
            'opacity': 0
        } );
    tl.set( this.splash, {
        'scale': .5,
        'opacity': 1,
    } );
    tl.to( this.splash, .15, {
        'scale': 1.5,
        'opacity': 0,
        'ease': Power1.easeOut,
        'onComplete': resetElem
    } );
}
              
            
!
999px

Console