bouncing dvd standby animation in javascript

To create a bouncing DVD standby animation in JavaScript, you can follow these steps:

  1. Create an HTML element for the DVD or any other image you want to use for the animation.
index.tsx
<div id="dvd-logo"></div>
26 chars
2 lines
  1. Use CSS to style the element.
index.tsx
#dvd-logo {
    width: 100px;
    height: 80px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
141 chars
9 lines
  1. Define a function to animate the element.
index.tsx
function animate() {
    var dvd = document.getElementById('dvd-logo');

    // Set the initial position and velocity of the element
    var x = 0;
    var y = 0;
    var vx = 5;
    var vy = 5;

    // Define the boundaries of the animation
    var maxX = window.innerWidth - dvd.offsetWidth;
    var maxY = window.innerHeight - dvd.offsetHeight;

    // Animate the element
    setInterval(function() {
        x += vx;
        y += vy;

        if (x < 0 || x > maxX) {
            vx = -vx;
        }

        if (y < 0 || y > maxY) {
            vy = -vy;
        }

        dvd.style.transform = 'translate(' + x + 'px, ' + y + 'px)';
    }, 10);
}
655 chars
30 lines
  1. Call the animate function when the page loads.
index.tsx
window.onload = function() {
    animate();
};
47 chars
4 lines

Now you have a bouncing DVD standby animation in JavaScript!

gistlibby LogSnag