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

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

              
                <div id="sky-top"></div>
<div id="sky-bottom"></div>
<canvas id="canvas"></canvas>

<!-- canvas fullscreen -->
<script>
  const height = document.body.offsetHeight;
  const width = document.body.offsetWidth;
  const cvs = document.getElementById('canvas');
  cvs.setAttribute("height", height);
  cvs.setAttribute("width", width);
</script>
              
            
!

CSS

              
                html,
body {
  height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  margin: 0;
  padding: 0;
  
}

body > canvas {
  border: 1px solid black;
  background-color: transparent;
  z-index: 10;
}

body > #sky-top {
  height: 100% !important;
  width: 100% !important;
  background-color: rgb(46, 46, 46);
  position: absolute;
  z-index: 1;
  animation: lightning 20s ease-in-out infinite;
}

@keyframes lightning {
  0% {
    background-color: rgb(46, 46, 46);
  }
  6.25% {
    background-color: rgb(46, 46, 46);
  }
  8% {
    background-color: rgb(255, 255, 255);
  }
  9% {
    background-color: rgb(46, 46, 46);
  }
  11% {
    background-color: rgb(255, 255, 255);
  }
  30% {
    background-color: rgb(46, 46, 46);
  }
  100%{
    background-color: rgb(46, 46, 46);
  }
}

body > #sky-bottom {
  height: 100% !important;
  width: 100% !important;
  position: absolute;
  z-index: 2;
  background: linear-gradient(rgba(255, 255, 255, 0), rgb(120, 140, 155));
}
              
            
!

JS

              
                const canvas = document.getElementById("canvas");
const context = canvas.getContext('2d');
const canvasHeight = canvas.height;
const canvasWidth = canvas.width;

const clearCanvas = function(x, y, height, width) {
  rectX = x || 0;
  rectY = y || 0;
  rectHeight = height || canvasHeight;
  rectWidth = width || canvasWidth;
  context.clearRect(rectX, rectY, rectWidth, rectHeight);
  context.beginPath();
}

const circle = function(x, y, radius, filled) {
  const offset = radius / 2;
  x = x - offset;
  y = y - offset;
  context.beginPath();
  context.arc(x, y, radius, 0, 2 * Math.PI);
  if (filled) {
    context.stroke();
  }
  context.strokeStyle = '#fff';
  context.closePath();
}

const createVector = function(x, y) { return { x, y } }

const vectorAddition = function(vectorA, vectorB) {
  if (typeof vectorB === 'number') {
    return { x: vectorA.x + vectorB, y: vectorA.y + vectorB };
  }
  return { x: vectorA.x + vectorB.x, y: vectorA.y + vectorB.y };
}

const vectorSubtraction = function(vectorA, vectorB) {
  if (typeof vectorB === 'number') {
    return { x: vectorA.x - vectorB, y: vectorA.y - vectorB };
  }
  return { x: vectorA.x - vectorB.x, y: vectorA.y - vectorB.y };
}

const vectorMultiplication = function(vectorA, vectorB) {
  if (typeof vectorB === 'number') {
    return { x: vectorA.x * vectorB, y: vectorA.y * vectorB };
  }
  return { x: vectorA.x * vectorB.x, y: vectorA.y * vectorB.y };
}

const vectorDivision = function(vectorA, vectorB) {
  if (typeof vectorB === 'number') {
    return { x: vectorA.x / vectorB, y: vectorA.y / vectorB };
  }
  return { x: vectorA.x / vectorB.x, y: vectorA.y / vectorB.y };
}

const getRandomFloat = function(min, max) {
  const random = Math.random() * (max - min + 1) + min;
  return random;
}

const getRandomInteger = function(min, max) {
  return Math.floor(getRandomFloat(min, max)); 
}

