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.

            
              <canvas id="cv">
  Your Browser does not support canvas.
</canvas>
            
          
!
            
              body{
  margin: 0;
  font-family: sans-serif;
}
            
          
!
            
              // -----------------
// 基本設定
// -----------------
var W = window.innerWidth;
var H = window.innerHeight;
var cv = null;
var bubbleNum = 100; // bubbleの数
var maxBubbleRadius = 50; // bubbleのサイズ
var colorCodeNum = 1; // カラーコード番号

// -----------------
// 自作関数
// -----------------
// ランダムで-1か1を返す
function randomSign(){
  return Math.round( Math.random());
}
// カラーコードを返す
function colorCode(colorCodeNum, num){
  var colorCode1 = [
    "#394a51", // color1: bg-color
    "#7fa99b", // color2
    "#fdc57b", // color3
    "#fbf2d5" // color4
  ];
  var colorCode2 = [
    "#434343",// color1: bg-color
    "#a64452",// color2
    "#f7c873",// color3
    "#f8f8f8" // color4
  ];
  if(colorCodeNum == 1)
    return colorCode1[num];
  else if(colorCodeNum == 2)
    return colorCode2[num];
}
// ランダムなカラーコードを返す
function randomColor(colorCodeNum){
  return colorCode(colorCodeNum, Math.round(Math.random()*2)+1);
}

// -----------------
// main
// -----------------
function canvasSupport(){
  return ( cv && cv.getContext );
}

// 起動時に1度だけ行う処理
function init(){
  cv = document.getElementById("cv");
  if( !canvasSupport() ) return; // サポートしてないならここでreturn
  cv.width = W;
  cv.height = H;
  var context = cv.getContext("2d");
  main(context);
}

function main(context){
  var bubbles = [];
  for( var i = 0; i < bubbleNum; i++ ) {
    var bb = new bubble();
    bubbles.push(bb);
  }

  draw();
  setInterval(draw,24);

  function draw() {
    context.fillStyle = colorCode(colorCodeNum, 0);
    context.fillRect(0,0,W,H);
    
    for( var i = 0; i < bubbles.length; i++ ) {
      var grad = context.createRadialGradient(bubbles[i].x, bubbles[i].y, 0, bubbles[i].x, bubbles[i].y, bubbles[i].radius);
      grad.addColorStop(0, bubbles[i].color);
      context.beginPath();
      context.fillStyle = grad;
      
      context.arc(bubbles[i].x, bubbles[i].y, bubbles[i].radius, 0, Math.PI * 2 ,false);
      
      context.fill();
      bubbles[i].move();
    }
  }
}

function bubble() {
  this.x = Math.random() * W;
  this.y = Math.random() * H;
  this.vx = 0;
  // this.vx = Math.random() * 5 + 1*randomSign();
  this.vy = Math.random() * 5 * -1;
  this.radius = Math.random() * maxBubbleRadius;
  this.color = randomColor(colorCodeNum);
}

bubble.prototype.move = function() {
  this.x += this.vx;
  this.y += this.vy;

  if( this.y < -maxBubbleRadius*2 ) this.y = H + maxBubbleRadius*2;
}

window.onload = function() {
  init();
}
            
          
!
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