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

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

The best way to debug slow web pages cover image

The best way to debug slow web pages

Tools like Page Speed Insights and Google Lighthouse are great for providing advice for front end performance issues. But what these tools canโ€™t do, is evaluate performance across your entire stack of distributed services and applications.

Watch video