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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <!DOCTYPE html>
html(lang="en")
    head
        meta(charset="UTF-8")
        meta(name="viewport", content="width=device-width, initial-scale=1.0")
        meta(http-equiv="X-UA-Compatible", content="ie=edge")
        title HTML Canvas
    body
        canvas
    script(src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.6/dat.gui.min.js")
    script(src="./canvas.js")
    
              
            
!

CSS

              
                
              
            
!

JS

              
                // Utilities
const randomNum = (min, max) => Math.floor(Math.random() * (max - min + 1) + min)
const randomColor = colors => colors[Math.floor(Math.random() * colors.length)];

const colors = [
    '#9C4AFF',
    '#8C43E6',
    '#7638C2',
    '#5E2C99',
    '#492378'
]

//Setup
const canvas = document.querySelector('canvas')
const c = canvas.getContext('2d')

canvas.width = innerWidth
canvas.height = innerHeight

addEventListener('resize', () => {
    canvas.width = innerWidth
    canvas.height = innerHeight

    init()
})

//Controls 
const gui = new dat.GUI()

const controls = {
    count: -10,
    velocity: 5
}

gui.add(controls, 'count', -20, 0)
gui.add(controls, 'velocity', 0, 50)

//Drops
class Drop {
    constructor(x, y, dy, thickness, length, color) {
        this.x = x
        this.y = y
        this.dy = dy
        this.thickness = thickness
        this.color = color
        this.length = length
        this.gravity = .4
    }
}

Drop.prototype.draw = function () {
    c.beginPath()
    c.strokeStyle = this.color
    c.lineWidth = this.thickness
    c.moveTo(this.x, this.y)
    c.lineTo(this.x, this.y - this.length)
    c.stroke()
    c.closePath()
}

Drop.prototype.update = function () {
    if (this.dy > 0) this.dy += this.gravity
    this.y += this.dy

    if (this.y > canvas.height - 100) this.splash(this.x, this.y + (this.length * 2))

    this.draw()
}

Drop.prototype.splash = function (x, y) {
    for (let i = 0; i < 5; i++) {
        const dx = randomNum(-3, 3)
        const dy = randomNum(-1, -5)
        const radius = randomNum(1, 3)

        droplets.push(new Droplet(x, y, dx, dy, radius, randomColor(colors)))
    }
}

// Droplets
class Droplet {
    constructor(x, y, dx, dy, radius, color) {
        this.x = x
        this.y = y
        this.dx = dx
        this.dy = dy
        this.radius = radius
        this.color = color
        this.gravity = .1
    }
}

Droplet.prototype.draw = function () {
    c.beginPath()
    c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false)
    c.fillStyle = this.color
    c.fill()
    c.closePath()
}

Droplet.prototype.update = function () {
    this.dy += this.gravity
    this.y += this.dy * this.friction
    this.x += this.dx

    this.draw()
}

let droplets = []
let drops = []
let ticker = 0
const init = () => {
    drops = []
    droplets = []
}

const animate = () => {
    requestAnimationFrame(animate)
    c.fillStyle = 'rgba(33, 33, 33, .3)'; //Lower opacity creates a longer tail
    c.fillRect(0, 0, canvas.width, canvas.height);

    drops.forEach((drop, index) => {
        drop.update()
        if (drop.y >= canvas.height + drop.length) drops.splice(index, 1)
    })

    // Timing between drops
    ticker++
    let count = controls.count === 0 ? 0 : randomNum(controls.count + 5, controls.count)
    if (ticker % count == 0) {
        const x = randomNum(0, innerWidth)
        const y = 0
        const dy = controls.velocity === 0 ? 0 : randomNum(controls.velocity, controls.velocity + 10)
        const thickness = randomNum(3, 5)
        const length = randomNum(20, 50)

        drops.push(new Drop(x, y, dy, thickness, length, randomColor(colors)))
    }

    droplets.forEach((droplet, index) => {
        droplet.update()
        if (droplet.y >= canvas.height) droplets.splice(index, 1)
    })
}

init()
animate()
              
            
!
999px

Console