DEV Community

Kevin Ramirez
Kevin Ramirez

Posted on

Waving Lines of Code: Creating Hypnotic Canvas Animations with JavaScript ๐ŸŒŠ

Dancing Wave GIF


(A little preview of what we're creating - but way more hypnotic!)


Have you ever wanted to make your website background say "ooooh, fancy!" without actually saying it? Let's create some smooth, undulating line waves using JavaScript's Canvas API. It's easier than you think, and the result looks like digital seaweed dancing in the virtual ocean!

The Magic Ingredients ๐Ÿง™

We'll need just three basic components:

  1. A <canvas> element as our digital sketchpad
  2. Some wave configuration magic
  3. An animation loop to make it all move
// Let's start by creating our canvas
const canvas = document.createElement('canvas');
canvas.classList = 'wave';
const c = canvas.getContext('2d');
document.body.append(canvas);

// Set dimensions to match viewport
canvas.width = 130;
canvas.height = innerHeight * 2;
Enter fullscreen mode Exit fullscreen mode

Wait, what?

("Wait, why 130px width?" - Because we want vertical waves!)

Wave Configuration ๐ŸŒŠ

Here's where the fun begins. We create multiple waves with random properties:

const waves = Array.from({ length: 4 }, () => ({
  x: 65, 
  length: Math.random() * 0.002 + 0.005,
  amplitude: Math.random() * 15 + 20,
  frequency: Math.random() * 0.02 + 0.005,
}));
Enter fullscreen mode Exit fullscreen mode

Let's break down these wave properties:

  • x: center position (65 = middle of 130px canvas)
  • length: how squiggly the wave is
  • amplitude: wave height
  • frequency: how fast it undulates

The Animation Loop ๐Ÿ”„

This is where we bring our waves to life:

function animate() {
  requestAnimationFrame(animate);
  c.clearRect(0, 0, canvas.width, canvas.height);

  waves.forEach((wave, index) => {
    // Drawing magic happens here
    c.beginPath();
    c.moveTo(wave.x, 0);

    // Create wave path
    for (let y = 0; y < canvas.height; y++) {
      c.lineTo(
        wave.x + Math.sin(y * wave.length + increments[index]) * wave.amplitude,
        y
      );
    }

    c.stroke();
    increments[index] += wave.frequency;
  });
}
Enter fullscreen mode Exit fullscreen mode

Wave a minute


(Our code making waves (literally))

See It in Action ๐ŸŽฌ

Check out this CodePen demo to see the final result. Feel free to play with the values!

Level Up! ๐Ÿš€

Want to make it even cooler? Try:

  1. Adding mouse interaction
  2. Changing colors dynamically
  3. Creating horizontal waves
  4. Adding gradient effects

Cat down

(You after customizing these waves)

Remember: The best way to learn is to break things! Try changing values randomly and see what happens.

Happy coding! ๐ŸŒŠ๐Ÿ’ป

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)