How to Make a Bouncing DVD Screensaver With HTML, CSS and JavaScript

Updated · 6 min read · By

The bouncing DVD logo is one of the best first animations to build. It is one HTML file and about twenty lines of JavaScript. You do not need to install anything or know how to code. Copy the file below, save it, and open it.

Code for a bouncing DVD screensaver: the position changes each frame and the direction flips at the edges
See it working: our DVD Screensaver page runs the finished effect with a corner-hit counter.

The complete code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Bouncing DVD Screensaver</title>
  <style>
    body { margin: 0; height: 100vh; background: #000; overflow: hidden; }
    #logo {
      position: absolute; left: 0; top: 0;
      font: bold 60px Arial, sans-serif;
      color: #00ff66; padding: 0 12px;
    }
  </style>
</head>
<body>
  <div id="logo">DVD</div>

  <script>
    const logo = document.getElementById("logo");
    const colors = ["#ff3344", "#ffb000", "#00ff66", "#33ccff", "#ff4dd2", "#ffffff"];

    let x = 50, y = 50;   // where the logo is
    let dx = 2, dy = 2;   // how far it moves each frame
    let colorIndex = 0;

    function changeColor() {
      colorIndex = (colorIndex + 1) % colors.length;
      logo.style.color = colors[colorIndex];
    }

    function move() {
      const maxX = window.innerWidth - logo.offsetWidth;
      const maxY = window.innerHeight - logo.offsetHeight;

      x += dx;
      y += dy;

      if (x <= 0 || x >= maxX) { dx = -dx; x = Math.max(0, Math.min(x, maxX)); changeColor(); }
      if (y <= 0 || y >= maxY) { dy = -dy; y = Math.max(0, Math.min(y, maxY)); changeColor(); }

      logo.style.transform = "translate(" + x + "px, " + y + "px)";
      requestAnimationFrame(move);
    }

    move();
  </script>
</body>
</html>

How to use this code

  1. Get the file. Press Download above the code to save it as dvd-screensaver.html. Or press Copy code, paste it into a plain text editor (Notepad on Windows, TextEdit in plain-text mode on a Mac, or a code editor such as VS Code) and save it as dvd-screensaver.html. The name must end in .html, not .txt.
  2. Open it. Double-click the file. It opens in your web browser and starts working straight away.
  3. Change it. Open the same file in the text editor, edit something, save, and refresh the browser page to see the result.

Nothing needs to be installed, and the file works without an internet connection.

How the code works

The file has three parts: HTML for what is on the page, CSS for how it looks, and JavaScript for the movement.

The HTML

There is only one visible element: <div id="logo">DVD</div>. The id is a name, so the CSS and JavaScript can find it.

The CSS

The JavaScript

Easy changes to try

To changeEdit this
The textReplace DVD between the div tags with your name
The speedChange dx = 2, dy = 2 to 4 for faster, 1 for slower
The sizeChange 60px in the font line
The coloursAdd or remove colour codes in the colors list
The backgroundChange #000 to another colour code

Use a picture instead of text

Put an image file in the same folder and replace the div with <img id="logo" src="logo.png" width="200">. The movement code works without changes. The colour change only affects text, so it will have no visible effect on a picture.

Count corner hits

A corner hit is when both edges are reached in the same frame. Inside move, after the two if lines, you can test whether both bounces happened and add one to a counter. Our live version does this.

FAQ

Can I download the bouncing DVD screensaver code?

Yes. Use the Download button above the code to save it as an HTML file, or copy it and save it yourself. It is free to use and change.

Will the logo ever hit the corner?

It depends on the window and logo size. Some sizes produce regular corner hits and others never do. Resize the window to change the path.

How do I make it full screen?

Press F11 in the browser, or Control+Command+F on a Mac.

More tools

See all tools

Keep reading