Building An Interactive HTML5 Canvas Application

Quantum
Quest
Algorithms, Math, and Physics

Building an interactive HTML5 canvas application

When exploring ways to make a webpage more interactive, I decided to replace a traditional static banner image with an HTML5 canvas that features a live asteroid simulation. The objective was to add a subtle layer of interactivity that enhances the user experience without overwhelming the main content. In this article, I’ll outline the approach and code implementation for creating a canvas-based, dynamic banner, covering the necessary HTML, CSS, and JavaScript to bring it to life.

HTml structure

To start, I added the HTML structure for the header and canvas. Since the canvas sits behind the title, it’s essential to use CSS to position elements correctly, allowing the title to remain clear and readable. Here’s how I set it up:


<header class="masthead">
  <div class="container position-relative px-4 px-lg-5">
    <div class="row gx-4 gx-lg-5 justify-content-center">
      <div class="col-md-10 col-lg-8 col-xl-7">
        <div class="site-heading">
          <h1 style="position: relative; z-index: 2;">HTML5 APP</h1>
          <span class="subheading"></span>
        </div>
      </div>
    </div>
  </div>
  <canvas id="asteroid-canvas"></canvas>
</header>

This setup ensures that the h1 element remains on top, providing a clear title with the canvas positioned underneath it, forming the background for the header.

CSS Styling for full-width canvas

For the banner, the canvas should span the full width and adapt to different screen sizes. I styled the canvas to achieve a responsive design with a dark, space-like backdrop:


#asteroid-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #2C2C2C;
}

This CSS positions the canvas in the header and fills it to create a seamless banner effect. It also maintains responsiveness by adapting to varying screen sizes, ensuring a consistent visual experience across devices.

JavaScript for asteroid animation and starship movement

The JavaScript code powers the interactive elements of the simulation, from initializing asteroids to tracking the starship’s position as it follows the mouse pointer. Using window.innerWidth, I determined the number of asteroids based on screen size, optimizing performance for both desktop and mobile devices:


if (window.innerWidth > 768) { // Desktop
  createAsteroids(10);
} else { // Mobile
  createAsteroids(5);
}

By differentiating between desktop and mobile devices, I could ensure the animation runs smoothly without unnecessary resource usage on smaller screens. The createAsteroids function generates asteroids, setting random positions and velocities, while a separate function updates their movement in each animation frame.

In addition to asteroid creation, the code implements a simple mouse tracking function. This feature allows the starship to follow the cursor, creating a responsive interaction that feels natural:


document.addEventListener('mousemove', function(e) {
  const starship = document.getElementById('starship');
  starship.style.left = `${e.clientX}px`;
  starship.style.top = `${e.clientY}px`;
});
Conclusion

By combining HTML5, CSS, and JavaScript, I created a visually engaging banner with an asteroid simulation that adapts dynamically across screen sizes. The simulation brings a subtle, interactive dimension to the page, enhancing user engagement in a structured and efficient way.

For more insights into this topic, you can find the details here.