const checkRaindropCollision = function(location, radius) {
  let rain = { collided: false, location: null }
  if ((location.y - canvasHeight) >= radius) {
    rain.collided = true;
    rain.location = createVector(getRandomInteger(radius, canvasWidth-radius), radius - 10)
  } else if ((location.x + radius) <= 0) {
    rain.collided = true;
    rain.location = createVector(canvasWidth - radius, location.y)
  } else if ((location.x + radius) >= canvasWidth) {
    rain.collided = true;
    rain.location = createVector(radius, location.y)
  } else {
    rain.collided = false;
    rain.location = null; 
  }
  return rain;
}

const raintype = {
  drizzle: { count: 30, speed: 0.27 },
  light: { count: 100, speed: 0.3 },
  medium: { count: 250, speed: 0.4 },
  downpour: { count: 500, speed: 0.5 },
  afteshower: { count: 3, speed: 0.4 }
}

var environment = {
  wind: createVector(-0.05, 0),
  raintype: raintype.medium,
}

class RainParticle {
  constructor(x, accX, accY){
    this.damping = 0.025;
    this.location = createVector(x, canvasHeight); 
    this.radius = 0.4;
    this.velocity = createVector(0, 0);
    this.acceleration = createVector(accX, -(accY * this.damping));
    this.mass = 1;
  }

  draw(particles, index) {
    const { x, y } = this.location;
    if (this.acceleration.y >= 0.3) {
      delete particles[index];
      return particles.splice(index, 1)
    }
    return circle(x, y, this.radius, true);
  }

  splash() {
    this.velocity = vectorAddition(this.velocity, this.acceleration);
    this.location = vectorAddition(this.location, this.velocity);
    this.acceleration = vectorAddition(this.acceleration, { x: 0, y: 0.12 });
  }
}

class Raindrop {
  constructor(x, y, radius, accY){
    this.location = createVector(x, y); 
    this.radius = radius;
    this.velocity = createVector(0, 0);
    this.acceleration = createVector(0, accY);
    this.mass = 1;

    this.wind = environment.wind;
    this.acceleration = vectorAddition(this.acceleration, this.wind);
  }

  draw() {
    const { x, y } = this.location;
    return circle(x, y, this.radius, true);
  }

  fall() { 
    if (this.velocity.y <= (environment.raintype.speed * 50)) {
      this.velocity = vectorAddition(this.velocity, this.acceleration);
    }
    this.location = vectorAddition(this.location, this.velocity);
  }

  relive(raindrop) {
    const { location } = raindrop;
    this.location = createVector(location.x, location.y); 
    this.velocity = createVector(0, 0);
  }
}

const particleX = [-0.12, 0.06, 0, 0.06, 0.12];
const getParticleX = function() {
  const index = Math.floor(Math.random() * particleX.length);
  return particleX[index];
}

// init all objects here
let raindrop = [];
let particles = [];
const raindropCount = environment.raintype.count;

for (let i = 0 ; i < raindropCount ; i++) {
  let x = getRandomInteger(2, canvasWidth - 2);
  let y = getRandomInteger(-2000 , 0);
  // let accY = getRandomFloat(1, 5) * 0.05;
  let accY = environment.raintype.speed;
  raindrop[i] = new Raindrop(x, y, 1.3, accY);
}

// initiate all draw functions here
const setup = function() {
  for (let i = 0 ; i < raindropCount ; i++) {
    raindrop[i].draw();
  }
}

// continuous animation loop
const animate = function() {
  clearCanvas(); // don't remove this

  for (let i = 0 ; i < raindropCount ; i++) {
    let { location, radius, velocity } = raindrop[i];
    let rain = checkRaindropCollision(location, radius);
    if (rain.collided) {
      let particle1 = new RainParticle(location.x, getParticleX(), velocity.y);
      particles.push(particle1);
      let particle4 = new RainParticle(location.x, getParticleX(), velocity.y);
      particles.push(particle4);

      raindrop[i].relive(rain);
    }
    raindrop[i].fall();
    raindrop[i].draw();
  }

  for (let i = 0; i < particles.length; i ++) {
    particles[i].splash();
    particles[i].draw(particles, i);
  }
  requestAnimationFrame(animate);
}

// animation initiate
setup();
requestAnimationFrame(animate);
              
            
!
999px

Console