Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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.

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

              
                <h1>P5.js Bubbles</h1>
              
            
!

CSS

              
                canvas{
	position: fixed;
	top:0px;
	left:0px;
	z-index: 0;
}

h1{
  position: relative;
		z-index: 5;
		color: #fff;
		text-align: center;
		padding-top: 100px;
		font-family: 'Bai Jamjuree', sans-serif;
}
              
            
!

JS

              
                var bubbles = [];//holds bubble objects
var bubbleNum = 20;//# of bubbles?
var xVariation = 2;//how much the bubbles move in the x direction

//setup the canvas based on the window size
function setup() {
  createCanvas(window.innerWidth, window.innerHeight);
  noStroke();
  //frameRate(10);
  //make random bubble objects {x,y,size,speed}
  for (var i = 0; i < bubbleNum; i++) {
    bubbles[i] = {
      x: random(window.innerWidth),
      y: random(window.innerHeight),
      size: random(3, 10),
      speed: 0
    };
  }
  //set color of the bubbles 'feel free to play with opacity, I think .4 looks nice'
  fill("rgba(255,255,255, 0.4)");
}
//handle window resize events and fix the canvas
function windowResized() {
  resizeCanvas(windowWidth, windowHeight);
}
//animation loop
function draw() {
  background("#164899");
  bubbles.forEach(function(bub, index) {
    moveBubbles(bub);
    if (bub.y < -10) newBubble(index);
  });
}
//makes a new bubble at a specified index once its off screen
function newBubble(index) {
  bubbles[index] = {
    x: random(window.innerWidth),
    y: window.innerHeight,
    size: random(3, 10),
    speed: 0
  };
}

//move the bubbles based on their size
function moveBubbles(bubble) {
  bubble.x += random(-xVariation, xVariation);
  bubble.speed += bubble.size / 100;
  bubble.y -= bubble.speed;
  //draw the bubble NOTE they must be integer values to draw to the canvas
  ellipse(
    Math.round(bubble.x),
    Math.round(bubble.y),
    Math.round(bubble.size),
    Math.round(bubble.size)
  );
}

              
            
!
999px

Console