cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              <!-- 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>

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

HTML, BODY {
    height: 100%;
}

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;
}


            
          
!
            
              '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
🕑 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